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.

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.

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