Verityprep
Question Bank · Unit 3

AP® Computer Science A Class Creation Practice Questions

Work through AP Computer Science A practice questions on Class Creation, then check each answer against a worked solution.

Free · no sign-up Open questions need no account. Sign up or log in to open the rest.

Subtopic: Writing Classes Clear filter

Questions 331–340 of 402
  1. 331
  2. 332
  3. 333
  4. 334
  5. 335
  6. 336
  7. 337
  8. 338
  9. 339
  10. 340

Question 331

Multiple choice Writing Classes
Consider the following class.
public class Account
{
    private String owner;
    private int balance;

    public Account(String o)
    {
        owner = o;
        balance = 0;
    }

    public int getBalanceLen()
    {
        return ("" + balance).length();
    }

    public int getOwnerLen()
    {
        return owner.length();
    }
}
Consider the following code segment.
Account a = new Account(null);
System.out.println(a.getBalanceLen());
System.out.println(a.getOwnerLen());
What is the result of executing the code segment?
Pick an answer, then check it. The solution opens in a window.

Question 332

Free response Writing Classes

A small community library uses a single rack to display books available for checkout. The rack has a fixed physical capacity, and books sit on the rack in a definite left-to-right order. Patrons interact with the rack through four operations, and a librarian wants a LibraryRack class that supports them directly.

Books are identified by their titles, represented as String values. Two books with the same title are considered the same book — the rack never contains duplicate titles. Positions on the rack are 1-based: the leftmost book is at position 1.

Write the complete LibraryRack class. Your implementation must include the instance variables needed to support the behavior and the constructor and methods described below.

Class Specification

MemberDescription
public LibraryRack(int capacity)Constructs an empty rack with the given maximum capacity. You may assume capacity >= 1.
public boolean addBook(String title)Adds title to the right end of the rack and returns true. If the rack is already full, or a book with the same title is already on the rack, the method makes no change and returns false.
public String checkOut(int pos)Removes and returns the title at 1-based position pos. Books to the right of pos shift one position to the left to close the gap. If pos is not a valid position on the rack (including when the rack is empty), the method makes no change and returns null.
public boolean moveToFront(String title)If title is on the rack, removes it from its current position, inserts it at position 1 (all other books keeping their relative order), and returns true. If title is not on the rack, the method makes no change and returns false.
public int count()Returns the number of books currently on the rack.

Example

Consider the following sequence of calls on LibraryRack r = new LibraryRack(4);

CallReturnsRack afterward (left → right)
r.addBook("Emma")trueEmma
r.addBook("Dune")trueEmma, Dune
r.addBook("1984")trueEmma, Dune, 1984
r.addBook("Dune")falseEmma, Dune, 1984
r.moveToFront("1984")true1984, Emma, Dune
r.checkOut(2)"Emma"1984, Dune
r.checkOut(5)null1984, Dune
r.count()21984, Dune

Constraints

  • You must declare and use instance variables consistent with the behaviors above.
  • You must not use any String or ArrayList methods that are not in the AP Java Quick Reference.
  • Write the entire class, including necessary import statements if any.
The answer and the full solution open in a window.

Question 333

Multiple choice Writing Classes
Consider the following class. public class Counter { private int count; public Counter(int c) { count = c; } public void add(int n) { count += n; } public int…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 334

Multiple choice Writing Classes
Consider the following class.
public class Box
{
    private int width;
    private int height;
    public Box(int w, int h) { width = w; height = h; }
    public Box(int side) { this(side, side); }
    public int area() { return width * height; }
}
Consider the following code segment.
Box b1 = new Box(4, 5);
Box b2 = new Box(3);
System.out.println(b1.area() + b2.area());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 335

Multiple choice Writing Classes
Consider the following class. public class Ticket { private static int nextId = 100; private int id; private int price; public Ticket(int p) { id = nextId;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 336

Multiple choice Writing Classes
Consider the following class.
public class Node
{
    private String label;
    public Node(String l) { label = l; }
    public String getLabel() { return label; }
}
Consider the following code segment.
Node n = new Node("start");
Node m = null;
m = n;
n = new Node("mid");
System.out.println(n.getLabel() + " " + m.getLabel());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 337

Multiple choice Writing Classes
Consider the following class. public class Account { private int balance; public Account(int b) { balance = b; } public int withdraw(int amount) { if (amount >…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 338

Multiple choice Writing Classes
Consider the following class. public class Printer { public void show(int x) { System.out.print("int "); } public void show(double x) { System.out.print("double…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 339

Multiple choice Writing Classes
Consider the following class.
public class Coord
{
    private int x;
    private int y;
    public Coord(int x, int y) { this.x = x; this.y = y; }
    public String toString() { return "(" + x + "," + y + ")"; }
}
Consider the following code segment.
Coord c = new Coord(3, 4);
System.out.println("at " + c);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 340

Multiple choice Writing Classes
Consider the following class. public class Student { private String name; private int grade; public Student(String name, int grade) { name = this.name; grade =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 331 of 402
Page 34 of 41