Loops — for, while, range, enumerate & zip — Concept Check
Questions on Loops — for, while, range, enumerate & zip. Python's for loop iterates over ANY iterable directly — no index bookkeeping. range generates number sequences; enumerate gives index+value; zip walks multiple sequences together.
Questions in this quiz
8 questions on Loops — for, while, range, enumerate & zip — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a list of numbers and want to multiply each by 10. Which loop is most Pythonic?
- Afor num in nums: num *= 10
- Bfor i in range(len(nums)): nums[i] *= 10
- Cfor i, num in enumerate(nums): nums[i] = num * 10
- Dwhile i < len(nums): nums[i] *= 10
2.You want to iterate over two lists simultaneously. Which function should you use?
- Aenumerate
- Bzip
- Crange
- Dsorted
3.Given a dictionary 'marks = {"dsa": 85, "java": 90}', how do you iterate over key-value pairs?
- Afor subject in marks.keys()
- Bfor subject, mark in marks
- Cfor subject, mark in marks.items()
- Dfor mark in marks.values()
4.A loop iterates over a list of skills using 'for skill in skills'. What is the main advantage?
- AFaster execution
- BUses less memory
- CNo need for index bookkeeping
- DAutomatically sorts the list
5.What does the 'enumerate' function return when iterating over a list?
- AValue and index pairs
- BIndex and value pairs
- COnly values
- DOnly indexes
6.You have two lists of unequal length and use 'zip' to iterate over them. What happens?
- AStops at the shortest list
- BStops at the longest list
- CRaises an error
- DFills missing values with None
7.When should you prefer a 'while' loop over a 'for' loop in Python?
- AWhen iterating over a fixed list
- BWhen the loop depends on a condition
- CWhen you want to iterate in reverse
- DWhen you need index and value
8.A Python developer uses 'for i in range(len(items))' in a loop. What does this indicate?
- AThey are using a while loop alternative
- BThey may be coming from a Java background
- CThey are handling multiple iterables
- DThey are using the most efficient method