Python Flow Control and Conditional Blocks - 30 Multiple Choice Questions
1. Which statement is used to test a condition in Python?
A) `test`
B) `if`
C) `check`
D) `validate`
Answer: B
A) `test`
B) `if`
C) `check`
D) `validate`
Answer: B
2. What will be the output of the following code: `print(1 < 2 < 3)`?
A) `True`
B) `False`
C) `1 < 2 < 3`
D) `Error`
Answer: A
A) `True`
B) `False`
C) `1 < 2 < 3`
D) `Error`
Answer: A
3. Which keyword is used to define an alternative block in an if statement?
A) `elseif`
B) `else`
C) `switch`
D) `otherwise`
Answer: B
A) `elseif`
B) `else`
C) `switch`
D) `otherwise`
Answer: B
4. How do you check multiple conditions in Python?
A) Using multiple `if` statements
B) Using `elif`
C) Both A and B
D) None of the above
Answer: C
A) Using multiple `if` statements
B) Using `elif`
C) Both A and B
D) None of the above
Answer: C
5. What will the following code output: `print("Hello" if True else "Goodbye")`?
A) `Hello`
B) `Goodbye`
C) `Error`
D) `None`
Answer: A
A) `Hello`
B) `Goodbye`
C) `Error`
D) `None`
Answer: A
6. Which operator is used for comparison in Python?
A) `=`
B) `==`
C) `=` and `==`
D) `equals`
Answer: B
A) `=`
B) `==`
C) `=` and `==`
D) `equals`
Answer: B
7. What does the `pass` statement do in Python?
A) Exits the current loop
B) Does nothing
C) Raises an exception
D) Ends the program
Answer: B
A) Exits the current loop
B) Does nothing
C) Raises an exception
D) Ends the program
Answer: B
8. What is the output of `print(0 == False)`?
A) `True`
B) `False`
C) `0`
D) `Error`
Answer: A
A) `True`
B) `False`
C) `0`
D) `Error`
Answer: A
9. Which of the following is not a valid comparison operator?
A) `>=`
B) `<=`
C) `==`
D) `=>`
Answer: D
A) `>=`
B) `<=`
C) `==`
D) `=>`
Answer: D
10. How do you define a default action if no condition is met in an if statement?
A) Use `else`
B) Use `default`
C) Use `pass`
D) Use `finally`
Answer: A
A) Use `else`
B) Use `default`
C) Use `pass`
D) Use `finally`
Answer: A
11. Which of the following statements is true?
A) `True == 1`
B) `False == 0`
C) Both A and B
D) Neither A nor B
Answer: C
A) `True == 1`
B) `False == 0`
C) Both A and B
D) Neither A nor B
Answer: C
12. How do you check if a variable `x` is equal to `10`?
A) `if x = 10:`
B) `if x == 10:`
C) `if x === 10:`
D) `if x := 10:`
Answer: B
A) `if x = 10:`
B) `if x == 10:`
C) `if x === 10:`
D) `if x := 10:`
Answer: B
13. What will the following code output: `print("A" if 1 > 0 else "B")`?
A) `A`
B) `B`
C) `1`
D) `Error`
Answer: A
A) `A`
B) `B`
C) `1`
D) `Error`
Answer: A
14. Which statement is used to handle exceptions in Python?
A) `try...except`
B) `catch`
C) `if...else`
D) `raise`
Answer: A
A) `try...except`
B) `catch`
C) `if...else`
D) `raise`
Answer: A
15. What is the output of `print("Python" in "Python Programming")`?
A) `True`
B) `False`
C) `Error`
D) `None`
Answer: A
A) `True`
B) `False`
C) `Error`
D) `None`
Answer: A
16. How can you create a loop that runs until a condition is met?
A) Using `if`
B) Using `for`
C) Using `while`
D) Using `do...while`
Answer: C
A) Using `if`
B) Using `for`
C) Using `while`
D) Using `do...while`
Answer: C
17. What is the purpose of the `break` statement?
A) To skip the current iteration
B) To exit a loop
C) To terminate a program
D) To continue the loop
Answer: B
A) To skip the current iteration
B) To exit a loop
C) To terminate a program
D) To continue the loop
Answer: B
18. What will `print(not True)` output?
A) `True`
B) `False`
C) `0`
D) `1`
Answer: B
A) `True`
B) `False`
C) `0`
D) `1`
Answer: B
19. Which keyword can be used to introduce a function in Python?
A) `def`
B) `function`
C) `define`
D) `method`
Answer: A
A) `def`
B) `function`
C) `define`
D) `method`
Answer: A
20. Which of the following statements is incorrect?
A) `if True:`
B) `if 0:`
C) `if not False:`
D) `if 'a':`
Answer: B
A) `if True:`
B) `if 0:`
C) `if not False:`
D) `if 'a':`
Answer: B
21. How do you indicate a block of code that is part of an if statement?
A) Curly braces `{}`
B) Indentation
C) Parentheses `()`
D) Square brackets `[]`
Answer: B
A) Curly braces `{}`
B) Indentation
C) Parentheses `()`
D) Square brackets `[]`
Answer: B
22. What does the `elif` statement do?
A) Defines a default condition
B) Allows checking another condition if the previous one is false
C) Terminates the program
D) Skips the current iteration
Answer: B
A) Defines a default condition
B) Allows checking another condition if the previous one is false
C) Terminates the program
D) Skips the current iteration
Answer: B
23. What will be the output of `print(5 > 3 and 3 > 1)`?
A) `True`
B) `False`
C) `1`
D) `None`
Answer: A
A) `True`
B) `False`
C) `1`
D) `None`
Answer: A
24. What is the output of `print(bool(0))`?
A) `True`
B) `False`
C) `0`
D) `Error`
Answer: B
A) `True`
B) `False`
C) `0`
D) `Error`
Answer: B
25. Which method is used to handle exceptions?
A) `try...except`
B) `handle`
C) `catch`
D) `except`
Answer: A
A) `try...except`
B) `handle`
C) `catch`
D) `except`
Answer: A
26. What will `print(type(True))` return?
A) class 'int'
B) class 'bool'
C) class 'str'
D) class 'float'
Answer: B
A) class 'int'
B) class 'bool'
C) class 'str'
D) class 'float'
Answer: B
27. What is the result of `print("x" in "example")`?
A) `True`
B) `False`
C) `Error`
D) `None`
Answer: B
A) `True`
B) `False`
C) `Error`
D) `None`
Answer: B
28. How do you indicate an infinite loop in Python?
A) `while True:`
B) `for x in range(0, 0):`
C) `if False:`
D) `do while True:`
Answer: A
A) `while True:`
B) `for x in range(0, 0):`
C) `if False:`
D) `do while True:`
Answer: A
29. What will be the output of `print("A" if False else "B")`?
A) `A`
B) `B`
C) `Error`
D) `None`
Answer: B
A) `A`
B) `B`
C) `Error`
D) `None`
Answer: B
30. Which statement can be used to skip the current iteration of a loop?
A) `continue`
B) `break`
C) `pass`
D) `exit`
Answer: A
A) `continue`
B) `break`
C) `pass`
D) `exit`
Answer: A
1 Comments
Auraaaaaaa+++++++++
ReplyDelete