0
0
Javaprogramming~20 mins

Data hiding in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Data Hiding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Java code demonstrating data hiding?

Consider the following Java class that uses private fields to hide data. What will be printed when the main method runs?

Java
public class Person {
    private String name = "Alice";
    private int age = 30;

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p.name);
    }
}
ARuntime error
BAlice
Cnull
DCompilation error: name has private access in Person
Attempts:
2 left
πŸ’‘ Hint

Private fields can be accessed directly from main inside the same class using an instance reference.

❓ Predict Output
intermediate
2:00remaining
What is the output when accessing a private field via a public method?

Given the class below, what will be printed when the main method runs?

Java
public class BankAccount {
    private double balance = 1000.0;

    public double getBalance() {
        return balance;
    }

    public static void main(String[] args) {
        BankAccount account = new BankAccount();
        System.out.println(account.getBalance());
    }
}
A1000.0
BCompilation error: getBalance() not found
C0.0
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Private fields can be accessed inside the class. Public methods can expose them safely.

πŸ”§ Debug
advanced
2:30remaining
Why does this code fail to update the private field?

Look at this Java class. The setAge method tries to update the private field age. Why does the age not change after calling setAge(40)?

Java
public class Employee {
    private int age = 25;

    public void setAge(int age) {
        age = age;
    }

    public int getAge() {
        return age;
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        e.setAge(40);
        System.out.println(e.getAge());
    }
}
AThe parameter 'age' shadows the field 'age', so the assignment does nothing
BThe field 'age' is final and cannot be reassigned
CThe method setAge is never called
DPrivate fields cannot be changed once set
Attempts:
2 left
πŸ’‘ Hint

Check the difference between the parameter and the field names inside the method.

πŸ“ Syntax
advanced
2:00remaining
Which option correctly declares a private field with a public getter in Java?

Choose the correct code snippet that declares a private integer field count and a public method getCount that returns it.

A
private int count;
public int getCount() { return Count; }
B
int private count;
public int getCount() { return count; }
C
private int count;
public int getCount() { return count; }
D
private int count;
public int getCount() { return this.count; }
Attempts:
2 left
πŸ’‘ Hint

Remember Java syntax for access modifiers and case sensitivity.

πŸš€ Application
expert
3:00remaining
How many objects can access the private field directly in this scenario?

Given the following two classes in the same package, how many objects can directly access the private field secret of SecretBox?

public class SecretBox {
    private String secret = "hidden";
}

public class Spy {
    public void tryAccess() {
        SecretBox box = new SecretBox();
        System.out.println(box.secret);
    }
}
AZero objects can access the private field directly
BOnly objects of class Spy can access it
COnly objects of class SecretBox can access it
DAll objects in the same package can access it
Attempts:
2 left
πŸ’‘ Hint

Recall what private means in Java regarding access from other classes.