Java Encapsulation & Abstraction in OOP Quiz

Java
0 Passed
0% acceptance

35 in-depth questions (15 with code) covering encapsulation principles, getters/setters implementation, abstraction concepts, abstracting behavior, and design benefits in object-oriented programming.

35 Questions
~70 minutes
1

Question 1

What is the primary purpose of encapsulation in object-oriented programming?

A
To bundle data and methods that operate on that data into a single unit, while restricting direct access to internal state
B
To make all class members public for easy access
C
To create multiple instances of a class
D
To inherit properties from parent classes
2

Question 2

Which access modifier provides the highest level of encapsulation for a class field?

A
private
B
protected
C
default (package-private)
D
public
3

Question 3

What is the main benefit of using getters and setters instead of public fields?

A
They allow validation, logging, or computation during property access and modification
B
They make the code run faster
C
They reduce memory usage
D
They are required by the Java compiler
4

Question 4

What does abstraction mean in the context of object-oriented design?

A
Hiding complex implementation details and showing only essential features to the user
B
Making all methods and fields public
C
Creating multiple concrete implementations
D
Avoiding inheritance altogether
5

Question 5

Which of the following best describes the difference between encapsulation and abstraction?

A
Encapsulation hides data, abstraction hides implementation
B
They are the same concept
C
Encapsulation is only about access modifiers
D
Abstraction requires abstract classes only
6

Question 6

When should you use an abstract class instead of a concrete class?

A
When you want to provide a common base with some implemented methods and some abstract methods for subclasses
B
When the class will never be instantiated
C
Only when using interfaces
D
Never, concrete classes are always better
7

Question 7

What is the key benefit of abstraction in software design?

A
It reduces complexity by allowing programmers to focus on high-level operations without worrying about low-level details
B
It makes code run faster
C
It eliminates the need for encapsulation
D
It requires more memory
8

Question 8

In encapsulation, why is it important to make fields private and provide public getters/setters?

A
It allows the class to control how its data is accessed and modified, enabling validation and future changes
B
It makes the code more complex
C
It prevents inheritance
D
It is required for all Java classes
9

Question 9

What is an abstract method?

A
A method declared without implementation that must be implemented by concrete subclasses
B
A method that cannot be overridden
C
A method with default implementation
D
A method that is automatically generated
10

Question 10

How does encapsulation contribute to better software maintenance?

A
Changes to internal implementation don't affect external code that uses the class
B
It makes all code public
C
It prevents code reuse
D
It requires more documentation
11

Question 11

What is the output of this encapsulation example?

java
class BankAccount {
    private double balance;
    public void deposit(double amount) {
        if (amount > 0) balance += amount;
    }
    public double getBalance() { return balance; }
}
public class Test {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();
        acc.balance = -1000; // Direct access
        System.out.println(acc.getBalance());
    }
}
A
0.0
B
-1000.0
C
Compilation error
D
Runtime exception
12

Question 12

Which scenario demonstrates good use of abstraction?

A
A List interface hiding whether it's an ArrayList or LinkedList implementation
B
Making all methods private
C
Exposing all internal fields
D
Avoiding interfaces altogether
13

Question 13

What happens when you try to instantiate an abstract class directly?

A
Compilation error
B
Runtime exception
C
It works if the class has no abstract methods
D
It creates an anonymous subclass
14

Question 14

Why are getters and setters considered a form of encapsulation?

A
They provide controlled access to private fields, allowing validation and behavior
B
They make fields public
C
They eliminate the need for private fields
D
They are automatically generated
15

Question 15

What is the result of this abstraction example?

java
abstract class Shape {
    abstract double area();
    void display() { System.out.println("Shape"); }
}
class Circle extends Shape {
    double radius;
    Circle(double r) { radius = r; }
    double area() { return Math.PI * radius * radius; }
}
public class Test {
    public static void main(String[] args) {
        Shape s = new Circle(5);
        System.out.println(s.area());
    }
}
A
78.53981633974483
B
Compilation error
C
0.0
D
Runtime exception
16

Question 16

How does encapsulation improve code reusability?

A
By providing a stable interface that doesn't change when internal implementation changes
B
By making all code unique
C
By preventing inheritance
D
By requiring more code
17

Question 17

What is the difference between an abstract class and an interface in terms of abstraction?

A
Abstract classes provide partial abstraction with some implementation, interfaces provide pure abstraction
B
They are identical
C
Interfaces can have implemented methods
D
Abstract classes cannot have abstract methods
18

Question 18

Why should you avoid public fields in a class?

A
They break encapsulation by allowing direct manipulation of internal state
B
They make the code faster
C
They are required for inheritance
D
They reduce memory usage
19

Question 19

What is the output of this getter/setter example?

java
class Person {
    private String name;
    public String getName() { return name; }
    public void setName(String name) {
        if (name != null && !name.trim().isEmpty()) {
            this.name = name;
        }
    }
}
public class Test {
    public static void main(String[] args) {
        Person p = new Person();
        p.setName("   ");
        System.out.println("'" + p.getName() + "'");
    }
}
A
'null'
B
''
C
' '
D
Compilation error
20

Question 20

How does abstraction help in managing large software projects?

A
It allows developers to work with high-level concepts without understanding all implementation details
B
It makes projects smaller
C
It eliminates the need for documentation
D
It requires more developers
21

Question 21

What is the benefit of using interfaces for abstraction?

A
They enable multiple inheritance and loose coupling between components
B
They provide default implementations
C
They can have private methods
D
They are faster than abstract classes
22

Question 22

What happens in this encapsulation violation example?

java
class Employee {
    public int salary;
}
public class Test {
    public static void main(String[] args) {
        Employee e = new Employee();
        e.salary = -50000;
        System.out.println("Salary: " + e.salary);
    }
}
A
Salary: -50000
B
Compilation error
C
Salary: 0
D
Runtime exception
23

Question 23

When should you make a method abstract?

A
When the method behavior must be defined by each subclass but the signature is common
B
When the method should never be called
C
When the method is very long
D
Always, for better abstraction
24

Question 24

How does encapsulation support the single responsibility principle?

A
By hiding internal details, classes can focus on one responsibility without exposing implementation
B
By making classes larger
C
By requiring multiple responsibilities
D
By eliminating responsibilities
25

Question 25

What is the result of this abstract method implementation?

java
abstract class Animal {
    abstract void makeSound();
    void sleep() { System.out.println("Sleeping"); }
}
class Dog extends Animal {
    void makeSound() { System.out.println("Woof"); }
}
public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.makeSound();
        a.sleep();
    }
}
A
Woof Sleeping
B
Compilation error
C
Only Woof
D
Runtime exception
26

Question 26

Why is abstraction important for testing?

A
It allows mocking interfaces without concrete implementations, enabling isolated unit testing
B
It makes testing slower
C
It requires more test code
D
It prevents automated testing
27

Question 27

What is a common anti-pattern that violates encapsulation?

A
Having public fields that can be modified directly
B
Using private methods
C
Implementing getters and setters
D
Using abstract classes
28

Question 28

How does abstraction improve code maintainability?

A
Changes to implementation don't require changes to client code using the abstraction
B
It makes code harder to understand
C
It requires more documentation
D
It increases coupling
29

Question 29

What is the output of this interface abstraction example?

java
interface Drawable {
    void draw();
    default void erase() { System.out.println("Erasing"); }
}
class Circle implements Drawable {
    public void draw() { System.out.println("Drawing circle"); }
}
public class Test {
    public static void main(String[] args) {
        Drawable d = new Circle();
        d.draw();
        d.erase();
    }
}
A
Drawing circle Erasing
B
Compilation error
C
Only Drawing circle
D
Runtime exception
30

Question 30

Why is encapsulation crucial for thread safety?

A
It allows controlling access to shared state, preventing race conditions
B
It makes classes single-threaded
C
It eliminates the need for synchronization
D
It slows down multi-threaded code
31

Question 31

What is the key difference between encapsulation and information hiding?

A
They are essentially the same concept
B
Encapsulation is about bundling, information hiding is about access control
C
Information hiding is broader and includes encapsulation
D
They are completely different
32

Question 32

How does abstraction support the open-closed principle?

A
It allows extending functionality through new implementations without modifying existing code
B
It prevents any changes to code
C
It requires recompiling all code
D
It makes code less flexible
33

Question 33

What is the result of this encapsulation with validation?

java
class Account {
    private int balance;
    public void setBalance(int balance) {
        if (balance >= 0) {
            this.balance = balance;
        }
    }
    public int getBalance() { return balance; }
}
public class Test {
    public static void main(String[] args) {
        Account a = new Account();
        a.setBalance(-100);
        System.out.println(a.getBalance());
    }
}
A
0
B
-100
C
Compilation error
D
Runtime exception
34

Question 34

Why are abstract classes useful for framework design?

A
They provide a template with common functionality that subclasses customize
B
They prevent any customization
C
They are faster than interfaces
D
They eliminate the need for interfaces
35

Question 35

Mastering encapsulation and abstraction means you now understand that professional OOP code:

A
Hides complexity behind clean interfaces, protects data integrity, and enables flexible, maintainable systems
B
Makes everything public for simplicity
C
Avoids inheritance and polymorphism
D
Prioritizes speed over design

QUIZZES IN Java