Java OCA OCP Practice Question 3201

Question

Which statement is true about the compilation and execution of the following program with assertions enabled?

public class Main {
 String s1;/*www  . j a  v a  2s . com*/
 String s2 = "hello";
 String s3;

 Main() {
   s1 = "hello";
 }

 public static void main(String[] args) {
   (new Main()).f();
 }

 {
   s3 = "hello";
 }

 void f() {
   String s4 = "hello";
   String s5 = new String("hello");
   assert(s1.equals(s2)); // (1)
   assert(s2.equals(s3)); // (2)
   assert(s3 == s4);      // (3)
   assert(s4 == s5);      // (4)
 }
}

Select the one correct answer.

  • (a) The compilation will fail.
  • (b) The assertion on the line marked (1) will fail.
  • (c) The assertion on the line marked (2) will fail.
  • (d) The assertion on the line marked (3) will fail.
  • (e) The assertion on the line marked (4) will fail.
  • (f) The program will run without any errors.


(e)

Note

All the "hello" literals denote the same String object.

Any String object created using the new operator will be a distinct new object.




PreviousNext

Related