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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.