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

What is the difference between a getter method and a setter method?

Getter: Accesses the value of an instance variable. Setter: Modifies the value of an instance variable.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What is the difference between a getter method and a setter method?

Getter: Accesses the value of an instance variable. Setter: Modifies the value of an instance variable.

What is the difference between public and private access modifiers?

Public: Accessible from anywhere. Private: Accessible only within the declaring class.

What is the difference between returning a primitive type and returning a reference type in a getter method?

Primitive: Returns a copy of the value. Reference: Returns a copy of the reference to the object.

Compare the purpose of accessor methods and mutator methods.

Accessor methods: Provide read access to object's state. Mutator methods: Provide write access to object's state.

Compare direct access of instance variables vs. using accessor methods.

Direct access: Bypasses encapsulation, can lead to data corruption. Accessor methods: Provide controlled access, maintain data integrity.

Compare the use of toString() with getter methods for printing object information.

toString(): Returns a formatted string representation of the object. Getter methods: Return specific attribute values.

Compare the effect of returning a primitive data type vs. a reference type from a method.

Primitive data type: Returns a copy of the value. Reference type: Returns a copy of the reference to the object.

Compare the use of accessor methods with and without encapsulation.

With encapsulation: Protects data integrity and allows controlled access. Without encapsulation: Exposes data directly, risking unintended modifications.

Compare the use of public and private access modifiers for instance variables.

Public: Allows unrestricted access from any class. Private: Restricts access to within the same class, promoting encapsulation.

Compare the use of accessor methods and direct variable access regarding code maintainability.

Accessor methods: Allow internal implementation changes without affecting external code. Direct variable access: Requires changes in all places where the variable is used.

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.

What is the purpose of accessor methods?

To provide controlled access to an object's data, often instance variables.

Why don't we create accessor methods for all instance variables?

To encapsulate data and maintain privacy for certain variables.

When is the toString() method automatically called?

When an object is printed directly using System.out.println().

What is the return type of a getter method?

The same type as the instance variable it returns.

Can private variables be accessed directly within the same class?

Yes, even though they are private, they can be accessed directly within the class's methods.

How does the 'return' keyword affect program flow?

It immediately exits the current method and returns control to the caller.

What is the significance of the @Override annotation?

It indicates that a method is intended to replace a method with the same signature in a superclass.

What is encapsulation?

The practice of hiding internal data and implementation details of an object from outside access, and only exposing a controlled interface.

Why is encapsulation important?

It promotes data integrity, reduces dependencies, and allows for easier modification of internal implementation without affecting external code.

What is the difference between returning a primitive type and returning an object reference?

Returning a primitive type returns a copy of the value. Returning an object reference returns a copy of the reference, not a copy of the object.