All Flashcards
What are the general steps to write a getter method?
- 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?
- 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?
- 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?
- 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?
- 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?
- 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?
- 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?
- 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?
- 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?
- 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.
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.