We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details

30+ MCQs on Basic Python with Answers

K. C. Sabreena Basheer Last Updated : 15 Jul, 2024
6 min read

In the rapidly evolving field of data science, Python has emerged as a cornerstone programming language. It is valued for its versatility, extensive library support, and ease of use in handling complex data tasks. As the preferred programming language for data manipulation, analysis, and visualization, proficiency in Python is a crucial skill sought after by employers in the industry. Prepare yourself for Python Interview Questions

Consequently, job interviews often include questions that assess candidates’ competency in leveraging Python’s capabilities to solve real-world problems efficiently. Here, we present a curated list of 30+ Python Interview Questions along with their answers and explanations. Whether you’re preparing for an upcoming interview or looking to strengthen your skills, this collection serves as an invaluable resource for navigating the intricacies.

Python

30+ Python Interview Questions on Basic Python

Q1. Which of the following statements is true about Python?

a) Python is a compiled language
b) Python is a high-level language
c) Python is primarily used for numerical computation only
d) Python is not suitable for web development

Answer: b
Explanation: Python is a high-level programming language known for its readability and simplicity.

Q2. Which symbol is used for single-line comments in Python?

a) //
b) #
c) –
d) /* */

Answer: b
Explanation: The hash symbol (#) is used for single-line comments.

Q3. What will be the output of the following code snippet?

x = 5
y = 2
print(x ** y)

a) 10
b) 7
c) 25
d) 3

Answer: c
Explanation: The ** operator in Python represents exponentiation. Therefore, x ** y equals 5 raised to the power of 2, which is 25.

Q4. Which of the following is a valid Python variable name?

a) 2var
b) _myVar
c) my-var
d) my var

Answer: b
Explanation: Variable names in Python can start with a letter or an underscore (_) followed by letters, digits, or underscores.

Q5. What does the range() function in Python return?

a) A list of numbers
b) A tuple of numbers
c) A sequence of numbers
d) A dictionary of numbers

Answer: c
Explanation: The range() function in Python returns a generator object that generates a sequence of numbers.

Q6. Which of the following is not a valid data type in Python?

a) int
b) float
c) char
d) str

Answer: c
Explanation: In Python, there is no separate data type called “char”. Characters are represented as strings of length 1.

Q7. What does the strip() method do in Python?

a) Removes all leading and trailing spaces from a string
b) Splits a string into a list of substrings
c) Joins multiple strings into one
d) Replaces occurrences of a substring within a string

Answer: a
Explanation: The strip() method in Python removes all leading and trailing whitespace characters from a string.

Q8. Which of the following statements is true about Python’s variable naming convention?

a) Variables must start with a capital letter.
b) Variables cannot contain numbers.
c) Variable names can include underscores (_).
d) Variable names cannot be longer than 10 characters.

Answer: c
Explanation: Python’s variable naming convention allows the use of underscores (_) to separate words in variable names, making them more readable.

Q9. What will be the output of the following code snippet?

print(3 * 'abc')

a) abcabcabcabc
b) abcabc
c) abcabcabc
d) Error: cannot multiply sequence by non-int of type ‘str’

Answer: c
Explanation: The expression 3 * ‘abc’ will concatenate the string ‘abc’ three times, resulting in ‘abcabcabc’.

Q10. Which of the following is used to open a file in Python for reading and writing?

a) r+
b) rw
c) a+
d) Both a and c

Answer: d
Explanation: Both the modes ‘r+’ and ‘a+’ can be used to open a file for reading and writing in Python.

Q11. What does the __init__ method do in Python?

a) It initializes all the variables in a class.
b) It is a constructor method that is automatically called when a new instance of a class is created.
c) It is used to delete an instance of a class.
d) It is a reserved keyword and cannot be used as a method name.

Answer: b
Explanation: The __init__ method is a constructor method in Python that is automatically called when a new instance of a class is created. It is used to initialize the object’s state.

Q12. Which of the following is NOT a valid way to comment out multiple lines of code in Python?

a) Using triple single quotes (”’ ”’)
b) Using triple double quotes (“”” “””)
c) Using the pound sign (#) at the beginning of each line
d) Using the semicolon (;) at the end of each line

Answer: d
Explanation: Using a semicolon (;) at the end of each line is not a valid way to comment out multiple lines of code in Python. The correct options are a), b), and c).

Q13. What is the output of the following code snippet?

print(9 / 2)

a) 4.5
b) 4
c) 4.0
d) Error: division by zero

Answer: a
Explanation: In Python 3, division of two integers using the ‘/’ operator results in a float, so the output will be 4.5.

Q14. Which of the following is NOT a valid data type in Python?

a) tuple
b) array
c) set
d) dictionary

Answer: b
Explanation: In Python, an array is not a built-in data type. However, arrays can be implemented using libraries like NumPy.

Q15. What does the break statement do in Python?

a) It terminates the program immediately.
b) It breaks out of the current loop and resumes execution at the next statement.
c) It raises an exception.
d) It continues to the next iteration of the loop.

Answer: b
Explanation: The break statement in Python is used to exit the current loop prematurely.

Q16. Which of the following is a mutable data type in Python?

a) tuple
b) string
c) list
d) set

Answer: c
Explanation: Lists are mutable, meaning their elements can be changed after the list is created.

Q17. What will be the output of the following code snippet?

x = 10
y = 5
print(x > y and x < 15)

a) True
b) False
c) Error: invalid syntax
d) Error: x and y are not defined

Answer: a
Explanation: The expression x > y and x < 15 evaluates to True because both conditions are True: x is greater than y and x is less than 15.

Q18. Which of the following statements is true about Python?

a) Python is a statically typed language
b) Python is not suitable for machine learning tasks
c) Python is not an open-source language
d) Python uses indentation to define code blocks

Answer: d
Explanation: Python uses indentation to indicate blocks of code, making it highly readable.

Q19. What does the len() function in Python return?

a) The length of a string
b) The number of elements in a list or tuple
c) The number of keys in a dictionary
d) All of the mentioned

Answer: d
Explanation: The len() function in Python can be used to find the length of strings, lists, tuples, dictionaries, and other iterable objects.

Q20. Which of the following is the correct way to open a file named “example.txt” in Python for reading?

a) file = open(“example.txt”, “r”)
b) file = open(“example.txt”, “read”)
c) file = open(“example.txt”, “w”)
d) file = open(“example.txt”, “write”)

Answer: a
Explanation: The correct mode for opening a file for reading in Python is “r”.

Q21. What does the append() method do when used with lists in Python?

a) Adds an element to the end of the list
b) Removes the last element of the list
c) Sorts the elements of the list
d) Reverses the order of elements in the list

Answer: a
Explanation: The append() method adds an element to the end of a list in Python.

Q22. Which of the following is the correct way to define a function in Python?

a) def function_name:
b) function function_name():
c) define function_name():
d) def function_name():

Answer: d
Explanation: In Python, functions are defined using the def keyword followed by the function name and parentheses.

Q23. What will be the output of the following code snippet?

my_list = [1, 2, 3, 4, 5]
print(my_list[2:4])

a) [1, 2]
b) [3, 4]
c) [2, 3]
d) [2, 4]

Answer: b
Explanation: Slicing in Python returns a sublist containing elements from the specified start index (inclusive) to the specified end index (exclusive).

Q24. Which of the following is the correct way to import the math module in Python?

a) import math
b) include math
c) use math
d) require math

Answer: a
Explanation: The import keyword is used to import modules.

Q25. Which of the following is not a valid comparison operator in Python?

a) <=
b) =>
c) ==
d) !=

Answer: b
Explanation: The correct comparison operator for “greater than or equal to” is >=.

Q26. What does the pop() method do when used with lists in Python?

a) Removes the first element of the list
b) Removes the last element of the list
c) Removes the element at the specified index
d) Adds an element to the end of the list

Answer: b
Explanation: By default, the pop() method returns and removes the last element of a list.

Q27. Which of the following is true about Python’s None keyword?

a) It represents an empty string
b) It represents the absence of a value
c) It represents infinity
d) It represents a boolean value

Answer: b
Explanation: The None keyword in Python represents the absence of a value, similar to null in other programming languages.

Q28. What is the output of 10 / 3 in Python?

a) 3.3333
b) 3.0
c) 3
d) 4

Answer: a
Explanation: In Python 3, division (/) always returns a float result, even if the dividend and divisor are both integers.

Q29. Which of the following is a valid way to create a dictionary in Python?

a) {key1: value1, key2: value2}
b) dict(key1=value1, key2=value2)
c) dict([key1, value1], [key2, value2])
d) Both a and b

Answer: d
Explanation: Dictionaries can be created using curly braces ({}) with key-value pairs separated by colons (:) or by using the dict function with key-value pairs separated by an equal sign (=).

Q30. What is the output of the following code?

my_string = "Hello World"
print(my_string[::-1])

a) dlroW olleH
b) Hello World
c) World Hello
d) olleH dlroW

Answer: a) dlroW olleH
Explanation: [::-1] is used to reverse a string.

Q31. What is the output of the following code?

print(type(3.5))

a) <class ‘float’>
b) <class ‘int’>
c) <class ‘string’>
d) <class ‘number’>

Answer: a) <class ‘float’>
Explanation: The type() function is used to get the data type of the value. 3.5 is a floating-point number, hence the type is <class ‘float’>.

Q32. Which of the following is a valid variable name in Python?

a) 1_variable
b) variable_name
c) variable-name
d) None

Answer: b) variable_name
Explanation: A valid variable name must start with a letter or an underscore, followed by letters, digits, or underscores.

Q33. What is the result of the following expression?

'Hello' == 'hello'

a) True
b) False
c) Error
d) None

Answer: b) False
Explanation: String comparison is case-sensitive. Hence, ‘Hello’ and ‘hello’ are not equal.

Sabreena Basheer is an architect-turned-writer who's passioante about documenting anything that interests her. She's currently exploring the world of AI and Data Science as a Content Manager at Analytics Vidhya.

Responses From Readers