Challenge - 5 Problems
Instance Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput 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()); } }
Attempts:
2 left
π‘ Hint
Look at what the getModel() method returns.
β Incorrect
The instance method getModel() returns the model field, which was set to "Tesla" in the constructor. So the output is "Tesla".
β Predict Output
intermediateInstance 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()); } }
Attempts:
2 left
π‘ Hint
Each call to increment increases count by one.
β Incorrect
The increment method increases the count field by 1 each time it is called. It is called twice, so count becomes 2.
π§ Debug
advancedIdentify 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(); } }
Attempts:
2 left
π‘ Hint
Static methods cannot call instance methods without an object.
β Incorrect
The main method is static and tries to call printName() which is an instance method without an object. This causes a compilation error.
β Predict Output
advancedInstance 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); } }
Attempts:
2 left
π‘ Hint
Multiply 3 by 4.
β Incorrect
The multiply method returns the product of the two parameters. 3 * 4 = 12.
π§ Conceptual
expertUnderstanding 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()); } }
Attempts:
2 left
π‘ Hint
Each object has its own isOn state.
β Incorrect
kitchenLight switches on, so its isOn is true. bedroomLight remains off, so its isOn is false. Output is "On Off".
