1Z0-811 Java Basics and Syntax Practice Question
Exhibit
class A {
private int x = 5;
}
class B extends A {
public void print() {
System.out.println(x);
}
}Refer to the exhibit. What is the result of attempting to compile and run the code?
⚠ Common exam trap
Oracle often tests the misconception that private members are accessible in subclasses, especially when the subclass is in the same file or package, but Java's access control is strictly class-based, not file-based.
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
✓
Compilation error because x is private in A
The code fails to compile because class B attempts to access the private field `x` of class A from a different class. In Java, private members are only accessible within the same class, not from subclasses or other classes, even if they are in the same file. The `super.x` reference in class B is illegal, causing a compilation error.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Compilation error because x is private in A
Why this is correct
Private members are not inherited.
- ✗
Prints 0
Why it's wrong here
No default value; compilation fails.
- ✗
Runtime error
Why it's wrong here
Code does not compile.
- ✗
Prints 5
Why it's wrong here
x is private, so inaccessible.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.