Python For Loops - 30 Multiple Choice Questions
1. What is the output of `for i in range(3): print(i)`?
A) 0 1 2
B) 1 2 3
C) 0 1 2 3
D) Error
Answer: A
A) 0 1 2
B) 1 2 3
C) 0 1 2 3
D) Error
Answer: A
2. What will `for char in "hello": print(char)` print?
A) h e l l o
B) hello
C) Error
D) hll
Answer: A
A) h e l l o
B) hello
C) Error
D) hll
Answer: A
3. How do you iterate over a list called `my_list`?
A) for item in my_list:
B) for i in range(len(my_list)):
C) Both A and B
D) for my_list in item:
Answer: C
A) for item in my_list:
B) for i in range(len(my_list)):
C) Both A and B
D) for my_list in item:
Answer: C
4. What is the output of `for i in range(2, 5): print(i)`?
A) 2 3 4
B) 2 3 4 5
C) 1 2 3 4
D) Error
Answer: A
A) 2 3 4
B) 2 3 4 5
C) 1 2 3 4
D) Error
Answer: A
5. How can you print each element in a dictionary called `my_dict`?
A) for key in my_dict:
B) for key, value in my_dict.items():
C) Both A and B
D) for item in my_dict.values():
Answer: C
A) for key in my_dict:
B) for key, value in my_dict.items():
C) Both A and B
D) for item in my_dict.values():
Answer: C
6. What does `for i in range(5, 0, -1): print(i)` output?
A) 5 4 3 2 1
B) 1 2 3 4 5
C) 5 4 3 2
D) Error
Answer: A
A) 5 4 3 2 1
B) 1 2 3 4 5
C) 5 4 3 2
D) Error
Answer: A
7. What will be printed by `for i in range(4): print("Python")`?
A) Python (4 times)
B) Python
C) 4 Python
D) Error
Answer: A
A) Python (4 times)
B) Python
C) 4 Python
D) Error
Answer: A
8. How can you iterate over a string called `text`?
A) for char in text:
B) for i in range(len(text)):
C) Both A and B
D) for text in char:
Answer: C
A) for char in text:
B) for i in range(len(text)):
C) Both A and B
D) for text in char:
Answer: C
9. What will `for i in range(1, 6): print(i * i)` output?
A) 1 4 9 16 25
B) 1 2 3 4 5
C) 1 2 3 4 5 6
D) Error
Answer: A
A) 1 4 9 16 25
B) 1 2 3 4 5
C) 1 2 3 4 5 6
D) Error
Answer: A
10. How do you access values in a dictionary using a for loop?
A) for value in my_dict:
B) for value in my_dict.values():
C) for key in my_dict:
D) Both B and C
Answer: D
A) for value in my_dict:
B) for value in my_dict.values():
C) for key in my_dict:
D) Both B and C
Answer: D
11. What does `for i in range(3, 7): print(i)` output?
A) 3 4 5 6 7
B) 3 4 5 6
C) 3 4 5
D) Error
Answer: B
A) 3 4 5 6 7
B) 3 4 5 6
C) 3 4 5
D) Error
Answer: B
12. What is the result of `for i in range(0, 10, 2): print(i)`?
A) 0 2 4 6 8
B) 1 3 5 7 9
C) 0 1 2 3 4 5 6 7 8 9
D) Error
Answer: A
A) 0 2 4 6 8
B) 1 3 5 7 9
C) 0 1 2 3 4 5 6 7 8 9
D) Error
Answer: A
13. How do you create a list from 0 to 4 using a for loop?
A) my_list = [i for i in range(5)]
B) my_list = list(range(5))
C) Both A and B
D) my_list = range(5)
Answer: C
A) my_list = [i for i in range(5)]
B) my_list = list(range(5))
C) Both A and B
D) my_list = range(5)
Answer: C
14. What will be printed by `for i in "data": print(i)`?
A) d a t a
B) data
C) dta
D) Error
Answer: A
A) d a t a
B) data
C) dta
D) Error
Answer: A
15. What will `for key in my_dict.keys(): print(key)` do?
A) Print all keys in `my_dict`
B) Print all values in `my_dict`
C) Print both keys and values
D) Error
Answer: A
A) Print all keys in `my_dict`
B) Print all values in `my_dict`
C) Print both keys and values
D) Error
Answer: A
16. What is the output of `for i in range(1, 10, 3): print(i)`?
A) 1 4 7
B) 1 3 5 7 9
C) 1 2 3 4
D) Error
Answer: A
A) 1 4 7
B) 1 3 5 7 9
C) 1 2 3 4
D) Error
Answer: A
17. How can you loop through both keys and values in a dictionary?
A) for key, value in my_dict.items():
B) for item in my_dict:
C) for value in my_dict.values():
D) Both A and C
Answer: A
A) for key, value in my_dict.items():
B) for item in my_dict:
C) for value in my_dict.values():
D) Both A and C
Answer: A
18. What is the output of `for i in range(5): print(i + 1)`?
A) 1 2 3 4 5
B) 0 1 2 3 4
C) 1 2 3 4
D) Error
Answer: A
A) 1 2 3 4 5
B) 0 1 2 3 4
C) 1 2 3 4
D) Error
Answer: A
19. How do you append to a list called `my_list` in a loop?
A) my_list.append(i)
B) my_list += [i]
C) Both A and B
D) None of the above
Answer: C
A) my_list.append(i)
B) my_list += [i]
C) Both A and B
D) None of the above
Answer: C
20. What will `for i in range(5): print(i * 2)` output?
A) 0 2 4 6 8
B) 0 1 2 3 4
C) 1 2 3 4 5
D) Error
Answer: A
A) 0 2 4 6 8
B) 0 1 2 3 4
C) 1 2 3 4 5
D) Error
Answer: A
21. What does `for item in my_list: print(item)` do?
A) Prints each item in `my_list`
B) Prints the length of `my_list`
C) Error
D) None of the above
Answer: A
A) Prints each item in `my_list`
B) Prints the length of `my_list`
C) Error
D) None of the above
Answer: A
22. How can you create a new list of squares from `my_list`?
A) squares = [i ** 2 for i in my_list]
B) squares = list(map(lambda x: x ** 2, my_list))
C) Both A and B
D) Error
Answer: C
A) squares = [i ** 2 for i in my_list]
B) squares = list(map(lambda x: x ** 2, my_list))
C) Both A and B
D) Error
Answer: C
23. What is the output of `for i in range(3): print(i + 3)`?
A) 3 4 5
B) 1 2 3
C) 0 1 2
D) Error
Answer: A
A) 3 4 5
B) 1 2 3
C) 0 1 2
D) Error
Answer: A
24. How do you iterate through a string and create a list of its characters?
A) characters = [char for char in text]
B) characters = list(text)
C) Both A and B
D) Error
Answer: C
A) characters = [char for char in text]
B) characters = list(text)
C) Both A and B
D) Error
Answer: C
25. What will `for i in my_dict:` output if `my_dict` is empty?
A) Nothing
B) Error
C) None
D) Empty string
Answer: A
A) Nothing
B) Error
C) None
D) Empty string
Answer: A
26. What is the output of `for i in range(10): print(i, end=", ")`?
A) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
B) 0 1 2 3 4 5 6 7 8 9
C) 0 1 2 3 4 5 6 7 8 9,
D) Error
Answer: A
A) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
B) 0 1 2 3 4 5 6 7 8 9
C) 0 1 2 3 4 5 6 7 8 9,
D) Error
Answer: A
27. What will `for i in range(2): for j in range(2): print(i, j)` output?
A) 0 0; 0 1; 1 0; 1 1
B) 0 0; 1 1
C) 0 1; 1 2
D) Error
Answer: A
A) 0 0; 0 1; 1 0; 1 1
B) 0 0; 1 1
C) 0 1; 1 2
D) Error
Answer: A
28. How can you create a list of even numbers from 0 to 10?
A) even_numbers = [i for i in range(11) if i % 2 == 0]
B) even_numbers = list(range(0, 11, 2))
C) Both A and B
D) Error
Answer: C
A) even_numbers = [i for i in range(11) if i % 2 == 0]
B) even_numbers = list(range(0, 11, 2))
C) Both A and B
D) Error
Answer: C
29. What does `for index, value in enumerate(my_list): print(index, value)` do?
A) Prints index and value of each item in `my_list`
B) Prints values only
C) Prints indices only
D) Error
Answer: A
A) Prints index and value of each item in `my_list`
B) Prints values only
C) Prints indices only
D) Error
Answer: A
30. What will `for i in range(1, 4): print(i ** 3)` output?
A) 1 8 27
B) 1 2 3
C) 1 4 9
D) Error
Answer: A
A) 1 8 27
B) 1 2 3
C) 1 4 9
D) Error
Answer: A
0 Comments