ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
Boolean b1 = true, b2 = true; Character c1 = 'a', c2 = 'a'; Short s1 = 100, s2 = 100; System.out.print((b1 == b2) + " " + (c1 == c2) + " " + (s1 == s2));
- A. true false false
- B. true true true
- C. false false false
- D. true true false
Correct answer: B. true true true
The JLS guarantees that boxing true/false, a char in \u0000–\u007f, or a short/int in −128..127 always yields the same object, so == compares equal references here.
More OOP with C++ and Java questions
- What is printed? java List<Integer> l = Arrays.asList(3, 1, 2); Collections.sort(l); System.out.print(l + " "); l.clear();
- What is printed? java List<String> l = new ArrayList<>(List.of("a", "b", "c")); ListIterator<String> it = l.listIterator(); while…
- What is printed? java System.out.print("a.b.c".split(".").length + " " + "a.b.c".split(".").length);
- What is printed? java System.out.print("a.b".replace(".", "x") + " " + "a.b".replaceAll(".", "x"));
- What is printed? java List<Integer> src = List.of(1, 2); List<Integer> dst = new ArrayList<>(5); Collections.copy(dst, src)…
- What is printed? java enum Size { S; public String toString() { return "small"; } } // in main: System.out.print(Size.S + " " +…