professor-curious-logo
professor-curious-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What are the general steps to write a getter method?

  1. Determine the instance variable to access. 2. Define a public method with the return type matching the variable's type. 3. Use a return statement to return the variable's value.
Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What are the general steps to write a getter method?

  1. Determine the instance variable to access. 2. Define a public method with the return type matching the variable's type. 3. Use a return statement to return the variable's value.

What are the general steps to write a toString() method?

  1. Use the @Override annotation. 2. Construct a string representation of the object's state using string concatenation. 3. Return the constructed string.

What is the process when System.out.println(object) is called?

  1. The object's toString() method is automatically called. 2. The toString() method returns a string. 3. The string is printed to the console.

What are the steps to access an object's data using a getter method from another class?

  1. Create an instance of the class containing the getter method. 2. Call the getter method on the object using the dot operator. 3. Store or use the returned value.

What happens when a getter method is called?

  1. The program executes the code within the getter method. 2. The getter method returns a copy of the value of the instance variable. 3. The program continues execution from where the getter method was called.

What happens when the return keyword is encountered in a method?

  1. The method's execution is immediately terminated. 2. The specified value is returned to the calling method. 3. Execution resumes in the calling method at the point immediately following the method call.

What is the process of creating a new object using a constructor?

  1. Allocate memory for the new object. 2. Execute the constructor code, initializing instance variables. 3. Return a reference to the newly created object.

What are the steps to call a method on an object?

  1. Use the object's reference followed by the dot operator. 2. Specify the method name and any required arguments within parentheses. 3. The method is executed using the object's data.

What are the steps when a program calls a method?

  1. The program jumps to the beginning of the called method. 2. The code inside the method is executed. 3. Once the method finishes, the program returns to the point where the method was called.

What is the process of accessing a private variable from within the same class?

  1. The private variable is directly accessed by its name within a method of the class. 2. No special syntax is required, as access is implicitly granted within the class scope.

What is an accessor method?

A method that allows clients to access the data of an object.

What is a getter method?

An accessor method used to get the value of a specific instance variable.

What is the toString() method?

A method that returns information about an object as a string.

What is a client in the context of accessor methods?

Objects, classes, and users that access the data of an object.

What does '@Override' do?

Indicates that a method is overriding a method in a superclass.

What is 'return by value'?

Returning a copy of the value of a primitive data type.

What is returned when a getter method returns a reference to an object?

A copy of the reference to the object is returned, not a copy of the object itself.

What happens when the 'return' keyword is triggered?

The program immediately returns to the point after whatever called the method.

What is the access modifier of accessor methods?

public

Why are accessor methods made public?

To allow other clients (objects, classes, and users) to access the data of an object.

Identify the error in the following code:

java
public class Assignment {
 private boolean answer;
 @Override
 public String toString() {
 return "This is an assignment with correct answer " + answer;
 }
}

The instance variable is named 'correctAnswer', not 'answer'.

What does the following code output?

java
Student bob = new Student(10, "Bob Smith", 16);
System.out.println(bob);

Bob Smith, a 10th grade high school student

Complete the getter method for the 'name' instance variable in the Student class:

java
public class Student {
 private String name;
 public String ________() {
 return name;
 }
}

getName

What is the return type of the getGradeLevel() method in the Student class?

int

What is the return type of the getName() method in the Student class?

String

What will be the output of the following code snippet?

java
Assignment assignment = new Assignment(true);
System.out.println(assignment);

This is an assignment with correct answer true

Identify the error in the following code:

java
public class Student {
 private int age;
 public void getAge() {
 return age;
 }
}

The return type of getAge() should be int, not void.

Complete the following getter method:

java
public class Student {
 private Assignment assignment;
 public ________ getCurrentAssignment() {
 return assignment;
 }
}

Assignment

What is the purpose of the following method?

java
public Assignment returnCurrentAssignment() {
 return assignment;
}

It returns the current assignment the student is working on.

What is wrong with the following code?

java
public class Student {
 private int gradeLevel;
 public String getGradeLevel() {
 return gradeLevel;
 }
}

The return type of the getGradeLevel() method should be int, not String.