AP Computer Science A Unit 3: Class Creation Practice Questions

Practice questions for AP Computer Science A Unit 3 on class creation in Java: classes, objects, constructors, and abstraction, with explanations for every answer.

109 questions · AP Computer Science A · by Study Foxy

Study these questions free

Questions

  1. What is abstraction in programming?

    • Hiding details to focus on what matters
    • Writing every step of a program inside the main method
    • Making all instance variables public so they are easy to change
    • Removing comments from code
  2. A program models a library. Which is the best choice for a class?

    • Book
    • title
    • checkOut
    • isOverdue
  3. In a design for a Book class, which is best modeled as an instance variable?

    • The book's title
    • Checking the book out
    • Printing a receipt
    • Searching the catalog for other books
  4. What is procedural abstraction?

    • Using a method by knowing what it does, not how
    • Writing a separate class for every individual variable in the program
    • Making every method static
    • Replacing methods with loops
  5. Why break a large program into several classes and methods?

    • Each part is easier to write, test, and reuse
    • The program will always run faster
    • Java requires at least five classes in every program
    • It removes the need for testing
  6. Data abstraction lets code use an object without knowing how its data is stored inside.

    True or false

  7. Abstraction requires the programmer to understand every line of code in the classes they use.

    True or false

  8. A program tracks students' grades. Which are good candidate behaviors (methods) for a Student class?

    • addGrade
    • getAverage
    • gpa
    • studentName
  9. What is the term for hiding implementation details so programmers can focus on what something does?

  10. A company's app crashes whenever users enter an unexpected value. Which design concern does this raise most directly?

    • System reliability
    • Intellectual property ownership of the code
    • Choice of variable names
    • Code indentation style
  11. A developer copies code from an open-source project into a commercial product. What must the developer do?

    • Follow the terms of that project's license
    • Nothing, since open-source code has no rules of any kind
    • Delete the comments so the code looks original
    • Pay the original author a fixed fee in every case
  12. Which is the best example of an unintended consequence of program design?

    • A fitness app's public map revealed users' home locations
    • A calculator app adds two numbers correctly
    • A game shows a high score
    • A text editor saves a file
  13. Programmers have a responsibility to consider the ethical and social impacts of the programs they design.

    True or false

  14. Code posted publicly online may be copied and used in any way without permission.

    True or false

  15. Why should a program that collects personal data be designed to collect only what it needs?

    • It reduces privacy risks if the data is exposed
    • It makes the code compile faster on older computers
    • Collecting less data is required for every Java class
    • It guarantees the program has no bugs
  16. What type of license lets anyone view, use, and modify a program's source code, usually with conditions? (two words)

  17. Why are instance variables usually declared private?

    • To protect them from outside changes
    • So that they can be used by every class in the program
    • Because public variables cause compile errors
    • So the class runs faster
  18. Why does this code fail to compile? Dog has a constructor Dog(String n, int a), getters getName() and getAge(), a method haveBirthday() that adds 1 to age, and toString() returning name + " (" + age + ")". The instance variables are private. Dog d = new Dog("Rex", 3); System.out.println(d.age);

    • age is private, so other classes can't access it
    • Dog objects cannot be printed
    • age must be accessed as Dog.age
    • The constructor needs three arguments
  19. What is encapsulation?

    • Bundling data with methods and restricting direct access to the data
    • Writing all code inside one very large method
    • Making every variable static
    • Converting objects to Strings
  20. Which access modifier makes a method usable from other classes?

    • public
    • private
    • static
    • void
  21. What is an accessor (getter) method?

    • A method that returns the value of an instance variable
    • A method that changes an instance variable's value
    • A constructor with no parameters
    • A static method in the Math class
  22. What is a mutator (setter) method?

    • A method that changes an instance variable
    • A method that only returns a value without changing anything
    • A method that creates new objects
    • A method with no body
  23. Which are typically part of a class?

    • Instance variables
    • Constructors
    • Methods
    • An import of every Java library
    • A main method in every class
  24. A class's private methods can be called by other methods in the same class.

    True or false

  25. Instance variables should generally be public so other classes can update them directly.

    True or false

  26. Which access modifier is normally used for instance variables?

  27. What is printed? public class Dog { private String name; private int age; public Dog(String n, int a) { name = n; age = a; } public String getName() { return name; } public int getAge() { return age; } public void haveBirthday() { age++; } public String toString() { return name + " (" + age + ")"; } } Dog d = new Dog("Rex", 3); System.out.println(d.getName() + " " + d.getAge());

    • Rex 3
    • 3 Rex
    • null 0
    • Rex3
  28. What is printed? public class Cat { private String name; public Cat(String name) { name = name; } public String getName() { return name; } } Cat c = new Cat("Tom"); System.out.println(c.getName());

    • null
    • Tom
    • name
    • An empty line
  29. What is printed? public class Thing { private int x; private double y; private boolean flag; private String label; public Thing() { } public String toString() { return x + " " + y + " " + flag + " " + label; } } System.out.println(new Thing());

    • 0 0.0 false null
    • 0 0 false null
    • null null null null
    • A compile error
  30. Why does this code fail to compile? Dog's only constructor is Dog(String n, int a). Dog d = new Dog();

    • Dog has no constructor with zero parameters
    • Dog objects must be created with the keyword make
    • Constructors cannot be called directly
    • d must be declared final
  31. How is a constructor different from a method?

    • It has the class's name and no return type
    • It must always return an int value to the caller
    • It can only be called from the main method
    • It cannot have parameters
  32. What does a constructor typically do?

    • Gives instance variables their starting values
    • Prints the object's information to the console for debugging
    • Deletes the object when the program ends
    • Compiles the class
  33. A class has no constructors written in it. What happens when code calls new on it with no arguments?

    • Java supplies a default constructor
    • The code fails to compile because a constructor is required
    • A NullPointerException is thrown
    • The object is created without any instance variables
  34. A class may have more than one constructor.

    True or false

  35. An uninitialized String instance variable has the default value "" (empty String).

    True or false

  36. What is the default value of an int instance variable that is never assigned?

  37. What is printed? Dog has a constructor Dog(String n, int a), getters getName() and getAge(), a method haveBirthday() that adds 1 to age, and toString() returning name + " (" + age + ")". Dog d = new Dog("Bo", 4); d.haveBirthday(); d.haveBirthday(); System.out.println(d.getAge());

    • 6
    • 4
    • 5
    • 2
  38. What is printed? Dog has a constructor Dog(String n, int a), getters getName() and getAge(), a method haveBirthday() that adds 1 to age, and toString() returning name + " (" + age + ")". Dog d = new Dog("Bo", 4); System.out.println(d);

    • Bo (4)
    • Dog@1b6d3586
    • Bo 4
    • d
  39. What is printed? Dog has a constructor Dog(String n, int a), getters getName() and getAge(), a method haveBirthday() that adds 1 to age, and toString() returning name + " (" + age + ")". Dog d = new Dog("Bo", 4); String s = "My dog: " + d; System.out.println(s.length());

    • 14
    • 8
    • 12
    • 16
  40. Why does this class fail to compile? class Num { public int sign(int x) { if (x > 0) { return 1; } } }

    • Not every path through sign returns a value
    • sign must be declared void
    • x cannot be compared with 0
    • Methods cannot contain if statements
  41. What is printed? public class Account { private double balance; public Account(double start) { balance = start; } public void deposit(double amt) { if (amt > 0) { balance += amt; } } public boolean withdraw(double amt) { if (amt <= balance) { balance -= amt; return true; } return false; } public double getBalance() { return balance; } } Account a = new Account(100); a.deposit(-50); a.deposit(25); System.out.println(a.getBalance());

    • 125.0
    • 75.0
    • 125
    • 50.0
  42. Account's withdraw(double amt) subtracts amt and returns true only if amt <= balance; otherwise it returns false and changes nothing. What is printed? Account a = new Account(100); boolean ok = a.withdraw(150); System.out.println(ok + " " + a.getBalance());

    • false 100.0
    • true -50.0
    • false -50.0
    • true 100.0
  43. Why might a mutator such as setAge check its argument before changing the variable?

    • To keep the object in a valid state
    • Because Java refuses to compile mutators without an if statement
    • To make the method return a value
    • Checking arguments makes methods static
  44. What is the purpose of a toString method?

    • To return a String describing the object
    • To print the object automatically every time it changes
    • To convert a String into an object
    • To compare two objects
  45. Which header correctly declares an accessor for a private double instance variable price?

    • public double getPrice()
    • public void getPrice()
    • private double getPrice(double price)
    • public getPrice()
  46. A non-void method must return a value on every possible path through its code.

    True or false

  47. A void method can include the statement return 5;.

    True or false

  48. What does Java print for an object whose class doesn't define toString? Answer with the name of the class that supplies the default version.

  49. What is printed? public static void change(int n) { n = 99; } int a = 5; change(a); System.out.println(a);

    • 5
    • 99
    • 0
    • A compile error
  50. What is printed? Box stores an int value, with constructor Box(int v), getValue(), and setValue(int v). Box b = new Box(1); update(b); System.out.println(b.getValue()); (where update(Box x) calls x.setValue(10))

    • 10
    • 1
    • 11
    • A compile error

Showing 50 of 109. Open the quiz to study them all.