0
0
Javaprogramming~10 mins

Why encapsulation is required in Java - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a private variable in a class.

Java
public class Person {
    private String [1];
}
Drag options to blanks, or click blank then click option'
APerson
BgetName
Cname
Dpublic
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using method names or access modifiers instead of variable names.
2fill in blank
medium

Complete the code to create a public getter method for the private variable.

Java
public String [1]() {
    return name;
}
Drag options to blanks, or click blank then click option'
AsetName
Bname
CName
DgetName
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using setter method names or variable names instead of getter method names.
3fill in blank
hard

Fix the error in the setter method to properly set the private variable.

Java
public void setName(String [1]) {
    this.name = [1];
}
Drag options to blanks, or click blank then click option'
AsetName
Bname
CName
Dthis
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different parameter names or keywords incorrectly.
4fill in blank
hard

Fill both blanks to complete the encapsulated class with private variable and public getter.

Java
public class [1] {
    private int [2];

    public int getAge() {
        return age;
    }
}
Drag options to blanks, or click blank then click option'
AStudent
Bage
Cscore
DPerson
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using variable names that do not match getter methods.
5fill in blank
hard

Fill all three blanks to complete the class with private variable, getter, and setter methods.

Java
public class [1] {
    private String [2];

    public String getName() {
        return [2];
    }

    public void setName(String [3]) {
        this.name = [3];
    }
}
Drag options to blanks, or click blank then click option'
AEmployee
Bname
Did
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different names for setter parameter and private variable.