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

What is a class?

A blueprint for creating objects, defining their attributes and behaviors.

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

All Flashcards

What is a class?

A blueprint for creating objects, defining their attributes and behaviors.

What are instance variables?

Variables that characterize objects within a class.

What is a constructor?

A special method used to create objects of a class.

What is a method?

A function associated with an object, defining its behavior.

What is encapsulation?

Bundling of data and methods that operate on that data, and restricting access from outside.

What is a client in the context of a class?

Other users and classes that interact with a class and its objects.

What is a class header?

The first line of a class definition, specifying the class name and visibility.

What is a class body?

The code block enclosed in curly braces that contains variable declarations and methods.

What is a default constructor?

A constructor with no parameters, providing default values for instance variables.

What is an overloaded constructor?

A constructor with the same name as another constructor but with different parameters.

What is the main purpose of a class in Java?

To define the structure and behavior of objects.

Why are instance variables often declared as private?

To enforce encapsulation and protect data integrity.

What is the role of a constructor in object creation?

To initialize the object's state by assigning initial values to its instance variables.

How does encapsulation contribute to code maintainability?

By hiding internal implementation details and preventing unintended modifications from outside the class.

What is the significance of the 'new' keyword in Java?

It is used to create a new instance (object) of a class.

Explain the concept of information hiding.

Restricting access to certain components of a class to prevent improper access or modification.

What is the purpose of a getter method?

A method used to access the value of a private instance variable.

What is the purpose of a setter method?

A method used to modify the value of a private instance variable.

What is the role of the main method?

It is the entry point for the program.

What are the three main parts of a class?

Variable declarations, constructors, and methods.

Identify the error in the following code:

java
public class Square {
 private int side;
 public Square(int s) {
  side = s
 }
}

Missing semicolon after side = s.

What does the following code output?

java
public class Example {
 public static void main(String[] args) {
  Square mySquare = new Square(5);
  System.out.println(mySquare.getArea());
 }
}

class Square {
 private int side;
 public Square(int side) {
  this.side = side;
 }
 public int getArea() {
  return side * side;
 }
}

25

Complete the following code snippet to create a default constructor for the Rectangle class:

java
public class Rectangle {
 private int width;
 private int height;

 // Complete the code here

}
java
 public Rectangle() {
 width = 0;
 height = 0;
 }

Identify the error in the following code:

java
public class Circle {
 private double radius;

 public void Circle(double r) {
  radius = r;
 }
}

The constructor Circle should not have a void return type.

What does the following code output?

java
public class Test {
 public static void main(String[] args) {
  House myHouse = new House("123 Main St", 3);
  System.out.println(myHouse.getAddress());
 }
}

class House {
 private String address;
 private int rooms;
 public House(String address, int rooms) {
  this.address = address;
  this.rooms = rooms;
 }
 public String getAddress() {
  return address;
 }
}

123 Main St

Complete the following code snippet to create a getter method for the make attribute of the Car class:

java
public class Car {
 private String make;

 // Complete the code here

}
java
 public String getMake() {
 return make;
 }

What does the following code output?

java
public class Example {
 public static void main(String[] args) {
  Plant myPlant = new Plant("Rose", 1.5);
  System.out.println(myPlant.getHeight());
 }
}

class Plant {
 private String species;
 private double height;
 public Plant(String species, double height) {
  this.species = species;
  this.height = height;
 }
 public double getHeight() {
  return height;
 }
}

1.5

Identify the error in the following code:

java
public class Student {
 private String name;
 public Student(name) {
  this.name = name;
 }
}

Missing data type in the constructor parameter declaration. Should be public Student(String name).

What does the following code output?

java
public class Example {
 public static void main(String[] args) {
  Phone myPhone = new Phone("Apple", "iPhone 13", "Blue");
  myPhone.makeCall();
 }
}

class Phone {
 private String brand;
 private String model;
 private String color;
 public Phone(String brand, String model, String color) {
  this.brand = brand;
  this.model = model;
  this.color = color;
 }
 public void makeCall() {
  System.out.println("Calling...");
 }
}

Calling...

Complete the following code snippet to create a setter method for the rooms attribute of the House class:

java
public class House {
 private int rooms;

 // Complete the code here

}
java
 public void setRooms(int rooms) {
 this.rooms = rooms;
 }