ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
class Outer {
int x = 1;
class Inner {
int x = 2;
void show(int x) {
System.out.print(x + " " + this.x + " " + Outer.this.x);
}
}
}
// in main:
new Outer().new Inner().show(3);- A. 3 2 1
- B. 3 3 3
- C. 1 2 3
- D. 3 2 2
Correct answer: A. 3 2 1
The parameter shadows both fields; this.x is the Inner field and Outer.this.x reaches the enclosing instance's field.
More OOP with C++ and Java questions
- 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 ")…
- What is printed? java enum Dir { N, E, S, W } // in main: System.out.print(Dir.values()[2] + " " + Dir.valueOf("W").ordinal() + " ")…
- What is printed? java class Box implements Cloneable { int[] data = {1, 2}; int n = 5; public Box clone() throws…
- What is printed? java // static method of the main class: static void swap(int[] a, int[] b) { int[] t = a; a = b; b = t; a[0] = 100; } //…
- What is printed? java Object o = "hi"; String s = (String) o; Integer n = null; System.out.print((n instanceof Integer) + " " + (o…