0
0
Javaprogramming~20 mins

Real-world modeling in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Real-world Modeling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of class inheritance and method overriding
What is the output of this Java program that models animals and their sounds?
Java
class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
ABark
BCompilation error
CSome sound
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Think about which method is called when an Animal reference points to a Dog object.
🧠 Conceptual
intermediate
2:00remaining
Choosing the right data structure for modeling a library
You want to model a library system where each book has a unique ISBN and you need to quickly find a book by its ISBN. Which Java collection is best suited for this?
ALinkedList<Book>
BArrayList<Book>
CHashMap<String, Book>
DTreeSet<Book>
Attempts:
2 left
πŸ’‘ Hint
Consider which collection allows fast lookup by a unique key.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in this class modeling a bank account
What error will this Java code produce when compiled or run?
Java
public class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        balance = initialBalance;
    }

    public void withdraw(double amount) {
        if (balance >= amount) {
            balance = balance - amount;
        } else {
            System.out.println("Insufficient funds");
        }
    }

    public double getBalance() {
        return balance;
    }

    public static void main(String[] args) {
        BankAccount acc = new BankAccount(100);
        acc.withdraw(150);
        System.out.println("Balance: " + acc.getBalance());
    }
}
AOutput: Insufficient funds\nBalance: 100.0
BRuntime error: negative balance allowed
COutput: Balance: -50.0
DCompilation error: missing return type
Attempts:
2 left
πŸ’‘ Hint
Check what happens when withdrawal amount is more than balance.
πŸ“ Syntax
advanced
2:00remaining
Identify the syntax error in this Java class modeling a vehicle
Which option correctly identifies the syntax error in this code?
Java
public class Vehicle {
    private String model;

    public Vehicle(String model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }
}
AMissing return type in getModel method
BMissing semicolon after 'this.model = model' in constructor
CClass name should be lowercase
DConstructor name does not match class name
Attempts:
2 left
πŸ’‘ Hint
Look carefully at the constructor's assignment line.
πŸš€ Application
expert
3:00remaining
Modeling a simple employee hierarchy with abstract classes
Given the abstract class Employee and two subclasses Manager and Developer, which code snippet correctly implements the abstract method and prints the correct output when run?
Java
abstract class Employee {
    String name;
    Employee(String name) {
        this.name = name;
    }
    abstract void work();
}

class Manager extends Employee {
    Manager(String name) {
        super(name);
    }
    void work() {
        System.out.println(name + " manages the team.");
    }
}

class Developer extends Employee {
    Developer(String name) {
        super(name);
    }
    void work() {
        System.out.println(name + " writes code.");
    }
}

public class Company {
    public static void main(String[] args) {
        Employee e1 = new Manager("Alice");
        Employee e2 = new Developer("Bob");
        e1.work();
        e2.work();
    }
}
ACompilation error: cannot instantiate abstract class Employee
BAlice writes code.\nBob manages the team.
CRuntime error: abstract method not implemented
DAlice manages the team.\nBob writes code.
Attempts:
2 left
πŸ’‘ Hint
Check how abstract methods are implemented in subclasses and which method runs.