ProgrammingModerateProduct-company MCQ pattern
What is printed (default JVM settings)?
What is printed (default JVM settings)?
Integer x = 127, y = 127; Integer p = 128, q = 128; System.out.print((x == y) + " " + (p == q));
- A. true true
- B. false false
- C. true false
- D. false true
Correct answer: C. true false
Autoboxing uses Integer.valueOf, which returns cached objects for -128 to 127, so x and y are the same object. 128 is outside the cache, so p and q are different objects and == compares references. Use equals for wrapper values.
More OOP with C++ and Java questions
- 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 methods of the main class: static void f(long x) { System.out.print("long"); } static void f(Integer x) {…
- What is printed? java class P { P() { show(); } void show() { System.out.print("P "); } } class C extends P { int x = 10; void show() {…
- What is printed? java class T { static { System.out.print("S"); } { System.out.print("I"); } T() { System.out.print("C"); } } // in main…
- What is printed? java // static methods of the main class: static int f() { int x = 1; try { return x; } finally { x = 5; } } static int…
- What is printed? java System.out.print(1 + 2 + "3" + 4 + 5);