Preview

05 - AP Exam question set #5 (Hard)

 1. What is the output of the following code?
public class Test
{
   String aFunc(String str)
   {
       return anotherFunc(str + " Hey");
   }

   String anotherFunc(String str)
   {
       return "Hey " + str;
   }

   public static void main(String[] args)
   {
       Test x = new Test();
       System.out.print("Well " + x.aFunc("Well"));
   }
}

  Well Hey Well Hey

  Well Well Hey Hey

  Hey Woo Hey

  Well Hey Hey Well

 2. Given the following code segment, which of these statements is true?
String s1 = new String("Test Track");
String s2 = new String("Test Track");
String s3 = s1;

I.   (s1 == s2)
II.  (s1.equals(s2))
III. (s1 == s3)
IV.  (s2.equals(s3))

   II only

   I, II, III, IV

  II and IV

  II, III, and IV

 3. What does the following code print?
System.out.println("13" + 5 + 3);

  1353

  135

  21

  133

 4. Assuming that x and y have been declared as valid integer values, which of the following is equivalent to this statement?
(x > 15 && x < 18) || (x > 10 || y < 20)

  (x < 10 && y > 20) && (x < 15 || x > 18)

  (y < 20) || (x > 15 && x < 18)

  ((x > 10) || (x > 15 && x < 18)) || (y < 20)

   (x > 15 && x < 18) && (x > 10)

 5. What is the output of the following code?
int x = 3;
int y = 2;
if (x > 2)
   x++;
if (y > 1)
   y++;
if (x > 2)
   System.out.print("test ");
if (y < 3)
   System.out.print("and ");
System.out.print("track");

  test

  test and

  test track

  test and track

 6. For the following code, 15 stars are output on program execution.
for (int i = 0; i < 5; i++) {
   for (int j = 0; j < 5; j++)
      System.out.println("*");
}

  FALSE

  TRUE

 7. What is in the list nums if it initially contained {5, 3, 1} and the following code is executed?
nums.add(6);
nums.add(0,4);
nums.remove(1);

  [4, 3, 6]

  [4, 3, 1, 6]

   [5, 3, 6]

  [5, 3, 1, 6]

 8. What is output when the main method is executed?
public class AmazingSort
{

    public static void main(String[] args)
    {
        int i, j;
        String key;
        String[] letters = {"E","D","C","B","A","B"};
        for (j = 1; j < letters.length; j++)
        {
            key = letters[j];
            i = j - 1;
            while (i >= 0)
            {
                if (key.compareTo(letters[i]) > 0)
                {
                    break;
                }
                letters[i + 1] = letters[i];
                i--;
            }
            letters[i + 1] = key;
        }
        for (int t = 0; t < letters.length; t++)
        {
            System.out.print((letters[t]) + "");
        }
    }
}

  A B B C D

  A B B C D E

  E D C B B

  E D C B B A

 9. The above code shows ________________which sorts the array in alphabetical order using the compareTo() method.

  bubble sort

  insertion sort

  quick sort

  merge sort

 10. When the main method is executed, what is printed?
public class CountingNumbers
{
    public static void main(String[] args)
    {
        int count = 0;
        int[] numbers = {-5,4,-5,3,-2,-4};
        for (int j = 0; j < numbers.length; j++)
        {
            if(numbers[j] < 0 && numbers[j] % 2 != 0)
            {
                count++;
            }
        }
    System.out.println(count);
    }
}

  2

  12

  4

  1

 11. What is the output of the following code?
public class CreationList
{
    public static void main(String[] args)
    {
        int count = 0;
        String[] guestList = {"Adam", "Eve", "Cain", "Abel"};
        String subj1 = null;
        String subj2 = null;
        for (int j = 0; j < guestList.length; j++)
        {
            subj1 = guestList[j].substring(0,1);
            subj2 = guestList[j].substring(guestList[j].length()-1);
            if(subj1.equalsIgnoreCase(subj2))
            {
                count--;
            }
            else if(subj1.equalsIgnoreCase("a"))
            {
                count++;
            }
        }
        System.out.println(count);
    }
}

  1

  4

  3

  2

 12. The contents of arr after the following code has been executed is { {3, 2, 1}, {1, 4, 8}}.
int[][] arr = { {3,2,1},{1,2,3}};
int value = 0;
for (int row = 1; row < arr.length; row++) {
   for (int col = 1; col < arr[0].length; col++) {
      if (arr[row][col] % 2 == 1)
      {
          arr[row][col] = arr[row][col] + 1;
      }
      if (arr[row][col] % 2 == 0)
      {
          arr[row][col] = arr[row][col] * 2;
      }
   }
}

  TRUE

  FALSE

 13. Assume that Base b = new Derived(); appears in a client program. The result of the call b.methodOne(); is ABC.
public class Base
{
   public void methodOne()
   {
      System.out.print("A");
      methodTwo();
   }

   public void methodTwo()
   {
      System.out.print("B");
   }
}

public class Derived extends Base
{
   public void methodOne()
   {
      super.methodOne();
      System.out.print("C");
   }

   public void methodTwo()
   {
      super.methodTwo();
      System.out.print("D");
   }
}

  FALSE

  TRUE

 14. Given the following method, redo(82, 3) executes _______ times, with the return value incremented each time i is not equal to zero.
public static int redo(int i, int j)
{
   if (i==0)
      return 0;
   else
      return redo(i/j, j)+1;
}

  8

  2

  3

  6