Java certification Quiz
Java Quiz - 20

« 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 »

1. For following :
public class TestParent { 
    static {
      System.out.print(" parent");
    }
}
public class Test extends TestParent{ 
    static {
      System.out.print(" child");
    }
    public static void main(String args[]) { 
      System.out.print(" main");
    }
}
a) Gives output: parent child main
b) Gives output: child parent main
c) Gives output: main parent child
d) Gives output: main
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!  The correct answer is a) Gives output: parent child main
static block of Parent is called first
Correct! Gives output: parent child main
static block of Parent is called first
2. What is the output?
public class TestParent { 
   static void m1() {
       System.out.print(" parent");
   }
}
public class Test extends TestParent{ 
   static void m1() {
       System.out.print(" child");
   }
   public static void main(String args[]) { 
      TestParent t = new Test();
      t.m1();
      System.out.print( " main");
   }
}
a) Gives output: child main
b) Compile error : cannot override static method
c) Gives output: parent main
d) Gives output: parent child main

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Gives output: parent main
static method of reference class is called
Correct! c) Gives output: parent main
static method of reference class is called
3. What is output?
public class TestParent { 
   public void m1() {
       System.out.print(" parent");
   }
}
public class Test extends TestParent{ 
   public static void m1() {
       System.out.print(" child");
   }
   public static void main(String args[]) { 
      TestParent t = new Test();
      t.m1();
      System.out.print( " main");
   }
} 
a) Compile error
b) Gives output: child main
c) Gives output: parent main
d) None of the above
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Compile error: Cannot change overridden method to static
Correct! Compile error: Cannot change overridden method to static
4. What is the output?
public class Test{ 
   static {
       System.out.print(" static1");
   }
   public static void main(String args[]) { 
      System.out.print( " main");
   }
   static {
       System.out.print(" static2");
   }
}
a) main static1 static2
b) static1 main static2
c) Compile error
d) static1 static2 main

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) static1 static2 main
static blocks are executed first, in the order declared
Correct! static1 static2 main
static blocks are executed first, in the order declared
5. Which of the following are marker interfaces?
1. java.io.Serializable 
2. java.lang.Cloneable
3. java.io.Externalizable
4. java.lang.Runnable
5. java.lang.Throwable 
a) 1 and 2
b) 1, 2 and 3
c) 1, 3 and 5
d) All of them

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) 1 and 2.
Serializable and Cloneable are marker interfaces. Marker interface indicate, signal or command the compiler or JVM to do something special
Correct! 1 and 2.
Serializable and Cloneable are marker interfaces. Marker interface indicate, signal or command the compiler or JVM to do something special
6.  For the following :
public class TestParent { 
   protected void m1() {
       System.out.print(" parent");
   }
}
public class Test extends TestParent{ 
   void m1() {
       System.out.print(" child");
   }
   public static void main(String args[]) { 
      TestParent t = new Test();
      t.m1();
   }
} 
a) Compile Error
b) Gives output : child
c) Gives output : parent
d) Runtime Exception

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Compile Error. 'protected' has higher access privilege to 'default'
Correct! Compile Error. 'protected' has higher access privilege to 'default'
7.  What is the output?
 public class Test { 
    static {
      System.out.print(" static");
    }
    public void Test () {
      System.out.print("  constr");
    }
   public static void main(String args[]) { 
      System.out.print(" main");
      Test t = new Test();
    }
 } 
a) static main
b) static main constr
c) static constr main
d) Will not compile
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) static main
public void Test () is a method
Correct!   static main
public void Test () is a method