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

What are the general steps to write a method?

  1. Define access modifier. 2. Define return type. 3. Name the method. 4. Define parameters (if any). 5. Write the method body.
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 method?

  1. Define access modifier. 2. Define return type. 3. Name the method. 4. Define parameters (if any). 5. Write the method body.

What are the steps to submit an assignment?

  1. Grade the assignment. 2. Increment assignmentsComplete. 3. If grade is correct, increment correctAssignments.

How does pass-by-value affect primitive variables?

Changes to the parameter inside the method do not affect the original variable outside the method.

How does pass-by-value affect reference variables?

Changing the object's state (e.g., using a setter) inside the method affects the original object outside the method.

What is the general format of a method header in Java?

()

Why should mutable objects passed as parameters be handled carefully?

Modifications within the method can unintentionally alter the object's state outside the method.

What determines a method's accessibility?

The access modifier (public, private, protected) used in the method header.

What is the role of parameters in a method?

To receive input values that the method uses during its execution.

What is the significance of the return type in a method?

It specifies the data type of the value that the method returns to the caller.

What does the @Override annotation indicate?

That a method in a subclass is overriding a method from its superclass.

What are the common access modifiers in Java?

public, private, and protected.

What is the purpose of Javadoc comments?

To document the purpose, parameters, and return values of methods for API documentation.

What does the following code output?

java
Assignment a = new Assignment(true);
System.out.println(a.gradeAssignment(false));

false

What does the following code output?

java
Student s = new Student(10, "John Doe", 16);
System.out.println(s.getGradeLevel());

10

Identify the error in the following code:

java
public class Student {
  private int age;

  public void setAge(String newAge) {
    age = newAge;
  }
}

The setAge method attempts to assign a String value to an integer variable age. The parameter type should be int.

What does the following code output?

java
Student s = new Student(9, "Jane Smith", 14);
System.out.println(s.returnCurrentAssignment());

null

Identify the error in the following code:

java
public class Assignment {
  private boolean correctAnswer;

  public Assignment(boolean answer) {
    correctAnswer = answer
  }
}

Missing semicolon at the end of the correctAnswer = answer statement inside the Assignment constructor.

What does the following code output?

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

This is an assignment with correct answer true

Complete the following code snippet to increment assignmentsComplete:

java
public class Student {
  private int assignmentsComplete = 0;

  public void incrementAssignments() {
    // Complete this method
  }
}
java
assignmentsComplete++;

What does the following code output?

java
Student s = new Student(11, "Peter Jones", 17);
s.setName("Robert Jones");
System.out.println(s.getName());

Robert Jones

Identify the error in the following code:

java
public class Student {
  private int gradeLevel;

  public void setGradeLevel(String level) {
    gradeLevel = level;
  }
}

The setGradeLevel method attempts to assign a String value to an integer variable gradeLevel. The parameter type should be int.

What does the following code output?

java
Student s = new Student(12, "Alice Brown", 18);
System.out.println(s.getGradeDecimal());

0.0