ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
// static method of the main class:
static void f(Object... a) {
System.out.print(a == null ? "N " : a.length + " ");
}
// in main:
f(); f(null); f((Object) null); f(1, null);- A. 0 1 1 2
- B. 0 N N 2
- C. 0 N 1 2
- D. 1 N 1 2
Correct answer: C. 0 N 1 2
f() passes an empty array. A bare null is assignable to Object[] itself, so it is passed as the array (a == null). Casting to Object forces varargs wrapping into a 1-element array; f(1, null) gives 2 elements.
More OOP with C++ and Java questions
- 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?
- Which line fails to compile? java class Outer { int x = 1; class In { int get() { return x; } } static class SN { int get() { return 2; }…
- What is printed? java class Outer { int x = 1; class Inner { int x = 2; void show(int x) { System.out.print(x + " " + this.x + " " +…
- What is printed? java int count = 0; Runnable r = () -> System.out.print(count); count++; r.run();
- What is printed? java enum Level { LOW(1), HIGH(3); Level(int v) { System.out.print(name() + v + " "); } static { System.out.print("S ")…