Prerequisite: Loops
Note: Output of all these programs is tested on Python3
1. What is the output of the following?
mylist = ['geeks', 'forgeeks'] for i in mylist: i.upper() print(mylist) |
- [‘GEEKS’, ‘FORGEEKS’].
- [‘geeks’, ‘forgeeks’].
- [None, None].
- Unexpected
Output:
2. [‘geeks’, ‘forgeeks’]
Explanation: The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.
2. What is the output of the following?
mylist = ['geeks', 'forgeeks'] for i in mylist: mylist.append(i.upper()) print(mylist) |
- [‘GEEKS’, ‘FORGEEKS’].
- [‘geeks’, ‘forgeeks’, ‘GEEKS’, ‘FORGEEKS’].
- [None, None].
- None of these
Output:
4. None of these
Explanation:The loop does not terminate as new elements are being added to the list in each iteration.
3. What is the output of the following?
i = 1while True: if i % 0O7 == 0: break print(i) i += 1 |
- 1 2 3 4 5 6.
- 1 2 3 4 5 6 7.
- error.
- None of these
Output:
1. 1 2 3 4 5 6
Explanation: The loop will terminate when i will be equal to 7.
4. What is the output of the following?
True = Falsewhile True: print(True) break |
- False.
- True.
- Error.
- None of these
Output:
3. Error
Explanation: SyntaxError, True is a keyword and it’s value cannot be changed.
5. What is the output of the following?
i = 1while True: if i % 3 == 0: break print(i) i + = 1 |
- 1 2 3.
- 1 2.
- Syntax Error.
- None of these
Output:
3. Syntax Error
Explanation: SyntaxError, there shouldn’t be a space between + and = in +=.
Recommended Posts:
- Output of Python Programs | Set 23 (String in loops)
- Output of C programs | Set 61 (Loops)
- Output of C programs | Set 35 (Loops)
- Output of C programs | Set 59 (Loops and Control Statements)
- Output of Java Programs | Set 43 (Conditional statements & Loops)
- Output of Python program | Set 15 (Loops)
- Output of Python programs | Set 8
- Output of Python programs | Set 7
- Output of Python Programs | Set 19 (Strings)
- Output of Python Programs | (Dictionary)
- Output of Python Programs | Set 24 (Dictionary)
- Output of Python programs | Set 9 (Dictionary)
- Output of Python Programs | Set 24 (Sets)
- Output of Python Programs | Set 21 (Bool)
- Output of Python Programs | Set 20 (Tuples)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



