Java certification Quiz
Java Quiz - 21

« 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 Baby { 
    String gender = "girl";
    public String toString() {
         return gender;
    }
}
public class Test { 
    public static void main(String args[]) { 
      Baby b = new Baby();
      String str = "It's a " + b;
      System.out.print(str);
    }
}
a) Gives output: It's a girl
b) Gives output similar to: It's a Baby@a234fd
c) Gives output: It's a class Baby
d) Does not compile
Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!  The correct answer is a) Gives output: It's a girl
toString() is called to convert object to String
Correct! Gives output: It's a girl
toString() is called to convert object to String

2. What is the output?

public class MySingleton {
     private static MySingleton ms;
     public String name="Singleton";
     private MySingleton (){
     }
     public static MySingleton getInstance () {
        if (ms==null)
         ms = new MySingleton();
        return ms;
     }
}
public class TestSingleton extends MySingleton{ 
   public static void main(String args[]) { 
     MySingleton  ms1 = MySingleton.getInstance ();
     MySingleton  ms2 = MySingleton.getInstance ();
     if(ms1==ms2)
       System.out.println(ms2.name);
     else
       System.out.println("Not Singleton");
   }
}
a) Singleton
b) Not Singleton
c) Compile error
d) NullPointerException thrown

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Compile error
Child class calls super constructor which is private
Correct! c) Compile error
Child class calls super constructor which is private

3. What is the output?

String str = "baba";
str.chars().forEach(c->System.out.print(c+"-"));
a) 98-97-98-97-
b) b-a-b-a-
c) B-A-B-A-
d) Exception thrown
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) 98-97-98-97-
Correct! 98-97-98-97-

4. What is output?

Random r = new Random();
for (int i = 0; i < 3; i++) {
   System.out.print(r.ints(1,5,11).findFirst().getAsInt()+ " ");
}
a) Three numbers 6 to 11
b) Three numbers 6 to 10
c) Three numbers 5 to 11
d) Three numbers 5 to 10

Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!
The correct answer is d)Three numbers 5 to 10
Correct! Three numbers 5 to 10

5. What happens when you compile the code?

1. import java.util.*;
2. public class Forever {
3.  public static void main(String[] args) {
4.    List x1 = new ArrayList();
5.    List x3 = new ArrayList<>();
6.  } 
7. } 
a) Compilation succeeds
b) Compilation fails due to multiple errors
c) Compilation fails due to an error on line 5
d) Compilation fails due to an error on line 4

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Compilation succeeds
Correct! Compilation succeeds

6. What is the output?

public class Test extends Thread  { 
    public void run()  { 
       System.out.println("[run] ");
       throw new RuntimeException(); 
    } 
    public static void main(String args[]) { 
           Test t1 = new Test();
           try {
              t1.setName("t1");
              t1.start(); 
              t1.join();
              System.out.println("[end] ");
          }
          catch(Exception e) {
             System.out.println("[error1] ");
          } 
   }
}
a) [run] Exception in thread "t1" java.lang.RuntimeException [end]
b) [run] [error1]
c) [run] Exception in thread "t1" java.lang.RuntimeException [error1]
d) [run] Exception in thread "t1" java.lang.RuntimeException [error1] [end]

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) [run] Exception in thread "t1" java.lang.RuntimeException [end]
Exception does not reach main thread
Correct! [run] Exception in thread "t1" java.lang.RuntimeException [end]
Exception does not reach main thread

7. What is output?

   List<Integer> elements = new ArrayList<>();
   elements.add(10);
   int firstElmnt = elements.get(1);
   System.out.println(firstElmnt);
a) 10
b) null
c) Compile error
d) Excepton thrown
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) Excepton thrown
Correct!   Excepton thrown