Python Programming - Loops
Exercise : Loops - General Questions
- Loops - General Questions
26.
Which loop is used for iterating over the characters of a string?
Answer: Option
Explanation:
The
for
loop is commonly used for iterating over the characters of a string in Python.
my_string = "Hello"
for char in my_string:
print(char)
In this example, the for
loop iterates over each character in the string my_string
and prints it to the console.
27.
What does the
range
function return when used with two arguments, start
and stop
?
Answer: Option
Explanation:
The
range
function with two arguments returns a sequence of numbers from start
to stop
(exclusive).
28.
What is the purpose of the
reverse
parameter in the sorted
function?
Answer: Option
Explanation:
The
reverse
parameter in the sorted
function is used to sort the elements in descending order.
29.
In Python, what does the
enumerate
function return when used with a for
loop?
Answer: Option
Explanation:
The
enumerate
function returns the index and value of each element in a sequence when used with a for
loop.
30.
Which of the following is the correct syntax for a do-while loop?
Answer: Option
Explanation:
Python does not have a built-in
do-while
loop, but it can be simulated using a while
loop with a break
statement to exit the loop based on a condition.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers