Preview

03 - AP Exam question set #3 (Medium)

 1. What does the following code do when it is executed?
System.out.println(5 / 0);

  It will result in a run-time error

  it will print 5

  It will not compile (compile-time error)

  It will print 0

 2. The output of the following code would be exactly 0.33.
System.out.println(1.0 / 3);

  TRUE

  FALSE

 3. Consider the following class. Which of the following code segments would successfully create a new Book object?
public class Book
{
    private String title;
    private String author;
    private double price;
    private boolean inStores;

    public Book(String t, String a, double p)
    {
        title = t;
        author = a;
        price = p;
        inStores = false;
    }

    public Book(String t)
    {
        title = t;
        author = "unknown";
        price = 0.0;
        inStores = false;
    }
}

  Book three = new Book(title, price, author);

  Book one = new Book("Screwtape letters", "C.S Lewis");

  Book four = new Book("Chronicles of Narnia", "C.S Lewis", "9.99");

  Book two = new Book("The Great Divorce");

 4. Given the PocketMoney class definition below, what is the output of the code in the main method?
public class PocketMoney
{
    private int accountID;
    private double total;

    public PocketMoney(int id, double initialDeposit)
    {
        accountID = id;
        total = initialDeposit;
    }

    public void deposit(double money)
    {
        total = total + money;
    }

    public void withdraw(double money)
    {
        total = total - money;
    }

    public void printCurrentTotal()
    {
        System.out.print(total);
    }

    public static void main(String[] args)
    {
        PocketMoney newAccount = new PocketMoney(12345, 100.00);
        newAccount.withdraw(30.00);
        newAccount.deposit(40.00);
        newAccount.printCurrentTotal();
    }
}

  100

  10

  90

  110

 5. Which of the following expressions is equivalent to !(c || d) ?

   (c || d)

  (c && d)

  (!c) || (!d)

  (!c) && (!d)

 6. Which of the following is equivalent to the code segment below?
if (x > 2)
   x = x * 2;
if (x > 4)
   x = 0;

  if (x > 2) x = 0;

  x = 0;

  if (x > 2) x *= 2;

  if (x > 2) x = 0; else x *= 2;

 7. How many @ signs are output when the following code is executed?
for (int i = 0; i < 5; i++) {
   for (int j = 0; j < 5; j++)
      System.out.println("@");
}

  50

  5

  10

  25

 8. Which of the following statements is a valid conclusion. Assume that variable b is an array of k integers and that the following is true:
b[0] != b[i] for all i from 1 to k-1

  Array b is sorted

  The value in b[0] does not occur anywhere else in the array

  Array b is not sorted

  The value in b[0] is the smallest value in the array

 9. Consider the following code segment. Which of the following statements best describes the condition when it returns true?
boolean temp = false;
for (int i = 0; i < a.length; i++) {
   temp = (a[i] == val);
}
return temp;

  Whenever a contains any element which equals val

  Whenever exactly 1 element in a is equal to val

   Whenever the last element in a is equal to val

  whenever the first element in a is equal to val

 10. Assume that numList has been initialized with the following Integer objects: [5, 7, 8, 12]. Which of the following shows the values in numList after a call to mystery(11)?
private List numList;
public void mystery(int value)
{
    int i = 0;
    while (i < numList.size() && numList.get(i) < value)
    {
        i++;
    }
    numList.add(i, value);
}

  [5, 7, 8, 11, 12]

  [5, 7, 8, 12, 11]

   [11, 5, 7, 8, 12]

  [5, 7, 8, 12]

 11. Given the following code segment, what is the value of sum after this code executes?
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};

int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
   sum = sum + matrix[row][col];
}

  8

  12

  4

  9

 12. What are the contents of xyz after the following code segment has been executed?
int [][] xyz = new int [4][3];
for (int row = 0; row < xyz.length; row++) {
   for (int col = 0; col < xyz[0].length; col++) {
      if (row < col)
         xyz[row][col] = 1;
      else if (row == col)
         xyz[row][col] = 2;
      else
         xyz[row][col] = 3; } }

   { {1 1 1 1}, {2 2 2 2}, {3 3 3 3}}

  { {2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}}

  { {2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}}

   { {2 1 1 1}, {3 2 1 1}, {3 3 2 1}}

 13. Given the following class declarations and code, what is the result when the code is run?
public class Book
{
   public String getISBN()
   {
      // implementation not shown
   }

   // constructors, fields, and other methods not shown
}

public class Dictionary extends Book
{
   public String getDefinition(String word)
   {
      // implementation not shown
   }

   // constructors, fields, and methods not shown
}

Assume that the following declaration appears in a client class.

Book b = new Dictionary();

Which of the following statements would compile without error?
I.  b.getISBN();
II. b.getDefinition("wonderful");
III. ((Dictionary) b).getDefinition("wonderful");

  II only

  I only

  I, II and III

  I and III only

 14. Given the following method declaration, what value is returned as the result of the call func(5)?
public static int func(int n)
{
   if (n == 0)
      return 0;
   else if (n == 1)
      return 1;
   else return func(n-1) + func(n-2);
}

  4

  3

  5

  8

 15. Given the following method declaration, what value is returned as the result of the call consume(5)?
public static int consume(int n)
{
   if (n <= 1)
      return 1;
   else
      return n * consume(n - 2);
}

  15

  250

  1

  10