Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Câu hỏi về vòng lặp trong Python

Total questions: 97

Worksheet time: 54mins

Name
Class
Date
1.

How many tasks need to be repeated with a defined number of times?

a)

3.

b)

4.

c)

5.

d)

2.

2.

In the for loop variable i in range(), how much does it increase after each iteration?

a)

1.

b)

2.

c)

0.

d)

All are incorrect.

3.

Among the following statements, identify the correct one:

a)

x = 0 for i in range(10): x = x + 1

b)

x = 0 for i in range(10): x:= x + 1

c)

x = 0 for i in range(10) x = x + 1

d)

x:= 0 for i in range(10): x = x + 1

4.

In Python, which of the following statements is written correctly?

a)

for i in range(10): prin("A").

b)

for i in range(10): print("A").

c)

for i in range(10): print(A).

d)

for i in range(10) print("A").

5.

In the loop statement: j=0 for j in range(10): j = j + 2 print(j) How many times is the print(j) statement executed at the end?

a)

10 times.

b)

1 time.

c)

5 times.

d)

Not executed.

6.

In the loop statement: j = 0 for j in range(10): print("A") How many times does the letter "A" appear at the end?

a)

10 times.

b)

1 time.

c)

5 times.

d)

Not executed.

7.

Given the program: j = 0 for i in range(5): j = j + i print(j) What is the value of variable j after executing the program?

a)

10.

b)

12.

c)

15.

d)

14.

8.

After executing the program, what is the value of variable j?

a)

10.

b)

12.

c)

15.

d)

14.

9.

Determine the number of loops for the problem: calculating the sum of integers from 1 to 100?

a)

1.

b)

100.

c)

99.

d)

All are wrong.

10.

What problem does the following program solve?

a)

Calculating the sum of numbers divisible by 3 or 5 in the range from 1 to 100.

b)

Calculating the sum of numbers divisible by 3 or 5 in the range from 1 to 101.

c)

Calculating the sum of numbers divisible by 3 and 5 in the range from 1 to 101.

d)

Calculating the sum of numbers divisible by 3 and 5 in the range from 1 to 100.

11.

What is the result when running the following program?

a)

12.

b)

10.

c)

8.

d)

6.

12.

An is executing the following program but it reports an error. In your opinion, at which line did An make a mistake?

a)

4.

b)

3.

c)

2.

d)

5.

13.

The following program has an error in one of the statements. In your opinion, which statement is it?

a)

Wrong comment symbol.

b)

Wrong when declaring the list.

c)

Wrong when calling the part.

14.

In Python, what is the result of the following program? for i in range(10, 0, -1): print(i, '')

a)

10, 9, 8, 7, 6, 5, 4, 3, 2, 1.

b)

Prints 10 spaces.

c)

1 2 3 4 5 6 7 8 9 10.

d)

Does not produce any output.

15.

Which command prints the following values? 11111 22222 33333 44444 55555

a)

for i in range(1, 6): print(i, i, i, i, i).

b)

for i in range(1, 6): print(str(i)*5).

c)

for i in range(1, 5): print(str(i)*5).

d)

for i in range(0, 5): print(str(i)*5).

16.

Fill in the blank ... in the following code to get the result below? 55555 44444 33333 22222 11111 for i in range(5, 0, ...): print(str(i)*5)

a)

-1.

b)

0.

c)

None.

d)

1.

17.

In the following statements, which statement is inaccurate? A. While is a loop statement with an unknown number of iterations. B. For is a loop statement with a predetermined number of iterations. C. The while loop block is executed until = False. D. The number of iterations of the for loop is always determined by the range() statement.

a)

While is a loop statement with an unknown number of iterations.

b)

For is a loop statement with a predetermined number of iterations.

c)

The while loop block is executed until = False.

d)

The number of iterations of the for loop is always determined by the range() statement.

18.

After the following Python program is executed: Tong = 0 while Tong < 10: Tong = Tong + 1 What is the value of Tong?

4 lines
19.

After executing the following Python program, what is the value of Tong? Tong = 0 while Tong < 10: Tong = Tong + 1

a)

9.

b)

10.

c)

11.

d)

12.

20.

What is the result of the following program? a = 10 while a < 11: print(a)

a)

A number 10 appears on the screen.

b)

10 letter a appears on the screen.

c)

A number 11 appears on the screen.

d)

The program loops infinitely.

21.

What problem does the following statement solve? while M != N: if M > N: M = M - N else: N = N - M

a)

Find the GCD of M and N.

b)

Find the LCM of M and N.

c)

Find the smallest difference of M and N.

d)

Find the largest difference of M and N.

22.

Calculate the sum S = 1 + 2 + 3 + 4 +… + n + … until S>10000. Which of the following conditions for the while loop is correct?

a)

while S >= 10000.

b)

while S < 10000.

c)

while S <= 10000.

d)

While S >10000.

23.

What is the result of the following code? x = 1 while (x <= 5): print("python") x = x + 1

a)

5 times python.

b)

4 times python.

c)

3 times python.

d)

No result.

24.

Given the following program: n = int(input("Enter n<=1000: ")) k=0 n=abs(n) while n!=0: n=n//10 k=k+1 print(k) What statement is true?

a)

k is the number of significant digits of n.

b)

k is the unit digit of n.

c)

k is the largest non-zero digit of n.

d)

k is the number of non-zero digits of n.

25.

Choose the most correct answer: i = 0; x = 0 while i < 10: if i%2 == 0: x += 1 i += 1 print(x)

a)

2.

b)

3.

c)

4.

d)

5.

26.

Choose the correct answer: i = 0; x = 0 while i < 10: if i%2 == 0: x += 1 i += 1 print(x)

a)

2

b)

3

c)

4

d)

5

27.

Which of the following statements is false?

a)

There are three basic programming structures in programming languages.

b)

The sequential structure consists of blocks of commands executed in order from top to bottom.

c)

The block of commands is executed depending on certain conditions expressed by the for, while statements.

d)

All three options above are correct.

28.

When does a while-do loop end?

a)

When a certain condition is met.

b)

When the required number of loops is reached.

c)

When the output is found.

d)

All options.

29.

Every computational process can be described and executed based on the basic structure:

a)

Sequential structure.

b)

Branching structure.

c)

Loop structure.

d)

All three structures.

30.

Which of the following activities repeats with an unknown number of iterations?

a)

Taking a bath twice a day.

b)

Studying until the material is learned.

c)

Going to the bookstore once a week.

d)

Brushing teeth twice a day.

31.

The syntax of a loop with an unknown number of iterations:

a)

while to .

b)

while to do .

c)

while do: .

d)

while : .

32.

The result of the following program: x = 1 y = 5 while x < y: print(x, end = ' ') x = x + 1

a)

1 2 3 4.

b)

2 3 4 5.

c)

1 2 3 4 5.

d)

2 3 4.

33.

What is the result of the following program? x = 1 y = 5 while x < y: print(x, end = " ") x = x + 1

a)

1 2 3 4.

b)

2 3 4 5.

c)

1 2 3 4 5.

d)

2 3 4.

34.

What is the result of the following program? x = 8 y = 2 while y < x: x = x - 2 print(x, end = " ")

a)

8, 6, 4, 2.

b)

8, 6, 4.

c)

6, 4, 2.

d)

8, 6, 4, 2, 0.

35.

What does the following command do? A = [] for x in range(10): append(int(input()))

a)

Input data from the keyboard for array A with 10 integer elements.

b)

Input data from the keyboard for array A with 10 real number elements.

c)

Input data from the keyboard for array A with 10 string elements.

d)

None of the above.

36.

Choose the correct statement about list data type in Python.

a)

List data type is a collection of unordered elements and all elements have the same data type.

b)

List data type is a collection of ordered elements and each element in the list can have different data types.

c)

List data type is a collection of ordered elements and all elements must have the same data type.

d)

All of the above are incorrect.

37.

Which of the following array declarations is incorrect?

a)

ls = [1, 2, 3]

b)

ls = [x for x in range(3)]

c)

ls = [int(x) for x in input().split()]

d)

ls = list(3).

38.

Given the array declaration: A = list("3456789") To print the value of the second element of the one-dimensional array A to the screen, you write:

a)

print(A[2]).

b)

print(A[1]).

c)

print(A[3]).

d)

print(A[0]).

39.

Assuming there is a list: i = [2, 3, 4]. If you want to print this list in reverse order, which method should you use?

a)

print(list(reversed(i))).

b)

print(list(reverse(i))).

c)

pri

40.

Given a list: i = [2, 3, 4]. If you want to print this list in reverse order, which method should you use?

a)

print(list(reversed(i))).

b)

print(list(reverse(i))).

c)

print(reversed(i)).

d)

print(reversed(i)).

41.

What does the following program do? A=[] for i in range(1, 1001): if(i % 7 == 0) and (i % 5 !=0): A.append(str(i)) print(','.join(A))

a)

Find all numbers divisible by 7 but not multiples of 5, within the range of 1 to 1000.

b)

Find all numbers divisible by 7 but not multiples of 5, within the range of 1 to 1000 and print the resulting string on one line, separated by commas.

c)

Find all numbers divisible by 7 and are multiples of 5, within the range of 1 to 1000.

d)

Find all numbers not divisible by 7 but are multiples of 5, within the range of 1 to 1000.

42.

What data type does the following object belong to? A = [1, 2, '3']

a)

list.

b)

int.

c)

float.

d)

string.

43.

Which method is used to add an element to a list in Python?

a)

abs().

b)

link().

c)

append().

d)

add().

44.

What will the list A look like after the following commands? >>> A = [2, 3, 5, 6] >>> A.append(4) >>> del (A[2])

a)

2, 3, 4, 5, 6, 4.

b)

2, 3, 4, 5, 6.

c)

2, 4, 5, 6.

d)

2, 3, 6, 4.

45.

What is the result of the following program? A = [2, 3, 5, 'python', 6] A.append(4) A.append(2) A.append('x') del(A[2]) print(len(A))

a)

5.

b)

6.

c)

7.

d)

8.

46.

What is the value of arr[3] for arr = ['xuan', 'hạ', 1.4, 'đông', '3', 4.5, 7]?

a)

1.4.

b)

đông.

c)

hạ.

d)

3.

47.

Which command is used to iterate through each element of a list?

a)

for.

b)

while - for.

c)

for combined with range().

d)

while combined with range().

48.

The command to delete an element from a list A with index i is:

a)

list.del(i).

b)

A.del(i).

c)

del A[i].

d)

A.del[i].

49.

What does the following program do? S = 0 for i in range(len(A)): if A[i] > 0: S = S + A[i] print(S)

a)

Iterate through each element in A.

b)

Calculate the sum of all elements in A.

c)

Calculate the sum of non-negative elements in A.

d)

Calculate the sum of positive elements in A.

50.

Complete the program to calculate the product of positive elements in list A. S = (...) for i in range(len(A)): (...) S = S * A[i] print(S)

a)

1, if A[i] > 0:

b)

0, if A[i] > 0:

c)

1, if A[i] >= 0.

d)

0, if A[i] > 0.

51.

What operator is used to check if a value is in a list?

a)

in

b)

int

c)

range

d)

append

52.

What is the result of the following program? A = [1, 2, 3, 4, 5, 6, 5] for k in A: print(k, end = ' ')

a)

1 2 3 4 5 6

b)

1 2 3 4 5 6 5

c)

1 2 3 4 5

d)

2 3 4 5 6 5

53.

Besides using the for and range commands to iterate through a list, what other command can be used?

a)

int

b)

while

c)

in range

d)

in

54.

Assuming A = ['a', 'b', 'c', 'd', 2, 3, 4]. Do the following expressions return true or false? 6 in A, 'a' in A

a)

True, False

b)

True, False

c)

False, True

d)

False, False

55.

Assuming A = [5, 6, 7, 8, 9, 10, 11, 12]. Does the following expression return true or false? (3 + 4 - 5 + 18 // 4) in A

a)

True

b)

False

c)

Undefined

d)

Command error

56.

Which command deletes the entire list?

a)

clear()

b)

exit()

c)

remove()

d)

del()

57.

What command clears the entire list?

a)

clear()

b)

exit()

c)

remove()

d)

del()

58.

Which statement is false?

a)

After executing the clear() command, the original list becomes empty.

b)

The remove() command has the function of deleting an element with a specified value.

c)

The remove() command deletes all elements with a specified value in the list.

d)

Clear() has the effect of deleting all lists.

59.

How many statements are correct?

a)

1) After executing the clear() command, the elements return the value 0.

b)

2) The remove command returns False if not in the list.

c)

3) remove() has the effect of deleting an element with a specified value in the list.

d)

4) The remove() command has the effect of deleting an element at a specified position.

60.

After executing the following commands, how does array A look?

a)

[1, 2, 3, 4]

b)

[2, 3, 4, 5]

c)

[1, 2, 4, 5]

d)

[1, 3, 4, 5]

61.

Which element in array A is deleted?

a)

2

b)

3

c)

8

d)

4

62.

What is the result of executing the following program?

a)

1, 2, 3, 4

b)

1, 2, 4, 3, 5

c)

1, 2, 3, 4, 5

d)

1, 2, 4, 5

63.

In the following command, at which position is the element added in list A?

a)

3

b)

1

c)

0

d)

2

64.

At which position in list A?

a)

insert(-5, 3)

b)

3

c)

1

d)

0

e)

2

65.

List A before and after the insert() command is [1, 3, 5, 0] and [1, 3, 4, 5, 0]. What command was used?

a)

insert(2, 4)

b)

insert(4, 2)

c)

insert(3, 4)

d)

insert(4, 3)

66.

After 1 remove() command and 3 append() commands, list A has 8 elements. How many elements did list A initially have?

a)

8

b)

7

c)

4

d)

6

67.

Choose the incorrect statement about the insert command in Python:

a)

insert(index, value) will insert value at index and push elements to the right.

b)

If the insert index < 0, it inserts at the beginning of the list.

c)

Element at index k will be replaced by the inserted element.

d)

If the insert index > len(A), it inserts at the end of the list.

68.

In Python, which command is used to calculate the length of string s?

a)

len(s)

b)

length(s)

c)

s.len()

d)

s.length()

69.

How many valid strings are there?

a)

5

b)

6

c)

4

d)

3

70.

What is the length of the string '1234%^^%TFRESDRG'?

a)

16

b)

17

c)

18

d)

15

71.

Which of the following statements is incorrect?

4 lines
72.

Which statement is false?

a)

You can access each character of a string by index.

b)

Indexing starts from 0.

c)

You can change each character of a string.

d)

Python does not have a character data type.

73.

After executing the following code, what will the variable s contain? s1 = "3986443" s2 = "" for ch in s1: if int(ch) % 2 == 0: s2 = s2 + ch print(s2)

a)

3986443.

b)

8644.

c)

39864.

d)

443.

74.

Does the following expression return true or false? S1 = "12345" S2 = "3e4r45" S3 = "45" S3 in S1 S3 in S2

a)

True, False.

b)

True, True.

c)

False, False.

d)

False, True.

75.

What will be the result of the following program if s = "python1221"? s = input("Enter any string:") kq = False for i in range(len(s)-1): if s[i] == "2" and s[i+1] == "1": kq = True break print(kq)

a)

True.

b)

False.

c)

The program has an error.

d)

Infinite loop.

76.

What will the following program output? name = "Codelearn" print(name[0])

a)

"C".

b)

"o".

c)

"c".

d)

The command has an error.

77.

What will be the result of the following program? >>> s = "0123145" >>> s[0] = '8' >>> print(s[0])

a)

'8'.

b)

'0'.

c)

'1'.

d)

The program has an error.

78.

Which string will cause an error in Python?

a)

'This is a string in Python' message = "This is also a string".

b)

"Beautiful is better than ugly." Said Tim Peters.

c)

No string will cause an error.

d)

'It\'s also a valid string.'

79.

What is the result of the following program? >>> s = "abcdefg" >>> print(s[2])

a)

'c'

b)

'b'

c)

'a'

d)

'd'

80.

What problem does the following program solve? s = "" for i in range(10): s = s + str(i) print(s)

a)

Print a string of characters from 0 to 10.

b)

Print a string of characters from 0 to 9.

c)

Print a string of characters from 1 to 10.

d)

Print a string of characters from 1 to 9.

81.

Given s = "123456abcdefg", s1 = "124", s2 = "235", s3 = "ab", s4 = "56" + s3, how many logical expressions are true? 1) s1 in s. 2) s2 in s. 3) s3 in s. 4) s4 in s.

a)

1

b)

2

c)

3

d)

4

82.

What problem does the following program solve? n = input("Enter n") s = "" for i in range(n): if i % 2 == 0: s.append(i) print(s)

a)

Print a string of numbers from 0 to n.

b)

The program has an error.

c)

Print a string of odd numbers from 0 to n.

d)

Print a string of even numbers from 0 to n - 1.

83.

How many times is the following string printed? s = "abcdefghi" for i in range(10): if i % 4 == 0: print(s)

a)

0

b)

1

c)

2

d)

3

84.

Which command is used to find the position of a substring in another string?

a)

test()

b)

in()

c)

find()

d)

split()

85.

What is the result of the following commands? s = "12 34 56 ab cd de " print(s.find(" ")) print(s.find("12")) print(s.find("34"))

a)

2, 0, 3.

b)

2, 1, 3.

c)

3, 5, 2.

d)

1, 4, 5.

86.

Which statement is correct regarding the methods in Python?

a)

Python has a special command dedicated to strings.

b)

The syntax of the find command is: . Find().

c)

The find command will find the first position of the substring in the parent string.

d)

The find command has a unique syntax.

87.

What value does the following command return? >> "abcdabcd". find("cd") >> "abcdabcd". find("cd", 4)

a)

2, 6.

b)

3, 3.

c)

2, 2.

d)

2, 7.

88.

Which command is used to split a string?

a)

split()

b)

join()

c)

remove()

d)

copy().

89.

What is the result of the following program? >>> s = "Một năm có bốn mùa" >>> s.split() >>> st = "a, b, c, d, e, f, g, h" >>> st.split()

a)

'Một năm có bốn mùa', ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].

b)

['Một', 'năm', 'có', 'bốn', 'mùa'], ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].

c)

'Một năm có bốn mùa', 'abcdefgh'

d)

['Một', 'năm', 'có', 'bốn', 'mùa'], 'abcdefgh'.

90.

Choose the incorrect statement from the following statements:

a)

The join() command concatenates the elements of a list into a string, separated by spaces.

b)

In the join command, the joining character depends on the command.

c)

split() has the effect of splitting a string.

d)

The default character for splitting in split() is a space.

91.

Fill in the correct answer in the blank (...) to complete the correct statement: "Python has special ... for string processing, which is ... used to split a sentence into a list and the join() command is used to ... strings into a single string."

a)

commands, split(), concatenate.

b)

special commands, split(), split strings.

c)

special commands, copy(), concatenate lists.

d)

special commands, split(), concatenate lists.

92.

What is the result of the following program? a = "Hello" b = "world" c = a + " " + b print(c)

a)

hello world.

b)

Hello World.

c)

Hello word.

d)

Helloword.

93.

What is the output of the following code? greeting = 'Good '; time = 'Afternoon'; greeting = greeting + time + '!'; print(greeting)

a)

'GoodAfternoon'.

b)

'GoodAfternoon!'.

c)

The program reports an error.

d)

'Good Afternoon !'

94.

What is the result of the following code? line = 'Geek1 Geek2 Geek3'; print(line.split()); print(line.split(' ', 1))

a)

['Geek1', 'Geek2', 'Geek3'] ['Geek1', 'Geek2 Geek3'].

b)

['Geek1', 'Geek2', 'Geek3'] ['Geek1', 'Geek2', 'Geek3'].

c)

['Geek1 Geek2', 'Geek3'] ['Geek1', 'Geek2', 'Geek3'].

d)

['Geek1 Geek2', 'Geek3'] ['Geek1', 'Geek2 Geek3'].

95.

Complete the following command to print the length of the string: x = 'Hello World'; print(…)

a)

x.len().

b)

len(x).

c)

copy(x).

d)

x.length().

96.

For the string s = '1, 2, 3, 4, 5'. To remove the character ',' and replace it with a space, which commands should be used?

a)

remove() and join().

b)

del() and replace().

c)

split() and join().

d)

split() and replace().

97.

For the string s = 'Python'. To convert it to s = 'P y t h o n', which commands should be used?

a)

split() and join().

b)

split() and replace().

c)

del() and replace().

d)

replace().