0
0
Javaprogramming~20 mins

This keyword usage in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
This Keyword Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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();
    }
}
ARuntime error
B20
CCompilation error
D10
Attempts:
2 left
πŸ’‘ Hint
Remember that 'this' refers to the current object instance's field, not local variables.
❓ Predict Output
intermediate
2: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);
    }
}
ACompilation error due to recursive constructor call
B
Constructor with int: 5
No-arg constructor
C
No-arg constructor
Constructor with int: 5
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
The this() call invokes another constructor in the same class.
❓ Predict Output
advanced
2: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();
    }
}
ACompilation error: Cannot use 'this' in a static context
BPrints the class name Sample
CPrints null
DRuntime NullPointerException
Attempts:
2 left
πŸ’‘ Hint
'this' refers to an instance, but static methods have no instance.
❓ Predict Output
advanced
2: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();
    }
}
A
100
200
300
B
300
200
100
C
300
100
200
DCompilation error due to ambiguous 'this'
Attempts:
2 left
πŸ’‘ Hint
Inner class can refer to outer class instance using Outer.this.
🧠 Conceptual
expert
3: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());
    }
}
A42
BCompilation error: Cannot use 'this' in method reference
CRuntime error: NullPointerException
D0
Attempts:
2 left
πŸ’‘ Hint
Method references can use 'this' to refer to instance methods.