Bird
0
0

Which of the following is the correct syntax for a getter method for a private String variable named title?

easy📝 Syntax Q3 of 15
Java - Encapsulation
Which of the following is the correct syntax for a getter method for a private String variable named title?
Apublic void getTitle() { return title; }
Bpublic String setTitle() { return title; }
Cpublic String getTitle() { return title; }
Dpublic void setTitle(String title) { this.title = title; }
Step-by-Step Solution
Solution:
  1. Step 1: Identify getter method syntax

    Getter returns the variable's value and has return type matching variable type.
  2. Step 2: Check options

    public String getTitle() { return title; } returns String and returns title correctly.
  3. Final Answer:

    public String getTitle() { return title; } -> Option C
  4. Quick Check:

    Getter returns variable value [OK]
Quick Trick: Getter returns variable value with matching return type [OK]
Common Mistakes:
  • Using void return type for getter
  • Confusing setter syntax with getter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes