Bird
Raised Fist0
Javaprogramming~20 mins

Private access modifier in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Private Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of accessing private field within the same class
What is the output of this Java program?
Java
public class Test {
    private int number = 5;
    public void printNumber() {
        System.out.println(number);
    }
    public static void main(String[] args) {
        Test t = new Test();
        t.printNumber();
    }
}
A0
B5
CCompilation error: number has private access in Test
DRuntime error
Attempts:
2 left
💡 Hint
Private fields can be accessed inside the same class.
❓ Predict Output
intermediate
2:00remaining
Output when accessing private field from another class
What happens when you try to compile and run this code?
Java
public class A {
    private int secret = 10;
}

public class B {
    public static void main(String[] args) {
        A a = new A();
        System.out.println(a.secret);
    }
}
A10
BRuntime error: IllegalAccessException
C0
DCompilation error: secret has private access in A
Attempts:
2 left
💡 Hint
Private fields are not accessible outside their own class.
🔧 Debug
advanced
2:00remaining
Why does this code cause a compilation error?
Identify the reason for the compilation error in this code snippet.
Java
class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
}

class Employee extends Person {
    public Employee(String name) {
        super(name);
    }
    public void printName() {
        System.out.println(name);
    }
}
AMissing return type in printName method
BConstructor call super(name) is invalid
CCannot access private field 'name' in subclass Employee
DNo compilation error
Attempts:
2 left
💡 Hint
Private fields are not inherited directly accessible in subclasses.
📝 Syntax
advanced
2:00remaining
Which option correctly declares a private method and calls it within the same class?
Select the code snippet that compiles and runs correctly, printing "Hello".
A
public class Demo {
    private void greet() {
        System.out.println("Hello");
    }
    public void call() {
        greet();
    }
    public static void main(String[] args) {
        new Demo().call();
    }
}
B
public class Demo {
    private void greet() {
        System.out.println("Hello");
    }
    public void call() {
        this.greet();
    }
    public static void main(String[] args) {
        Demo.call();
    }
}
C
public class Demo {
    private void greet() {
        System.out.println("Hello");
    }
    public static void call() {
        greet();
    }
    public static void main(String[] args) {
        call();
    }
}
D
public class Demo {
    private static void greet() {
        System.out.println("Hello");
    }
    public void call() {
        greet();
    }
    public static void main(String[] args) {
        new Demo().call();
    }
}
Attempts:
2 left
💡 Hint
Private methods can be called inside the same class instance methods.
🚀 Application
expert
2:00remaining
How many private fields are accessible in this nested class?
Given the code below, how many private fields of Outer are accessible directly inside Inner?
Java
public class Outer {
    private int a = 1;
    private int b = 2;
    int c = 3;
    public class Inner {
        public int sum() {
            return a + b + c;
        }
    }
}
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Inner classes can access all members of the outer class, including private ones.

Practice

(1/5)
1. What does the private access modifier do in Java?
easy
A. It restricts access to the member only within its own class.
B. It allows access from any class in the same package.
C. It allows access from any class in any package.
D. It allows access only from subclasses.

Solution

  1. Step 1: Understand the meaning of private

    The private modifier restricts access to the member so that only code inside the same class can use it.
  2. Step 2: Compare with other access levels

    Other modifiers like public or protected allow wider access, but private is the most restrictive.
  3. Final Answer:

    It restricts access to the member only within its own class. -> Option A
  4. Quick Check:

    Private means only inside class [OK]
Hint: Private means only accessible inside the same class [OK]
Common Mistakes:
  • Confusing private with protected or public
  • Thinking private allows subclass access
  • Assuming private allows package access
2. Which of the following is the correct way to declare a private integer variable named count in a Java class?
easy
A. private count int;
B. int private count;
C. private int count;
D. count private int;

Solution

  1. Step 1: Recall Java variable declaration syntax

    In Java, the access modifier comes first, then the type, then the variable name.
  2. Step 2: Check each option

    private int count; follows the correct order: private int count;. Others have wrong order or syntax.
  3. Final Answer:

    private int count; -> Option C
  4. Quick Check:

    Access modifier + type + name [OK]
Hint: Access modifier comes before type and variable name [OK]
Common Mistakes:
  • Placing 'private' after the type or variable name
  • Mixing order of keywords
  • Missing semicolon
3. What will be the output of the following Java code?
class Test {
  private int number = 5;
  public void printNumber() {
    System.out.println(number);
  }
}
public class Main {
  public static void main(String[] args) {
    Test t = new Test();
    t.printNumber();
  }
}
medium
A. Runtime error
B. Compilation error
C. 0
D. 5

Solution

  1. Step 1: Understand private member access inside class

    The variable number is private but accessed inside its own class method printNumber(), which is allowed.
  2. Step 2: Check main method output

    The main method creates an object and calls printNumber(), which prints the value 5.
  3. Final Answer:

    5 -> Option D
  4. Quick Check:

    Private accessible inside class method [OK]
Hint: Private fields can be used inside their own class methods [OK]
Common Mistakes:
  • Thinking private prevents access even inside class
  • Expecting compilation error due to private
  • Confusing default value with actual initialization
4. Identify the error in the following Java code:
class Person {
  private String name = "Alice";
}
public class Main {
  public static void main(String[] args) {
    Person p = new Person();
    System.out.println(p.name);
  }
}
medium
A. Missing constructor in Person class.
B. Cannot access private field name from outside the class.
C. Syntax error in class declaration.
D. No error; code runs fine.

Solution

  1. Step 1: Check access to private field from outside class

    The field name is private in Person class, so it cannot be accessed directly from Main class.
  2. Step 2: Understand Java access rules

    Accessing p.name from Main causes a compilation error due to private access.
  3. Final Answer:

    Cannot access private field name from outside the class. -> Option B
  4. Quick Check:

    Private fields block outside access [OK]
Hint: Private fields can't be accessed outside their class [OK]
Common Mistakes:
  • Assuming private fields are accessible everywhere
  • Thinking missing constructor causes error here
  • Ignoring access modifiers
5. You want to keep a user's password private in a Java class but allow other classes to check if a password matches. Which is the best way to do this?
hard
A. Make the password field private and provide a public method to check the password.
B. Make the password field public so other classes can read it directly.
C. Make the password field protected and access it from other classes.
D. Make the password field private and provide a public method to return the password.

Solution

  1. Step 1: Protect sensitive data with private fields

    Passwords should be private to prevent direct access and protect security.
  2. Step 2: Provide controlled access via public method

    A public method can check if a given password matches without revealing the actual password.
  3. Final Answer:

    Make the password field private and provide a public method to check the password. -> Option A
  4. Quick Check:

    Private data + public check method = secure design [OK]
Hint: Keep data private; use public methods to interact safely [OK]
Common Mistakes:
  • Making password public exposes sensitive data
  • Returning password directly breaks security
  • Using protected exposes data to subclasses unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard