MCQs on Python with answers
- Get link
- X
- Other Apps
1. What is Python?
a. A snake b. A programming language c. A type of database d. A web browser Answer: b. A programming language
2. Which symbol is used for comments in Python? a. // b. # c. /* */ d. %% Answer: b. #
3. Which data type is used for whole numbers in Python? a. float b. int c. str d. bool Answer: b. int
4. What does the print() function do in Python?
a. Reads input from the user
b. Displays output to the console
c. Performs mathematical calculations
d. Imports external libraries
Answer: b. Displays output to the console
5. Which of the following is a valid variable name in Python? a. 123var b. my-variable c. _my_var d. break Answer: c. _my_var
6. How do you assign a value to a variable in Python?
a. x = 5
b. value = x
c. 5 = x
d. x == 5
Answer: a. x = 5
7. What is the output of 5 + 3?
a. 8
b. 53
c. 15
d. Error
Answer: a. 8
8. Which of the following is used to take user input in Python? a. input() b. get() c. read() d. scan() Answer: a. input()
9. What is the result of 7 / 3 in Python?
a. 2.3333333333333335
b. 2.0
c. 3.5
d. Error
Answer: a. 2.3333333333333335
10. Which of the following is a valid way to create a list in Python? a. (1, 2, 3) b. [1, 2, 3] c. {1, 2, 3} d. "1, 2, 3" Answer: b. [1, 2, 3]
11. How do you access the third element of a list in Python? a. list[2] b. list(3) c. list[3] d. list.get(2) Answer: a. list[2]
12. What is the output of "Hello" + "World" in Python?
a. HelloWorld
b. Hello World
c. "Hello" + "World"
d. Error
Answer: a. HelloWorld
13. What does the len() function do in Python?
a. Returns the largest integer less than or equal to a number
b. Returns the smallest integer greater than or equal to a number
c. Returns the length of a sequence (e.g., string, list)
d. None of the above
Answer: c. Returns the length of a sequence (e.g., string, list)
14. Which operator is used for exponentiation in Python? a. ^ b. ** c. % d. / **Answer: b. **
15. What is the result of True and False in Python?
a. True
b. False
c. None
d. Error
Answer: b. False
16. How do you create a tuple in Python? a. (1, 2, 3) b. [1, 2, 3] c. {1, 2, 3} d. "1, 2, 3" Answer: a. (1, 2, 3)
17. Which of the following is used to define a function in Python? a. function my_function(): b. def my_function(): c. function = my_function(): d. def = my_function(): Answer: b. def my_function():
18. What is the output of 5 % 2 in Python?
a. 2
b. 2.5
c. 1
d. 0.4
Answer: c. 1
19. How do you check if a key exists in a dictionary in Python? a. key in my_dict b. my_dict.key_exists(key) c. my_dict.has_key(key) d. my_dict.contains(key) Answer: a. key in my_dict
20. What does the append() method do for lists in Python?
a. Removes the last element from the list
b. Adds an element to the end of the list
c. Sorts the list in ascending order
d. Reverses the list
Answer: b. Adds an element to the end of the list
21. What is the result of "Python"[::-1] in Python?
a. "Python"
b. "nohtyP"
c. "P"
d. "n"
Answer: b. "nohtyP"
22. Which of the following is used to represent a single-line comment in Python? a. // b. ''' c. # d. /* Answer: c. #
23. What is the output of bool(0) in Python?
a. True
b. False
c. 0
d. Error
Answer: b. False
24. How do you import an entire module in Python? a. import module_name b. import module_name as alias c. from module_name import * d. include module_name Answer: a. import module_name
set:II
1. What is Python primarily known for? a. Web development b. Data analysis c. Game development d. All of the above Answer: b. Data analysis
2. Which of the following is a valid Python identifier? a. 2var b. my-variable c. _my_var d. break Answer: c. _my_var
3. In Python, how do you comment a single line of code? a. // Comment b. # Comment c. /* Comment */ d. ' Comment Answer: b. # Comment
4. Which of the following data types is mutable? a. int b. float c. str d. list Answer: d. list
**5. What will the following code print?
pythonprint(7 / 3)
a. 2 b. 2.0 c. 2.3333333333333335 d. Error Answer: c. 2.3333333333333335
6. What is the output of print("Hello, " + "World!")?
a. Hello, World!
b. Hello World!
c. HelloWorld!
d. Error
Answer: a. Hello, World!
7. Which of the following is used to take user input in Python? a. input() b. get() c. read() d. scan() Answer: a. input()
8. What does the len() function do in Python?
a. Returns the largest integer less than or equal to a number
b. Returns the smallest integer greater than or equal to a number
c. Returns the length of a sequence (e.g., string, list)
d. None of the above
Answer: c. Returns the length of a sequence (e.g., string, list)
9. What is the output of range(5)?
a. [0, 1, 2, 3, 4]
b. (0, 1, 2, 3, 4)
c. 5
d. Error
Answer: b. (0, 1, 2, 3, 4)
10. What is the purpose of the break statement in Python?
a. To start a new loop iteration
b. To exit the current loop prematurely
c. To skip the current iteration of a loop
d. None of the above
Answer: b. To exit the current loop prematurely
11. Which of the following is not a valid way to create a dictionary in Python?
a. my_dict = {}
b. my_dict = dict()
c. my_dict = {1: 'one', 2: 'two'}
d. my_dict = {1, 2, 3}
Answer: d. my_dict = {1, 2, 3}
12. What is the output of 2 + 3 * 4?
a. 20
b. 14
c. 18
d. 35
Answer: b. 14
13. Which of the following is the correct way to open a file named "example.txt" for reading in Python?
a. file = open("example.txt", "r")
b. file = open("example.txt", "w")
c. file = open("example.txt", "a")
d. file = open("example.txt", "x")
**Answer: a. file = open("example.txt", "r")
14. What is the result of 3 == "3" in Python?
a. True
b. False
c. TypeError
d. None
Answer: b. False
15. Which of the following is a Python keyword? a. function b. lambda c. declare d. variable Answer: b. lambda
16. What is the output of bool(0)?
a. True
b. False
c. 0
d. Error
Answer: b. False
17. How do you declare a function in Python?
a. def my_function():
b. function my_function():
c. define my_function():
d. function = my_function():
Answer: a. def my_function():
18. What is the output of list("hello")?
a. ['h', 'e', 'l', 'l', 'o']
b. 'hello'
c. ['hello']
d. Error
Answer: a. ['h', 'e', 'l', 'l', 'o']
19. What does the append() method do for lists in Python?
a. Removes the last element from the list
b. Adds an element to the end of the list
c. Sorts the list in ascending order
d. Reverses the list
Answer: b. Adds an element to the end of the list
20. What is the output of "Python"[::-1]?
a. "Python"
b. "nohtyP"
c. "P"
d. "n"
Answer: b. "nohtyP"
21. Which of the following is used to define a class in Python?
a. class MyClass:
b. def MyClass():
c. class = MyClass:
d. Class MyClass:
Answer: a. class MyClass:
22. How do you check if a key exists in a dictionary?
a. key in my_dict
b. my_dict.key_exists(key)
c. my_dict.has_key(key)
d. my_dict.contains(key)
Answer: a. key in my_dict
23. What is the output of 5 % 2?
a. 2
b. 2.5
c. 1
d. 0.4
Answer: c. 1
24. Which of the following is not a valid way to import a module in Python?
a. import my_module
b. import my_module as mm
c. from my_module import *
d. include my_module
Answer: d. include my_module
- Get link
- X
- Other Apps
Comments
Post a Comment