Python mcq
Python MCQs are very important for improving your Python skills. With the help of Python MCQs, you can score well in interviews and exams. These Python MCQs will be very useful for you.
Q1. Which of the following is NOT a data type?
print is not a data type. It is a function in Python used to display output.
Q2. Which data type is used to store decimal (point) values?
The float data type is used to store decimal (point) values. Any number that contains a decimal point is a float, such as 12.65.
Q3. Which function is used in Python to find the data type of a variable?
In Python, the type() function is used to find the data type of any variable. It tells us what kind of data is stored in the variable.
Q4. What will be the output of the given code?
x = 10
print(type(x))
In this question, the output will be class 'int' because the variable x stores an integer value (10), and the type() function is used to check its data type.
Q5. Which of the following is NOT a valid variable name?
1name is not a valid variable name because variable names in Python cannot start with a number.
Q6. Identify the Bitwise Operator from the following:
& is a Bitwise Operator. It is used to compare the binary (bits) of two numbers.
Q7. Which of the following is NOT a Python operator?
input is not an operator. It is a function used to take input from the user.
Q8. What will be the output of the given code?
x = 5
y = 10
print(x == y)
In this program, x and y are compared using the equality operator (==). Since 5 is not equal to 10, the output will be False.
Q9. What will be the output of the given code?
x = 5
x += 3
print(x)
In this program, an assignment operator (+=) is used. It adds 3 to the value of x. So, 5 + 3 = 8, which is the output.
Q10. What is the use of the if statement?
The if statement is used to check a condition. If the condition is true, the code inside the if block is executed.
Q11. Which keyword is used in Python to create a new function?
In Python, the def keyword is used to create a new function. It indicates the start of a function definition.
Q12. What should be used when we need to check more than two conditions?
When we need to check more than two conditions, we use if-elif-else. The code block where the condition is true gets executed.
Q13. What will be the output of the given code?
x = 15
if x > 20:
print("A")
elif x > 10:
print("B")
elif x > 5:
print("C")
else:
print("D")
The output of the given code will be B because the condition x > 10 is true. Once a condition becomes true, its block is executed and the remaining conditions are not checked.
Q14. Which of the following loop does NOT exist in Python?
Python does not have a do while loop. It only has while loops, for loops, and nested loops.
Q15. Which type of loop is used in the given code?
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
The given code uses a nested loop. When one loop is inside another loop, it is called a nested loop.
Q16. What does the break statement do in Python?
The break statement completely stops a loop. It works with both while loops and for loops.
Q17. Identify the list from the given options:
my_list = [1, 2, 3, 4] is a list. Lists in Python are created using square brackets [].
Q18. What will be the output of the given code?
fruits = ["Apple", "Banana", "Mango", "Orange"]
print(fruits[1])
According to the code, the output will be Banana because list indexing starts from 0. "Apple" is at index 0 and "Banana" is at index 1.
Q19. What is the use of append() in Python?
The append() method is used to add a new item to a list. It adds the item at the end of the list.
Q20. Identify the tuple from the given options:
t = (1, 2, 3, 4, 5) is a tuple. Tuples are created using parentheses ().
Q21. Which of the following data structures works with key-value pairs?
A dictionary works with key-value pairs. We can access values using their keys. In a dictionary, all keys must be unique.
Q22. Which is the correct code to perform string slicing in Python?
s = "python"
In Python, slicing is done by using the variable name followed by square brackets with a range. The syntax s[start:end] is used, so print(s[1:4]) is correct.
Q23. What will be the output of the given code?
text = "hello python world"
print(text.upper())
upper() is a string method that converts all characters into uppercase. Therefore, the output will be HELLO PYTHON WORLD.
Q24. What will be the output of the given code?
name = "Ram"
age = 20
print(f"My name is {name} and I am {age} years old")
The output will be My name is Ram and I am 20 years old because an f-string is used. f-strings allow variables to be directly included and displayed inside a string.
Q25. Why are functions created in Python?
Functions in Python are used to reuse code. When we need the same functionality again, we can call the function instead of writing the same code repeatedly.