Python is a popular, versatile, and high-level programming language known for its ease of use and clean syntax. Whether you’re just starting on your Python programming journey or revisiting the basics, these MCQs can provide a concise review. Here are 50+ MCQs on Python fundamentals, accompanied by explanations.
Topics covered:
- Python Introduction
- OOPs
- File Handling
- Data Types
- Strings
- Exception Handling
- List
- Functions
- Operators
- Dictionary
- Variables
1. Which company developed the Python programming language?
Answer:
Explanation:
Python was created by Guido van Rossum and was first released in 1991 while he was working at CWI.
2. Who developed the Python language?
Answer:
Explanation:
Python was created by Guido van Rossum and was first released in 1991. The other names listed are not associated with the development of the Python language.
3. In which programming language, Python was written?
Answer:
Explanation:
The primary implementation of Python, known as CPython, is written in the C programming language. While there are other implementations of Python (like Jython, which is written in Java), CPython is the most widely used and is considered the standard implementation.
4. Which one of the following is the correct extension of the Python file?
Answer:
Explanation:
Python scripts and programs are saved with the “.py” file extension. This is the standard file extension used for Python source code files.
5. What do we use to define a block of code in Python language?
Answer:
Explanation:
Unlike many programming languages that use curly braces { } to define a block of code, Python uses indentation (usually spaces or tabs) to define blocks. Proper indentation is crucial in Python as it determines the scope and grouping of statements.
6. Which of the following is the correct way to print “Hello, World!” in Python?
Answer:
Explanation:
In Python, the print() function is used to display output. Options b, c, and d are used in other programming languages, but not in Python.
7. Which of the following is used for single-line comments in Python?
Answer:
Explanation:
In Python, the # symbol is used to denote a single-line comment.
8. Which is not an OOP principle?
Answer:
Explanation:
All the others are OOP principles. Concatenation is an operation on strings.
9. Which is used to create an object in Python?
Answer:
Explanation:
In OOP, a class definition is used to instantiate objects.
10. Which is used for private data members in Python?
Answer:
Explanation:
Prefixing a data member with an underscore suggests it’s meant to be private, though it’s just a convention.
11. Which function opens a file for reading in Python?
Answer:
Explanation:
The built-in open() function is used to open files in Python.
12. Which mode is used for binary reading in Python?
Answer:
Explanation:
The ‘rb’ mode is used to read files in binary mode.
13. Which of these is mutable?
Answer:
Explanation:
Among the options, only lists are mutable.
14. Which of the following can’t be a dictionary key?
Answer:
Explanation:
Dictionary keys must be immutable, so lists can’t be keys.
15. What is the output of “Python”.lower()?
Answer:
Explanation:
The lower() method converts all uppercase characters in a string to lowercase.
16. Which method is used to replace parts of a string?
Answer:
Explanation:
The replace() method replaces specified values with other values.
17. Which block is executed no matter if an exception occurs or not?
Answer:
Explanation:
The finally block always executes after the try and except blocks.
18. Which exception is raised when dividing by zero in Python?
Answer:
Explanation:
Division by zero raises a ZeroDivisionError exception.
19. What is the result of list(“Python”)?
Answer:
Explanation:
This function separates each character and creates a list of them.
20. How to add an element to the end of a list?
Answer:
Explanation:
append() adds an element to the end of a list.
21. Which keyword is used to define a function?
Answer:
Explanation:
In Python, functions are defined using the def keyword.
22. What does a function without a return statement return?
Answer:
Explanation:
If there’s no return statement in a function, Python returns None.
23. Which operator is used for floor division?
Answer:
Explanation:
// is the floor division operator in Python.
24. Which operator is used for exponentiation?
Answer:
Explanation:
** is the exponentiation operator in Python.
25. Which of these creates an empty dictionary?
Answer:
Explanation:
Curly braces {} are used for dictionary creation.
26. How to access the value for the key “apple” in a dictionary d?
Answer:
Explanation:
Values in dictionaries are accessed by their keys using square brackets.
27. Which of the following can be a valid variable name in Python?
Answer:
Explanation:
Variable names can start with a letter or underscore and can have letters, numbers, or underscores in them.
28. Which method returns the position of the first occurrence of a specified String value?
Answer:
Explanation:
Both find() and index() can be used, but index() raises an exception if the value isn’t found, whereas find() returns -1.
29. Which operator is used to concatenate two strings?
Answer:
Explanation:
The + operator is used to concatenate two strings.
30. Which statement raises an exception manually?
Answer:
Explanation:
The raise statement allows the programmer to force a specific exception to occur.
31. Which method removes the specified item from the list?
Answer:
Explanation:
The remove() method removes the specified item.
32. What does [1, 2, 3] * 2 give?
Answer:
Explanation:
This operation replicates the list.
33. Which built-in function returns the absolute value?
Answer:
Explanation:
The abs() function returns the absolute value of the specified number.
34. Which keyword is used for variable-length argument lists?
Answer:
Explanation:
*args is used for non-keyworded variable-length arguments, and **kwargs is used for keyworded variable-length arguments.
35. Which of the following checks if the value is not in the list?
Answer:
Explanation:
The not in operator checks if a value is not present in the list.
36. What is the output of 5 // 2?
Answer:
Explanation:
// is the floor division operator which truncates the decimal and rounds down.
37. Which method returns a list of all the values in the dictionary?
Answer:
Explanation:
The values() method returns a list of all the values in the dictionary.
38. Which method is used to remove all items from a dictionary?
Answer:
Explanation:
The clear() method removes all items from a dictionary.
39. Which of the following is true for variables in Python?
Answer:
Explanation:
Variables in Python are dynamically typed, meaning they get their type at runtime based on the value they are assigned.
40. Which of the following creates a new reference to the same list?
Answer:
Explanation:
Assigning one list to another creates a reference to the same object, rather than a new copy.
41. Which method checks if the string starts with a particular substring?
Answer:
Explanation:
The startswith() method is used to check if a string starts with a specified substring.
42. Which string method removes white spaces from both the beginning and the end?
Answer:
Explanation:
The strip() method is used to remove any leading (spaces at the beginning) and trailing (spaces at the end) characters.
43. Which keyword can be used to handle an exception in Python?
Answer:
Explanation:
In Python, the except keyword is used to catch and handle exceptions.
44. What is the purpose of the global keyword in Python?
Answer:
Explanation:
The global keyword allows a function to use a variable defined at the module level.
45. Which of the following defines a lambda function that doubles a number?
Answer:
Explanation:
Lambda functions are anonymous functions defined using the lambda keyword. The correct syntax is the one given in option c.
46. Which operator is used for element-wise multiplication in numpy arrays?
Answer:
Explanation:
In numpy, the * operator is used for element-wise multiplication, while the dot product requires the dot function.
47. Given a dictionary d = {“a”: 1, “b”: 2}, what will d.get(“c”, 3) return?
Answer:
Explanation:
The get method returns the value for the specified key if it exists, otherwise, it returns the default value provided (3 in this case).
48. What will list({“a”: 1, “b”: 2, “c”: 3}.keys()) output?
Answer:
Explanation:
The keys method returns the keys in the dictionary. When converted to a list, it gives the list of keys.
49. Which of the following is not a valid variable name in Python?
Answer:
Explanation:
Variable names cannot start with a number in Python.
50. How can you make a variable private in a Python class?
Answer:
Explanation:
Prefixing a variable with double underscores (__) makes it private. It’s not truly private but is name-mangled to make it less accessible from the outside.