5.9. Medium Multiple Choice QuestionsΒΆ

These problems are similar to those you will see on the AP CS A exam.

    5-9-1: Which of the following expressions is equivalent to !(c || d) ?
  • (!c) && (!d)
  • NOTing (negating) an OR expression is the same as the AND of the individual values NOTed (negated). See DeMorgans laws.
  • (c || d)
  • NOTing an OR expression does not result in the same values ORed.
  • (c && d)
  • You do negate the OR to AND, but you also need to negate the values of c and d.
  • !(c && d)
  • This would be equivalent to (!c || !d)
  • (!c) || (!d)
  • This would be equivalent to !(c && d)

    5-9-2: Which of the following is equivalent to the code segment below?

    if (x > 2) x = x * 2;
    if (x > 4) x = 0;
    
  • x = 0;
  • If x was set to 1 then it would still equal 1.
  • if (x > 2) x *= 2;
  • What happens in the original when x is greater than 2?
  • if (x > 2) x = 0;
  • If x is greater than 2 it will be set to 0.
  • if (x > 2) x = 0; else x *= 2;
  • In the original what happens if x is less than 2? Does this give the same result?

    5-9-3: Which of the following is equivalent to the code segment below?

    if (x > 0) x = -x;
    if (x < 0) x = 0;
    
  • x = 0;
  • No matter what x is set to originally, the code will reset it to 0.
  • if (x > 0) x = 0;
  • Even if x is < 0, the above code will set it to 0.
  • if (x < 0) x = 0;
  • Even if x is > than 0 originally, it will be set to 0 after the code executes.
  • if (x > 0) x = -x; else x = 0;
  • The first if statement will always cause the second to be executed unless x already equals 0, such that x will never equal -x.
  • if (x < 0) x = 0; else x = -1;
  • The first if statement will always cause the second to be executed unless x already equals 0, such that x will never equal -x.

    5-9-4: At a certain high school students receive letter grades based on the following scale: 93 or above is an A, 84 to 92 is a B, 75 to 83 is a C, and below 75 is an F. Which of the following code segments will assign the correct string to grade for a given integer score?

    I.   if (score >= 93)
          grade = "A";
         if (score >= 84 && score <=92)
          grade = "B";
         if (score >=75 && score <= 83)
          grade = "C";
         if (score < 75)
          grade = "F";
    
    II.  if (score >= 93)
          grade = "A";
         if (score >= 84)
          grade = "B";
         if (score >=75)
          grade = "C";
         if (score < 75)
          grade = "F";
    
    III. if (score >= 93)
          grade = "A";
         else if (score >= 84)
          grade = "B";
         else if (score >=75)
          grade = "C";
         else
          grade = "F";
    
  • I and III only
  • Choice I uses multiple if's with logical ands in the conditions to check that the numbers are in range. Choice II won't work since if you had a score of 94, it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". Choice III uses ifs with else if to make sure that only one conditional is executed.
  • II only
  • Choice II won't work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
  • III only
  • III is one of the correct answers. However, choice I is also correct. Choice I uses multiple if's with logical ands in the conditions to check that the numbers are in range. Choice II uses ifs with else if to make sure that the only one conditional is executed.
  • I and II only
  • Choice II won't work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
  • I, II, and III
  • Choice II won't work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
Next Section - 5.10. Hard Multiple Choice Questions