3.12. Answers to sample exam questions

Q3-1.

Which option defines a well-encapsulated class?

  1. class Template {
        public String font;
    }
  2. class Template2 {
        public String font;
        public void setFont(String font) {
            this.font = font;
        }
        public String getFont() {
            return font;
        }
    }
  3. class Template3 {
        private String font;
        public String author;
        public void setFont(String font) {
            this.font = font;
        }
        public String getFont() {
            return font;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
        public String getAuthor() {
            return author;
        }
    }
  4. None of the above

Answer: d

Explanation: Options (a), (b), and (c) are incorrect because they all define a public instance variable. A well-encapsulated class should be like a capsule, hiding its ...

Get OCA Java SE 8 Programmer I Certification Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.