Bird
0
0

Given a class with private fields firstName and lastName, how can you create a getter method that returns the full name?

hard📝 Application Q9 of 15
Java - Encapsulation
Given a class with private fields firstName and lastName, how can you create a getter method that returns the full name?
Apublic String getFullName(String firstName, String lastName) { return firstName + lastName; }
Bpublic String getFullName() { return firstName + " " + lastName; }
Cpublic void getFullName() { System.out.println(firstName + lastName); }
Dpublic String setFullName() { return firstName + lastName; }
Step-by-Step Solution
Solution:
  1. Step 1: Understand getter method signature

    Getter returns a String and takes no parameters.
  2. Step 2: Combine fields correctly

    public String getFullName() { return firstName + " " + lastName; } concatenates firstName and lastName with a space and returns it.
  3. Final Answer:

    public String getFullName() { return firstName + " " + lastName; } -> Option B
  4. Quick Check:

    Getter returns combined string [OK]
Quick Trick: Getter can return combined values without parameters [OK]
Common Mistakes:
  • Adding parameters to getter
  • Using void return type for getter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes