MCQs on Programming Basics

[Set - 5]

1. Which of the following is an example of polymorphism?

A) Defining a function only once in code
B) Reusing a variable name across modules
C) Using the same method in multiple classes
D) Creating a class without initialization

Correct Answer: C) Using the same method in multiple classes
Explanation: Polymorphism allows methods in different classes to have the same name but behave differently based on the class.

2. How is exception handling implemented in Python?

A) Using try-except blocks for errors
B) Using if-else conditions for logic
C) Using while loops for conditions
D) Using variables to catch issues

Correct Answer: A) Using try-except blocks for errors
Explanation: In Python, exceptions are handled using try blocks to test code and except blocks to handle errors that occur.

3. What is the result of len("Hello World")?

A) 10 as the character count
B) 11 for all characters
C) 12 for spaces included
D) Error due to syntax issue

Correct Answer: B) 11 for all characters
Explanation: The len() function counts all characters in a string, including spaces, so the result is 11.

4. How can you iterate over both keys and values of a dictionary in Python?

A) Use range() for indices
B) Use keys() for iteration
C) Use items() method directly
D) Use dict() to access all

Correct Answer: C) Use items() method directly
Explanation: The items() method returns key-value pairs, allowing iteration over both keys and values in a dictionary.

5. What is the difference between is and == in Python?

A) is checks identity, == checks equality
B) is compares values, == compares types
C) is handles errors, == evaluates logic
D) is defines conditions, == assigns values

Correct Answer: A) is checks identity, == checks equality
Explanation: The is operator checks whether two variables point to the same object, while == checks if their values are equal.

6. What is the correct way to define a list comprehension in Python?

A) {key: value for key in range}
B) (x for x in sequence)
C) {x for x in sequence}
D) [x for x in sequence]

Correct Answer: D) [x for x in sequence]
Explanation: List comprehensions in Python are enclosed in square brackets and provide a concise way to create lists.

7. Which of the following is used to handle errors in Python?

A) pass statement logic
B) def function blocks
C) try-except blocks
D) break statements

Correct Answer: C) try-except blocks
Explanation: The try-except structure is used to handle runtime errors by catching exceptions and executing alternative code.

8. How do you open a file for reading in Python?

A) open(“file.txt”, “r”)
B) open(“file.txt”, “w”)
C) open(“file.txt”, “a”)
D) open(“file.txt”, “x”)

Correct Answer: A) open(“file.txt”, “r”)
Explanation: The r mode opens a file for reading. If the file doesn’t exist, an error is raised.

9. Which method is used to close a file in Python?

A) file.save() for changes
B) file.close() for closure
C) file.flush() for buffer
D) file.quit() to terminate

Correct Answer: B) file.close() for closure
Explanation: The close() method ensures the file is properly closed and resources are released.

10. What is the purpose of the finally block in exception handling?

A) Ensure code runs only if no errors
B) Execute code after the try block
C) Terminate the program execution
D) Skip remaining lines in code

Correct Answer: B) Execute code after the try block
Explanation: The finally block runs regardless of whether an exception occurs, often used for cleanup operations.

11. Which of the following is NOT a reserved keyword in Python?

A) return for functions
B) break in loops
C) execute for calls
D) continue in conditions

Correct Answer: C) execute for calls
Explanation: The word execute is not a reserved keyword in Python, unlike return, break, and continue.

12. How do you generate random numbers in Python?

A) Use random.randint()
B) Use math.random()
C) Use random.randint()
D) Use seed.random()

Correct Answer: C) Use random.randint()
Explanation: The random.randint() function generates a random integer within a specified range.

13. Which of the following functions checks if all elements in a list are true?

A) reduce() for outputs
B) any() for conditions
C) map() for functions
D) all() for all elements

Correct Answer: D) all() for all elements
Explanation: The all() function returns True if all elements in a list evaluate to True.

14. What is the output of the following code: [x**2 for x in range(3)]?

A) [0, 1, 4] in result
B) [1, 4, 9] output
C) Error in syntax logic
D) Null due to no operation

Correct Answer: A) [0, 1, 4] in result
Explanation: The list comprehension squares each number from 0 to 2 (range(3)), resulting in [0, 1, 4].

15. How do you check the type of a variable in Python?

A) Use isinstance(type)
B) Use type(variable)
C) Use len(variable)
D) Use str(variable)

Correct Answer: B) Use type(variable)
Explanation: The type() function returns the data type of a variable, such as int, str, or list.

16. Which of the following methods is used to sort a list in ascending order?

A) list.sort() directly
B) sorted(list) function
C) range() for elements
D) map() for iteration

Correct Answer: A) list.sort() directly
Explanation: The sort() method modifies a list in place to arrange its elements in ascending order.

17. What is the result of int('10') + float('5.5')?

A) 15.5 for numeric addition
B) Error in conversion syntax
C) 10.5 for combined values
D) Null due to type mismatch

Correct Answer: A) 15.5 for numeric addition
Explanation: The string '10' is converted to an integer, '5.5' to a float, and their sum is 15.5.

18. How do you check if a string starts with a specific character in Python?

A) str.match(‘char’)
B) str.contains(‘char’)
C) str.startswith(‘char’)
D) str.prefix(‘char’)

Correct Answer: C) str.startswith(‘char’)
Explanation: The startswith() method checks if a string begins with the specified substring.

19. What is the purpose of the enumerate function in Python?

A) Create lists from sequences
B) Sort elements by order
C) Map keys to values directly
D) Iterate with index pairs

Correct Answer: D) Iterate with index pairs
Explanation: The enumerate() function adds an index to each item in a sequence during iteration.

20. Which keyword is used to define a generator in Python?

A) Use yield in functions
B) Use return in loops
C) Use map for iteration
D) Use break in recursion

Correct Answer: A) Use yield in functions
Explanation: The yield keyword creates a generator, which returns values one at a time, pausing execution between each.

Share this with your friends

Solve More MCQs on Programming Basics

Show All
Lorem Ipsum is simply dumy text of the printing typesetting industry lorem ipsum.

Subscribe to our Newsletter

Subscribe to our Newsletter

& stay ahead of your competitors!

You have been successfully Subscribed! Ops! Something went wrong, please try again.
Subscribe Form

* Subscribe to our email newsletter to get the latest posts delivered right to your email.

© MCQs 4 Exams. All rights reserved.