I had a test in AP Computer Science and I got the following question incorrect:

Given the following class
A public Person class is written, and the class has two private String instance variables to store the person's name: one named firstName and one named lastName. The class has two public accessor methods for these instance variables: public String getFirstName() and public String getLastName().

The class has another method: public String getLastPlusInitial(). This method returns a String consisting of the last name, then a comma and a space, then the first initial, and then a period. For example, if the person's name is Fred Flintstone, then getLastPlusInitial() would return "Flintstone, F."

The class has a fourth method: public String getFirstInitialPlusLast(). This method returns a String consisting of the first initial, then a period, and then the last name. For example, if the person's name is Wilma Flintstone, then getfirsInitialPlusLast() would return "W. Flintstone."

Which of the following could not be used to implement getfirstInitialPlusLast()? (3 points)

I. return (firstName.substring(0,1) + ". " + lastName);
II. return (getFirstName().substring(0,1) + ". " + getLastName());
III. return (getFirstName.substring(0,1) + ". " + getLastName);

Choose one answer.
    a. Statement III only.  
    b. Statements I and II only.    
    c. Statement II only.   
    d. Statements II and III only.  
    e. Statements I, II, and III.  Incorrect    

I chose e as my answer but it was wrong. I was thinking the correct answer is b? Could someone explain it to me just so I know for future reference? Thank you!

3 Years
Discussion Span
Last Post by Markland

Hi Brandon 7,

I am always of the belief that all programmers should be in the habit of testing situation to learn different techniques or solution in programming.

My initial suggest would be that you try to actual create the class "Person" in an IDE (Eclipse, IntelliJ IDEA or any other Java editor), then create the instance variables and generate the getters and setters for the variables in the class (see example below).

public class Person {

    private static String firstName;
    private static String lastName;

    public static String getFirstName() {
        return firstName;

    public static String getLastName() {
        return lastName;

    public static void setFirstName(String fName) {
        firstName = fName;

    public static void setLastName(String lName) {
        lastName = lName;


Then you could test the method, possible like this:

public static String getFirstInitialPlusLastI(){

        return (firstName.substring(0,1) + ". " + lastName);

    public static String getFirstInitialPlusLastII(){

        return (getFirstName().substring(0, 1) + ". " + getLastName());

    //Note: commented because it will not run....
    /*public static String getFirstInitialPlusLastIII(){

        return (getFirstName.substring(0, 1) + ". " + getLastName);

Then the proof would be trying to see what each version of the method generates:

using main() function:

public static void main(String[] args) {
        Scanner user_input = new Scanner( System.in );

        System.out.println("Please enter your first name: ");
        firstName = user_input.next( );

        System.out.println("Please enter your last name: ");
        lastName = user_input.next( );


This would satisfy you curiosity as to what the codes generates:

Therefore, the short response to your question is as follows:
(Based on the code you presented)

Which of the following COULD NOT BE USED to implement getfirstInitialPlusLast()? (3 points)

a. POSSIBLE SOLUTION ( III - could not be used)
b. INCORRECT (This return statement generates the correct answer)
c. INCORRECT (This return statement generates the correct answer)
d. POSSIBLE SOLUTION ( II - will work, however III - could not be used)
e. POSSIBLE SOLUTION ( I and II - will work, however III -could not used)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.