Challenge - 5 Problems
Instance Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of this Java code using instance variables?
Consider the following Java class and main method. What will be printed when the program runs?
Java
public class Car { String color = "red"; int year = 2020; public static void main(String[] args) { Car myCar = new Car(); System.out.println(myCar.color + " " + myCar.year); } }
Attempts:
2 left
π‘ Hint
Instance variables get default values if not initialized, but here they are initialized.
β Incorrect
The instance variables 'color' and 'year' are initialized with 'red' and 2020 respectively. So the output prints these values.
β Predict Output
intermediate2:00remaining
What is the value of the instance variable after object creation?
Look at this Java class. What will be the value of 'count' for the object 'obj' after creation?
Java
public class Counter { int count; public Counter() { count = 5; } public static void main(String[] args) { Counter obj = new Counter(); System.out.println(obj.count); } }
Attempts:
2 left
π‘ Hint
Instance variables get default values but constructor can change them.
β Incorrect
The constructor sets the instance variable 'count' to 5 when the object is created. So printing obj.count outputs 5.
π§ Debug
advanced2:30remaining
Why does this code print 0 instead of 10?
This Java code intends to set the instance variable 'score' to 10, but it prints 0. What is the cause?
Java
public class Game { int score; public void setScore(int score) { score = score; } public static void main(String[] args) { Game g = new Game(); g.setScore(10); System.out.println(g.score); } }
Attempts:
2 left
π‘ Hint
Look at variable names inside the method and how to refer to instance variables.
β Incorrect
Inside setScore, the parameter 'score' hides the instance variable 'score'. The assignment 'score = score;' assigns the parameter to itself, leaving the instance variable unchanged at 0.
π Syntax
advanced1:30remaining
Which option correctly declares and initializes an instance variable?
Choose the correct way to declare and initialize an instance variable 'name' of type String in a Java class.
Attempts:
2 left
π‘ Hint
Instance variables are declared inside the class but outside methods, without static keyword for instance scope.
β Incorrect
Option A correctly declares an instance variable 'name' initialized to "Alice". Option A makes it static (class variable). Option A is a method declaration. Option A uses invalid syntax.
π Application
expert2:30remaining
How many instance variables does this Java object have after creation?
Given the class below, how many instance variables does an object of this class have after creation?
Java
public class Person { String firstName; String lastName; int age; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public static void main(String[] args) { Person p = new Person("John", "Doe"); } }
Attempts:
2 left
π‘ Hint
Count all instance variables declared in the class, regardless of initialization.
β Incorrect
The class declares three instance variables: firstName, lastName, and age. All exist in every object, even if age is not set explicitly.