You are on page 1of 13

1) public class Main {

private static void main (String[] xyz) {


System.out.println("Nizar");}}
a) compiler error: xyz unknown parameter
b) compile and run
c) compiler error (method main must be public)
d) compile and not run

2) public class Main {


public static void main(String[] nizar88) {
System.out.println("Nizar");}}
a) compiler error (cannot use numbers in main arguments)
b) compile and run
c) compiler error (must use args instead of nizar88)

3) public class Main {


public static void main(String[] Nizar#hammod) {
System.out.println("Nizar");}}
a) compiler error ( no # )
b) compile and run
c) compile and not run

4) public class Main {


public static void main(String[] Nizar$Hammod) {
System.out.println("tafees");}}
a) compiler error (no $)
b) compile and run
c) compile and not run
5) public class Main {
public static void main(String[] args) {
System.out.println("Nizar");}}
public class _Main {
public static void main(String[] args) {
System.out.println("Hammood");}}
a) compiler error (2 public static methods )
b) compile and run, output is "Nizar"
c) compile and run, output will be both "Nizar" and "Hammood"
d) compile and run, output may be "Nizar" or "Hammood"
e) compile and not run

6) class LifeTimeCycle {
void method() {
int a = 0;
int aa;
{ int b = 10; int a = 1; aa +=1; }
b+=1; }
}
a) compiler error (at aa+: variable aa not Initiated)
b) compiler error (int a in internal scope is duplicate identifier )
c) compiler error (b+=1 :variable b not found)
d) compile and run
e) a ,b, and c

7) class EqualTest {
int a, b, c;
EqualTest(int a, int b, int c) {
a = a; b = b; c = c; }
}
public class Main {
public static void main(String[] args) {
EqualTest e = new EqualTest(1,2,3);}
}
a) compiler error: in EqualTest non deterministic assignment
b) compile and run, and both all a,b ,c get the value 1
c) compile and run, but EqualTest data members not initialized
d) other

8) class EqualTest {
int a, b, c;
EqualTest(int x, int b, int c) { a = x; this.b = b; c = c; }
}
public class Main {
public static void main(String[] test) {
EqualTest test = new EqualTest(1, 2, 3);
EqualTest test_ = new EqualTest(1, 2, 4);
if (test==test_)
System.out.println("Nizar");
else if ((test.a==test_.a)&& (test.b==test_.b) && (test.c==test_.c))
System.out.println("new-tech");
else
System.out.println("Hammood");
}
}
a) output is "Nizar"
b) output is "Hammood"
c) output is both " new-tech "
e) compiler error in EqualTest non deterministic assignment
let us have those three following codes :
9) class Grand {
public void Grand() {
System.out.println("Grand Normal Constructor"); }
}
public class Main {
public static void main(String[] args) {
Grand g = new Grand();
g. Grand();}}

10) class Grand {


protected Grand() {
System.out.println("Grand Normal Constructor"); }
}
public class Main {
public static void main(String[] args) {
Grand g = new Grand();
g. Grand();}
}

11) class Grand {


protected void Grand() { System.out.println("Grand Normal
Constructor"); }
}
public class Main {
public static void main(String[] args) {
Grand g = new Grand(); g. Grand();}}
Now choose the correct choice for each code from three above:
a) output is: "Grand Normal Constructor"
b) output is: "Grand Normal Constructor", "Grand Normal Constructor"
c) compile but with no output
d) compiler error

12) class test{


void non_finalMethod() {
System.out.println("I am not final method"); }
final void finalMethod() {
System.out.println("I am final method"); }
void finalMethod(int v) {
System.out.println("You can OVERLOAD final methods"); }
}
class Itest extends test{
final void finalMethod() {
System.out.println("You can OVERRID final methods "); }
Final void non_finalMethod (){ }
}
a) compile and run
b) cannot override final method
c) cannot override non-final method with final one
d) cannot overload final method

13) final class FinalClass {


FinalClass() {
System.out.println("final class constructor"); }
public void toto(){
System.out.println("toto"); }
public final void toto(int i){
System.out.println("toto overloading"); }
public class Main {
public static void main(String[] args) {
FinalClass f = new FinalClass ();}}
a) compiler error: you cannot declare an object from final class
b) compiler error: you cannot call final method
c) compiler error: you cannot overload non-final method with final method
d) other

14) abstract class ror{


abstract public void tot();
}
class fof extends ror{
void tot(){ }}
a) compiler error, tot in fof must be protected
b) compiler error, tot in fof must be public
c) compiler error, tot in ror class must be default
d) a or b or c
e) b or c
f) compile and run

15) interface I1 { int i ;


}
class I1_ implements I1 {
int i ;
I1_() { System.out.println("I1_ Constructor .." + ++ i); }}
a) compiler error variable i in I1 must be initialized
b) compiler error interface cannot involve variables
c) run and System.out.println prints 1
d) compiler error, syntax error
16) interface I1 { int i=0 ;}
interface I2 extends I1 { protected void WhatEver();}
class zxcv{
void WhatEver(){}
}
a) compiler error, write implements instead of extends
b) compiler error, WhatEver method in interface must be public
c) compiler error, WhatEver method in interface must be default
d) 1,2,3
e) 2,3

17) interface ICareful { void mtod();}


class Careful { void mtod() { System.out.println(" Be careful"); }}
class soso extends Careful implements ICareful {
soso() {
System.out.println(" No need you override mtod, it's in Careful Class ");
}}
a) compile and run
b) compiler error, must override mtod in class soso
c) compiler error, mtod in class Carful must be public
d) b,c

18) interface o {
public interface y{
protected interface pt{ } }}
a) compiler error, cannot write nested interfaces
b) compiler error, cannot be public modifier with interface y
c) compiler error, cannot be protected modifier with interface pt
d) 2,3
e) Compile and run
19) class note1{
static int out_static =0;
int out_nonstatic = 0;
static void non_nonstatic(){
out_static++;
static int i = 0;
this.out_static++;
out_static++;
}
}
a) Compiler error (static int i = 0; ) illegal start of expression
b) Compiler error (this.out_static++;) cannot use this in static method
c) Warming (this.out_static++;) : try to access static data member in
class note1
d) 1, 2
e) Others

20) class Couter{


interface Iinner{ }}
interface Iouter{
class Cinner{ }}
class Couter1{
interface Iinner{
interface i{ }
class c{
interface ii{ } } }}
class s{
interface dd{}
class ss{
interface sss{ } }}
a) compiler error, Couter class cannot involve interfaces
b) compiler error, Iouter interface cannot involve class
c) compiler error , Couter1 class cannot involve nested interfaces (Iinner,i)
d) 1,3
e) Compiler error Ineer class ss cannot involves inner interface dd in class s
f) This code compiled successfully and works

21) class A{
class B{
static class C{ }
}
}
a) warning: C is static data member in non static class B
b) compiler error, modifier static is not allowed at class C, some solution is
making class B a static class
c) compiler error, modifier static is not allowed at class C, some solution is
making class A a static class
d) compiler error, modifier static is not allowed at class C, some solution is
making both classes B,A static classes
e) compiler error, static class C should be in package named C

22) class Outer{


class Inner2{}
Inner2 CreatInner2Ref() {
return new Inner2();
}
}

Which of the following code is not correct?


a) static Outer o11 = new Outer(); static I o4=o11.CreatInner2Ref();
b) Outer o11 = new Outer(); static I o4 = o11.CreatInner2Ref();
c) static Outer o11 = new Outer(); I o4 = o11.CreatInner2Ref();
d) Outer o11 = new Outer(); I o4 = o11.CreatInner2Ref();
e) 1,2,3

23) class aaa{


class Aaa{
private void callme(){ } }
}
class bbb{
class Bbb extends aaa.Aaa{
void callme(){ } }
}

a) compile and run


f) compiler error at class bbb (class Bbb extends aaa.Aaa)
g) compiler error at class bbb (can be solved be making class Aaa as static
class)
h) compiler error inner class Aaa cannot involve private method
i) 2,3

24) final class FinalClass {


FinalClass() {
System.out.println("final class constructor"); }
void T(){ System.out.println("non final method"); } }
class Main {
Main() { System.out.println("main"); }
public static void main(String[] args) {
new FinalClass().T(); }
}
a) compiler error, Main class cannot involve a constructor
b) compiler error, final class cannot involve non-final method
c) compiler error, cannot define object from final class
d) other

25) class t{
static void tt() throws FileNotFoundException{
try{
File f = new File("C:\\myFolder");
}
catch(FileNotFoundException e){
}
}
}

a) compiler error, static method cannot throws exceptions


b) compiler error, method cannot handle and throw the same
exceptions
c) FileNotFoundException is never thrown in the body
d) Other

26) public class Main {


public static void main(String[] args) {
File f = new File("c:\\InputOutputStreamFile");
DataOutputStream data;
try{
data = new DataOutputStream(
new BufferedOutputStream(new
FileOutputStream(f)));
}
catch(FileNotFoundException e1){ }
try{
for (byte i = 1; i <= 11; i++){ data.writeByte(i); }
data.flush();
}
catch(IOException e2){ }
finally{
try{
data.close();
}
catch(IOException e3){
} } }}
a) compiler error, data.writeByte(i) takes byte data type argument
only
b) compiler error, IOException is never thrown
c) compiler error, variable data may be not initialized
d) compiler error, DataOutputStream cannot warp
BufferedOutputStream class
e) other

27) public class Main {


public static void main(String[] args) {
int i,j,k,l ;
try { i=1; }
catch(Exception e){ j=1; }
finally { k=1; }
{ l=1; }
System.out.println("i = "+i+" j = "+j+" k = "+k+" l = "+l);}}

a) compile and print :(i=1,j=1,k=1,l=1)


b) compile and print :(i=1,j=0,k=1,l=1)
c) compiler error variables j and l are not initialized
d) compiler error variable i is not initialized
e) compiler error variables I and j are not initialized

Best Wishes...
Mu_Nizar & Adel Al- Jaffan
Question no Answer no Question no Answer no Question no Answer no Question no Answer no
1 d 9 a 17 D 25 C
2 a 10 d 18 C 26 C
3 a 11 a 19 D 27 e
4 b 12 B 20 E -
5 d 13 D 21 B -
6 e 14 E 22 D -
7 d 15 A 23 F -
8 c 16 E 24 d -

You might also like