13.9. Hard Multiple Choice QuestionsΒΆ

These problems are harder than those you will see on the AP CS A exam.

    12-8-1: What is printed when the following main method is executed?

    public class AlphaSort
    {
    
        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
  • This would be true if the for loop inside the main method did not interate through every value in the array.
  • E D C B B A
  • This would be true if the conditional statement inside the for loop stated "if (key.compareTo(letters[i]) < 0)", because that would put the array in a reverse alphabetical order.
  • A B B C D E
  • This is an insertion sort which sorts the array in alphabetical order using the compareTo() method.
  • E D C B A B
  • This would be true if array was not modified at all in the main method.
  • E D C B B
  • This would be true if the conditional statement inside the for loop stated "if (key.compareTo(letters[i]) < 0)" and if the loop did not iterate through every item of the letters array, because that would put the array in a reverse alphabetical order.

You can step through the code above by clicking on the following link Ex-12-8-1.

    12-8-2: What is printed when the following main method is executed?

    public class NumberCount
    {
        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);
        }
    }
    
  • 4
  • This would be true if the if statement was not trying to check if the numbers in the array were negative and odd.
  • 2
  • This answer is correct because the for loop iterates through every element and increments the count if the current number is negative and odd.
  • 12
  • This may be a result of misunderstanding the question, as 12 cannot be an answer because the array length itself is only 6.
  • 1
  • This would be true if the code was looking for the numbers in the array that were positive and odd.

You can step through the code above by clicking on the following link Ex-12-8-2.

    12-8-3: What is printed when the following main method is executed?

    public class GuestList
    {
        public static void main(String[] args)
        {
            int count = 0;
            String[] guestList = {"Anna", "Briana", "Alex", "John"};
            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);
        }
    }
    
  • -3
  • This would be true if there were three strings in the array that had the same first letter as the last letter.
  • -4
  • This would be true if there were four strings in the array that had the same first letter as the last letter.
  • 4
  • This would be true if there had been four strings in the array that had the first letter as an A and those strings' last letter was not an A.
  • 0
  • This is the correct answer. The for loop is iterating through every element in the guestList array and the first if statement is checking to see if the current element in the array starts with the same letter and ends with the same letter. The variable, count decreases by one if that is true. However if that is false, the program goes to the else if statment and checks to see if the first letter is an A. If that is true count increases by one.

You can step through the code above by clicking on the following link Ex-12-8-3.

    12-8-4: What is printed when the following main method is executed?

    public class OddEvenMod
    {
        public static void main(String[] args)
        {
            int[] arr = {8,7,7,3,4,1};
            for (int i = 0; i < arr.length; i++)
            {
                if(arr[i] % 2 == 0)
                {
                    int temp = arr[0];
                    arr[0] = arr[i];
                    arr[i] = temp;
                }
            }
            for (int t = 0; t < arr.length; t++)
            {
                System.out.print((arr[t]) + ",");
            }
        }
    }
    
  • 8,7,7,3,4,1
  • This would be true if the array was not modified at all.
  • 4,7,7,3,8,1
  • This is the correct answer. The for loop is iterating through every element in the array. The if statement is checking to see if the current element is even or odd. If it is even, then the first element of the array and the current element will swap places in the array.
  • 4,8,7,1,3,7
  • This would be true if the loop had brought all the even numbers to the beginning of the array.
  • 1,8,7,7,4,3
  • This would be true if the if statement had said: if(arr[i] % 2 == 1).

You can step through the code above by clicking on the following link Ex-12-8-4.

    12-8-5: What is printed when the following main method is executed?

    public class PrimeOrNot
    {
        private static boolean check(int n)
        {
            for(int i = 2; i < n; i++)
            {
                if(n % 1 == 0)
                    return false;
            }
            return true;
        }
    
        public static void main(String[] args)
        {
            int[] arr = {5,3,2,9,3,4};
            for (int i = 0; i < arr.length; i++)
            {
                if(check(arr[i]))
                {
                    int temp = arr[0];
                    arr[0] = arr[i];
                    arr[i] = temp;
                }
            }
            for (int t = 0; t < arr.length; t++)
            {
                System.out.print((arr[t]) + ",");
            }
        }
    }
    
  • 2,3,5,9,3,4
  • This is the correct answer. The check method is using a for loop and an if statement to return true if the prameter is prime and false if it is not prime. In the main method, the for loop iterates through every element in the array and checks to see if it is prime. If it is prime, then the program will swap that element with the first element in the array.
  • 4,5,2,3,9,3
  • This would be true if the if statement had said: if(!check(arr[i])).
  • 5,3,2,9,3,4
  • This would be true if the array had not been modified at all.
  • 2,3,5,9,3
  • This would be true if the final for loop did not iterate through every element in the array.

You can step through the code above by clicking on the following link Ex-12-8-5.

    12-8-6: What is printed when the following main method is executed?

    public class GradeSort
    {
       public static void main(String[] args)
       {
            String[] names = {"Anna","John","Billy","Bob","Roger","Dominic"};
            int[] grades = {93,100,67,84,86, 93};
            int i, j, first, temp;
            String temp2;
            for (i = grades.length - 1; i > 0; i--)
            {
                first = 0;
                for (j = 1; j <= i; j++)
                {
                    if (grades[j] < grades[first])
                        first = j;
                }
                temp = grades[first];
                grades[first] = grades[i];
                grades[i] = temp;
                temp2 = names[first];
                names[first] = names[i];
                names[i] = temp2;
            }
            for (int t = 0; t < names.length; t++)
            {
                System.out.print((names[t]) + " ");
            }
       }
    }
    
  • Anna John Billy Bob Roger Dominic
  • This would be true if the program did not modify the names array at all.
  • John Dominic Anna Roger Bob Billy
  • This is the correct answer. The program is ordering the grades array from greatest to least as well as keeping the names with the grades.
  • Billy Bob Roger Anna Dominic John
  • This would be true if the program sorted the grades array from the smallest value to the largest value.
  • Anna John Billy Bob Roger
  • This would be true if the program did not modify the names array and if the for loop at the end of the program did not output all the values of the array.

You can step through the code above by clicking on the following link Ex-12-8-6.

    12-8-7: What is printed when the following main method is executed?

    public class DivisibleBy2or3
    {
        private static boolean divCheck(int n)
        {
            if(n % 2 == 0 || n % 3 == 0)
            {
              return true;
            }
            return false;
        }
    
        public static void main(String[] args)
        {
            int[] arr = {6,7,17,3,2,9,1,5};
            for (int i = 0; i < arr.length; i++)
            {
                if(divCheck(arr[i]))
                {
                    int temp = arr[0];
                    arr[0] = arr[i];
                    arr[i] = temp;
                }
            }
            for (int t = 0; t < arr.length; t++)
            {
                System.out.print((arr[t]) + " ");
            }
        }
    }
    
  • 6 7 17 3 2 9 1 5
  • This would be true if the program had not modified the array at all.
  • 9 6 3 2 3 1 5 17
  • This would be true if the loop was moving the position of odd numbers in the array to arr.length-1.
  • 5 1 2 3 6 17 7 9
  • This would be true if the array was printed in the reversed order.
  • 9 7 17 6 3 2 1 5
  • This is the correct answer, because the divCheck method is checking to see if the values in the array are divisible by 2 or 3. If they are, they are swapped with the value at the first position (index 0).

You can step through the code above by clicking on the following link Ex-12-8-7.

Next Section - 14. Preparing for the Exam