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

Why use 'this' to differentiate instance and local variables?

To resolve naming conflicts when a local variable has the same name as an instance variable.

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

All Flashcards

Why use 'this' to differentiate instance and local variables?

To resolve naming conflicts when a local variable has the same name as an instance variable.

How does 'this()' help with constructor overloading?

It allows one constructor to call another constructor within the same class, avoiding code duplication.

What is the benefit of using overloaded constructors?

Provides flexibility in object creation by allowing different ways to initialize an object with varying parameters.

What is the purpose of encapsulation?

To hide the internal state of an object and protect it from outside access.

What is the purpose of inheritance?

To allow a class to inherit the properties and methods of another class.

What is the purpose of polymorphism?

To allow objects of different classes to be treated as objects of a common type.

What is the difference between a class and an object?

A class is a blueprint for creating objects, while an object is an instance of a class.

What is the difference between a method and a function?

A method is a function that is associated with an object, while a function is a standalone block of code.

What is the purpose of abstraction?

To hide the complex implementation details of a system and expose only the essential features.

What is the purpose of modularity?

To divide a system into smaller, independent modules that can be developed and tested separately.

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