Preview

07 - AP Exam (50 questions Mixed)

 1. Which one of these is a valid boolean declaration?

  boolean varOne = 1;

  boolean varOne = 'false';

  boolean varOne = 'true';

  boolean varOne = false;

 2. What will the value of varOne be after the following expression is evaluated?
double varOne = 8+7x4-2.0/4 + (5-3x4-2);

  26.5

  2.5

  64

  39.5

 3. What is printed as a result of executing this code segment?
String varOne = "Hello World!";
String varTwo = "Java";
System.out.println(varOne.compareTo(varTwo));

  Answer: -1

  Answer: 2

  Answer: -2

  Answer: 20

 4. Which of the following is equivalent to the statement below? Recall DeMorgan?s Law.
!((a <= b) && (b < 0))

  (a >= b) && (b >= 0)

  !(a > b) || !(b >= 0)

  (a > b) || (b >= 0)

  (a > b) && (b >= 0)

 5. Consider the following recursive method. What does guess(4) return?
public int guess(int m)
{
  if (m == 1)
  {
      return 3;
  } else
  {
      return 3 * guess(m - 1);
  }
}

  81

  9

  3

  243

 6. In which of these cases will an ascending order (from smallest to largest) insertion sort have the fastest run time?
I.   An array that is in reverse order (from largest to smallest).
II.  An array that is in sorted order already (from smallest to largest).
III. An array that is in random order (not already sorted).

  I only

  III only

  II only

  I and III

 7. Which of the following statements is correct, considering the below code.
int max = 5;

//Loop I
for (int i = 0; i < max; i++)
{
   System.out.print(i);
}


//Loop II
int j = 0;
while (j < max)
{
   System.out.print(j);
   j++;
}


//Loop III
int k = 0;
for (int i = max; i > 0; i--)
{
   System.out.print(i);
}

  II and III will output 1234

  II will output 12345

  I and III will output 12345

  I and II will output 1234

 8. Consider the following block of code. What are the first and last numbers printed after running the code?
int value = 15;
while (value < 30)
{
    value++;
    System.out.println(value);
}

  First: 15 Last: 29

  First: 16 Last: 30

  First: 15 Last: 30

  First: 16 Last: 29

 9. Consider the following block of code. What value is returned from myfunc(5)?
public int myfunc(int limit)
{
  int s = 0;

  for (int outside = 1; outside <= limit; outside++)
  {
      for (int middle = 1; middle <= limit; middle++)
      {
          for (int inside = 1; inside <= limit; inside++)
          {
              s++;
          }
      }
  }
  return s;
}

  125

  15

  25

  625

 10. Given that both count and n are integer values, which of the following statements is true about both code blocks?
// Code block I
for (count = 0; count <= n; count++)
{
    System.out.println(count);
}

//Code block II
count = 0;
while (count <= n)
{
    count = count + 1;
    System.out.println(count);
}

   I and II are equivalent for all values except when n = 0

   I and II are only equivalent when n is an even number.

   I and II are never going to have the exact same outputs.

  I and II are exactly equivalent for all input values n

 11. Consider the following class declarations. Which statements are true?
 public class Bat
 {
  /* Some code */
 }

 public class FireBat extends Bat
 {
    /* Some code */
 }

I. FireBat inherits the constructors of Bat
II. FireBat cannot add new methods and private instance variables that Bat does not have.
III. FireBat can override existing public methods of Bat

  III only

  I and III

  I, II and III are all true

  I and II

 12. : Consider the following code. What is the maximum amount of times that TESTANDTRACK could possibly be printed?
for (int i = 0; i <= k; i++)
{
   if (arr[i] < someValue)
   {
     System.out.print("TESTANDTRACK")
   }
}

  k+1

  k

  1

  k-1

 13. The following method stringRecursion would only produce a run time error when the length of the input string is greater than or equal to 16.
public void stringRecursion(String s)
{

  if (s.length() < 16)
  {
    System.out.println(s);
  }
  stringRecursion(s + "*");
}

  FALSE

  TRUE

 14. Which of these declarations will not cause an error?
I List stringList = new ArrayList();
II List intList = new ArrayList();
III ArrayList stringList = new List();

  I only

  I and III

  II only

  I and II

 15. The method alter shifts the values in the columns, starting at column c + 1, one column to the left. It also _______________.
  private int[][] matrix;

  /* matrix looks like this initially
  1 3 5 7
  2 4 6 8
  3 5 7 9
  */

  public void alter(int c)
  {
    for (int i = 0; i < matrix.length; i++)
    {
      for (int j = c + 1; j < matrix[0].length; j++)
      {
        matrix[i][j - 1] = matrix[i][j];
      }
    }
  }

CONTENTS OF MATRIX (after a call to alter(1))
===================
   1 5 7 7
   2 6 8 8
   3 7 9 9

  overwrites all numbers equal to 0

  overwrites column c.

  overwrites column A

  shifts column c one column to the right.