Bird
Raised Fist0
Javaprogramming~20 mins

This keyword usage in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the this keyword refer to in a Java class?
easy
A. A static method
B. The current object instance
C. A superclass object
D. A local variable

Solution

  1. Step 1: Understand the role of this

    The this keyword always points to the current object instance inside a class.
  2. Step 2: Differentiate from other options

    It does not refer to static methods, superclass objects, or local variables.
  3. Final Answer:

    The current object instance -> Option B
  4. Quick Check:

    this = current object instance [OK]
Hint: Remember: this means "this object" [OK]
Common Mistakes:
  • Confusing this with static context
  • Thinking this refers to superclass
  • Assuming this is a local variable
2. Which of the following is the correct way to use this to call another constructor in the same class?
easy
A. call(this);
B. super();
C. this.call();
D. this();

Solution

  1. Step 1: Identify constructor call syntax

    In Java, calling another constructor in the same class uses this(); as the first statement.
  2. Step 2: Eliminate incorrect options

    super(); calls superclass constructor, this.call(); is invalid syntax, and call(this); is not a constructor call.
  3. Final Answer:

    this(); -> Option D
  4. Quick Check:

    Constructor chaining uses this(); [OK]
Hint: Use this(); to call another constructor [OK]
Common Mistakes:
  • Using super(); instead of this();
  • Trying to call constructor like a method
  • Placing this(); not as first statement
3. What will be the output of the following code?
class Test {
  int x = 10;
  void printX() {
    int x = 20;
    System.out.println(x);
    System.out.println(this.x);
  }
  public static void main(String[] args) {
    new Test().printX();
  }
}
medium
A. 10\n20
B. 20\n20
C. 20\n10
D. 10\n10

Solution

  1. Step 1: Identify local and instance variables

    Inside printX(), local x is 20, instance x is 10.
  2. Step 2: Understand this.x usage

    System.out.println(x); prints local 20, System.out.println(this.x); prints instance 10.
  3. Final Answer:

    20 10 -> Option C
  4. Quick Check:

    Local x = 20, this.x = 10 [OK]
Hint: Local variable hides instance; use this for instance [OK]
Common Mistakes:
  • Confusing local and instance variables
  • Ignoring this keyword
  • Expecting both prints to be same
4. Identify the error in the following code snippet:
class Sample {
  int value;
  Sample(int value) {
    value = value;
  }
}
medium
A. The constructor does not assign parameter to instance variable
B. Syntax error: missing this keyword
C. Cannot have parameter and instance variable with same name
D. No error, code works fine

Solution

  1. Step 1: Analyze assignment in constructor

    The statement value = value; assigns the parameter to itself, not to the instance variable.
  2. Step 2: Correct usage with this

    To assign parameter to instance variable, use this.value = value;.
  3. Final Answer:

    The constructor does not assign parameter to instance variable -> Option A
  4. Quick Check:

    Missing this. causes no instance update [OK]
Hint: Use this.variable = variable; to assign correctly [OK]
Common Mistakes:
  • Assuming parameter assigns instance variable automatically
  • Thinking same names cause syntax error
  • Ignoring need for this keyword
5. Consider the following class:
class Point {
  int x, y;
  Point() {
    this(0, 0);
  }
  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  void move(int x, int y) {
    x = x;
    this.y = y;
  }
  String display() {
    return "(" + this.x + ", " + this.y + ")";
  }
  public static void main(String[] args) {
    Point p = new Point();
    p.move(5, 10);
    System.out.println(p.display());
  }
}

What will be the output when running the main method?
hard
A. (0, 10)
B. (5, 10)
C. (0, 0)
D. Compilation error

Solution

  1. Step 1: Understand constructor chaining

    The no-arg constructor calls this(0, 0); setting x=0 and y=0.
  2. Step 2: Analyze move method

    Inside move, x = x; assigns parameter to itself, so instance x remains 0. this.y = y; updates instance y to 10.
  3. Step 3: Check display output

    Returns string with instance variables: (0, 10).
  4. Final Answer:

    (0, 10) -> Option A
  5. Quick Check:

    this.x unchanged, this.y updated [OK]
Hint: Assign instance vars with this.var = var; inside methods [OK]
Common Mistakes:
  • Assuming x = x; updates instance variable
  • Ignoring constructor chaining effect
  • Expecting both coordinates to update