AMP

Chapter 7: Loops in PHP — Online MCQ Test

COMPUTER APPLICATIONS · CLASS 12th · Tamil Nadu State Board
Practice Chapter 7: Loops in PHP with a free chapter-wise online MCQ test. This chapter covers: Exploring iterative execution this chapter details looping structures in PHP scripts. Students master for loops while loops do-while loops and foreach loops specifically tailored f.... AI-generated questions from basic to board-exam level, with instant results and explanations.

Start Exam on Full Site →

Chapter 7: Loops in PHP — Important Questions & Answers

Which loop in PHP is best suited for repeating a block of code a fixed number of times?
  • A. for loop
  • B. while loop
  • C. do-while loop
  • D. foreach loop
Answer: A. for loop
The for loop is used when the number of iterations is known in advance. It has initialization, condition, and increment/decrement parts.
Which loop executes the block of statements at least once, even if the condition is false?
  • A. for loop
  • B. while loop
  • C. do-while loop
  • D. foreach loop
Answer: C. do-while loop
In a do-while loop, the condition is checked after the loop body executes. So the body runs at least once.
Which of the following is the correct syntax of a for loop in PHP?
  • A. for(initialization; condition; increment) { statements; }
  • B. for(condition; initialization; increment) { statements; }
  • C. for{ initialization; condition; increment; statements; }
  • D. for(initialization, condition, increment) { statements; }
Answer: A. for(initialization; condition; increment) { statements; }
The standard PHP for loop syntax includes initialization, condition, and increment/decrement in that order inside parentheses.
Which loop is most appropriate when the number of elements in an array is not known in advance?
  • A. for loop
  • B. while loop
  • C. foreach loop
  • D. do-while loop
Answer: C. foreach loop
Foreach automatically handles every element in an array without requiring the programmer to know the size beforehand.
What is the output of the following code? $arr = array(10, 20, 30); foreach($arr as $value) { echo $value . ' '; }
  • A. 10 20 30
  • B. 30 20 10
  • C. 10,20,30
  • D. 123
Answer: A. 10 20 30
Foreach traverses the array from the first element to the last element. Each value is echoed in order with a space.