Discussion
Which line rests on a mistaken assumption?
public class Sample17
{
public static void main(String[] args)
{
String a = "java";
String b = new String("java");
assert a == b : "not equal";
System.out.println("same");
}
}
Answer: A
== compares references, and new String is required to produce a distinct object, so the assertion fails even though the text matches. The intent was clearly value equality, which is a.equals(b). The line that creates the second object is legal; only the comparison is wrong.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development