Advanced If-Statement Challenge: Conditional Logic
18 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Consider the following code snippet: x = 5; y = 10; if x > y: x, y = y, x; print(x, y). What will be the output of this code?

  • (10, 10)
  • (5, 10)
  • (5, 5)
  • (10, 5) (correct)
  • Consider the following code snippet: x = [1, 2, 3, 4, 5]; if len(x) > 3: x = x[1:]; print(x). What will be the output of this code?

  • [1, 2, 3, 4]
  • [1, 2, 3]
  • [2, 3, 4, 5] (correct)
  • [1, 2, 3, 4, 5]
  • What is the output of the code if a > 10 and b > 10: if a + b == 50: print("Sum is 50") else: print("Sum is not 50") else: print("One or both numbers are 10 or less")?

  • Sum is 50
  • Error
  • One or both numbers are 10 or less
  • Sum is not 50 (correct)
  • What is the output of the code list = [10, 20, 30, 40, 50]; if list[:2] == [10, 20]: print("First two elements match.") else: print("Mismatch.")?

    <p>First two elements match.</p> Signup and view all the answers

    What is the output of the loop for i in range(5): if i % 2 == 0: if i != 0: print(f"{i} is even and not zero.") else: print(f"{i} is odd.")?

    <p>1 is odd., 3 is odd., 2 is even and not zero.</p> Signup and view all the answers

    What is the condition for printing "Sum is 50" in the code if a &gt; 10 and b &gt; 10: if a + b == 50: print("Sum is 50")?

    <p>a + b == 50</p> Signup and view all the answers

    In the code list = [10, 20, 30, 40, 50]; if list[:2] == [10, 20]: print("First two elements match."), what is the purpose of list[:2]?

    <p>To get the first two elements of the list</p> Signup and view all the answers

    What is the purpose of the condition if i % 2 == 0: in the loop for i in range(5): if i % 2 == 0: if i != 0: print(f"{i} is even and not zero.") else: print(f"{i} is odd.")?

    <p>To check if the number is even</p> Signup and view all the answers

    What is the output of the code if data['info']['status'] == 'active' and data['info']['count'] &gt; 30: result = 'Proceed' else: result = 'Hold'?

    <p>Proceed</p> Signup and view all the answers

    What does the code if s[:5] == 'Hello': print('Greeting detected!') else: print('No greeting.') print?

    <p>Greeting detected!</p> Signup and view all the answers

    What will be the output of the code snippet involving a loop and an if statement: numbers = [1, 2, 3, 4, 5]; sum = 0; for n in numbers: if n % 2 == 0: sum += n; print(sum)?

    <p>6</p> Signup and view all the answers

    What are the values of a and b after the swap in the code: a = 5; b = 10; if a < b: a, b = b, a?

    <p>a = 10, b = 5</p> Signup and view all the answers

    What is the output of the code that includes an if-else structure within a loop: numbers = [5, 3, 10, 18, 6, 20]; for num in numbers: if num % 2 == 0: print('Even') else: print('Odd')?

    <p>Odd, Odd, Even, Even, Even, Even</p> Signup and view all the answers

    What is the value of message in the code: x = 30; y = 40; if x > 10: if y < 50: message = 'Both conditions met.' else: message = 'Second condition failed.' else: message = 'First condition failed.'?

    <p>Both conditions met.</p> Signup and view all the answers

    What happens when the code a = 5; b = 10; if a < b: a, b = b, a is executed?

    <p>The variables a and b are swapped.</p> Signup and view all the answers

    What is the purpose of the if statement in the code: numbers = [1, 2, 3, 4, 5]; sum = 0; for n in numbers: if n % 2 == 0: sum += n?

    <p>To add only even numbers to the sum.</p> Signup and view all the answers

    What is the output of the code: s = 'Hello World'; if s[:5] == 'Hello': print('Greeting detected!') else: print('No greeting.')?

    <p>Greeting detected!</p> Signup and view all the answers

    What does the code numbers = [5, 3, 10, 18, 6, 20]; for num in numbers: if num % 2 == 0: print('Even') else: print('Odd') do?

    <p>It prints whether each number is even or odd.</p> Signup and view all the answers

    More Like This

    Use Quizgecko on...
    Browser
    Browser