Blocks in Python MCQs||Python Blocks

Python Blocks MCQs - Test Your Programming Knowledge

Understanding Python Blocks - 25 Multiple Choice Questions

1. What is a block of code in Python?
A) A single line of code
B) A collection of statements
C) A variable declaration
D) None of the above
Answer: B
2. Which of the following defines a block of code?
A) Indentation
B) Curly braces
C) Parentheses
D) Semicolons
Answer: A
3. In Python, which statement starts a block of code?
A) if
B) for
C) while
D) All of the above
Answer: D
4. What happens if the indentation is not consistent?
A) The code runs correctly
B) An IndentationError is raised
C) The program stops execution
D) None of the above
Answer: B
5. Which of the following is a valid block in Python?
A) if x > 0: print("Positive")
B) if x > 0: print("Positive") else print("Non-positive")
C) if x > 0: print("Positive")
print("Non-positive")
D) None of the above
Answer: A
6. What is the purpose of a function block?
A) To store variables
B) To define reusable code
C) To create a loop
D) None of the above
Answer: B
7. How do you define a block of code for a function?
A) Using indentation after the function definition
B) Using curly braces
C) Using parentheses
D) None of the above
Answer: A
8. Which of the following is NOT a valid block structure in Python?
A) if statements
B) for loops
C) try-except blocks
D) All of the above are valid
Answer: D
9. What does the `else` block do in a loop?
A) It executes if the loop is never entered
B) It runs after the loop completes normally
C) It runs only if the loop is broken
D) None of the above
Answer: B
10. What is the output of this code: `if True:\n print("Yes")`?
A) Yes
B) True
C) Nothing
D) None of the above
Answer: A
11. How can you identify the beginning of a block?
A) By a colon (`:`)
B) By a semicolon (`;`)
C) By parentheses
D) By indentation
Answer: A
12. What is the effect of breaking out of a loop?
A) It executes the `finally` block
B) It skips the `else` block
C) Both A and B
D) None of the above
Answer: C
13. What will happen if you forget to indent a block under an `if` statement?
A) The program runs without errors
B) IndentationError will occur
C) The condition will not be checked
D) None of the above
Answer: B
14. Which of the following can contain a block of code?
A) Function
B) Class
C) Conditional statements
D) All of the above
Answer: D
15. What is the purpose of a `try-except` block?
A) To handle errors
B) To define functions
C) To create loops
D) None of the above
Answer: A
16. How do you define a block of code that executes if an exception occurs?
A) Using `try`
B) Using `except`
C) Using both `try` and `except`
D) None of the above
Answer: C
17. What is the correct way to write a block for a `for` loop?
A) for i in range(5): print(i)
B) for i in range(5) print(i)
C) for i in range(5) {print(i)}
D) None of the above
Answer: A
18. What will happen if an exception is not caught?
A) The program continues to run
B) The program will terminate
C) The block of code will be skipped
D) None of the above
Answer: B
19. Which keyword is used to define a block that handles exceptions?
A) handle
B) except
C) catch
D) None of the above
Answer: B
20. What will happen if the indentation is different in the same block?
A) It will execute without issues
B) IndentationError will occur
C) It will skip the block
D) None of the above
Answer: B
21. Can you have nested blocks in Python?
A) Yes
B) No
C) Only in functions
D) None of the above
Answer: A
22. What does the `finally` block do?
A) It executes only if an exception occurs
B) It executes no matter what
C) It executes only if there are no exceptions
D) None of the above
Answer: B
23. Which of the following is a valid way to write a `while` loop?
A) while condition:
print("Looping")
B) while(condition) { print("Looping"); }
C) while condition print("Looping")
D) None of the above
Answer: A
24. Can a block of code contain another block?
A) Yes, it can be nested
B) No, blocks cannot be nested
C) Only in functions
D) None of the above
Answer: A
25. What is the result of inconsistent indentation in Python?
A) The program will run correctly
B) An IndentationError will be raised
C) It will be ignored
D) None of the above
Answer: B

© 2024 Understanding Python Blocks MCQs

Post a Comment

0 Comments