Skip to main content
Logo image

Section 2.24 Unit 2 Test

The following questions are similar to what you might see on the AP CSA exam. You may only take this test once while logged in. There are no time limits, but it will keep track of how much time you take. Click on the finish button after you have answered all the questions, and the number correct and feedback on the answers will be displayed.
We estimate that a score of about 50% on this test would correspond to the passing grade of 3 on the AP exam, a score of 65% to a 4, and a score of 80% and above to a 5 on the AP exam. These are just estimates and may not correspond to individual scores.

Exercises Exercises

    1.

    Which of the following code segments will produce the displayed output?
    /* Output:
    1
    22
    333
    4444
    55555
    */
    
    //Loop I
    for (int i = 1; i <= 5; i++)
    {
       for (int j = i; j > 0; j--)
       {
          System.out.print(i);
       }
       System.out.println();
    }
    
    //Loop II
    for (int i = 0; i < 5; i++)
    {
       for (int j = 0; j < i; j++)
       {
          System.out.print(i);
       }
       System.out.println();
    }
    
    //Loop III
    for (int i = 1; i < 5; i++)
    {
       for (int j = i; j > 0; j--)
       {
          System.out.print(i);
       }
       System.out.println();
    }
    
    //Loop IV
    for (int i = 1; i < 6; i++)
    {
       for (int j = 0; j < i; j++)
       {
          System.out.println(i);
       }
    }
    
    //Loop V
    for (int i = 0; i < 5; i++) {
       for (int j = 0; j < i; j++) {
          System.out.print(i+1);
       }
       System.out.println();
    }
    
    • I only
    • Correct! This will loop with i changing from 1 to 5 and then for each i, j will loop from i to 0 printing the value of i and then a new line.
    • I and II only
    • II will loop i from 0 to 4 and j from 0 to i, neglecting to ouput 5.
    • III only
    • III will loop with i changing from 1 to 4 and j from i to 0.
    • IV and V only
    • IV will loop with i changing from 1 to 5 and j from 0 to i but it will print each value on a different line.
    • V only
    • V will loop with i changing from 0 to 4 and j from 0 to i.

    2.

    Consider the following method. What is the output from conditionTest(3,-2);?
    public static void conditionTest(int num1, int num2)
    {
       if ((num1 > 0) && (num2 > 0))
       {
          if (num1 > num2)
             System.out.println("A");
          else
             System.out.println("B");
       }
       else if ((num2 < 0) || (num1 < 0))
       {
          System.out.println("C");
       }
       else if (num2 < 0)
       {
          System.out.println("D");
       }
       else
       {
          System.out.println("E");
       }
    }
    
    • num2 is negative
    • Only one letter will be printed.
    • Correct because num2 is negative and an or is used.
    • Only one letter will be printed.
    • One of the other conditions is true.

    3.

    Which of these loops will output 01234?
    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);
    }
    
    • I only
    • Loop I will produce this output, but it is not the only loop that will output these values.
    • II only
    • Loop II will produce this output, but it is not the only loop that will output these values.
    • II and III only
    • Loop II is correct, but loop III will produce the reverse output, 43210.
    • I and II only
    • Correct! Both of these loops will produce the correct output.
    • I, II, and III
    • While loop I and II will produce the correct output, loop III will actually produce the reverse of the correct output.

    4.

    Consider the following block of code. What value is returned from solution(5)?
    public int solution(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;
    }
    
    • This would be the correct answer if there were only two loops nested, but there are three. Try again!
    • Take a look at how many times each inner loop will execute every time the outer loop runs.
    • Correct!
    • Try again - check the difference between <= and < in each loop.
    • If you got this value you probably made one extra call to the each of the loops, notice that the loops start at 1 and not 0.

    5.

    Which of the following is equivalent to !((x > 10) && (x <= 5)) ?
    • (x < 10) && (x > 5)
    • Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (!A && !B).
    • (x > 10) && (x <=5)
    • Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (A && B).
    • (x <= 10) && (x > 5)
    • Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (!A && !B). The AND should be changed to an OR.
    • (x <= 10) || (x > 5)
    • Correct!
    • (x > 10) || (x <= 5)
    • Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (A || B). Both A and B should also be negated.

    6.

    Consider the following class with the method test. What is the output after the main method is executed calling test(s,b)?
    public class Test1
    {
        public static void test(String str, int y)
        {
            str = str + "bow";
            y = y * 2;
        }
    
        public static void main(String[] args)
        {
            String s = "rain";
            int b = 4;
            test(s, b);
            System.out.println("s=" + s + "; b=" + b);
        }
    }
    
    • s="rainbow"; b=8;
    • Strings are immutable so changing str doesn’t affect the string that s refers to.
    • s="rain"; b=8;
    • Nothing done in the method test affects the value of b.
    • s="rainbow"; b=4;
    • Strings are immutable so changing str doesn’t affect the string that s refers to.
    • s="rain"; b=4;
    • Correct!
    • s="bow"; b=4;
    • All changes to string s result in a new string object.

    7.

    What are the values of var1 and var2 after the following code segment is executed and the while loop finishes?
    int var1 = 0;
    int var2 = 2;
    while ((var2 != 0) && ((var1 / var2) >= 0))
    {
       var1 = var1 + 1;
       var2 = var2 - 1;
    }
    
    • var1 = 0, var2 = 2
    • This would be true if the body of the while loop never executed. This would have happened if the while check was if var1 != 0 instead of var2 != 0
    • var1 = 1, var2 = 1
    • This would be true if the body of the while loop only execued one time, but it executes twice.
    • var1 = 3, var2 = -1
    • This would be true if the body of the while loop executed three times, but it executes twice.
    • var1 = 2, var2 = 0
    • Correct!
    • The loop won’t finish executing because of a division by zero.
    • 0/2 won’t cause a division by zero. The result is just zero.

    8.

    What does the following code print?
    int x = -5;
    while (x < 0)
    {
       x++;
       System.out.print(x + " ");
    }
    
    • 5 4 3 2 1
    • x is initialized (set) to -5 to start and incremented (x++) before the print statement executes.
    • -5 -4 -3 -2 -1
    • x is incremented (x++) from -5 before the print statement executes.
    • -4 -3 -2 -1 0
    • Correct!
    • -5 -4 -3 -2 -1 0
    • x is incremented (x++) from -5 before the print statement executes.
    • -4 -3 -2 -1
    • 0 is printed out the last time through the loop when x is -1 and is incremented.

    9.

    What will be printed after this code is executed?
    for (int i = 0; i <= 15; i++)
    {
       if (i % 3 == 0)
       {
          System.out.print(i + " ");
       }
    }
    
    • 0 3 6 9 12
    • It would also print 15.
    • 0 1 2 3 4 5
    • The conditional would only match multiples of three.
    • 1 4 7 10 13
    • The conditional would only match multiples of three.
    • 0 3 6 9 12 15
    • Yes, the multiples of 3 from 0 to 15.
    • This code will not print anything.
    • This code would print the multiples of 3.

    10.

    Which option will evaluate to true, if and only if both a and b are false?
    • !(a && b)
    • This would be true in any case where a and b weren’t both true
    • !a && b
    • If b was false, this option would be false.
    • !a && !b
    • Correct!
    • a && b
    • This will only be true only when both a and b are true.
    • a || !b
    • This will only be true if a is true, or b is false.

    11.

    After the following code is executed, what does the variable mystery hold?
    public class Mysterious
    {
        public static void main(String[] args)
        {
            String mystery = "";
            String starter = "Hello World!";
            for (int i = 0; i < starter.length(); i++)
            {
                if (i % 2 == 0)
                {
                    mystery += starter.substring(i, i + 1);
                }
            }
        }
    }
    
    • "Hello World!"
    • The variable holds all characters that were stored at even indices for the original phrase.
    • "Hello "
    • The variable holds all characters that were stored at even indices for the original phrase.
    • The variable holds all characters that were stored at even indices for the original phrase.
    • "HloWrd"
    • Correct! The variable holds all characters that were stored at even indices for the original phrase.
    • "el ol!"
    • The variable holds all characters that were stored at even indices for the original phrase.

    12.

    What are the values of a and b after the for loop finishes?
    int a = 10, b = 3, t = 0;
    for (int i = 1; i <= 6; i++)
    {
       t = a;
       a = i + b;
       b = t - i;
    }
    
    • a = 6 and b = 7
    • This would be true if the loop stopped when i was equal to 6. Try again!
    • a = 6 and b = 13
    • Take another look at how a and b change in each iteration of the loop.
    • a = 13 and b = 0
    • Correct!
    • a = 6 and b = 0
    • Almost there! b = 0, but take another look at how a changes in each iteration of the loop.
    • a = 0 and b = 13
    • Take another look at how a and b change within each iteration of the loop. You are close!

    13.

    Consider the following code. What string is referenced by s1 after the code executes?
    String s1 = "Hi There";
    String s2 = s1;
    String s3 = s2;
    String s4 = s1;
    s2 = s2.toLowerCase();
    s3 = s3.toUpperCase();
    s4 = null;
    
    • hi there
    • Strings are immutable and so any change to a string returns a new string.
    • HI THERE
    • Strings are immutable and so any change to a string returns a new string.
    • Hi There
    • Correct!
    • Strings are immutable and so any changes to a string returns a new string.
    • hI tHERE
    • Strings are immutable and so any changes to a string returns a new string.

    14.

    Given following code, which of the following statements is a valid assignment statement using these variables?
    int a = 5;
    int b = 3;
    int c = 4;
    
    • a = 6.7
    • Check the data type of a.
    • b = 87.7
    • Check the data type of b.
    • 12 = c * b
    • Assignment statements must have a variable on the left.
    • c = a - b
    • Correct!
You have attempted of activities on this page.