A keyword that refers to the object calling the method or the object the constructor is trying to make.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
What is the definition of 'this' keyword?
A keyword that refers to the object calling the method or the object the constructor is trying to make.
What does 'this.variableName' refer to?
The instance variable of the current object.
What is an overloaded constructor?
A constructor with the same name as the class but with different parameters.
What is an instance variable?
A variable defined in a class (i.e. a member variable) for which every object of the class has its own copy.
What is a local variable?
A variable that is given local scope. It is accessible only from the function or block in which it is defined.
What does the @Override annotation do?
Indicates that a method declaration is intended to override a method declaration in a superclass.
What is the purpose of a constructor?
To initialize the state of an object.
What is the purpose of a mutator method?
To modify the state of an object.
What is the purpose of an accessor method?
To access the state of an object.
What is the purpose of a static variable?
A variable that is shared by all objects of the class.
Identify the error in the following code:
```java
public class Assignment {
private boolean correctAnswer;
public Assignment(boolean answer) {
correctAnswer = answer;
}
public Assignment() {
double number = Math.random();
if number < 0.5 {
this(false);
} else {
this(true);
}
}
}
```
The 'if' statement condition is missing parentheses: `if (number < 0.5)`.
What does the following code output?
```java
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Student s = new Student("John Doe");
System.out.println(s.getName());
}
}
```
John Doe
Identify the error in the following code:
```java
public class Student {
private int gradeLevel;
public void setGradeLevel(gradeLev) {
gradeLevel = gradeLev;
}
}
```
Missing data type for the parameter `gradeLev`. Should be `public void setGradeLevel(int gradeLev)`.
What does the following code output?
```java
public class Assignment {
private boolean correctAnswer;
public Assignment(boolean correctAnswer) {
this.correctAnswer = correctAnswer;
}
public boolean gradeAssignment(boolean studentAnswer) {
return studentAnswer == correctAnswer;
}
public static void main(String[] args) {
Assignment a = new Assignment(true);
System.out.println(a.gradeAssignment(true));
}
}
```
true
Identify the error in the following code:
```java
public class Student {
private int assignmentsComplete;
public void submitAssignment() {
assignmentsComplete++;
if grade {
correctAssignments++
}
}
}
```
The variable `grade` is not defined, and `correctAssignments` is not a member of the `Student` class. Also missing a closing bracket for the if statement.
What does the following code output?
```java
public class Student {
private String name;
public Student() {
this.name = "Jane Doe";
}
public String toString() {
return name;
}
public static void main(String[] args) {
Student s = new Student();
System.out.println(s);
}
}
```
Jane Doe
Identify the error in the following code:
```java
public class Student {
private static String school = "The Fiveable School";
public static void setSchool(String schoolName) {
schoolName = school;
}
}
```
The assignment is backwards. It should be `school = schoolName;`
What does the following code output?
```java
public class Student {
private int gradeLevel;
public Student(int gradeLevel) {
this.gradeLevel = gradeLevel;
}
public int getGradeLevel() {
return gradeLevel;
}
public static void main(String[] args) {
Student s = new Student(12);
System.out.println(s.getGradeLevel());
}
}
```
12
Identify the error in the following code:
```java
public class Student {
private String name;
public String getName() {
Name = name;
}
}
```
The return type is incorrect. It should be `return name;`
What does the following code output?
```java
public class Student {
private int age = 16;
public int getAge() {
return age;
}
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.getAge());
}
}
```
16
How is the 'this' keyword used in event handling in GUI programming?
The 'this' keyword can be used to pass the current object as an event listener to a GUI component.
How are overloaded constructors useful in database interactions?
They allow creating objects with varying amounts of data retrieved from a database.
How is the concept of instance variables applied in a game development?
To store information about a player, such as health, score, and position.
How are accessor methods applied in a social media application?
To retrieve user profile information, such as name, email, and profile picture.
How are mutator methods applied in an e-commerce application?
To update product information, such as price, quantity, and description.
How is the concept of static variables applied in a banking application?
To store the interest rate for all accounts.
How is the concept of encapsulation applied in a library management system?
To protect the internal state of a book object, such as title, author, and ISBN.
How is the concept of inheritance applied in a vehicle management system?
To create specialized vehicle types, such as cars, trucks, and motorcycles, from a common vehicle class.
How is the concept of polymorphism applied in a graphics editor?
To allow different shapes, such as circles, squares, and triangles, to be drawn using a common draw method.
How is the concept of abstraction applied in a operating system?
To hide the complex implementation details of the hardware and expose only the essential features to the user.