Java certification Quiz
Java Quiz - 5

« 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. What is the output?

class Test {}
class Another extends Test {
 public static void main (String[] args) {
  Another ano = new Another();
  Test t = new Test();
  System.out.println(ano.super.getClass());
  System.out.println(t.getClass());
 }
}
a) Test, Test
b) Another, Test
c) Another Another
d) Compile error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) Compile error. Cannot refer super from an instance reference
Correct!
Compile error. Cannot refer super from an instance reference

2. For the following code :

public class Test{ 
  public String getCountryName(){
  	return "USA";
  }
  public StringBuffer getCountryName(){
      StringBuffer sb = new StringBuffer();
      sb.append("UK");
      return sb;
  }
  public static void main(String args[]) {
      Test tst = new Test();
      System.out.println(tst.getCountryName());
  }
}
a) Compile error
b) Gives output : USA
c) Gives output : UK
d) Runtime exception

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) Compile error : Cannot have methods with same names, though different return types.
Correct!
Cannot have methods with same names, though different return types.

3. For the following code :

 import java.util.ArrayList;
 import java.util.List;
 public class Test{ 
       public static void main(String args[]) {
        List<Integer> list = new ArrayList<>(); 
	list.add(0, 59);
	int total = list.get(0);
	System.out.println(total);  
     }
}
a) Gives output : 0
b) Will not compile
c) Gives output : 59
d) Runtime Exception

Wrong! Try again..
Wrong!
The correct answer is c) Gives output : 59 - Autoboxing will take care of type Integer to int casting.
Wrong Again! Try one more chance.
Correct!
Autoboxing will take care of type Integer to int casting.

4. The following code :

public class Test{ 
  public static void main(String args[]) {
	B b = new B();
  }
}
class A {
  A() {
    System.out.print("A");
  }
}
class B extends A{
  B() {
    System.out.print("B");
  }
}
a) Gives output : BA
b) Gives output : AB
c) Gives output : B
d) Compile Error

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) Gives output : AB - First constructor of super is called and then child
Correct!
First constructor of super is called and then child

5. The following class is an example of which design pattern?

public class Mine {
   private static Mine m; 
   private Mine() { 
   }
   public static synchronized Mine getInstance() {
     if (m == null) {
       m = new Mine();
     } 
     return m;
   }
}
a) Composite Pattern
b) Façade Pattern
c) Singleton Pattern
d) Factory Pattern

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) Singleton Pattern : Singleton pattern ensures only one instance is created.
Correct!
Singleton pattern ensures only one instance is created.

6. Which expression can be placed at XX?

int[] arr = {1,2,3,4,5};
int i=0;
for (XX) {
   System.out.print("int ");
}
A. ; i < 5; i++
B. int i=0; i < 5; i++
C. ; i < 1;
D. int j: arr
a) A, D
b) B
c) B, D
d) A, C, D

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) A, D .
Correct!
A, D

7. What is the output?

      String k ="big "; 
      k.concat("crowded ");
      k += "city";
      System.out.println(k);
a) big crowded city
b) big city
c) big crowded
d) Compile error

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) big city. String is immutable..
Correct!
big city. String is immutable.