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.

Questions 391–400 of 402
  1. 391
  2. 392
  3. 393
  4. 394
  5. 395
  6. 396
  7. 397
  8. 398
  9. 399
  10. 400

Question 391

Multiple choice Writing Classes
Consider the following class. public class Bank { private int balance; public Bank(int b) { balance = b; } public int update(int amount) { int old = balance;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 392

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 393

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 394

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 395

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 396

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 397

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 398

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 399

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 400

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 391 of 402
Page 40 of 41