🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Welcome to the Python Loops Quiz! This quiz is designed for learners who are familiarizing themselves with the concept of loops in Python. Loops are a fundamental aspect of programming in Python, allowing the execution of a block of code multiple times. This quiz will test your knowledge of both 'for' and 'while' loops, their syntax, and common patterns. Let's begin and see how well you understand Python loops!
1. What is the purpose of a loop in Python?
Answer:
Explanation:
Loops are used in Python to repeatedly execute a block of code as long as a condition is met.
2. Which keyword is used to exit a loop in Python?
Answer:
Explanation:
The 'break' keyword is used to exit a loop prematurely.
3. What type of loop will execute at least once regardless of the condition?
Answer:
Explanation:
The do-while loop will execute its block at least once before checking its condition. However, it's important to note that Python does not have a built-in do-while loop, but this concept exists in other programming languages.
4. How do you iterate over a list named 'myList' using a for loop in Python?
Answer:
Explanation:
The syntax for iterating over a list in Python is: for item in myList.
5. What does the 'continue' keyword do in a loop?
Answer:
Explanation:
The 'continue' keyword is used to skip the current iteration of the loop and continue with the next one.
6. How do you create an infinite loop in Python?
Answer:
Explanation:
Infinite loops can be created using 'while True:' or 'while 1:' as these conditions are always true.
7. What is the output of the following code?
for i in range(3):
print(i)
Answer:
Explanation:
The range(3) function generates numbers from 0 up to, but not including, 3.
8. Which loop is typically used when the number of iterations is known?
Answer:
Explanation:
A for loop is used when the number of iterations is known beforehand.
9. How do you loop through both the index and elements of a list named 'myList'?
Answer:
Explanation:
The enumerate() function is used to get both the index and the value of each element in the list.
10. What is the correct way to write a while loop in Python?
Answer:
Explanation:
The correct syntax for a while loop in Python is: while condition: ...
11. What will be the output of the following code?
count = 0
while count < 5:
print(count)
count += 1
Answer:
Explanation:
The loop prints numbers from 0 to 4. It stops when count becomes 5.
12. What is the purpose of the 'range' function in loops?
Answer:
Explanation:
The range function is used to generate a sequence of numbers, often used for looping a specific number of times.
13. How do you write a for loop that repeats an action 5 times in Python?
Answer:
Explanation:
The range(5) function generates numbers from 0 to 4, which is used to repeat the loop 5 times.
14. What will the following code output?
i = 5
while i > 0:
i -= 1
if i == 2:
break
print(i)
Answer:
Explanation:
The loop breaks when i becomes 2, so it only prints 4 and 3.
15. How do you loop through a dictionary named 'myDict' to print its keys?
Answer:
Explanation:
When looping through a dictionary, it defaults to iterating over its keys.
16. What will be the output of the following code?
for x in 'Python':
if x == 'h':
continue
print(x)
Answer:
Explanation:
The loop skips the iteration when x is 'h', so 'h' is not printed.
17. How do you ensure a loop runs at least once in Python?
Answer:
Explanation:
Python doesn't have a do-while loop construct that guarantees the execution of the loop body at least once.
18. What is an infinite loop?
Answer:
Explanation:
An infinite loop is a loop that has no end condition, or the condition never becomes false, so it runs indefinitely.
19. How can you iterate over a range of numbers in reverse order in a for loop?
Answer:
Explanation:
The range function can take a third argument for step size, and a negative step size iterates in reverse.
20. What is the best loop to use when you need to perform an action a specific number of times?
Answer:
Explanation:
A for loop is most suitable when the number of iterations is known beforehand, as it can iterate over a sequence of numbers generated by the range function.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment