All Flashcards
What are the general steps to write a method?
- 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?
- Grade the assignment. 2. Increment assignmentsComplete. 3. If grade is correct, increment correctAssignments.
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
What is pass-by-value?
A method receives a copy of the variable's value; changes inside the method do not affect the original variable outside.
What is a reference variable?
A variable that stores the memory address of an object.
What is an access modifier?
Keywords in Java that control the visibility and accessibility of classes, methods, and variables.
What is the return type of a method?
The data type of the value that a method returns after its execution.
What is a parameter in a method?
A variable that receives a value passed into a method.
What is a method header?
The first line of a method definition, including access modifier, return type, method name, and parameters.
What is a mutable object?
An object whose state can be modified after it is created.
What is a getter method?
A method used to retrieve the value of an object's attribute.
What is an instance variable?
A variable defined in a class for which every object of the class has its own copy.
What is the purpose of the 'void' return type?
Indicates that the method does not return any value.