3.12. Hard Multiple Choice Questions

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

    3-11-1: Which of the following would be the correct result from the following expression: 150 (in decimal) - 21 (in octal) + 101 (in binary) - E (in hexadecimal)?

  • 186
  • This would be true if it was 150 + 17 (21 in octal) + 5 (101 in binary) + 14 (E in hexadecimal) which is 186.
  • 123
  • This would be true if it was 150 - 17 (21 in octal) + 5 (101 in binary) - 15 (F in hexadecimal) which is 123.
  • 125
  • This would be true if it was 150 - 17 (21 in octal) + 5 (101 in binary) - 13 (D in hexadecimal) which is 123.
  • 168
  • How did you get this? Maybe look up how to convert from octal and hexadecimal to decimal.
  • 124
  • This is 150 - 17 (21 in octal) + 5 (101 in binary) - 14 (E in hexadecimal) which is 124.

The following question assumes you know about arrays and methods. You can skip it if you haven’t covered these yet and come back when you have.

    3-11-2: Given the following code are the contents of itemArray and val after a call of mod(itemArray,val)?

    int[] itemArray = {9, 8, 7, 6};
    int val = 5;
    
    public static void mod(int[] a, int value)
    {
       for (int i=0; i < a.length; i++)
       {
          a[i] = i;
       }
       value = a[a.length-1];
    }
    
  • itemArray = {0, 1, 2, 3} and val = 3;
  • This would be true if Java used pass by reference rather than pass by value (it creates copies of the values that are passed).
  • itemArray = {0, 1, 2, 3} and val = 5;
  • Java passes parameters by copying the value. With an array it creates a copy of the object reference. So, mod will change the itemArray, but val won't change since mod only changes the copy of the primitive value.
  • itemArray = {0, 0, 0, 0} and val = 0;
  • How could this have happened?
  • itemArray = {9, 8, 7, 6} and val = 3;
  • Java passes parameters by passing the values this means that the contents of itemArray will be changed by the mod method, but val won't change.
  • itemArray = {9, 8, 7, 6} and val = 5;
  • Java passes parameters by passing the values this means that the contents of itemArray will be changed by the mod method.
You have attempted of activities on this page
Next Section - 3.13. Variables - Summary