0
0
Javaprogramming~15 mins

Private access modifier in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Private Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code 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
💻 code 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
🔧 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
📝 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
🚀 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