# 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) ?
• (A) (!c) && (!d)
• NOTing (negating) an OR expression is the same as the AND of the individual values NOTed (negated). See DeMorgans laws.
• (B) (c || d)
• NOTing an OR expression does not result in the same values ORed.
• (C) (c && d)
• You do negate the OR to AND, but you also need to negate the values of c and d.
• (D) !(c && d)
• This would be equivalent to (!c || !d)
• (E) (!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;

• (A) x = 0;
• If x was set to 1 then it would still equal 1.
• (B) if (x > 2) x *= 2;
• What happens in the original when x is greater than 2?
• (C) if (x > 2) x = 0;
• If x is greater than 2 it will be set to 0.
• (D) 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;

• (A) x = 0;
• No matter what x is set to originally, the code will reset it to 0.
• (B) if (x > 0) x = 0;
• Even if x is < 0, the above code will set it to 0.
• (C) if (x < 0) x = 0;
• Even if x is > than 0 originally, it will be set to 0 after the code executes.
• (D) 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.
• (E) 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)
if (score >= 84 && score <=92)
if (score >=75 && score <= 83)
if (score < 75)

II.  if (score >= 93)
if (score >= 84)
if (score >=75)
if (score < 75)

III. if (score >= 93)
else if (score >= 84)
else if (score >=75)
else

• (A) 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.
• (B) 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.
• (C) 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.
• (D) 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.
• (E) 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 - Hard Multiple Choice Questions