0
0
Javaprogramming~20 mins

OOP principles overview in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.