Table of Contents
TogglePython Functions MCQ
A Python function is a block of reusable code that is used to perform a specific task. These Python functions help us organize our code into smaller, manageable pieces. They can be called multiple times throughout a program. It is defined using the def keyword, followed by the function’s name, parentheses, and a colon.
1. What is a lambda used for in Python?
a) To declare a function
b) To define a function in a compact way
c) To execute a function
d) To define a variable
Answer: b) To define a function in a compact way
2. Lambda is a function in Python.
a) True
b) False
Answer: a) True
3. How is a function declared in Python?
a) def function_name[]:
b) def function_name():
c) function_name():
d) function function_name:
Answer: b) def function_name():
4. What is type casting in Python?
a) Converting a variable to a different type
b) Changing a function’s behavior
c) Converting a variable to a list
d) Changing a variable’s value
Answer: a) Converting a variable to a different type
5. Which function is used to read a string from the user in Python?
a) scan()
b) input()
c) get()
d) read()
Answer: b) input()
6. What is the purpose of the pass statement in Python?
a) To skip the current iteration of a loop
b) To terminate a function
c) To act as a placeholder for future code
d) To raise an exception
Answer: c) To act as a placeholder for future code
7. What function can be used to round a floating point number in Python?
a) round()
b) ceil()
c) floor()
d) roundOff()
Answer: a) round()
8. How can you convert a NumPy array to a list in Python?
a) array.tolist()
b) list.array()
c) array.convert(list)
d) to_list(array)
Answer: a) array.tolist()
Python Exception Handling MCQ
Exception handling in Python is a mechanism that allows you to manage errors or exceptional situations that arise during the execution of a program, rather than letting the program crash. Python provides a way to handle exceptions using the try, except, else, and finally blocks.
- try block: Contains code that may raise exceptions.
- except block: Handles exceptions raised in the try block.
- else block: Executes if no exception occurs in the try block.
- finally block: Executes code after try, regardless of exceptions, for cleanup.
You can learn best practices for error handling in Python.
1. When is the finally block executed in Python?
a) Before the try block
b) After the except block
c) After the try block, regardless of exceptions
d) Only when an exception occurs
Answer: c) After the try block, regardless of exceptions
2. Which of the following is used to handle exceptions in Python?
a) try…except
b) try…catch
c) do…catch
d) catch…finally
Answer: a) try…except
3. What is the correct way to handle a KeyError in Python?
a) try…catch
b) try…except
c) if…else
d) try…finally
Answer: b) try…except
Python Lists MCQ
1. How can we check the type of a variable in Python?
a) type(variable)
b) isinstance(variable)
c) check_type(variable)
d) var_type(variable)
Answer: a) type(variable)
2. Which of the following methods is used to remove an item from a list in Python?
a) remove()
b) pop()
c) delete()
d) Both a and b
Answer: d) Both a and b
3. How can we concatenate two lists in Python?
a) list1.concat(list2)
b) list1 + list2
c) list1.append(list2)
d) list1.append(list2, axis=0)
Answer: b) list1 + list2
4. Which of the following is used to remove the last item from a list in Python?
a) pop()
b) remove()
c) del()
d) erase()
Answer: a) pop()
5. What will be the output of the following code?
x = [1, 2, 3]
y = x
x[0] = 10
print(y)
a) [1, 2, 3]
b) [10, 2, 3]
c) [1, 2, 3, 10]
d) None
Answer: b) [10, 2, 3]
6. What is the default value of the argument end in Python’s print() function?
a) ‘ ‘
b) ”
c) ‘\n’
d) None
Answer: c) ‘\n’
7. Which operator is used for floor division in Python?
a) /
b) //
c) %
d) **
Answer: b) //
Python Tuples MCQ
1. Which of the following data types is immutable in Python?
a) List
b) Dictionary
c) Set
d) Tuple
Answer: d) Tuple
2. Which of the following is a valid tuple?
a) [1, 2, 3]
b) {1, 2, 3}
c) (1, 2, 3)
d) <1, 2, 3>
Answer: c) (1, 2, 3)
Python Dictionaries MCQ
A dictionary in Python is a collection of key-value pairs. Each key is unique and maps to a value. It is mutable and is created using curly braces {} with keys and values separated by a colon.
1. Which of the following is used to create a new dictionary in Python?
a) []
b) {}
c) ()
d) dict()
Answer: b) {}
2. Which of the following is not a valid set operation in Python?
a) union()
b) intersection()
c) add()
d) difference()
Answer: c) add()
Python Basic MCQ
1. What is the order of precedence in Python?
a) Exponentiation, Multiplication, Addition, Subtraction
b) Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction
c) Multiplication, Addition, Subtraction, Parentheses
d) Division, Exponentiation, Addition, Multiplication
Answer: b) Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction
2. What is the purpose of NumPy in Python?
a) To handle file operations
b) To support large multidimensional arrays and matrices
c) To create graphical user interfaces
d) To execute web-based applications
Answer: b) To support large multidimensional arrays and matrices
3. What is the use of id() function in Python?
a) To get the memory location of an object
b) To get the identifier of a variable
c) To get the type of an object
d) To identify the value of an object
Answer: a) To get the memory location of an object
4. What is PEP 8 in Python?
a) A Python package
b) A style guide for Python code
c) A tool for testing Python code
d) A Python version
Answer: b) A style guide for Python code
5. What is the maximum possible length of an identifier in Python?
a) 10
b) 20
c) 100
d) Unlimited
Answer: d) Unlimited
6. Which of the following is not a feature of Python?
a) Easy to learn
b) Dynamic typing
c) Compiled language
d) Object-oriented
Answer: c) Compiled language
7. Which operator is used for floor division in Python?
a) /
b) //
c) %
d) **
Answer: b) //
8. Which of the following is used to comment a block of code in Python?
a) #
b) /* */
c)
d) //
Answer: a) #
9. Which of the following will sort a list in ascending order in Python?
a) list.sort()
b) list.order()
c) sorted(list)
d) Both a and c
Answer: d)
This completes all the 60 basic Python MCQs which cover functions, exception handling, lists, tuples, and dictionaries. We hope you got most of the MCQ answers. You can also learn about
You can also read our other blogs on How to sort a dictionary in Python and how to write a list to a file in Python.
Pingback: How to truncate decimals in Python
Pingback: Solved EmptyDataError: No columns to parse from file