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 351–359 of 359
  1. 351
  2. 352
  3. 353
  4. 354
  5. 355
  6. 356
  7. 357
  8. 358
  9. 359

Question 351

Multiple choice Writing Classes

Consider the following method.

public static void adjust(int[] data, int n)
{
    data[n] = data[0] + n;
    n = n + 1;
    data[0] = data[n - 1];
}

What is printed by the following code?

int[] vals = {3, 5, 7, 9};
int k = 2;
adjust(vals, k);
System.out.println(vals[0] + " " + vals[2] + " " + k);
Pick an answer, then check it. The solution opens in a window.

Question 352

Multiple choice Writing Classes

Consider the following class.

public class Ticket
{
    private static int next = 100;
    private int number;

    public Ticket()
    {
        number = next;
        next = next + 5;
    }

    public int getNumber()
    {
        return number;
    }

    public static int peekNext()
    {
        return next;
    }
}

What is printed by the following code?

Ticket a = new Ticket();
Ticket b = new Ticket();
System.out.println(a.getNumber() + " " + b.getNumber() + " " + Ticket.peekNext());
Pick an answer, then check it. The solution opens in a window.

Question 353

Multiple choice Writing Classes

Consider the following class.

public class Counter
{
    private int value;

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

    public int bump(int n)
    {
        value = value + n;
        return value;
    }

    public int read()
    {
        return value;
    }
}

What is printed by the following code?

Counter c = new Counter(3);
int a = c.bump(2);
int b = c.bump(a - 1);
System.out.println(a + " " + b + " " + c.read());
Pick an answer, then check it. The solution opens in a window.

Question 354

Multiple choice Writing Classes
Consider the following method. public static int calc(int a, int b) { if (a > b) { a = a - b; }…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 355

Multiple choice Writing Classes

Consider the following class.

public class Box
{
    private int value;

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

    public int add(int n)
    {
        int old = value;
        value = value + n;
        return old;
    }

    public int get()
    {
        return value;
    }
}

What is printed by the following code?

Box p = new Box(4);
Box q = p;
int x = q.add(3);
p.add(x);
System.out.println(q.get());
Pick an answer, then check it. The solution opens in a window.

Question 356

Multiple choice Writing Classes

Consider the following method.

public static void change(int[] a)
{
    a[1] = a[0] + a[2];
    a = new int[3];
    a[0] = 1;
}

What is printed by the following code?

int[] data = {2, 4, 6};
change(data);
System.out.println(data[0] + " " + data[1] + " " + data[2]);
Pick an answer, then check it. The solution opens in a window.

Question 357

Multiple choice Writing Classes
Consider the following class. public class Log { private static int total = 0; private int local; public Log() { local = total; total++; } public int bump() {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 358

Multiple choice Writing Classes
Consider the following class. public class Pair { private int a; private int b; public Pair(int x, int y) { a = x; b = y; } public void mix() { int old = a; a =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 359

Multiple choice Writing Classes
Consider the following class. public class Score { private int value; public Score(int v) { value = v; } public int add(int n) { int old = value; value = value…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 351 of 359
Page 36 of 36