Java OCA OCP Practice Question 179

Question

Given:

public class Main { 
  String name; //w  w  w  .j av  a2s  .c  o m
  Main(String s) { name = s; } 
  public static void main(String[] args) { 
    Main d1 = new Main("Boi"); 
    Main d2 = new Main("Tyri"); 
    System.out.print((d1 == d2) + " "); 
    Main d3 = new Main("Boi"); 
    d2 = d1; 
    System.out.print((d1 == d2) + " "); 
    System.out.print((d1 == d3) + " "); 
  } 
} 

What is the result?

  • A. true true true
  • B. true true false
  • C. false true false
  • D. false true true
  • E. false false false
  • F. An exception will be thrown at runtime


C is correct.

Note

The == operator tests for reference variable equality, not object equality.




PreviousNext

Related