Consider the following Java code snippet, which of these options is the right ou
ID: 3840100 • Letter: C
Question
Consider the following Java code snippet, which of these options is the right output of this code snippet?
public class ComparisonDemo
{
public static void main(String[] args)
{
Employee emp1 = new Employee("John", "Smith");
Employee emp2 = new Employee("John", "Smith");
Employee emp3 = emp1;
System.out.printf("[1] %s ", emp1.equals(emp2) ? "emp1 equals emp2" : "emp1 does not equal emp2");
System.out.printf("[2] %s ", emp1.equals(emp3) ? "emp1 equals emp3" : "emp1 does not equal emp3");
System.out.printf("[3] %s ", (emp1 == emp2) ? "emp1 equals emp2" : "emp1 does not equal emp2");
System.out.printf("[4] %s ", (emp1 == emp3) ? "emp1 equals emp3" : "emp1 does not equal emp3");
System.out.println();
}
}
[1] emp1 does not equal emp2
[2] emp1 equals emp3
[3] emp1 does not equal emp2
[4] emp1 equals emp3
[1] emp1 equals emp2
[2] emp1 equals emp3
[3] emp1 equals emp2
[4] emp1 equals emp3
[1] emp1 equals emp2
[2] emp1 does not equal emp3
[3] emp1 equals emp2
[4] emp1 does not equal emp3
None of the above
a.[1] emp1 does not equal emp2
[2] emp1 equals emp3
[3] emp1 does not equal emp2
[4] emp1 equals emp3
[1] emp1 equals emp2
[2] emp1 equals emp3
[3] emp1 equals emp2
[4] emp1 equals emp3
[1] emp1 equals emp2
[2] emp1 does not equal emp3
[3] emp1 equals emp2
[4] emp1 does not equal emp3
None of the above
Explanation / Answer
Ans) b
[1] emp1 equals emp2
[2] emp1 equals emp3
[3] emp1 equals emp2
[4] emp1 equals emp3
_________
Reason:
Employee emp1 = new Employee("John", "Smith");
Employee emp2 = new Employee("John", "Smith");
//Creating Employee objects emp1 and emp2 wiith the same contents.
Employee emp3 = emp1;//Assigning the Employee object emp1 to the reference of emp3;
All Employee objects are having same contents .So option B is correct
_________________ThankYou
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.