Challenge - 5 Problems
This Keyword Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of this keyword in instance method
What is the output of this Java program?
Java
public class Test { int x = 10; void printX() { int x = 20; System.out.println(this.x); } public static void main(String[] args) { Test obj = new Test(); obj.printX(); } }
Attempts:
2 left
π‘ Hint
Remember that 'this' refers to the current object instance's field, not local variables.
β Incorrect
The local variable x shadows the instance variable x. Using 'this.x' accesses the instance variable, which is 10.
β Predict Output
intermediate2:00remaining
Using this() to call constructor
What is the output of this Java program?
Java
public class Demo { Demo() { System.out.println("No-arg constructor"); } Demo(int x) { this(); System.out.println("Constructor with int: " + x); } public static void main(String[] args) { Demo d = new Demo(5); } }
Attempts:
2 left
π‘ Hint
The this() call invokes another constructor in the same class.
β Incorrect
The constructor with int calls the no-arg constructor first, so its message prints first, then the int constructor message.
β Predict Output
advanced2:00remaining
this keyword in static context
What error does this Java code produce?
Java
public class Sample { static void show() { System.out.println(this); } public static void main(String[] args) { show(); } }
Attempts:
2 left
π‘ Hint
'this' refers to an instance, but static methods have no instance.
β Incorrect
'this' cannot be used inside static methods because there is no current object instance.
β Predict Output
advanced2:00remaining
this keyword with inner class
What is the output of this Java program?
Java
public class Outer { int x = 100; class Inner { int x = 200; void print() { int x = 300; System.out.println(x); System.out.println(this.x); System.out.println(Outer.this.x); } } public static void main(String[] args) { Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.print(); } }
Attempts:
2 left
π‘ Hint
Inner class can refer to outer class instance using Outer.this.
β Incorrect
Local x is 300, inner class field x is 200, outer class field x is 100. 'this.x' in inner class refers to inner's x.
π§ Conceptual
expert3:00remaining
this keyword and method reference
Consider the following Java code snippet. What will be the output when main runs?
Java
import java.util.function.Supplier; public class RefTest { int value = 42; Supplier<Integer> getValueSupplier() { return this::getValue; } int getValue() { return value; } public static void main(String[] args) { RefTest obj = new RefTest(); Supplier<Integer> supplier = obj.getValueSupplier(); System.out.println(supplier.get()); } }
Attempts:
2 left
π‘ Hint
Method references can use 'this' to refer to instance methods.
β Incorrect
The method reference this::getValue refers to the instance method getValue of the current object, returning 42.