brianwallace1993
brianwallace1993 20h ago β€’ 0 views

Conditional Statements Debugging Quiz: Test Your Understanding

Hey everyone! πŸ‘‹ Ready to sharpen your coding skills? Debugging conditional statements can be tricky, but it's super important for writing robust code. This quiz is designed to really test your understanding and help you spot those common errors. Let's see how well you can pinpoint the problems! πŸš€
πŸ’» Computer Science & Technology
πŸͺ„

πŸš€ Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

βœ… Best Answer

πŸ“š Quick Study Guide: Conditional Statement Debugging

  • πŸ’‘ Understanding Conditional Logic: Conditional statements (if, else if/elif, else) control program flow based on whether a condition is true or false.
  • 🚨 Common Syntax Errors:
    • πŸ“ Missing colons (:) in Python.
    • πŸ”— Unmatched curly braces ({}) in C++, Java, JavaScript.
    • 🚫 Missing semicolons (;) at the end of statements in C++, Java, JavaScript.
  • 🧠 Logical Errors:
    • ❌ Incorrect comparison operators (e.g., using = instead of == for comparison).
    • πŸ”„ Flawed boolean logic (e.g., using AND where OR is needed, or vice-versa).
    • πŸ”’ Incorrect order of conditions (e.g., placing a general condition before a more specific one).
    • 🎯 Off-by-one errors in range checks.
  • πŸ› οΈ Debugging Strategies:
    • πŸ–¨οΈ Use print() or console.log() statements to trace variable values and execution path.
    • πŸ” Step through code line-by-line using an IDE's debugger.
    • πŸ”¬ Isolate the problematic code block and test with various inputs.
    • πŸ€” Carefully review the problem statement and your logic against expected behavior.
  • βš–οΈ Truthiness & Falsiness: Be aware of how different languages evaluate non-boolean values in a conditional context (e.g., 0, empty strings, null, undefined are often "falsy").

🎯 Practice Quiz: Conditional Statements Debugging

Identify the error in each code snippet or determine its output.

Question 1:


# Python
score = 85
if score > 90
    print("Excellent!")
else:
    print("Good effort.")

What is the primary error in this Python code?

  1. Missing indentation for the print statement.
  2. Incorrect comparison operator.
  3. Missing colon after the if condition.
  4. The else statement is unnecessary.

Question 2:


// Java
int age = 17;
String status;
if (age = 18) {
    status = "Adult";
} else {
    status = "Minor";
}
System.out.println(status);

What will be the output of this Java code?

  1. "Adult"
  2. "Minor"
  3. Compilation Error
  4. Runtime Error

Question 3:


// JavaScript
let temp = 25;
if (temp < 0 || temp > 30) {
    console.log("Extreme temperature.");
} else if (temp >= 0 && temp <= 20) {
    console.log("Cool or mild.");
} else {
    console.log("Warm.");
}

What will be printed to the console?

  1. "Extreme temperature."
  2. "Cool or mild."
  3. "Warm."
  4. Nothing, it's an error.

Question 4:


# Python
x = 10
if x > 5:
    if x < 15:
        print("X is between 5 and 15.")
else:
    print("X is not greater than 5.")

What will be the output of this Python code?

  1. "X is between 5 and 15."
  2. "X is not greater than 5."
  3. Syntax Error
  4. No output.

Question 5:


// C++
int num = 0;
if (num) { // This checks if num is non-zero
    std::cout << "Number is truthy." << std::endl;
} else {
    std::cout << "Number is falsy." << std::endl;
}

What will be the output of this C++ code?

  1. "Number is truthy."
  2. "Number is falsy."
  3. Compilation Error
  4. Runtime Error

Question 6:


// JavaScript
let isLoggedIn = false;
let isAdmin = true;
if (isLoggedIn && isAdmin) {
    console.log("Admin logged in.");
} else if (isLoggedIn || isAdmin) {
    console.log("User or Admin logged in.");
} else {
    console.log("Not logged in.");
}

What will be printed to the console?

  1. "Admin logged in."
  2. "User or Admin logged in."
  3. "Not logged in."
  4. Compilation Error.

Question 7:


# Python
grade = 75
if grade >= 90:
    result = "A"
elif grade >= 80:
    result = "B"
elif grade >= 70:
    result = "C"
else:
    result = "F"
print(result)

What will be the output of this Python code?

  1. "A"
  2. "B"
  3. "C"
  4. "F"
Click to see Answers
  1. Question 1: C (Missing colon after if score > 90)
  2. Question 2: C (Compilation Error: age = 18 is an assignment, not a comparison, which is not allowed in a Java if condition for a primitive type. It would be age == 18.)
  3. Question 3: C (temp is 25. The first condition (temp < 0 || temp > 30) is false. The second condition (temp >= 0 && temp <= 20) is false. So, it falls to the else block.)
  4. Question 4: A (x is 10. x > 5 is true. Then x < 15 is true. The inner print executes. The else is associated with the outer if, but the outer if condition was true, so its else is skipped.)
  5. Question 5: B (In C++, 0 evaluates to false in a boolean context. Any non-zero integer evaluates to true.)
  6. Question 6: B (isLoggedIn is false, isAdmin is true. isLoggedIn && isAdmin is false. isLoggedIn || isAdmin (false || true) is true. So, "User or Admin logged in." is printed.)
  7. Question 7: C (grade is 75. grade >= 90 is false. grade >= 80 is false. grade >= 70 (75 >= 70) is true. So, result becomes "C".)

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€