0
0
Javaprogramming~20 mins

Instance methods in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Instance Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of instance method call
What is the output of this Java program when run?
Java
public class Car {
    private String model;
    public Car(String model) {
        this.model = model;
    }
    public String getModel() {
        return model;
    }
    public static void main(String[] args) {
        Car car = new Car("Tesla");
        System.out.println(car.getModel());
    }
}
ATesla
Bcar
Cnull
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Look at what the getModel() method returns.
❓ Predict Output
intermediate
2:00remaining
Instance method modifying object state
What will be printed after running this Java code?
Java
public class Counter {
    private int count = 0;
    public void increment() {
        count++;
    }
    public int getCount() {
        return count;
    }
    public static void main(String[] args) {
        Counter c = new Counter();
        c.increment();
        c.increment();
        System.out.println(c.getCount());
    }
}
A1
B0
CCompilation error
D2
Attempts:
2 left
πŸ’‘ Hint
Each call to increment increases count by one.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in instance method usage
What error will this Java code produce when compiled or run?
Java
public class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public void printName() {
        System.out.println(name);
    }
    public static void main(String[] args) {
        printName();
    }
}
ANo error, prints the name
BCannot make a static reference to the non-static method printName()
CCompilation error: missing semicolon
DNullPointerException at runtime
Attempts:
2 left
πŸ’‘ Hint
Static methods cannot call instance methods without an object.
❓ Predict Output
advanced
2:00remaining
Instance method with parameter and return value
What is the output of this Java program?
Java
public class Calculator {
    public int multiply(int a, int b) {
        return a * b;
    }
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        int result = calc.multiply(3, 4);
        System.out.println(result);
    }
}
A12
B7
C34
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Multiply 3 by 4.
🧠 Conceptual
expert
3:00remaining
Understanding instance method behavior with multiple objects
Consider this Java code. What will be printed when it runs?
Java
public class Light {
    private boolean isOn = false;
    public void switchOn() {
        isOn = true;
    }
    public void switchOff() {
        isOn = false;
    }
    public String getState() {
        return isOn ? "On" : "Off";
    }
    public static void main(String[] args) {
        Light kitchenLight = new Light();
        Light bedroomLight = new Light();
        kitchenLight.switchOn();
        System.out.println(kitchenLight.getState() + " " + bedroomLight.getState());
    }
}
AOn On
BOff Off
COn Off
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Each object has its own isOn state.