ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
Integer a = 1000; Integer b = a; a++; System.out.print(a + " " + b + " " + (a == b));
- A. 1001 1001 true
- B. 1001 1000 false
- C. 1000 1000 true
- D. 1001 1001 false
Correct answer: B. 1001 1000 false
Integer is immutable: a++ unboxes, adds 1 and boxes a NEW Integer into a. b still refers to the old object holding 1000.
More OOP with C++ and Java questions
- What is printed? java class P { int id; P(int id) { this.id = id; } public boolean equals(P o) { return o != null && id == o.id; } } // in…
- What is printed? java List<String> l = new ArrayList<>(List.of("pear", "fig", "apple", "kiwi", "date"))…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("Object"); } static void f(String s)…
- What is printed? java // static method of the main class: static void f(Object... a) { System.out.print(a == null ? "N " : a.length + "…
- What is printed? java interface Shape { double area(); } class Sq implements Shape { double area() { return 4; } } // in main: Shape s =…
- Which declaration is LEGAL inside an abstract Java class?