Python Programming - Conditional Statements

Exercise : Conditional Statements - General Questions
  • Conditional Statements - General Questions
31.
What will be the output of the following code?
num = 0
if num:
    print("Non-zero")
else:
    print("Zero")
Non-zero
Zero
Error
No output
Answer: Option
Explanation:
The condition if num checks if num is truthy. Since num is 0, which is falsy, the code inside the else block is executed, printing "Zero."

32.
Which of the following is the correct way to handle multiple exceptions in a try-except block?
try:
    # code block
except Exception1:
    # handle Exception1
except Exception2:
    # handle Exception2
try:
    # code block
except Exception1, Exception2:
    # handle exceptions
try:
    # code block
except (Exception1, Exception2):
    # handle exceptions
try:
    # code block
catch Exception1:
    # handle Exception1
catch Exception2:
    # handle Exception2
Answer: Option
Explanation:
The correct syntax is to use a tuple in the except clause to handle multiple exceptions.

33.
How can you check if a key exists in a dictionary?
Using if key in dict
Using if exists(dict[key])
Using if contains(dict, key)
Using if key: dict
Answer: Option
Explanation:
The in keyword is used to check if a key exists in a dictionary in Python.