Challenge - 5 Problems
Real-world Modeling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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(); } }
Attempts:
2 left
π‘ Hint
Think about which method is called when an Animal reference points to a Dog object.
β Incorrect
In Java, the method called depends on the actual object type, not the reference type. Since 'a' refers to a Dog object, Dog's sound() method runs, printing 'Bark'.
π§ Conceptual
intermediate2: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?
Attempts:
2 left
π‘ Hint
Consider which collection allows fast lookup by a unique key.
β Incorrect
HashMap allows fast lookup by key (ISBN), making it ideal for quickly finding books by their unique identifier.
π§ Debug
advanced2: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()); } }
Attempts:
2 left
π‘ Hint
Check what happens when withdrawal amount is more than balance.
β Incorrect
The withdraw method checks if balance is enough before subtracting. If not, it prints a message and does not change balance. So balance stays 100.
π Syntax
advanced2: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; } }
Attempts:
2 left
π‘ Hint
Look carefully at the constructor's assignment line.
β Incorrect
Java statements must end with a semicolon. The line 'this.model = model' is missing a semicolon, causing a syntax error.
π Application
expert3: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(); } }
Attempts:
2 left
π‘ Hint
Check how abstract methods are implemented in subclasses and which method runs.
β Incorrect
Both Manager and Developer implement the abstract method work(). The output matches their respective implementations.