LearnFrenzy
Sign in Take a free test

Encapsulation · Exercise 3 of 4

Encapsulation Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program contains an encapsulation mistake that javac rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Battery {
    private int charge = 50;
    public void setCharge(int c) { if (c >= 0 && c <= 100) charge = c; }
    public int getCharge() { return charge; }
}

public class Main {
    public static void main(String[] args) {
        Battery b = new Battery();
        b.charge = 150;
        System.out.println(b.getCharge());
    }
}
A charge must be static
B charge is private, so Main cannot assign it directly; it must use setCharge, which would reject 150
C getCharge must be private
D 150 is not an int
Question page & discussion
Question 2 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Aadhaar {
    private final String number;
    Aadhaar(String n) { number = n; }
    public String getNumber() { return number; }
    public void setNumber(String n) { number = n; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Aadhaar("1234").getNumber());
    }
}
A number must be public
B number is final, so it can only be assigned in the constructor; a read-only property must not have a setter
C Getters cannot return final fields
D Strings cannot be final
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Order {
    private String status = "NEW";
    private void setStatus(String s) { status = s; }
    public void ship() { setStatus("SHIPPED"); }
    public String getStatus() { return status; }
}

public class Main {
    public static void main(String[] args) {
        Order o = new Order();
        o.setStatus("DELIVERED");
        System.out.println(o.getStatus());
    }
}
A ship must be static
B setStatus is private, so only Order can call it; outside code must use operations such as ship()
C status must be final
D Strings cannot be compared
Question page & discussion
Question 4 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Grade {
    private int marks = 72;

    public String getGrade() {
        if (marks >= 75) return "A";
        else if (marks >= 50) return "B";
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Grade().getGrade());
    }
}
A marks must be public
B When marks is below 50, no return statement runs, so javac reports "missing return statement"
C else if is not allowed in getters
D String getters must be static
Question page & discussion
Question 5 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
record Point(int x, int y) { }

public class Main {
    public static void main(String[] args) {
        Point p = new Point(1, 2);
        p.x = 10;
        System.out.println(p);
    }
}
A Records need setters
B Record fields are private and final; x cannot be read as a field or assigned from outside – create a new Point instead
C Records cannot have int components
D println cannot print records
Question page & discussion
Question 6 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Item {
    private int quantity;
    public void setQuantity(String q) { quantity = q; }
    public int getQuantity() { return quantity; }
}

public class Main {
    public static void main(String[] args) {
        Item i = new Item();
        i.setQuantity("5");
        System.out.println(i.getQuantity());
    }
}
A quantity must be static
B A String cannot be assigned to the int field; take an int parameter or parse it with Integer.parseInt(q)
C Setters cannot take Strings
D getQuantity must return String
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Student {
    private String name = "Asha";
    public static String getName() { return name; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Student.getName());
    }
}
A name must be public
B getName is static but name is an instance field – there is no object whose name it could return; remove static and call it on an object
C Getters must be void
D Student needs a constructor
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Account {
    private double balance = 1000;
    protected double getBalance() { return balance; }
}

class SavingsAccount extends Account {
    double interest() { return balance * 0.04; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new SavingsAccount().interest());
    }
}
A interest must be public
B balance is private to Account, so SavingsAccount must use the protected getBalance() instead
C SavingsAccount must be abstract
D 0.04 must be a float
Question page & discussion
Question 9 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Invoice {
    private final String number;
    private double amount;

    Invoice(double amount) { this.amount = amount; }

    public String getNumber() { return number; }
    public double getAmount() { return amount; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Invoice(500).getAmount());
    }
}
A amount must be final
B The final field number is never assigned in the constructor, so javac reports it "might not have been initialized"
C getNumber must be private
D Invoice needs a setter
Question page & discussion
Question 10 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
interface HasPrice {
    double getPrice();
}

class Book implements HasPrice {
    private final double price = 399;
    private double getPrice() { return price; }
}

public class Main {
    public static void main(String[] args) {
        HasPrice h = new Book();
        System.out.println(h.getPrice());
    }
}
A price must not be final
B getPrice implements a public interface method, so it must be public; private would hide what the interface promises
C Interfaces cannot declare getters
D Book must be abstract
Question page & discussion
Up next · Exercise 4Encapsulation Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links