0
0
Javaprogramming~20 mins

Getter and setter methods in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Getter and Setter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of getter and setter usage
What is the output of this Java program that uses getter and setter methods?
Java
public class Person {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public static void main(String[] args) {
        Person p = new Person();
        p.setName("Alice");
        System.out.println(p.getName());
    }
}
AAlice
Bnull
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint
Look at what value is assigned using the setter before printing with the getter.
❓ Predict Output
intermediate
2:00remaining
Value of private field after setter call
What is the value of the private field 'age' after running this code?
Java
public class Animal {
    private int age;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        if(age > 0) {
            this.age = age;
        }
    }
    public static void main(String[] args) {
        Animal a = new Animal();
        a.setAge(-5);
        System.out.println(a.getAge());
    }
}
ACompilation error
B0
CRuntime error
D-5
Attempts:
2 left
πŸ’‘ Hint
Check the condition inside the setter before assigning the value.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in setter method
What error will this code cause when compiled or run?
Java
public class Car {
    private String model;
    public void setModel(String model) {
        model = model;
    }
    public String getModel() {
        return model;
    }
    public static void main(String[] args) {
        Car c = new Car();
        c.setModel("Tesla");
        System.out.println(c.getModel());
    }
}
ACompilation error due to variable shadowing
BOutput is Tesla
COutput is null
DRuntime NullPointerException
Attempts:
2 left
πŸ’‘ Hint
Check if the setter actually changes the private field or just the parameter.
πŸ“ Syntax
advanced
2:00remaining
Syntax error in getter method
Which option shows the correct syntax for a getter method for a private int field 'score'?
Apublic int getScore() { return score; }
Bpublic int getScore() return score; }
Cpublic int getScore() { score; }
Dpublic int getScore() { return; }
Attempts:
2 left
πŸ’‘ Hint
A getter must return the field value inside braces with a return statement.
πŸš€ Application
expert
2:00remaining
Effect of setter on immutable field
Given this class, what happens when trying to set the 'id' field using the setter?
Java
public class User {
    private final int id;
    public User(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}
ASetter is ignored silently
BSetter changes the id successfully
CRuntime error when calling setId
DCompilation error because 'id' is final and cannot be assigned in setter
Attempts:
2 left
πŸ’‘ Hint
Final fields cannot be reassigned after initialization.