← Quiz: Encapsulation and Abstraction: all 10 questions
Quiz: Encapsulation and Abstraction · Question 6 of 10
What does this print? (code)
What does this print?
class Student {
private int marks;
public int getMarks() { return marks; }
public void setMarks(int m) {
if (m >= 0 && m <= 100) marks = m;
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.setMarks(70);
s.setMarks(-20);
s.setMarks(105);
System.out.println(s.getMarks());
}
}
- A.-20
- B.105
- C.0
- D.70
Answer & explanation
Correct answer: 70
The setter ignores invalid values, so the last valid mark, 70, stays.
Can you get the rest right without peeking?
Take the timed Quiz: Encapsulation and Abstraction quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →