Skip to main content
Logo image

Section 8.3 Practice Exam 3 for the AP CSA Exam

The following 20 questions are similar to what you might see on the AP CSA exam. Please answer each to the best of your ability.
Click the Start button when you are ready to begin the exam, but only then as you can only take the exam once. Click on the Next button to go to the next question. Click on the Previous button to go to the previous question. Use the number buttons to jump to a particular question. Click the Pause button to pause the exam (you will not be able to see the questions when the exam is paused). Click on the Finish button after you have answered all the questions. The number correct, number wrong, and number skipped will be displayed.

Exercises Exercises

    1.

    What is the value of total after the following code executes?
    int[][] matrix = { {4, 5, 6, 7}, {0, 1, 2, 3}, {3, 2, 1, 0}, {8, 9, 1, 2}};
    
    int total = 0;
    for (int row = 0; row < matrix.length; row++)
    {
       total = total + matrix[row][1];
    }
    
    • 10
    • This would be true if it was adding up all the values in the third column, the one at index 2.
    • 15
    • This would be true if it was adding up all the values in the first column, the one at index 0.
    • 17
    • This code adds up all the values in the second column, the one at index 1 since column indices start at 0.
    • 12
    • This would be true if it was adding up all the values in the last column, the one at index 3.
    • 22
    • This would be true if it was adding up all the values in the first row, but the row changes each time through the for loop.

    2.

    Assume that list has been initialized with the following Integer objects: [9, 3, 17, 2, 16, 4, 1]. Which of the following shows the values in list after a call of mystery(4)?
    private List list<Integer>;
    public void mystery(int n)
    {
       for (int i= 0; i < n; i++)
       {
          Object obj = list.remove(0);
          list.add((Integer)obj);
       }
    }
    
    • [9, 3, 17, 2, 16, 4, 1]
    • This would be true if it moved all of the values from the front to the back. But does it?
    • [1, 4, 16, 2, 17, 3, 9]
    • This would be true if the code reversed the list. But does it? Remember that remove(0) removes the first item in the list and returns it. The add method adds the item to the end of the list.
    • [9, 3, 17, 16, 4, 1, 2]
    • This would be true if only the value 2 was moved to the end of the list. Is that what this code does?
    • [16, 4, 1, 9, 3, 17, 2]
    • This code moves the first item to the end of the list 4 times. So it moves the 9, 3, 17, and 2.
    • [2, 16, 4, 1, 9, 3, 17]
    • This would be true if the call was mystery(3) instead of mystery(4). Then it would move the first 3 values in the list to the end of the list.

    3.

    Which of the following expressions can be use to replace the body of mystery so that mystery will return the same result for all values of y?
    public static int mystery(int y)
    {
       y = 2 * y + y;
       y = 2 * y + y;
       return y;
    }
    
    • return 9 * y;
    • The first line is the same as 3y. Then you have to substitute in the new value of y in the second line (2 * 3y + 3y) = 6y + 3y = 9y.
    • return 7 * y;
    • Remember that the second line is using the value of y calculated in the first line for both places y appears.
    • return y;
    • This would be true if the body only had the last line in it. What do the first 2 lines do?
    • return 3 * y;
    • This would be true if the it was missing the second line. What does that line do?
    • return 4 * y;
    • Remember that 2 * y + y is the same as 3 * y.

    4.

    Given the following method declaration. What value is returned from recur(5)?
    public static int recur(int n)
    {
       if (n <= 1)
           return 1;
       else
           return (recur(n-1) + recur(n-2));
    }
    
    • 8
    • The call recur(5) will return recur(4) + recur(3). The call recur(4) returns 5. The call recur(3) returns 3. So recur(5) returns 5 + 3 = 8.
    • 1
    • This method will only return 1 when n is less than or equal to 1. In this case n is 5.
    • 2
    • This would be true if the call was recur(2). This would return recur(1) + recur(0). Both recur(1) and recur(0) would return 1 so recur(2) would return 1 + 1 = 2.
    • 5
    • This would be true if the call was recur(4). This would return recur(3) + recur(2). The call recur(3) returns 3. The call recur(2) returns 2. So recur(4) returns 3 + 2 = 5.
    • 3
    • This would be true if the call was recur(3). This would return recur(2) + recur(1). The call to recur(1) would return 1. The call to recur(2) would return recur(1) + recur(0). Both recur(1) and recur(0) would return 1 so recur(2) would return 1 + 1 = 2. Thus recur(3) would return 2 + 1 = 3.

    5.

    What is printed when the following code is run?
    for (int k = 0; k < 20; k = k + 1)
    {
       if (k % 2 == 0)
          System.out.print(k + " ");
    }
    
    • 1 3 5 7 9 11 13 15 17 19
    • This would be true if k was printed when the reminder was equal to 1 (when the value was odd).
    • 0 2 4 6 8 10 12 14 16 18
    • This code will loop through all the values from 0 to 19, but only print the ones that are even (dividing by 2 has a remainder of 0).
    • 2 4 6 8 10 12 14 16 18
    • The first time through the loop k will have the value 0 and 0 % 2 returns 0 so the 0 will print.
    • 3 6 9 12 15 18
    • This would be true if the test was (k % 3 == 0) and the loop started with k = 1.
    • 0 2 4 6 8 10 13 14 16 18 20
    • The loop will stop when k has the value of 20. So it won’t print a 20.

    6.

    What is printed when the following code executes (runs)?
    List<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.set(1,"c");
    list.add(2, "d");
    list.set(2, "e");
    list.add("g");
    System.out.println(list);
    
    • [a, c, e, d, g]
    • This would be true if it was list.add(2, "e") instead of list.set(2, "e").
    • [c, e, d, b, g]
    • This would be true if the first item in a list was at index 1, but it is at index 0.
    • [a, c, e, g]
    • This code adds "a" to the end of the list: ["a"] and then "b" to the end of the list: ["a", "b"]. Then it changes the value at index 1 to "c": ["a", "c"]. Then it adds "d" at position 2 which first moves to the right any existing values ["a", "c", "d"]. Then it sets the value at index 2 to "e": ["a", "c", "e"]. Then it adds "g" to the end: ["a", "c", "e", "g"].
    • [a, b, e, d, g]
    • For this to be true the 3rd line would have to be list.add("c"). Is it?
    • [a, c, e, d, b, g]
    • This would be true if all of the sets were adds.

    7.

    Which of the following best explains what the method m does?
    public void m(int[][]p)
    {
       int height = p.length;
       for (int row = 0; row < height / 2; row++)
       {
          for (int col = 0; col <p[0].length; col++)
          {
             p[row][col] = p[height - row - 1][col];
          }
       }
    }
    
    • Mirrors the values from the top half to the bottom half of the 2D array
    • This would be true if it was p[height - row - 1][col] = p[row][col];
    • Mirrors the values from the left halt to the right half of the 2D array
    • This would be true if it was looping through all the rows and half the columns and copying from p[row][width - col - 1] = p[row][col];
    • Mirrors the values from the bottom half to the top half of the 2D array
    • This loops through the top half rows (height / 2) and mirrors the values from the bottom half p[row][col] = p[height - row - 1][col]; So p[0][0] = p[height - 1][0] and p[0][1] = p[height - 1][1].
    • Mirrors the values from the right half to the left half of the 2D array
    • This would be true if it was looping through all the rows and half the columns and copying from p[row][width - col - 1] = p[row][col];
    • All values remain the same.
    • How can this be true since p[row][col] = p[height - row - 1][col]?

    8.

    What is the output from mystery(4321) when mystery is defined as follows?
    //precondition: x >=0
    public static void mystery (int x)
    {
       System.out.print(x % 10);
    
       if ((x / 10) != 0)
       {
          mystery(x / 10);
       }
       System.out.print(x % 10);
    }
    
    • 12344321
    • This method prints the right most digit (x % 10 returns the right most digit) and then if x / 10 is not equal to 0 (x < 10) it returns mystery of the current number after chopping off the right most digit. So mystery(4321) prints 1 and then calls mystery(432) which prints 2 and then calls mystery(43) which prints 3 and then calls mystery (4) which prints 4. Since 4 / 10 is equal to 0 it won’t do a recursive call. It prints 4 again and mystery(4) returns. Execution will return to mystery(43) after the recursive call to mystery(4) and the 3 will print and then mystery (43) will return. Execution will return to mystery(432) after the recursive call to mystery(43) and the 2 will print and then mystery (432) will return. Execution will return to mystery(4321) after the recursive call to mystery(432) and the 1 will print and then mystery (4321) will return.
    • 1234
    • This would be true if there wasn’t a second System.out.print(x % 10) after if.
    • 4321
    • This would be true if the first call to System.out.print(x % 10); wasn’t in the method.
    • 43211234
    • This would be true if it was mystery(1234).
    • 32144123
    • How does the 3 get printed first? Remember that x % 10 returns the right most digit in x.

    9.

    Given the following code which of the answers best describes the conditions needed for temp to be true when it is returned?
    boolean temp = false;
    int count = 0;
    for ( int testVal : a)
    {
       if ( testVal == val )
           count++;
    }
    temp = count > 1;
    return temp;
    
    • Whenever the first element in a is equal to val
    • What is count for?
    • Whenever a contains any element which equals val.
    • It only sets temp to true when count is greater than 1.
    • Whenever more than 1 element in a is equal to val.
    • This increments count once for each value in a that is equal to val. It returns true when count is greater than 1.
    • Whenever exactly 1 element in a is equal to val.
    • This would be true if it was temp = count == 1.
    • Whenever the last element in a is equal to val.
    • This could be one line of code return (a[a.length-1] == val).

    10.

    What is the output from the following code segment?
    for (int j = 1; j <=5; j++)
    {
       for (int k = 1; k < 3; k++)
          System.out.print(j * k + " ");
    }
    
    • 1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2 5 1 5 2
    • This would be true if line 3 was: System.out.print(j + " " + k + " ");
    • 1 2 2 4 3 6 4 8
    • This would be true if line 1 was: for (int j = 1; j < 5; j++).
    • 1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2
    • This would be true if line 1 was: for (int j = 1; j < 5; j++) and if line 3 was: System.out.print(j + " " + k + " ");
    • 5 10 15 4 8 12 3 6 9 2 4 6 1 2 3
    • This would be true if line 1 was: for (int j = 5; j >=1; j--) and line 2 was: for (int k = 1; k <= 3; k++).
    • 1 2 2 4 3 6 4 8 5 10
    • This prints j * k and for each value of j from 1 to 5, k changes from 1 to 2. So when j is 1 it will print 1 2. When j is 2 it will print 2 4. When j is 3 it will print 3 6. When j is 4 it will print 4 8. When j is 5 it will print 5 10.

    11.

    Consider the following methods. Which of method1, method2, and method3 would give the same result as sample?
    public void sample(int num1, int num2)
    {
       int result = 99;
       if (num1==num2)
       {
            result = 0;
       }
       else if (num1>num2)
       {
           result = 1;
       }
       else {
           result = -1;
       }
       System.out.println(result);
    }
    
    public void method1(int num1, int num2)
    {
       int result=99;
       if (num1 == num2)
       {
            result = 0;
       }
       else {
          if(num1 > num2)
          {
               result = 1;
          }
          else
          {
              result = -1;
          }
       }
       System.out.println(result);
    }
    
    public void method2(int num1, int num2)
    {
       int result = 99;
       if (num1 == num2)
       {
            result = 0;
       }
       if (num1 >= num2)
       {
            result = 1;
       }
       else
       {
           result = -1;
       }
       System.out.println(result);
    }
    
    public void method3(int num1, int num2)
    {
       int result = 99 ;
       if (num1 == num2)
       {
            result = 0;
       }
       if (num1 > num2)
       {
            result = 1;
       }
       if (num1 < num2)
       {
            result = -1;
       }
       System.out.println(result);
    }
    
    • method1 and method3
    • The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
    • method1 only
    • Another will work as well.
    • method2 only
    • This one won’t work. The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
    • method2 and method3
    • While method3 will work, method2 won’t. The problem with method2 is that if num1==num2 the first if will execute, but so will the second if and result will be set to 1.
    • all of them
    • Two will work, but one will not.

    12.

    What are the first and last values output by the following code segment?
    int t = 13;
    while (t < 29)
    {
       System.out.println(t);
       t++;
    }
    
    • 13, 28
    • It will print the value of t before changing it, so it will print 13 first and the loop ends when t is equal to 29 so the last time it will print 28.
    • 13, 29
    • It prints the value of t before changing it and the loop ends when t equals 29 so how can this be true?
    • 14, 28
    • It prints the value of t before changing it and t starts at 13 so it will print 13 first.
    • 14, 29
    • It prints the value of t before changing it, so neither of these is correct.
    • 1, 28
    • How could it print 1 for the value of t when t is set to 13 initially?

    13.

    Given the following code.
    String s1 = new String("hi");
    String s2 = new String("hi");
    String s3 = s2;
    
    Which of the following would return true:
    I.  s1.equals(s2)
    II. s1 == s2
    III. s2.equals(s3);
    IV. s2 == s3;
    
    • I and III
    • These are both true, but one more is also true.
    • All are true
    • Since s1 and s2 were created using the new operator they do not refer to the same object so s1 == s2 is false.
    • I, III, and IV
    • I is true since they have the same characters in the same order. III and IV are both true since they refer to the same object.
    • II and IV
    • II is not true. Since s1 and s2 were created using the new operator they do not refer to the same object so s1 == s2 is false.
    • III and IV
    • These are both true, but one more is also true.
You have attempted of activities on this page.