0
0
Javaprogramming~20 mins

Instance variables in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Instance Variable Master
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 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);
    }
}
Ared 2020
Bnull 0
Cred 0
Dnull 2020
Attempts:
2 left
πŸ’‘ Hint
Instance variables get default values if not initialized, but here they are initialized.
❓ Predict Output
intermediate
2: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);
    }
}
ACompilation error
B0
Cnull
D5
Attempts:
2 left
πŸ’‘ Hint
Instance variables get default values but constructor can change them.
πŸ”§ Debug
advanced
2: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);
    }
}
AThe instance variable 'score' is final and cannot be changed.
BThe method parameter 'score' shadows the instance variable; assignment affects only the parameter.
CThe code has a syntax error and does not compile.
DInstance variables cannot be assigned inside methods.
Attempts:
2 left
πŸ’‘ Hint
Look at variable names inside the method and how to refer to instance variables.
πŸ“ Syntax
advanced
1: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.
AString name = "Alice";
Bstatic String name = "Alice";
CString name();
DString name := "Alice";
Attempts:
2 left
πŸ’‘ Hint
Instance variables are declared inside the class but outside methods, without static keyword for instance scope.
πŸš€ Application
expert
2: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");
    }
}
A0
B2
C3
D1
Attempts:
2 left
πŸ’‘ Hint
Count all instance variables declared in the class, regardless of initialization.