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.
Questions
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
A program models a library. Which is the best choice for a class?
- Book
- title
- checkOut
- isOverdue
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
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
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
Data abstraction lets code use an object without knowing how its data is stored inside.
Abstraction requires the programmer to understand every line of code in the classes they use.
A program tracks students' grades. Which are good candidate behaviors (methods) for a Student class?
- addGrade
- getAverage
- gpa
- studentName
What is the term for hiding implementation details so programmers can focus on what something does?
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
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
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
Programmers have a responsibility to consider the ethical and social impacts of the programs they design.
Code posted publicly online may be copied and used in any way without permission.
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
What type of license lets anyone view, use, and modify a program's source code, usually with conditions? (two words)
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
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
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
Which access modifier makes a method usable from other classes?
- public
- private
- static
- void
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
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
Which are typically part of a class?
- Instance variables
- Constructors
- Methods
- An import of every Java library
- A main method in every class
A class's private methods can be called by other methods in the same class.
Instance variables should generally be public so other classes can update them directly.
Which access modifier is normally used for instance variables?
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
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
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
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
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
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
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
A class may have more than one constructor.
An uninitialized String instance variable has the default value "" (empty String).
What is the default value of an int instance variable that is never assigned?
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
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
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
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
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
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
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
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
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()
A non-void method must return a value on every possible path through its code.
A void method can include the statement return 5;.
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.
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
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