# 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