0
0
Javaprogramming~20 mins

Object interaction in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Master of Object Interaction
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 involving object interaction?

Consider the following Java classes and main method. What will be printed when the program runs?

Java
class Engine {
    int power;
    Engine(int power) {
        this.power = power;
    }
    int getPower() {
        return power;
    }
}

class Car {
    Engine engine;
    Car(Engine engine) {
        this.engine = engine;
    }
    int totalPower() {
        return engine.getPower() + 50;
    }
}

public class Main {
    public static void main(String[] args) {
        Engine e = new Engine(100);
        Car c = new Car(e);
        System.out.println(c.totalPower());
    }
}
A150
B0
C50
D100
Attempts:
2 left
πŸ’‘ Hint

Look at how Car uses the Engine object to calculate total power.

❓ Predict Output
intermediate
2:00remaining
What is the output when two objects interact with method calls?

Given these classes, what will be printed?

Java
class Wallet {
    private int money;
    Wallet(int money) {
        this.money = money;
    }
    void spend(int amount) {
        money -= amount;
    }
    int getMoney() {
        return money;
    }
}

class Person {
    Wallet wallet;
    Person(Wallet wallet) {
        this.wallet = wallet;
    }
    void buySomething(int price) {
        wallet.spend(price);
    }
}

public class Main {
    public static void main(String[] args) {
        Wallet w = new Wallet(200);
        Person p = new Person(w);
        p.buySomething(70);
        System.out.println(w.getMoney());
    }
}
A70
B200
C130
D-70
Attempts:
2 left
πŸ’‘ Hint

Think about how the Person uses the Wallet to spend money.

πŸ”§ Debug
advanced
2:00remaining
What error does this code produce when objects interact incorrectly?

Examine the code below. What error will occur when running it?

Java
class Book {
    String title;
    Book(String title) {
        this.title = title;
    }
}

class Library {
    Book book;
    Library() {
        // book not initialized
    }
    void printTitle() {
        System.out.println(book.title);
    }
}

public class Main {
    public static void main(String[] args) {
        Library lib = new Library();
        lib.printTitle();
    }
}
AArrayIndexOutOfBoundsException
BCompilation error: book not initialized
CPrints empty string
DNullPointerException
Attempts:
2 left
πŸ’‘ Hint

Think about what happens if you try to access a field of an object that was never set.

πŸ“ Syntax
advanced
2:00remaining
Which option causes a compilation error due to incorrect object interaction?

Look at these code snippets. Which one will NOT compile?

Aclass A { void foo() {} } class B { void bar() { A a; a.foo(); } }
Bclass A { void foo() {} } class B { A a = null; void bar() { a.foo(); } }
Cclass A { void foo() {} } class B { A a = new A(); void bar() { a.foo(); } }
D} } ;)(oof.a { )(rab diov ;)(A wen = a A { B ssalc } }{ )(oof diov { A ssalc
Attempts:
2 left
πŸ’‘ Hint

Check if the object a is initialized before calling its method.

πŸš€ Application
expert
3:00remaining
How many objects are created and what is the final output?

Analyze the code below. How many objects are created and what is printed?

Java
class Node {
    int value;
    Node next;
    Node(int value) {
        this.value = value;
    }
    void setNext(Node next) {
        this.next = next;
    }
    int sum() {
        if (next == null) return value;
        return value + next.sum();
    }
}

public class Main {
    public static void main(String[] args) {
        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);
        n1.setNext(n2);
        n2.setNext(n3);
        System.out.println(n1.sum());
    }
}
A1 object created; output: 6
B3 objects created; output: 6
C3 objects created; output: 3
D2 objects created; output: 6
Attempts:
2 left
πŸ’‘ Hint

Count each new Node(...) call and understand the recursive sum method.