1Z0-811 Java Basics and Syntax Practice Question
A method is declared as:
public void setAge(int age) { this.age = age; }Which statement is correct about this code assuming the class has an instance variable age?
⚠ Common exam trap
Oracle often tests the distinction between constructors and regular methods by including a `void` return type, which immediately disqualifies a method from being a constructor, even if it looks like one due to parameter assignment.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The method sets the instance variable age.
The method `setAge(int age)` uses the `this` keyword to assign the parameter value to the instance variable `age`, which is a standard setter pattern in Java. The method has a `void` return type and is not a constructor, and it compiles successfully because the instance variable `age` is assumed to exist in the class.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The method is a constructor.
Why it's wrong here
Constructors have no return type and name matches class name.
- ✗
The method will cause a compilation error.
Why it's wrong here
Code compiles successfully.
- ✓
The method sets the instance variable age.
Why this is correct
Uses 'this.age' to refer to instance variable.
- ✗
The method is static.
Why it's wrong here
No static modifier present.
Go deeper
Related to this question
About these practice questions
One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.