wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Trắc nghiệm ôn tập THDC A (Python cơ bản) Phần 2

Total questions: 85

Worksheet time: 43mins

Name
Class
Date
1.

Điền biểu thức để in ra phần tử thứ hai của danh sách fruits = ["apple", "banana", "cherry"] trong lệnh: print(____).

a)

fruits[1]

b)

fruits[2]

c)

fruits[0]

2.

Kết quả của đoạn mã sau là gì? mylist = ['apple', 'banana', 'cherry', 'orange', 'kiwi'] print(mylist[1:4])

a)

['banana', 'cherry', 'orange']

b)

['banana', 'cherry', 'orange', 'kiwi']

c)

['cherry', 'orange', 'kiwi']

3.

Dùng khoảng chỉ số thích hợp để in phần tử thứ ba, thứ tư và thứ năm của danh sách fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] trong lệnh: print(fruits[____]).

a)

2:5

b)

3:6

c)

1:4

4.

Cho danh sách mylist = ['apple', 'banana', 'cherry']. Thực hiện lệnh mylist[0] = 'kiwi' rồi in mylist[1]. Kết quả in ra là gì?

a)

apple

b)

banana

c)

cherry

d)

kiwi

5.

Chọn câu lệnh đúng để thay đổi giá trị "apple" thành "kiwi" trong danh sách fruits = ['apple', 'banana', 'cherry'].

a)

fruits[0] = "kiwi"

b)

fruits[1] = "kiwi"

c)

fruits.insert(0, "kiwi")

d)

fruits.append("kiwi")

6.

Cho đoạn mã: mylist = ['apple', 'banana', 'cherry'] mylist[1:2] = ['kiwi', 'mango'] print(mylist[2]) Kết quả in ra là gì?

a)

banana

b)

cherry

c)

kiwi

d)

mango

7.

Cho danh sách mylist = ['apple', 'banana', 'cherry']. Thực hiện mylist.insert(0, 'orange') rồi in mylist[1]. Kết quả in ra là gì?

a)

apple

b)

banana

c)

cherry

d)

orange

8.

Chọn câu lệnh đúng để dùng phương thức append thêm 'orange' vào danh sách fruits = ["apple", "banana", "cherry"].

a)

fruits.append("orange")

b)

fruits.insert(1, "orange")

c)

fruits.add("orange")

d)

append(fruits, "orange")

9.

Chọn câu lệnh đúng để sử dụng phương thức insert thêm 'lemon' làm phần tử thứ hai trong danh sách fruits = ["apple", "banana", "cherry"].

a)

fruits.insert(1, "lemon")

b)

fruits.insert(2, "lemon")

c)

fruits.append("lemon")

d)

fruits.add(1, "lemon")

10.

Có hai danh sách: fruits = ['apple', 'banana', 'cherry'] tropical = ['mango', 'pineapple', 'papaya'] Chọn cách thêm các phần tử của tropical vào fruits.

a)

fruits.join(tropical)

b)

fruits.concat(tropical)

c)

fruits.extend(tropical)

11.

Phương thức nào dùng để xóa một phần tử khỏi danh sách?

a)

pop()

b)

push()

c)

delete()

12.

Chọn câu lệnh đúng để dùng phương thức remove xóa "banana" khỏi danh sách fruits = ['apple', 'banana', 'cherry'].

a)

fruits.remove("banana")

b)

remove(fruits, "banana")

c)

fruits.pop("banana")

d)

fruits.delete("banana")

13.

Cho đoạn mã: mylist = ['apple', 'banana', 'cherry'] mylist.pop(1) print(mylist) Kết quả in ra là gì?

a)

['apple', 'banana']

b)

['apple', 'cherry']

c)

['banana', 'cherry']

14.

Hàm nào dùng để xóa toàn bộ các phần tử trong một danh sách fruits?

a)

del()

b)

pop()

c)

clear()

d)

delete()

15.

Cú pháp nào đúng để lặp qua từng phần tử của một danh sách?

a)

for x in ['apple', 'banana', 'cherry']: print(x)

b)

for x in ['apple', 'banana', 'cherry'] print(x)

c)

foreach x in ['apple', 'banana', 'cherry'] print(x)

16.

Hoàn thành vòng lặp while để lặp qua các phần tử trong danh sách: mylist = ['apple', 'banana', 'cherry'] i = 0 _____: print(mylist[i]) i = i + 1 Chọn dòng điều kiện đúng để điền vào chỗ trống.

a)

while i < len(mylist):

b)

while i <= mylist:

c)

while len(i) < mylist:

17.

Trong ba lựa chọn sau, đâu là cú pháp đúng để lặp qua các phần tử của một danh sách sử dụng list comprehension?

a)

print(x) for x in ['apple', 'banana', 'cherry']

b)

[print(x) for x in ['apple', 'banana', 'cherry']]

c)

for x in ['apple', 'banana', 'cherry'] print(x)

18.

Với đoạn mã sau: fruits = ['apple', 'banana', 'cherry'] newlist = [x for x in fruits if x == 'banana'] Giá trị của newlist sẽ là gì?

a)

['apple', 'cherry']

b)

['banana']

c)

True

19.

Hoàn thành list comprehension sau: fruits = ["apple", "banana", "cherry"] newlist = [x _____ x _____ fruits]

a)

for x in fruits → newlist = [x for x in fruits]

b)

for x in fruits → newlist = [for x in fruits x]

c)

in fruits for x → newlist = [x in fruits for x]

d)

for fruits in x → newlist = [x for fruits in x]

20.

Với đoạn mã: fruits = ['apple', 'banana', 'cherry'] newlist = ['apple' for x in fruits] Giá trị của newlist là gì?

a)

['apple', 'apple', 'apple']

b)

['apple']

c)

True

21.

Cú pháp nào đúng để sắp xếp một danh sách?

a)

mylist.orderby(0)

b)

mylist.order()

c)

mylist.sort()

22.

Cú pháp nào đúng để đảo ngược thứ tự hiện tại của một danh sách (không sắp xếp lại, chỉ lật ngược)?

a)

mylist.sort(-1)

b)

mylist.sort(reverse = True)

c)

mylist.reverse()

23.

Cú pháp nào đúng để sắp xếp một danh sách theo thứ tự giảm dần?

a)

mylist.sort(reverse = True)

b)

mylist.reverse(sort = True)

c)

mylist.sort(desc = True)

d)

mylist.order(descending = True)

24.

Cú pháp nào đúng để tạo một bản sao của danh sách list1?

a)

list2 = list1

b)

list2 = list1.copy()

c)

list2.copy(list1)

25.

Một cú pháp hợp lệ khác để sao chép danh sách list1 là gì?

a)

list2 = list1

b)

list2 = list1.list()

c)

list2 = list(list1)

26.

Cú pháp nào đúng để sao chép toàn bộ danh sách bằng cú pháp cắt (slicing)?

a)

list2 = list1[:]

b)

list2 = list1[]

c)

list2 = list1[-1]

27.

Cú pháp nào đúng để nối list1 và list2 thành list3?

a)

list3 = join(list1, list2)

b)

list3 = list1 + list2

c)

list3 = [list1, list2]

28.

Cú pháp nào đúng để thêm các phần tử của list2 vào cuối list1?

a)

list1.extend(list2)

b)

list1.join(list2)

c)

list1.push(list2)

29.

Với đoạn mã: list1 = ['a', 'b', 'c'] list2 = [1, 2, 3] for x in list2: list1.append(x) Giá trị của list1 sau khi chạy vòng lặp là gì?

a)

['a', 'b', 'c', 1, 2, 3]

b)

[1, 2, 3]

c)

['a', 1, 'b', 2, 'c', 3]

30.

Phần khai báo nào là một tuple?

a)

thistuple = ('apple', 'banana', 'cherry')

b)

thistuple = ['apple', 'banana', 'cherry']

c)

thistuple = {'apple', 'banana', 'cherry'}

31.

Dùng cú pháp nào để in ra số phần tử của tuple fruits = ('apple','banana','cherry')?

a)

print(len(fruits))

b)

print(count(fruits))

c)

print(length(fruits))

32.

Phát biểu sau đúng hay sai: "Không thể xóa các phần tử khỏi tuple sau khi đã tạo"?

a)

Đúng

b)

Sai

33.

Bạn có thể truy cập phần tử của tuple theo chỉ số. Chỉ số của phần tử đầu tiên là bao nhiêu?

a)

-1

b)

0

c)

1

34.

Điền cú pháp đúng để in phần tử đầu tiên của tuple fruits = ('apple','banana','cherry') trong lệnh print(...).

a)

fruits[0]

b)

fruits(0)

c)

fruits{0}

35.

Sử dụng chỉ số âm để in phần tử cuối cùng của tuple fruits = ('apple','banana','cherry') trong lệnh print(...).

a)

fruits[-1]

b)

fruits[-0]

c)

fruits[3]

36.

Sử dụng phạm vi chỉ số để in phần tử thứ ba, thứ tư và thứ năm của tuple fruits = ('apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango') trong lệnh print(fruits[...]).

a)

2:5

b)

3:6

c)

1:4

37.

Bạn không thể thay đổi các phần tử trong tuple, nhưng có cách "lách luật". Phương án nào sau đây sẽ hoạt động?

a)

Chuyển tuple thành danh sách, thay đổi giá trị rồi chuyển lại thành tuple.

b)

Chuyển tuple thành set, thay đổi giá trị rồi chuyển lại thành tuple.

c)

Chuyển tuple thành dictionary, thay đổi giá trị rồi chuyển lại thành tuple.

38.

Cú pháp nào đúng để xóa hẳn một tuple?

a)

delete mytuple

b)

mytuple.delete()

c)

del mytuple

39.

Phát biểu sau đúng hay sai: "Bạn được phép cộng (nối) một tuple vào một tuple hiện có."

a)

Đúng

b)

Sai

40.

Cho code: fruits = ('apple', 'banana', 'cherry') (x, y, z) = fruits print(y) Giá trị in ra của biến y là gì?

a)

apple

b)

banana

c)

cherry

41.

Cho code: fruits = ('apple', 'banana', 'cherry') (x, *y) = fruits print(y) Giá trị của y là gì?

a)

banana

b)

['banana', 'cherry']

c)

banana, cherry

42.

Cho code: fruits = ('apple', 'banana', 'cherry', 'mango') (x, *y, z) = fruits print(y) Giá trị của y là gì?

a)

['banana', 'cherry']

b)

['banana', 'cherry', 'mango']

c)

['cherry', 'mango']

43.

Cú pháp nào sau đây là đúng để lặp qua các phần tử của một tuple?

a)

for x in ('apple', 'banana', 'cherry'): print(x)

b)

for x in ('apple', 'banana', 'cherry') print(x)

c)

foreach x in ('apple', 'banana', 'cherry') print(x)

44.

Điền vào chỗ trống để hoàn chỉnh vòng lặp while dùng để duyệt qua các phần tử của một tuple. mytuple = ('apple', 'banana', 'cherry') i = 0 ___ i < ___(mytuple): print(mytuple[i]) i = i + 1

a)

while, len

b)

while, length

c)

for, len

45.

Điền vào chỗ trống để duyệt qua các phần tử của một tuple bằng chỉ số: for i in ___(___(thistuple)): print(thistuple[i])

a)

range

b)

len

c)

enumerate

d)

list

46.

Cú pháp nào là đúng để nối tuple1tuple2 thành tuple3?

a)

tuple3 = join(tuple1, tuple2)

b)

tuple3 = tuple1 + tuple2

c)

tuple3 = [tuple1, tuple2]

47.

Cách nào là hợp lệ để biến tuple (1,2,3) thành (1,2,3,1,2,3)?

a)

tuple1 = (1,2,3); tuple1 = tuple1 * 2

b)

tuple1 = (1,2,3); tuple1.double()

c)

tuple1 = (1,2,3); tuple1.add(_self)

48.

Xét đoạn code: tuple1 = ('a', 'b', 'c') tuple2 = (1, 2, 3) tuple3 = tuple2 + tuple1 Giá trị của tuple3 là gì?

a)

('a', 'b', 'c', 1, 2, 3)

b)

(1, 2, 3, 'a', 'b', 'c')

c)

('a', 1, 'b', 2, 'c', 3)

49.

Đoạn code nào sau đây tạo ra một set?

a)

myset = ('apple', 'banana', 'cherry')

b)

myset = ['apple', 'banana', 'cherry']

c)

myset = {'apple', 'banana', 'cherry'}

50.

Các phần tử trong set không thể bị xóa sau khi set đã được tạo.

a)

True

b)

False

51.

Một set không thể chứa hai phần tử có cùng giá trị.

a)

True

b)

False

52.

Hàm nào dùng để trả về số lượng phần tử trong một set? fruits = {'apple', 'banana', 'cherry'} print(___(fruits))

a)

len

b)

count

c)

size

d)

length

53.

Đúng hay Sai: Bạn có thể truy cập phần tử của set bằng chỉ số (index).

a)

True

b)

False

54.

Điền vào chỗ trống để kiểm tra "apple" có tồn tại trong set hay không. fruits = {'apple', 'banana', 'cherry'} if "apple" ___ fruits: print("Yes, apple is a fruit!")

a)

in

b)

contains

c)

has

d)

within

55.

Xét đoạn code: thisset = {'apple', 'banana', 'cherry'} print('banana' not in thisset) Kết quả in ra là gì?

a)

True

b)

False

c)

{'apple', 'cherry'}

56.

Cú pháp nào là đúng để thêm phần tử vào một set?

a)

add()

b)

insert()

c)

push()

57.

Sử dụng phương thức thích hợp để thêm "orange" vào set fruits. fruits = {'apple', 'banana', 'cherry'} ___

a)

fruits.add("orange")

b)

fruits.push("orange")

c)

fruits.append("orange")

d)

fruits.insert("orange")

58.

Sử dụng phương thức thích hợp để thêm nhiều phần tử vào set fruits. fruits = {'apple', 'banana', 'cherry'} more_fruits = ["orange", "mango", "grapes"] ___

a)

fruits.update(more_fruits)

b)

fruits.extend(more_fruits)

c)

fruits.append(more_fruits)

d)

fruits.add(more_fruits)

59.

Cú pháp nào là đúng để xóa một phần tử khỏi set?

a)

delete()

b)

remove()

c)

extract()

60.

Sử dụng phương thức remove để xóa "banana" khỏi set fruits. fruits = {'apple', 'banana', 'cherry'} ___

a)

fruits.remove("banana")

b)

fruits.delete("banana")

c)

fruits.pop("banana")

d)

fruits.clear("banana")

61.

Sử dụng phương thức discard để xoá "banana" khỏi set fruits. fruits = {'apple', 'banana', 'cherry'} ___

a)

fruits.discard("banana")

b)

fruits.remove("banana")

c)

fruits.delete("banana")

d)

fruits.pop("banana")

62.

Hàm nào dùng để xóa toàn bộ phần tử trong một set? fruits = {'apple', 'banana', 'cherry'} fruits.___

a)

clear()

b)

delete()

c)

remove()

d)

empty()

63.

Cú pháp nào là đúng để lặp qua các phần tử của một set?

a)

for x in {'apple', 'banana', 'cherry'}:\n print(x)

b)

for x in {'apple', 'banana', 'cherry'}\n print(x)

c)

foreach x in {'apple', 'banana', 'cherry'}\n print(x)

64.

Điền vào chỗ trống để hoàn chỉnh vòng lặp for dùng để duyệt qua các phần tử của một set. myset = {'apple', 'banana', 'cherry'} ___ x ___ myset: print(x)

a)

for

b)

while

c)

in

d)

of

65.

Đúng hay Sai: Set là không có thứ tự, nên khi lặp qua các phần tử thì thứ tự sẽ hoàn toàn ngẫu nhiên.

a)

True

b)

False

66.

Cú pháp nào sau đây là đúng để nối nhiều set thành một set mới set5?

a)

set5 = join(set1, set2, set3, set4)

b)

set5 = set1 | set2 | set3 | set4

c)

set5.union(set1, set2, set3, set4)

67.

Trong các phương thức sau, phương thức nào chỉ giữ lại các phần tử trùng nhau giữa các set?

a)

intersection()

b)

union()

c)

difference()

68.

Phát biểu nào sau đây là đúng về frozenset?

a)

It is a mutable set

b)

It is an immutable set

c)

It preserves insertion order

69.

Phép toán nào sau đây là hợp lệ với frozenset?

a)

add(1)

b)

remove(1)

c)

union({1,2})

70.

Kiểu dữ liệu của frozenset là gì?

a)

set

b)

frozenset

c)

list

71.

Đoạn code nào sau đây tạo ra một dictionary?

a)

x = ('apple', 'banana', 'cherry')

b)

x = {'type' : 'fruit', 'name' : 'banana'}

c)

x = ['apple', 'banana', 'cherry']

72.

Các phần tử trong dictionary có thể bị xóa sau khi dictionary được tạo.

a)

True

b)

False

73.

Đúng hay Sai: Một dictionary không thể có hai key trùng tên.

a)

True

b)

False

74.

Xét đoạn code sau: x = {'type' : 'fruit', 'name' : 'banana'}; print(___(x)). Hàm nào sau đây dùng để in ra số lượng cặp key/value trong dictionary?

a)

len

b)

size

c)

count

d)

keys

75.

Đúng hay Sai: Bạn có thể truy cập giá trị của dictionary bằng cách tham chiếu tới key.

a)

True

b)

False

76.

Sử dụng phương thức get để in ra giá trị của key "model" trong dictionary car.

car = { "brand": "Ford", "model": "Mustang", "year": 1964 }

print(___)

a)

car.get("model")

b)

car["get"]("model")

c)

get(car, "model")

d)

car.model()

77.

Xét đoạn code sau: x = {'type' : 'fruit', 'name' : 'banana'} print(x['type']) Kết quả được in ra là gì?

a)

banana

b)

fruit

c)

type = 'fruit'

78.

Xét đoạn code sau: x = {'type' : 'fruit', 'name' : 'banana'} Cú pháp nào sau đây là đúng để đổi giá trị của "type" từ "fruit" thành "berry"?

a)

x{'type'} = 'berry'

b)

x['type'] = 'berry'

c)

x.get('type') = 'berry'

79.

Điền vào chỗ trống để thay đổi giá trị của key "year" thành 2020. car = { "brand": "Ford", "model": "Mustang", "year": 1964 } ___ = ___

a)

car["year"] = 2020

b)

car.year = 2020

c)

update(car, {"year": 2020})

d)

car.get("year") = 2020

80.

Xét đoạn code sau: x = {'type' : 'fruit', 'name' : 'banana'} Cú pháp nào sau đây là đúng để đổi giá trị của "name" từ "banana" thành "apple"?

a)

x.update({'name': 'apple'})

b)

x.update('name' = 'apple')

c)

x.update('name','apple')

81.

Phương thức dictionary nào sau đây có thể dùng để thêm items vào dictionary?

a)

add()

b)

insert()

c)

update()

82.

Thêm cặp key/value "color" : "red" vào dictionary car.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
___

a)

car["color"] = "red"

b)

car.add({"color": "red"})

c)

car.insert("color", "red")

d)

car.update({"color": "red"})

83.

Điền vào chỗ trống để thêm một item vào dictionary. x = {'type' : 'fruit', 'name' : 'apple'} x.___(___)

a)

update({'color': 'green'})

b)

add({'color': 'green'})

c)

insert({'color': 'green'})

d)

setdefault({'color': 'green'})

84.

Phương thức nào sau đây dùng để xóa một item khỏi dictionary?

a)

delete()

b)

remove()

c)

pop()

85.

Sử dụng pop để xóa key "model" khỏi dictionary car.

a)

car.pop("model")

b)

car.remove("model")

c)

pop(car["model"])

d)

car.delete("model")