Java certification Quiz
Java Quiz - 15

« 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 output?

class MyResource1 implements AutoCloseable { 
     public void close() { System.out.print("1 "); } // A1
 }
 class MyResource2 implements AutoCloseable { 
   public void close() { System.out.print("2 "); } // A2
 }
 public class Triangle {
    public static void main(String[] args) {
      try ( MyResource1 r1 = new MyResource1(); MyResource2 r2 = new MyResource2();) { // B
     System.out.print("t ");
   }
   finally {
     System.out.print("f ");
   }
 }
}
a) Gives output: t 2 1 f
b) Compilation fails due to an error on line B
c) Compilation fails due to an error on lines A1, A2, B
d) Compilation fails due to an error on lines A1, A2
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) Gives output: t 2 1 f
Correct!
Gives output: t 2 1 f

2. Which are correct?

StringBuilder sb = new StringBuilder();
A. sb.deleteAll(); 
B. sb.delete(0, sb.size()); 
C. sb.delete(0, sb.length()); 
D. sb.removeAll();
a) A, B
b) C, D
c) A, C
d) C

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) C

Correct! d) C

3. Which is most closely associated with the object-oriented design concept known as "has-a"?


a) singleton
b) adapter
c) composition
b) factory

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! composition

Correct!
composition

4. What is output?

boolean log3 = ( 5.0 != 6.0) && ( 4 != 5);
boolean log4 = (4 != 4) || (4 == 4);
System.out.println("log3:"+ log3 + ", log4:" + log4);
a) log3:false, log4:true
b) log3:true, log4:false
c) log3:false, log4:false
d) log3:true, log4:true

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) log3:true, log4:true
Correct!
log3:true, log4:true

5. What is the output?

String name = "Nitin";
Runnable r1 = ()->System.out.println(name);
name += " M";
Runnable r2 = ()->System.out.println(name);
r2.run();
a) Nitin, Nitin M
b) Nitin M
c) Exception thrown
d) Compile error

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) Compile error : name is effectively final, cannot modify it
Correct! Compile error.
name is effectively final, cannot modify it

6. Using JDK 1.7, what is the output?

 public class Triangle { 
   public static void main(String[] args) {
     try ( MyResource1 r1 = new MyResource1(); MyResource2 r2 = new MyResource2();) { 
       System.out.print("t ");
     }
     finally {
       System.out.print("f ");
     }
  }
}
class MyResource1 implements AutoCloseable { 
    public void close() { 
	  System.out.print("1 "); 
    } 
}
class MyResource2 implements AutoCloseable { 
    public void close() { 
	  System.out.print("2 "); 
    } 
}
a) t 2 1 f
b) 2 1 t f
c) t 1 2 f
d) Compile Error

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a)t 2 1 f
Correct!
t 2 1 f

7. What is output ?

public class Test { 
   public Test() {
     short s = 1; int i = 2; long l = 3L;
    doIt(s);  doIt(i); doIt(l);
  }
  private void doIt(Short s) {
    System.out.println("short:"+s);
  }
  private void doIt(int i) {
    System.out.println("int:"+i);
  }
  private void doIt(long l) {
    System.out.println("long:"+l);
  }
   public static void main(String[] args) {
        new Test();
  }
}
a) Compile error
b) short:1 int:2 long:3
c) int:1 int:2 long:3
d) long:1 long:2 long:3

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) int:1 int:2 long:3 Use casting doIt((Short)s); or change signature private void doIt(short s)
Correct!
int:1 int:2 long:3 Use casting doIt((Short)s); or change signature private void doIt(short s)