If-Else Essentials: Branching Logic Quiz Quiz

Explore the fundamentals of if-else statements and branching logic with these practical questions. Designed to reinforce core programming concepts, this quiz covers syntax, nesting, order of evaluation, and handling multiple conditions in decision-making structures.

  1. Simple If-Else Structure

    Which output will result from the following pseudocode? If age is less than 18, print “Minor”; else, print “Adult”, given age = 20.

    1. Nothing is printed
    2. Error
    3. Minor
    4. Adult

    Explanation: Since age equals 20, which is not less than 18, the else branch is executed, printing 'Adult'. 'Minor' would be correct only if age was below 18. 'Error' is incorrect because the statement executes properly for age = 20. 'Nothing is printed' is wrong, as one of the branches will always result in an output.

  2. Nesting If Statements

    Given: If temperature u003E 30 then if humidity u003E 60 then print “Hot and Humid”; else print “Hot”, what is the output when temperature is 32 and humidity is 70?

    1. Hot
    2. No output
    3. Humid
    4. Hot and Humid

    Explanation: Both temperature (32 u003E 30) and humidity (70 u003E 60) conditions are true, so 'Hot and Humid' is printed. 'Hot' is printed only if humidity is not above 60. 'Humid' is never printed directly by the code, and 'No output' does not occur because both conditions are satisfied.

  3. If-ElseIf-Else Order

    In a code block with: if score u003E= 90 print 'A'; else if score u003E= 80 print 'B'; else print 'C', what grade is shown for score = 88?

    1. D
    2. B
    3. C
    4. A

    Explanation: Score 88 is not greater than or equal to 90, so the first condition fails, but it is greater than or equal to 80, meeting the second condition and printing 'B'. Option 'A' is incorrect as 88 does not meet the first condition. 'C' would be printed for scores below 80 and 'D' is not used in this code block.

  4. Multiple Conditions with Logical Operators

    Which option correctly completes this statement to print “Access granted” only if both isMember and hasPaid are true: if (_____)?

    1. isMember xor hasPaid
    2. not isMember and not hasPaid
    3. isMember or hasPaid
    4. isMember and hasPaid

    Explanation: The use of 'and' ensures that 'Access granted' is only printed if both conditions are true. Using 'or' would allow either one to be true, not both. 'Not isMember and not hasPaid' is the exact opposite, requiring both to be false. 'Xor' would allow access if exactly one is true, which is not the intended logic.

  5. Handling Unexpected Values

    A developer wants to display a warning if the input temperature is negative, an error if it's over 100, and 'OK' otherwise. Which if-else sequence is correct?

    1. If temp u003C 0: error; else if temp u003E 100: warning; else: OK;
    2. If temp u003C 0: warning; else if temp u003E 100: error; else: OK;
    3. If temp == 0: warning; else if temp u003E 100: error; else: OK;
    4. If temp u003E 100: error; else if temp u003C 0: warning; else: OK;

    Explanation: Checking for temp u003C 0 first catches negative temperatures, then temp u003E 100 handles overly high values, and the final else covers all other cases. Option two would incorrectly label negative temperatures as 'error' if temp u003E 100 is checked first. Option three incorrectly focuses on temp == 0 rather than negative values, and option four swaps the warning and error assignments.