ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Object[] objs = {1, "a", 2.0, 'c'};
for (Object o : objs) System.out.print(o.getClass().getSimpleName());- A. intStringdoublechar
- B. ObjectObjectObjectObject
- C. IntegerStringFloatCharacter
- D. IntegerStringDoubleCharacter
Correct answer: D. IntegerStringDoubleCharacter
Each primitive literal is autoboxed when stored in an Object[]: 1 → Integer, 2.0 (a double literal) → Double, 'c' → Character. getClass() reports the runtime class.
More OOP with C++ and Java questions
- What is printed? java class K { static int made = 0; K(int v) { made++; if (v < 0) throw new IllegalArgumentException(); } } // in main…
- What is printed? java interface Named { default String n() { return getClass().getSimpleName(); } } class Dog implements Named { } // in…
- What is printed? java Runnable r = new Runnable() { public void run() { } }; System.out.print("[" + r.getClass().getSimpleName() + "]" +…
- Which line fails to compile? java class P { final void f(int x) { } } class C extends P { void f(long x) { } // L1 void f(int y) { } // L2 }
- What is printed? java class P { int x; P(int x) { this.x = x; } public boolean equals(Object o) { return o != null && getClass() ==…
- What is printed? java Boolean b1 = true, b2 = true; Character c1 = 'a', c2 = 'a'; Short s1 = 100, s2 = 100; System.out.print((b1 == b2) +…