Verityprep
Question Bank · Unit 3

AP® Computer Science A Class Creation Multiple Choice Questions

Work through AP Computer Science A multiple-choice 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 291–300 of 359
  1. 291
  2. 292
  3. 293
  4. 294
  5. 295
  6. 296
  7. 297
  8. 298
  9. 299
  10. 300

Question 291

Multiple choice Writing Classes

Consider the following class.

public class Box
{
    public int value;

    public Box(int v)
    {
        value = v;
    }
}

Consider the following code segment.

Box a = new Box(10);
Box b = a;
Box c = new Box(10);
b.value = 20;
System.out.println(a.value + " " + b.value + " " + c.value);

What is printed when the code segment executes?

Pick an answer, then check it. The solution opens in a window.

Question 292

Multiple choice Writing Classes
Consider the following class.
public class Calc
{
    public int op(int a, int b)
    {
        return a + b;
    }

    public int op(int a)
    {
        return a * 2;
    }

    public double op(double a, double b)
    {
        return a - b;
    }
}
Consider the following code segment.
Calc c = new Calc();
double r = c.op(3, 5) + c.op(4) + c.op(3.0, 1.0);
What value is stored in r after the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 293

Multiple choice Writing Classes
Consider the following class.
public class Widget
{
    private String name;
    private int count;

    public Widget(String n, int c)
    {
        name = n;
        count = c;
    }

    public String toString()
    {
        return name + ":" + count;
    }
}
Consider the following code segment.
Widget w = new Widget("gear", 7);
System.out.println(w + " has " + w.toString().length() + " chars");
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 294

Multiple choice Writing Classes
Consider the following class.
public class Data
{
    private int n;
    private double d;
    private String s;
    private boolean b;

    public String snapshot()
    {
        return n + "," + d + "," + s + "," + b;
    }
}
Consider the following code segment.
Data data = new Data();
System.out.println(data.snapshot());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 295

Multiple choice Writing Classes
Consider the following class.
public class Util
{
    private boolean isEven(int n)
    {
        return n % 2 == 0;
    }

    public int countEven(int[] arr)
    {
        int c = 0;
        for (int i = 0; i < arr.length; i++)
        {
            if (isEven(arr[i]))
            {
                c++;
            }
        }
        return c;
    }
}
Which of the following is true of the method isEven in the class above?
Pick an answer, then check it. The solution opens in a window.

Question 296

Multiple choice Writing Classes
Consider the following class.
public class Pair
{
    private int a;
    private int b;

    public Pair(int a, int b)
    {
        this.a = a;
        this.b = b;
    }

    public Pair swapped()
    {
        return new Pair(b, a);
    }

    public String show()
    {
        return a + "," + b;
    }
}
Consider the following code segment.
Pair p = new Pair(2, 5);
Pair q = p.swapped();
System.out.println(p.show() + " | " + q.show());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 297

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 298

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 299

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 300

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 291 of 359
Page 30 of 36