Encapsulation in Java MCQs Exercise II

Ques 1 Encapsulation


Which of the following statements is true regarding encapsulation?

A It is not an important concept in Java programming
B It promotes data hiding and reduces the risk of unintended interference and misuse
C It is only applicable to primitive data types
D It can only be implemented with the 'public' access modifier

Ques 2 Encapsulation


Which of the following code snippets demonstrates encapsulation correctly?

A
public class Car {
    public String make;
    public String model;
}
B
public class Car {
    private String make;
	private String model;
	public void setMake(String make) {
		this.make = make;
	}
	public String getMake() {
		return make;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public String getModel() {
		return model;
	}
}
C
public class Car {
    String make;
    String model;
}
D
public class Car {
    private String make;
    private String model;
    public Car(String make, String model) {
	this.make = make;
	this.model = model;
    }
}