1Z0-811 Object-Oriented Programming Practice Question
Exhibit
Consider the following Java class:
public class Employee {
private String name;
public Employee(String name) {
name = name;
}
public String getName() {
return name;
}
}Refer to the exhibit. What is the problem with this class?
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 constructor parameter shadows the field, causing name to remain null
The constructor parameter 'name' shadows the field 'name'. The assignment 'name = name' assigns the parameter to itself, leaving the field 'name' uninitialized (null). Option A is incorrect because constructors do not have a return type. Option C is incorrect because getName() returns a String, and it is fine to not be void. Option D is incorrect because the private field is accessible within 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 constructor is missing a return type
Why it's wrong here
Constructors do not have return types; this is not an error.
- ✓
The constructor parameter shadows the field, causing name to remain null
Why this is correct
The assignment should be 'this.name = name;' to assign to the field.
- ✗
The getName method should be void
Why it's wrong here
The method correctly returns a String; void would be incorrect.
- ✗
The field name is private and cannot be accessed within the class
Why it's wrong here
Private fields are accessible within the same class.
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.