Bird
Raised Fist0
Javaprogramming~20 mins

OOP principles overview in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
OOP Mastery Badge
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 using encapsulation?

Consider the following Java class that uses encapsulation. What will be printed when the main method runs?

Java
class Person {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.setName("Alice");
        System.out.println(p.getName());
    }
}
ACompilation error due to private field access
BAlice
Cnull
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Think about how private fields are accessed through public methods.

❓ Predict Output
intermediate
2:00remaining
What is the output demonstrating inheritance and method overriding?

Look at this Java code with inheritance and method overriding. What will it print?

Java
class Animal {
    public String sound() {
        return "Some sound";
    }
}

class Dog extends Animal {
    @Override
    public String sound() {
        return "Bark";
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println(a.sound());
    }
}
ABark
BSome sound
CCompilation error due to override
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Remember how method overriding works with polymorphism.

❓ Predict Output
advanced
2:00remaining
What is the output of this Java code demonstrating abstraction with abstract classes?

Analyze this Java code using an abstract class. What will be printed when the main method runs?

Java
abstract class Vehicle {
    abstract void start();
    void stop() {
        System.out.println("Vehicle stopped");
    }
}

class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car started");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
        v.stop();
    }
}
ACompilation error due to abstract method
BRuntime error
CVehicle started\nVehicle stopped
DCar started\nVehicle stopped
Attempts:
2 left
πŸ’‘ Hint

Think about how abstract methods are implemented in subclasses.

❓ Predict Output
advanced
2:00remaining
What error does this Java code produce related to interface implementation?

Consider this Java code where a class implements an interface but misses a method. What error occurs?

Java
interface Flyer {
    void fly();
}

class Bird implements Flyer {
    // Missing fly() method implementation
}

public class Main {
    public static void main(String[] args) {
        Bird b = new Bird();
        b.fly();
    }
}
ACompilation error: Bird is not abstract and does not override fly()
BRuntime error: Method fly() not found
CNo error, prints nothing
DCompilation error: Interface Flyer cannot be implemented
Attempts:
2 left
πŸ’‘ Hint

Interfaces require all methods to be implemented unless the class is abstract.

🧠 Conceptual
expert
2:00remaining
Which OOP principle is best demonstrated by this Java code snippet?

Given this Java code snippet, which core OOP principle does it best illustrate?

class Shape {
    void draw() {
        System.out.println("Drawing shape");
    }
}

class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing circle");
    }
}

class Square extends Shape {
    @Override
    void draw() {
        System.out.println("Drawing square");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s1 = new Circle();
        Shape s2 = new Square();
        s1.draw();
        s2.draw();
    }
}
AEncapsulation
BInheritance
CPolymorphism
DAbstraction
Attempts:
2 left
πŸ’‘ Hint

Think about how the same method call behaves differently depending on the object type.

Practice

(1/5)
1. Which of the following is NOT one of the four main principles of Object-Oriented Programming (OOP)?
easy
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Compilation

Solution

  1. Step 1: Recall the four main OOP principles

    The four main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction.
  2. Step 2: Identify the option not in the list

    Compilation is a process of converting code to machine language, not an OOP principle.
  3. Final Answer:

    Compilation -> Option D
  4. Quick Check:

    OOP principles exclude Compilation [OK]
Hint: Remember the four OOP pillars: E, I, P, A [OK]
Common Mistakes:
  • Confusing compilation with abstraction
  • Mixing OOP principles with programming processes
2. Which Java keyword is used to inherit properties from a parent class?
easy
A. implements
B. inherits
C. extends
D. super

Solution

  1. Step 1: Identify the keyword for class inheritance in Java

    Java uses the keyword extends to inherit from a parent class.
  2. Step 2: Differentiate from other keywords

    implements is for interfaces, super accesses parent members, and inherits is not a Java keyword.
  3. Final Answer:

    extends -> Option C
  4. Quick Check:

    Inheritance keyword = extends [OK]
Hint: Use 'extends' to inherit classes in Java [OK]
Common Mistakes:
  • Using 'implements' for class inheritance
  • Confusing 'super' with inheritance keyword
3. What will be the output of the following Java code?
class Animal {
  void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
  void sound() { System.out.println("Bark"); }
}
public class Test {
  public static void main(String[] args) {
    Animal a = new Dog();
    a.sound();
  }
}
medium
A. Bark
B. Animal sound
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand method overriding and polymorphism

    The object a is declared as Animal but refers to a Dog instance. The sound() method is overridden in Dog.
  2. Step 2: Determine which method runs at runtime

    Java uses dynamic method dispatch, so the Dog's sound() method runs, printing "Bark".
  3. Final Answer:

    Bark -> Option A
  4. Quick Check:

    Overridden method runs = Bark [OK]
Hint: Overridden methods run from actual object type [OK]
Common Mistakes:
  • Thinking declared type method runs
  • Expecting compilation or runtime errors
4. Identify the error in the following Java code snippet:
class Vehicle {
  private int speed;
  public int getSpeed() { return speed; }
}
class Car extends Vehicle {
  public void setSpeed(int speed) { this.speed = speed; }
}
medium
A. Cannot access private field 'speed' directly in subclass
B. Cannot override getSpeed() method
C. Missing constructor in Car class
D. No error, code is correct

Solution

  1. Step 1: Check access to private fields in subclass

    The field speed is private in Vehicle, so Car cannot access it directly.
  2. Step 2: Analyze the setSpeed method in Car

    Car's setSpeed tries to assign this.speed, which is not accessible, causing a compile error.
  3. Final Answer:

    Cannot access private field 'speed' directly in subclass -> Option A
  4. Quick Check:

    Private fields inaccessible in subclass [OK]
Hint: Private fields are not visible in subclasses [OK]
Common Mistakes:
  • Assuming private fields are inherited
  • Ignoring access modifiers
5. You want to design a Java class hierarchy where different shapes (Circle, Rectangle) can calculate their area. Which OOP principle best supports writing a method double getArea() in a base class or interface and having each shape provide its own implementation?
hard
A. Encapsulation
B. Polymorphism
C. Abstraction
D. Inheritance

Solution

  1. Step 1: Understand the scenario of method overriding

    Each shape class provides its own version of getArea(), meaning the method behaves differently depending on the object.
  2. Step 2: Identify the OOP principle for multiple forms of a method

    This is Polymorphism, where the same method name works differently in subclasses.
  3. Final Answer:

    Polymorphism -> Option B
  4. Quick Check:

    Different behaviors for same method = Polymorphism [OK]
Hint: Same method, different behaviors = Polymorphism [OK]
Common Mistakes:
  • Confusing abstraction with polymorphism
  • Thinking inheritance alone handles this