ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
String s = "ab";
StringBuilder sb = new StringBuilder("ab");
System.out.print(s.equals(sb) + " " + s.contentEquals(sb) + " " + s.equals(sb.toString()));- A. true true true
- B. false false true
- C. false true false
- D. false true true
Correct answer: D. false true true
String.equals returns true only for another String with the same characters; a StringBuilder is not a String. contentEquals compares against any CharSequence's characters.
More OOP with C++ and Java questions
- What happens when this class is compiled? java class Code implements Comparable<String>, Comparable<Integer> { public int compareTo(String…
- Which statement about Java anonymous classes is correct?
- Which lines fail to compile? java // Java 10 or later var list = new ArrayList<>(); // L1 list.add(1); list.add("a"); // L2 var x; // L3…
- What happens when this is compiled? java // Java 17 sealed class Shape permits Circle, Square { } final class Circle extends Shape { }…
- What is printed? java class AppEx extends Exception { } class DbEx extends AppEx { } // static method of the main class: static void run()…
- What is printed? java // in main, which is declared throws Exception: try (AutoCloseable r = null) { System.out.print("body"); }…