ข้อใดเป็นการใช้ for loop ที่ถูกต้องใน Python
แบบทดสอบ Python เรื่อง Loop 10 ข้อ

Quiz
•
Computers
•
9th Grade
•
Medium
อนุวัฒน์ เสียงหวาน
Used 7+ times
FREE Resource
10 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
for(i=0; i<5; i++):
for i in range(5):
for i to 5:
foreach i in range(5):
Answer explanation
คำสั่ง 'for i in range(5):' เป็นการใช้ for loop ที่ถูกต้องใน Python ซึ่งจะทำให้ i มีค่าตั้งแต่ 0 ถึง 4 ในขณะที่ตัวเลือกอื่นๆ ไม่ถูกต้องตามไวยากรณ์ของ Python.
2.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
คำสั่งใดใช้หยุด while loop ได้ทันที
stop
halt
break
exit
Answer explanation
คำสั่ง 'break' ใช้เพื่อหยุดการทำงานของ while loop ทันที ในขณะที่คำสั่งอื่นๆ เช่น 'stop', 'halt', และ 'exit' ไม่ได้ถูกใช้ในบริบทนี้.
3.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
คำสั่ง continue ใน loop มีหน้าที่อะไร
หยุด loop ทันที
ข้ามไปยังรอบถัดไปของ loop โดยไม่รันโค้ดที่เหลือของรอบนั้น
ออกจาก loop และจบโปรแกรม
ทำให้ loop รันต่อไปแบบไม่มีที่สิ้นสุด
Answer explanation
คำสั่ง 'continue' ใน loop จะทำให้โปรแกรมข้ามไปยังรอบถัดไปโดยไม่รันโค้ดที่เหลือในรอบปัจจุบัน ซึ่งหมายความว่ามันจะไม่หยุด loop หรือออกจาก loop แต่จะเริ่มรอบใหม่ทันที.
4.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
คำสั่งใดสามารถใช้สร้าง loop ที่ไม่มีวันจบได้
while True:
for i in range(infinity):
for(; ;):
while(1 > 2):
Answer explanation
คำสั่ง 'while True:' สร้าง loop ที่ไม่มีวันจบเพราะเงื่อนไขใน while จะเป็นจริงตลอดเวลา ในขณะที่ตัวเลือกอื่นๆ จะมีเงื่อนไขที่ทำให้ loop จบได้ เช่น 'for i in range(infinity):' ไม่ถูกต้องเพราะ 'infinity' ไม่ใช่ค่าที่ถูกต้องใน Python.
5.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
ข้อใดเป็น for loop ที่ใช้ range() เพื่อแสดงเลขคู่ระหว่าง 1 ถึง 10
for i in range(1, 10, 2): print(i)
for i in range(2, 11, 2): print(i)
for i in range(2, 10, 3): print(i)
for i in range(1, 11, 3): print(i)
Answer explanation
คำตอบที่ถูกต้องคือ 'for i in range(2, 11, 2): print(i)' เพราะใช้ range() เริ่มจาก 2 ถึง 10 โดยเพิ่มทีละ 2 ซึ่งจะแสดงเลขคู่ 2, 4, 6, 8, 10 ในช่วงที่กำหนด.
6.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
ผลลัพธ์ของโค้ดต่อไปนี้คืออะไร?
แสดง "Hello" 3 ครั้ง
แสดง "Hello" 2 ครั้ง
แสดง "Hello" 4 ครั้ง
ไม่มีอะไรเกิดขึ้น
Answer explanation
โค้ดจะแสดง "Hello" 3 ครั้ง เนื่องจากมีการวนลูปที่ทำให้คำสั่งนี้ถูกเรียกใช้ 3 ครั้ง ดังนั้นผลลัพธ์ที่ถูกต้องคือ "แสดง 'Hello' 3 ครั้ง".
7.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
ข้อใดเป็นความแตกต่างระหว่าง while loop กับ for loop?
A) while loop ใช้เมื่อต้องการวนซ้ำเป็นจำนวนรอบที่แน่นอน
for loop ใช้เมื่อต้องการวนซ้ำตามเงื่อนไขที่กำหนด
while loop จะทำงานต่อเนื่องจนกว่าเงื่อนไขจะเป็นเท็จ ส่วน for loop ใช้กับตัววนซ้ำที่กำหนดช่วง
ไม่มีข้อแตกต่าง
Answer explanation
while loop จะทำงานต่อเนื่องจนกว่าเงื่อนไขจะเป็นเท็จ ซึ่งเหมาะสำหรับการวนซ้ำที่ไม่รู้จำนวนรอบแน่นอน ส่วน for loop ใช้เมื่อมีการกำหนดช่วงหรือจำนวนรอบที่แน่นอนในการวนซ้ำ.
Create a free account and access millions of resources
Similar Resources on Quizizz
10 questions
สอบท้ายหน่วยที่ 4 วิทยาการคำนวณ 1 ม.1

Quiz
•
9th - 12th Grade
10 questions
สนุกกับ AI

Quiz
•
9th - 12th Grade
15 questions
การเขียนโปรแกรมเบื้องต้น ม.3

Quiz
•
9th Grade - University
10 questions
ข้อสอบก่อนเรียนเรื่องอัลกอริทึม ม.3

Quiz
•
9th Grade
10 questions
แบบทดสอบเรื่องการรับค่าในภาษาไพทอน (รายบุคคล)

Quiz
•
9th - 12th Grade
10 questions
คำศัพท์เกี่ยวกับการสร้างเว็บไซต์

Quiz
•
9th Grade
10 questions
โมดูล pandas และการอ่านไฟล์ CSV

Quiz
•
9th Grade
10 questions
รอบรู้ IT ครั้งที่ 1

Quiz
•
7th - 9th Grade
Popular Resources on Quizizz
15 questions
Character Analysis

Quiz
•
4th Grade
17 questions
Chapter 12 - Doing the Right Thing

Quiz
•
9th - 12th Grade
10 questions
American Flag

Quiz
•
1st - 2nd Grade
20 questions
Reading Comprehension

Quiz
•
5th Grade
30 questions
Linear Inequalities

Quiz
•
9th - 12th Grade
20 questions
Types of Credit

Quiz
•
9th - 12th Grade
18 questions
Full S.T.E.A.M. Ahead Summer Academy Pre-Test 24-25

Quiz
•
5th Grade
14 questions
Misplaced and Dangling Modifiers

Quiz
•
6th - 8th Grade
Discover more resources for Computers
17 questions
Chapter 12 - Doing the Right Thing

Quiz
•
9th - 12th Grade
30 questions
Linear Inequalities

Quiz
•
9th - 12th Grade
20 questions
Types of Credit

Quiz
•
9th - 12th Grade
20 questions
Taxes

Quiz
•
9th - 12th Grade
17 questions
Parts of Speech

Quiz
•
7th - 12th Grade
20 questions
Chapter 3 - Making a Good Impression

Quiz
•
9th - 12th Grade
20 questions
Inequalities Graphing

Quiz
•
9th - 12th Grade
10 questions
Identifying equations

Quiz
•
KG - University