Bird
Raised Fist0
Javaprogramming~20 mins

Real-world modeling 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
πŸŽ–οΈ
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.

Practice

(1/5)
1. What is the main purpose of real-world modeling in Java programming?
easy
A. To avoid using variables in the program
B. To write code that runs faster on computers
C. To make programs use less memory
D. To create classes that represent real-life objects with properties and actions

Solution

  1. Step 1: Understand real-world modeling concept

    Real-world modeling means making classes that represent things from real life, like a Car or Person.
  2. Step 2: Identify the purpose of these classes

    These classes have properties (data) and methods (actions) to organize code and make it easier to understand.
  3. Final Answer:

    To create classes that represent real-life objects with properties and actions -> Option D
  4. Quick Check:

    Real-world modeling = Classes for real-life objects [OK]
Hint: Think: real-world objects become classes with data and actions [OK]
Common Mistakes:
  • Confusing performance optimization with modeling
  • Thinking it means avoiding variables
  • Believing it reduces memory usage automatically
2. Which of the following is the correct way to declare a class named Book in Java?
easy
A. class Book {}
B. Book class {}
C. class = Book {}
D. class Book() {}

Solution

  1. Step 1: Recall Java class declaration syntax

    In Java, a class is declared using the keyword class followed by the class name and curly braces.
  2. Step 2: Check each option

    class Book {} uses correct syntax: class Book {}. Others have wrong order, symbols, or parentheses.
  3. Final Answer:

    class Book {} -> Option A
  4. Quick Check:

    Java class declaration = class Name {} [OK]
Hint: Remember: class keyword + name + curly braces [OK]
Common Mistakes:
  • Putting parentheses after class name
  • Writing 'Book class' instead of 'class Book'
  • Using '=' sign in class declaration
3. What will be the output of this Java code?
class Car {
  String color;
  void displayColor() {
    System.out.println("Color: " + color);
  }
}

public class Main {
  public static void main(String[] args) {
    Car myCar = new Car();
    myCar.color = "Red";
    myCar.displayColor();
  }
}
medium
A. Color: null
B. Color: Red
C. Compilation error
D. No output

Solution

  1. Step 1: Understand object creation and property assignment

    A new Car object is created, and its color property is set to "Red".
  2. Step 2: Analyze method call output

    The displayColor() method prints "Color: " plus the color property, which is "Red".
  3. Final Answer:

    Color: Red -> Option B
  4. Quick Check:

    Property set to "Red" prints "Color: Red" [OK]
Hint: Check property value before method prints it [OK]
Common Mistakes:
  • Assuming default null prints instead of assigned value
  • Thinking code has syntax errors
  • Missing object creation step
4. Identify the error in this Java class modeling a Person:
public class Person {
  String name;
  int age;

  void Person(String n, int a) {
    name = n;
    age = a;
  }
}
medium
A. Constructor has void return type, so it's a method, not a constructor
B. Missing semicolon after variable declarations
C. Class name should be lowercase
D. Variables should be private

Solution

  1. Step 1: Check constructor syntax

    Constructors in Java do not have a return type. Here, void Person(...) is a method, not a constructor.
  2. Step 2: Understand impact of error

    Because of void, this method won't initialize the object when created, causing default values.
  3. Final Answer:

    Constructor has void return type, so it's a method, not a constructor -> Option A
  4. Quick Check:

    Constructor = no return type [OK]
Hint: Constructors never have a return type, not even void [OK]
Common Mistakes:
  • Thinking void is needed for constructors
  • Ignoring constructor syntax rules
  • Confusing methods with constructors
5. You want to model a Library that contains many Book objects. Which design correctly represents this real-world relationship in Java?
hard
A. class Book { Library library; }
B. class Library { Book book; }
C. class Library { List<Book> books = new ArrayList<>(); }
D. class Library { int bookCount; }

Solution

  1. Step 1: Understand the relationship between Library and Books

    A library contains many books, so it should hold a collection (like a list) of Book objects.
  2. Step 2: Analyze each option

    class Library { List<Book> books = new ArrayList<>(); } uses a List<Book> to hold many books, correctly modeling the relationship. class Library { Book book; } holds only one Book, whereas class Book { Library library; } reverses the relationship, and class Library { int bookCount; } only counts books without storing them.
  3. Final Answer:

    class Library { List<Book> books = new ArrayList<>(); } -> Option C
  4. Quick Check:

    Many books = collection in Library class [OK]
Hint: Use collections to model 'many' relationships [OK]
Common Mistakes:
  • Using single object instead of collection for many items
  • Confusing ownership direction between classes
  • Using only counters without storing objects