Nested Conditionals - AP Computer Science Principles

  1. Which construct is often preferred over deeply nested conditionals for readability?
    Elif ladder or switch-case. Reduces complexity and improves code readability.
  2. Which error occurs if a nested conditional lacks a closing brace in C++?
    SyntaxError or compilation error. Missing braces prevent proper code compilation.
  3. What error occurs if the indentation is incorrect in a Python nested conditional?
    IndentationError. Python requires consistent indentation for code blocks.
  4. Which logical operators are often used within nested conditionals?
    AND, OR, NOT. Used to combine multiple conditions within conditionals.
  5. What is the output of: if (x > 0) { if (x < 10) { print('Single Digit'); } } for x=5?
    Single Digit. x=5 satisfies both conditions (>0 and <10).
  6. What is the output of: if (n > 0) { if (n % 2 == 0) { print('Even'); } } for n=4?
    Even. n=4 is positive and divisible by 2.
  7. What is the result of this condition: if (x > 5) { if (x < 15) { print('Valid'); } } with x=10?
    Valid. x=10 satisfies both conditions (>5 and <15).
  8. What is the result of: if (height > 170) { if (height < 190) { print('Tall'); } } for height=180?
    Tall. Height 180 falls within the 170-190 range.
  9. What is the output of: if (x == 2) { if (y == 3) { print('Both Match'); } } for x=2, y=3?
    Both Match. Both conditions (x==2 and y==3) evaluate to true.
  10. What is the purpose of indentation in nested conditionals in Python?
    Defines the scope of each conditional block. Whitespace determines which statements belong together.
  11. What is a nested conditional in programming?
    A conditional inside another conditional structure. Allows complex decision-making with multiple levels of conditions.
  12. What happens if a nested conditional is not properly indented in Python?
    It raises an IndentationError. Incorrect indentation breaks Python's syntax rules.
  13. In a nested conditional, what does the term 'nested' imply?
    Contained within another structure. One conditional is placed inside another's scope.
  14. What is the output of: if (age > 18) { if (age < 65) { print('Adult'); } } for age=30?
    Adult. Age 30 is between 18 and 65.
  15. Identify the purpose of nesting conditionals.
    To test multiple conditions in a hierarchical manner. Creates decision trees for complex logic scenarios.
  16. What is the output of: if (p > q) { if (q > r) { print('Chain'); } } for p=5, q=4, r=3?
    Chain. All conditions form a decreasing sequence.
  17. Identify the error in this nested conditional: if (x > y) { if (y > z) }.
    Missing action or statement block. Inner conditional needs code to execute when true.
  18. What is the result of this nested conditional: if (a==b) { if (b==c) { print('Equal'); } } with a=1, b=1, c=1?
    Equal. All three variables have the same value (1).
  19. What signifies the end of a nested conditional block in Java?
    Closing curly brace }. Marks the end of a code block.
  20. What is the output of the nested condition: if (x > y) { if (y > z) { print('Yes'); } } else { print('No'); } when x=3, y=2, z=1?
    Yes. Both conditions (3>2 and 2>1) are true, so 'Yes' prints.
  21. What is the benefit of using nested conditionals?
    Allows complex decision making based on multiple criteria. Enables sophisticated logic with multiple criteria.
  22. Find the error: if (num < 0) { if (num > 10) { print('Invalid'); } }
    Logical error: num cannot be both <0 and >10. Impossible condition: number can't be both negative and >10.
  23. What is the output of: if (x == y) { if (y != z) { print('Mismatch'); } } else { print('Match'); } for x=1, y=2, z=3?
    Match. x≠y (1≠2), so outer condition fails, prints 'Match'.
  24. How do you denote a block of code in C-style languages for nested conditionals?
    Using curly braces { }. Curly braces group statements into blocks.
  25. What is the result of: if (x > y) { if (y > z) { print('Descending'); } } for x=3, y=2, z=1?
    Descending. Values decrease in order (3>2>1).
  26. Which conditional structure can be used to replace nested conditionals for clarity?
    Switch-case or elif ladder. Provides cleaner alternatives to deep nesting.
  27. Describe the function of an 'else' clause in a nested conditional.
    Executes if none of the preceding conditions are true. Provides fallback when all conditions fail.
  28. Which keyword typically introduces a nested conditional in Python?
    if. Standard keyword for conditional statements in Python.
  29. What is the output of: if (a < b) { if (b < c) { print('Ordered'); } } for a=1, b=2, c=3?
    Ordered. Values are in ascending order (1<2<3).
  30. What is the result of: if (x == y) { if (y == z) { print('All Equal'); } } for x=2, y=2, z=2?
    All Equal. All three variables have identical values.
  31. What is the result of: if (score > 50) { if (score < 75) { print('Pass'); } } for score=60?
    Pass. Score 60 falls within the range 50-75.
  32. What is the output of: if (temperature > 30) { if (humidity < 70) { print('Hot and Dry'); } } for temperature=35, humidity=65?
    Hot and Dry. Temperature >30 and humidity <70 both true.

Study these flashcards