🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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());
    }
}
  1. A.-20
  2. B.105
  3. C.0
  4. 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 →