0
0
Javaprogramming~20 mins

Private data members in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Private Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of accessing private data member directly
What will be the output of the following Java code?
Java
class Person {
    private String name = "Alice";
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p.name);
    }
}
ACompilation error
Bnull
CAlice
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Private members cannot be accessed directly outside their class.
❓ Predict Output
intermediate
2:00remaining
Output when accessing private member via getter
What will be the output of this Java program?
Java
class Person {
    private String name = "Bob";
    public String getName() {
        return name;
    }
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p.getName());
    }
}
ABob
Bnull
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Private members can be accessed inside the class and exposed via public methods.
❓ Predict Output
advanced
2:00remaining
Effect of private data member shadowing
What is the output of this Java code?
Java
class Animal {
    private String type = "Animal";
    public String getType() {
        return type;
    }
}

class Dog extends Animal {
    private String type = "Dog";
    public String getType() {
        return type;
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println(a.getType());
    }
}
AAnimal
BDog
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Method overriding works even if private fields are shadowed.
πŸ”§ Debug
advanced
2:00remaining
Identify the error with private data member access
Which option correctly identifies the error in this code snippet?
Java
class Car {
    private int speed;
    public void setSpeed(int speed) {
        speed = speed;
    }
    public int getSpeed() {
        return speed;
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
        c.setSpeed(50);
        System.out.println(c.getSpeed());
    }
}
AThe code will not compile because 'speed' is private.
BThe private field 'speed' cannot be accessed in getSpeed method.
CThe setSpeed method does not set the private field 'speed' because of variable shadowing.
DThe setSpeed method should be static to modify 'speed'.
Attempts:
2 left
πŸ’‘ Hint
Look at the assignment inside setSpeed method.
🧠 Conceptual
expert
2:00remaining
Why use private data members in Java?
Which option best explains the main reason for declaring data members as private in Java?
ATo improve the performance of the program by hiding data.
BTo make the data members accessible only within the same package.
CTo allow subclasses to access the data members directly.
DTo prevent other classes from accessing or modifying the data directly, ensuring controlled access.
Attempts:
2 left
πŸ’‘ Hint
Think about encapsulation and data protection.