0% found this document useful (0 votes)
3 views

Python Quizs

The document is a Python quiz consisting of 40 multiple-choice questions covering various topics such as data types, control structures, functions, and object-oriented programming. Each question presents a code snippet or concept, followed by four answer options. The quiz aims to assess the reader's knowledge and understanding of Python programming.

Uploaded by

ducggwp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Quizs

The document is a Python quiz consisting of 40 multiple-choice questions covering various topics such as data types, control structures, functions, and object-oriented programming. Each question presents a code snippet or concept, followed by four answer options. The quiz aims to assess the reader's knowledge and understanding of Python programming.

Uploaded by

ducggwp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Python Quiz

1. Which type of Python is?


A. Compiler
B. Interpreter
C. Programming
D. Script
2. What is the result of the expression 2 ** 3?
A. 6
B. 8
C. 9
D. 5
3. Which symbol is used to start a comment in Python?
A. // This is a comment
B. % This is a comment
C. # This is a comment
D. /* This is a comment */
4. What is output of the following code?
x = 29%5
if x >= 5:
print("Pass")
else:
print("Fail")
A. Pass
B. Fail
C. “Pass”
D. “Fail”
5. What is output of the following code?
a=0
b=1
if a>=0 and b>=1:
print("2")
elif a and b:
print("1")
else:
print("0")
A. 0
B. 1
C. 2
D. Error Compiling
6. What is output of the following code?
x = True
y = 10
z=3
if x:
if (y%z==0):
print(y%z)
else:
print(int(y/z))
else:
print(None)
A. None
B. 3
C. 4
D. Error Compiling
7. What will the following code output?
i=1
while i <= 3:
print(i)
i += 1
A. 1 2 3
B. 1 2
C. 1 2 3 4
D. Infinite loop
8. What will the following code output?
x=3
y=5
while x > 0:
while y > 3:
print(f"x: {x}, y: {y}")
y -= 1
x -= 1
y=5
A. x: 3, y: 5
x: 3, y: 4
x: 2, y: 5
x: 2, y: 4
x: 1, y: 5
x: 1, y: 4
B. x: 3, y: 5
x: 3, y: 4
x: 2, y: 4
C. Infinite loop
D. Error Compiling
9. What will the following code output?
a=1
b=2
count = 0
while a < 5 or b > 0:
if a % 2 == 0:
b -= 1
else:
a += 1
count += 1
if count > 10: # Safety condition to avoid infinite loop
break
print(f"a: {a}, b: {b}, count: {count}")
A. a: 5, b: 0, count: 11
B. a: 4, b: -1, count: 11
C. a: 2, b: -8, count: 11
D. Infinite loop
10. What will the following code output?
nums = [3, 6, 9, 12]
i=0
while i < len(nums):
if nums[i] % 2 == 0:
nums[i] //= 2
else:
nums[i] += 1
i += 1
print(nums)
A. [4, 3, 10, 6]
B. [4, 3, 6, 10]
C. [4, 3, 10, 12]
D. [4, 3, 12, 16]
11. What will the following code output?
for i in range(1, 4):
for j in range(i):
print(f"i: {i}, j: {j}")
A. i: 1, j: 0
i: 2, j: 0
i: 2, j: 1
i: 3, j: 0
i: 3, j: 1
i: 3, j: 2
B. i: 1, j: 1
i: 2, j: 1
i: 3, j: 3
C. i: 1, j: 0
i: 2, j: 1
i: 3, j: 2
D. Error Compiling
12. What will the following code output?
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
R = [sum([x for x in row if x % 2 == 0]) for row in A]
print(R)
A. [2, 8, 10]
B. [2, 10, 8]
C. [2, 10, 12]
D. [4, 5, 6]
13. What will the following code output?
x = 10
while x > 0:
x -= 1
if x == 7:
break
print(x)
A. 10
9
8
7
B. 9
8
7
C. 9
8
D. 10
9
8
14. What will the following code output?
for i in range(4):
if i == 2:
continue
print(i)
A. 0
1
B. 0
1
3
C. 0
1
2
3
D. 0
1
2
15. What is output of the following code?
def fun(a, b=1):
return a * b
print(fun(3, 2))
A. 3
B. 6
C. 9
D. Error Compiling
16. What is output of the following code?
def fun(n):
if n == 0:
return 1
elif n == 1:
return 1
else:
return fun(n-2)+fun(n-1)
print(fun(5))
A. 7
B. 8
C. 10
D. 13
17. What is output of the following code?
def output():
a = 10
def input():
a += 5
return a
return input()
print(output())
A. 5
B. 10
C. 15
D. Error Compiling
18. What is the result of the following command: print(len("HUST"))?
A. 4
B. 5
C. 6
D. 7
19. What is output of the following code?
text = "DS&AI-SoICT-HUST"
print(text[-7:-4])
A. S-I
B. CT-
C. S-
D. CT
20. What is output of the following code?
Scho = " SoICT "
Clas = "DSAI"
Cor1 = Scho.lstrip().replace("SoICT", "HUST").lower()
Cor2 = Scho.rstrip().replace("SoICT", "HUST").lower()
print(Clas + " " + Cor1)
print(Clas + " " + Cor2)
A. DSAI hust
DSAI hust
B. DSAI hust
DSAI hust
C. DSAI hust
DSAI hust
D. DSAI hust
DSAI hust
E. DSAI soict
DSAI soict
21. What is the result of list(range(5))?
A. [0, 1, 2, 3, 4]
B. [1, 2, 3, 4, 5]
C. [0, 1, 2, 3, 4, 5]
D. [1, 2, 3, 4]
22. What is output of the following code?
lst = [1, 2, 3]
lst.insert(2, 10)
lst.append(4)
lst.remove(1)
lst.sort(reverse=True)
print(lst)
A. [2, 3, 4, 10]
B. [10, 4, 3, 2]
C. [1, 2, 3, 10]
D. [10, 3, 2, 1]
23. What is output of the following code?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2)
A. {1, 2, 3, 3, 4, 5}
B. {1, 2, 4, 5}
C. {1, 2, 3, 4, 5}
D. {3}
E. Error Compiling
24. What is output of the following code?
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1 + set2
print(result)
A. {1, 2, 3, 4, 5, 6}
B. {1, 2, 5, 6}
C. {3, 4}
D. Compiling Error
25. What is output of the following code?
set1 = {1, 2, 3, 4, 5, 6}
result = {x**2 for x in set1 if x % 2 == 0}
print(result)
A. {1, 9, 25}
B. {4, 16, 36}
C. {1, 4, 16, 36}
D. Compiling Error
26. What is output of the following code?
my_dict = {"name": "Thieu", "age": 48}
my_dict["age"] = 49
my_dict["city"] = "Hanoi"
print(my_dict)
A. {'name': 'Thieu', 'age': 48, 'city': 'Hanoi'}
B. {'name': 'Thieu', 'age': 49, 'city': 'Hanoi'}
C. {'name': 'Thieu', 'age': 48}
D. {'name': 'Thieu', 'age': 49}
E. Compiling Error
27. What is output of the following code?
my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}
result = {key: value * 2 for key, value in my_dict.items() if value % 2 == 0}
print(result)
A. {"b": 4, "d": 8}
B. {"b": 2, "d": 4}
C. {"a": 1, "c": 3}
D. {"a": 2, "c": 6}
28. What is output of the following code?
my_dict = {
"team1": {"name": "Team A", "members": ["Alice", "Bob"]},
"team2": {"name": "Team B", "members": ["Charlie", "David"]},
"team3": {"name": "Team C", "members": ["Eve", "Frank"]}
}
result = [team["members"][1] for team in my_dict.values() if "Team" in team["name"]]
print(result)
A. ["Bob", "David", "Frank"]
B. ['Alice', 'Charlie', 'Eve']
C. [“Team A”,”Team B”,”Team C”]
D. Compiling Error
29. Which data type in Python is immutable?
A. list
B. dict
C. tuple
D. set
30. What is output of the following code?
my_tuple = (1, [2, 3], 4)
my_tuple[1][0] = 10
print(my_tuple)
A. (1, [10, 3], 4)
B. (1, [2, 3], 4)
C. (1, [2, 10], 4)
D. Compiling Error
31. What is output of the following code?
def fun(a, b):
return a-b, a + b, a * b
result = fun(2,3)
x,y,z = result
print(x,y,zs)
A. (-1,5,6)
B. -1,5,6s
C. -1 5 6
D. Compiling Error

32. What is output of the following code?


import math as ham_toan
print(ham_toan.sqrt(4))
A. 2.0
B. 2
C. 16
D. 16.0
E. Compiling Error
33. What is output of the following code?
import math as ham_toan
print(ham_toan.ceil(10/3)+ham_toan.ceil(20/3))
A. 9
B. 10
C. 11
D. Compiling Error
34. What will the following code do?
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()
A. Read the file and print its contents
B. Create a new file named "example.txt"
C. Throw an error if the file does not exist
D. Both a and c
35. Given that the file HUST.txt consists of following line:
HUST
SoICT
DSAI
What will the following code ouput?
with open("HUST.txt", "r") as file:
lines = [line.strip() for line in file]
print(lines)
A. ['HUST', 'SoICT', 'DSAI']
B. ['HUST'
'SoICT'
'DSAI']
C. [HUST,SoITC,DSAI]
D. Compiling Error
36. What will the following code output?
class Dog:
def __init__(self, name = “Cun”):
self.name = name
dog = Dog("Mic")
print(dog.name)
A. Cun
B. Mic
C. CunMic
D. Compiling Error
37. What will the following code output?
class Dog:
def speak(self):
return "I am a dog"
class Woof(Dog):
def speak(self):
return "I am a woof"
Cho = Woof()
print(Cho.speak())
A. I am a dog
B. I am a woof
C. I am a dog
I am a woof
D. Compiling Error
38. Which of the following is NOT a basic principle of OOP?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Concatenation
39. Which of the following is NOT a basic principle of OOP?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Concatenation
40. What is the output of the following code?
for i in range(2):
for j in range(2):
print(f"{i}-{j}")
A. 0-0
0-1
1-0
1-1
B. 0-1
1-0
1-1
C. 0 1
D. Error
Middle Exam: IT1110E – Introduction to Programming – 20241
1. Which of the following commands is used to print to the screen?
A. echo
B. printf
C. print
D. display
2. What is the result of the expression 20 % 3?
A. 2
B. 6
C. 6.666667
D. 60
3. Which of the following is a multi-line comment in Python?
A. ''' This is a multi-line comment '''
B. "This is a multi-line comment"
C. /* This is a multi-line comment */
D. // This is a multi-line comment
4. What is output of the following code?
x = 29/5
if x >= 5:
print("Pass")
else:
print("Fail")
A. Pass
B. Fail
C. “Pass”
D. “Fail”
5. What is output of the following code?
a=0
b=1
if a and b:
print("2")
elif a or b:
print("1")
else:
print("0")
A. 0
B. 1
C. 2
D. Error Compiling
6. What is output of the following code?
x=1
y = 20
z=3
if x:
if (y%z==0):
print(y%z)
else:
print(int(y/z))
else:
print(None)
A. None
B. 2
C. 6
D. Error Compiling
7. What will the following code output?
x=5
while x > 0:
print(x, end=” ”)
x -= 1
A. 54321
B. 543210
C. 4321
D. 43210
8. What will the following code output?
i=1
j = 10
while i < 5 and j > 6:
print(f"i: {i}, j: {j}")
i += 1
j -= 2
A. i: 1, j: 10
i: 2, j: 8
i: 3, j: 6
B. i: 1, j: 10
i: 2, j: 8
C. Infinite loop
D. Error Compiling
9. What will the following code output?
a=1
b=2
count = 0
while a < 5 or b > 0:
if a % 2 == 0:
b -= 1
else:
a += 1
count += 1
if count > 10: # Safety condition to avoid infinite loop
break
print(f"a: {a}, b: {b}, count: {count}")
A. a: 5, b: 0, count: 11
B. a: 2, b: -8, count: 11
C. a: 4, b: -1, count: 11
D. Infinite loop
10. What will the following code output?
nums = [3, 6, 9, 12]
i=0
while i < len(nums):
if nums[i] % 2 == 0:
nums[i] //= 2
else:
nums[i] += 1
i += 1
print(nums)
A. [3, 10, 6, 12]
B. [4, 3, 10, 6]
C. [3, 10, 12, 16]
D. [3, 12, 10, 16]
11. What will the following code output?
for i in range(1, 4):
for j in range(i):
print(f"{i},{j}")
A. 1, 1
2, 1
3, 3
B. 1,0
2,0
2,1
3,0
3,1
3,2
C. 1, 0
2, 1
3, 2
D. Error Compiling
12. What will the following code output?
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
R = [sum([x for x in row if x % 2 == 1]) for row in A]
print(R)
A. [2, 8, 10]
B. [2, 10, 12]
C. [4, 5, 16]
D. Compiling Error
13. What will the following code output?
x = 10
while x > 0:
x -= 1
print(x)
if x == 7:
break
A. 10
9
8
7
B. 9
8
7
C. 9
8
D. 10
9
8
14. What will the following code output?
for i in range(4):
print(i)
if i == 2:
continue
A. 0
1
B. 0
1
3
C. 0
1
2
3
D. 0
1
2
15. What is output of the following code?
def fun(a, b=1):
return a ** b
print(fun(3, 2))
A. 3
B. 6
C. 9
D. Error Compiling
16. What is output of the following code?
def fun(n):
if n == 0:
return 1
elif n == 1:
return 2
else:
return fun(n-2)+fun(n-1)
print(fun(5))
A. 7
B. 8
C. 10
D. 13
17. Which keyword is used to inherit a class in Python?
A. extends
B. inherit
C. superclass
D. class
18. What is output of the following code?
def output():
a=5
def input():
a += 10
return a
return input()
print(output())
A. 5
B. 10
C. 15
D. Error Compiling
19. What is output of the following code?
text = "DS&AI-SoICT-HUST"
print(text[-7:-4])
A. S-I
B. CT-
C. S-
D. CT
20. What is output of the following code?
text = "DS&AI-SoICT-HUST"
print(text[-7:-5])
A. S-I
B. CT-
C. S-
D. CT
21. What is output of the following code?
Scho = " SoICT "
Clas = "DSAI"
Cor1 = Scho.rstrip().replace("SoICT", "HUST").lower()
Cor2 = Scho.lstrip().replace("SoICT", "HUST").lower()
print(Clas + " " + Cor1)
print(Clas + " " + Cor2)
A. DSAI hust
DSAI hust
B. DSAI hust
DSAI hust
C. DSAI hust
DSAI hust
D. DSAI hust
DSAI hust
E. DSAI soict
DSAI soict
22. Which python command will print the result: [0, 1, 2, 3, 4, 5]?
A. print(list(range(5)))
B. print(list(range(6)))
C. print(list(range(0:5)))
D. print(list(range(0:6)))
23. What is output of the following code?
numbers = [1, 2, 3, 4, 5]
print([x**2 for x in numbers if x % 2 == 1])
A. [2,4]
B. [1,3,5]
C. [4,16]
D. [1,9,25]
E. [35]

24. What is output of the following code?


lst = [1, 2, 3]
lst.insert(2, 10)
lst.append(4)
lst.remove(1)
lst.sort(reverse=True)
print(lst)
A. [2, 3, 4, 10]
B. [10, 4, 3, 2]
C. [1, 2, 3, 10]
D. [10, 3, 2, 1]
25. What is output of the following code?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 & set2)
A. {1, 2, 3, 3, 4, 5}
B. {1, 2, 4, 5}
C. {1, 2, 3, 4, 5}
D. {3}
E. Error Compiling

You might also like