Search Tutorials


Python MCQ Questions and Answers | JavaInUse

Python Collection MCQ Questions and Answers

Q. What is the output of the code?

def greet(name):
    return f"Hello, {name}!"

print(greet("Alice"))    
A. Hello
B. Hello, Alice
C. Hello, World
D. None of the above

Q. What is the purpose of the "pass" statement in Python?

A. To indicate a placeholder for future code
B. To indicate that a function has no return value
C. To skip the execution of a particular block of code
D. To indicate that a function has no parameters

Q. What is the output of the code?

def add_numbers(a, b):
    return a + b

result = add_numbers(3, 4)
print(result)
A. 6
B. 7
C. None
D. An error occurs

Q. What is the purpose of the "if __name__ == '__main__':" block in Python?

A. To indicate the main execution block of the script
B. To import modules and packages
C. To define global variables
D. To handle command-line arguments

Q. What is the purpose of the "try-except" block in Python?

    def calculate_area(radius):
    pi = 3.14159
    area = pi * radius ** 2
    return area

radius = 5
area = calculate_area(radius)
print(area)
    
A. To handle errors and exceptions
B. To execute code conditionally
C. To repeat a block of code multiple times
D. To define custom error messages

Q. What is the output of the code?

def greet(name):
return "Hello, " + name

name = "Bob"
greeting = greet(name)
print(greeting)
    
A. Hello, Bob
B. Bob
C. Hello
D. None of the above

Q. What is the purpose of the "for" loop in Python?

A. To repeat a block of code a specified number of times
B. To iterate over the elements of a sequence (list, tuple, string, etc.)
C. To execute code as long as a condition is true
D. To handle exceptions and errors





Q. What is the output of the code?

def calculate_average(numbers):
    total = sum(numbers)
    average = total / len(numbers)
    return average

numbers = [1, 2, 3, 4, 5]
avg = calculate_average(numbers)
print(avg)
A. 2
B. 3
C. 4
D. 5

Q. What is the purpose of the "while" loop in Python?

A. To repeat a block of code a specified number of times
B. To iterate over the elements of a sequence
C. To execute code as long as a condition is true
D. To handle exceptions and errors

Q. What is the output of the code?

def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

num = 10
result = is_even(num)
print(result)
A. True
B. False
C. None
D. An error occurs

Q. What is the purpose of the "break" statement in Python?

A. To exit a loop prematurely
B. To skip the rest of the current iteration and move to the next one
C. To exit a function and return a value
D. To terminate the program immediately

Q. What is the output of the code?

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")
greet("Bob")
A. Hello, Alice!
Hello, Bob!
B. Hello, Bob!
C. None
D. An error occurs

Q. What is the purpose of the "continue" statement in Python?

A. To skip the rest of the current iteration and move to the next one
B. To exit a loop prematurely
C. To exit a function and return a value
D. To terminate the program immediately