Java Parameter Shadowing and this Keyword

Utilizing Java Object-Oriented Approach
Answer Correct answer: C — The output is 1001, 100, 1000 because the local parameter x is incremented but the instance variable this.x remains 100.

Given: What is the result? - image

  1. 100
  2. 101
  3. 1001 Correct Answer
  4. 1001

Community Votes

C
100%

100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

Tests understanding of scope: when a method parameter shares a name with an instance variable, 'this.x' accesses the instance field while 'x' accesses the parameter.

This question tests Java's parameter shadowing rules where a local variable hides an instance field. The correct answer is C because the final println outputs the unchanged local variable value.

Learners often select B (101) by confusing 'this.x' with the modified local variable x, or D (1001) by assuming the print order differs from the code flow.

Community Discussion (4 comments)

SrinivasJasti 👍 1 Selected: C
this.x is instance variable and int x = 1000 is local variable
xplorerpj 👍 1
Right answer is C this.x refers to publicly declared/initialized variable
james2033 👍 3 Selected: C
package q23; public class App { public int x = 100; public static void main(String[] args) { int x = 1000; App t = new App(); t.myMethod(x); System.out.println(x); } public void myMethod(int x) { x++; System.out.println(x); System.out.println(this.x); } } // Result: // 1001 // 100 // 1000
Tojose 👍 4
the right answer is C. 1001 100 1000

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The code defines an instance variable x = 100 and a local variable x = 1000. In myMethod, the parameter int x shadows the instance variable. The statement x++ increments the local parameter to 1001. System.out.println(x) prints 1001. Crucially, this.x explicitly refers to the instance variable, which remains unmodified at 100. Finally, System.out.println(x) in main prints the original local variable value 1000. The combined output matches option C.

Why the Other Options Are Wrong

Option A (100) ignores the increment on the local variable. Option B (101) incorrectly assumes this.x was incremented or that the second print refers to the instance variable after modification. Option D (1001) typically results from misreading the final print statement as printing the instance variable or misunderstanding the flow.

Community Comment Notes

Users like Tojose and james2033 confirm the output sequence: 1001, then 100, then 1000. SrinivasJasti highlights the distinction between the instance variable and local variable. xplorerpj notes that this.x refers to the publicly declared variable, reinforcing the shadowing concept.

Exam Strategy

Always distinguish between member variables and local parameters. Use the 'this' keyword to access the current object's fields when they are shadowed by local names.

Frequently Asked Questions

Why does x++ not affect this.x?

Because x++ modifies the local parameter variable, which shadows the instance variable. The instance variable is untouched.

What is the purpose of this.x?

It explicitly references the instance field of the current object, bypassing any local variable with the same name.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide