Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP Looping Statements Aptitude Questions and Answers
PHP Looping Statements Aptitude: This section contains aptitude questions and answers on Looping Statements.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP Looping Statements.
1) There are the following statements that are given below, which of them are correct about looping statements?
- The looping statements are used to execute one or more statements multiple times.
- The looping statements also known as iterative statements.
- The looping statements also known as decision-making statements.
- The looping statements can be used within the class only.
Options:
- A and B
- A and C
- A, B, and D
- A, C, and D
2) Which of the following loops can be used in PHP?
- while
- do ... while
- for
- foreach
Options:
- A and B
- A, B, and C
- A and C
- A, B, C, and D
Correct answer: 4
A, B, C, and D
Explanation:
Nothing
3) Which of the following loops are the entry control loop in PHP?
- while
- do ... while
- for
- None of the above
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct answer: 2
A and C
Explanation:
In PHP, while and for loops are the entry control loops. In the entry control loops, the condition of the loop checked before execution of the body of the loop.
4) What is the correct output of the given code snippets in PHP?
<?php
$iLoop = 1;
while ($iLoop <= 5)
{
echo $iLoop;
iLoop++;
}
?>
- 12345
- 01234
- Error
- Infinite loop
Correct answer: 3
Error
Explanation:
In the above we used ILoop++ instead of $iLoop so that iLoop is not a variable, then it generates an error if we use increment operator with iLoop.
5) What is the correct output of given code snippets in PHP?
<?php
$iLoop = 1;
while ($iLoop <= 5)
{
echo $iLoop;
iLoop;
}
?>
- 12345
- 01234
- Error
- Infinite loop
Correct answer: 4
Infinite loop
Explanation:
In the above we used ILoop++ instead of $iLoop so that iLoop is not a variable, then the loop will not terminate, and It goes infinite.
6) What is the correct output of given code snippets in PHP?
<?php
$iLoop = 1;
while ($iLoop <= 5) echo ++$iLoop;
?>
- 12345
- 01234
- 23456
- Error
Correct answer: 3
23456
Explanation:
The above code will print "23456" because we use a pre-increment operator, it prints from 2 to 6.
7) What is the correct output of given code snippets in PHP?
<?php
$iLoop = 1;
while ($iLoop <= 5, $iLoop++)
{
echo $iLoop;
}
?>
- 12345
- 01234
- 23456
- Error
Correct answer: 4
Error
Explanation:
The above will generate syntax error, because while($iLoop <= 5,$iLoop++) line is not correct, we cannot update $iLoop variable in while() like this.
8) What is the correct output of given code snippets in PHP?
<?php
$iLoop = 0;
while ($iLoop <= 5)
{
$++iLoop;
echo $iLoop * $iLoop . " ";
}
?>
- 1 4 9 16 25
- 0 1 4 9 16 25 36
- 0 1 4 9 16 25
- Error
Correct answer: 4
Error
Explanation:
The above code will generate a syntax error, because we used $++iLoop instead of ++$iLoop. We cannot use pre-increment operator like this.
9) What is the correct output of given code snippets in PHP?
<?php
$iLoop = 0;
++$iLoop;
while ($iLoop <= 5)
{
$iLoop++;
echo $iLoop * $iLoop . " ";
}
?>
- 1 4 9 16 25
- 4 9 16 25 36
- 4 9 16 25
- Error
Correct answer: 2
4 9 16 25 36
Explanation:
The above code will print "4 9 16 25 36" because before entering in the loop body, value of $iLoop is 1, and before printing value, it becomes 2, then it print like this:
$iLoop =2 , 2 * 2 = 4
$iLoop =3 , 3 * 3 = 9
$iLoop =4 , 4 * 4 = 16
$iLoop =5 , 5 * 5 = 25
$iLoop =6 , 6 * 6 = 36
Then the condition gets false and loops terminated.
10) What is the correct output of the given code snippets in PHP?
<?php
$iLoop = 10;
while ($iLoop <= 5)
{
echo $iLoop . " ";
$iLoop = $iLoop - 1;
}
?>
- 10 9 8 7 6 5
- 10 9 8 7 6
- Infinite loop
- None of the above
Correct answer: 4
None of the above
Explanation:
The above code will generate not print anything on the webpage, because condition of while() loop will never true, we use "less than equal to" (<=) operator, and initial value of $iLoop is 10, it is greater than 5 so condition get false.
11) What is the correct output of the given code snippets in PHP?
<?php
$iLoop = 5;
dowhile($iLoop > 0)
{
echo $iLoop . " ";
$iLoop = $iLoop - 1;
}
?>
- 5 4 3 2 1 0
- 5 4 3 2 1
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate error, because it is not proper way to use do while loop, then correct syntax of while loop is given below:
//initialization of counter variable
do
{
//body of the loop
//update counter variable
}
while(condition);
12) What is the correct output of the given code snippets in PHP?
<?php
$iLoop = 5;
do
{
echo $iLoop . " ";
$iLoop = $iLoop - 1;
}
while ($iLoop > 0)
?>
- 5 4 3 2 1 0
- 5 4 3 2 1
- Error
- None of the above
Correct answer: 2
5 4 3 2 1
Explanation:
The above code will print "5 4 3 2 1" on the webpage, because counter variable $iLoop initialized with 5 and print value till 1 and when it becomes 0, condition gets false.
13) Is do… while loop executes at least once?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, The do … while loop executes at least once if do not use the break or return statement.
14) The do … while loop is exit control loop?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, The do … while loop is an exit control loop because it checks loop condition after the body of the loop.
15) What is the correct output of the given code snippets in PHP?
<?php
$iLoop = 0;
while ($iLoop <= 5)
{
echo $iLoop . " ";
$iLoop = $iLoop + 1;
if ($iLoop == 2) continue;
}
?>
- 0 1 3 4 5
- 0 1 2 3 4 5
- Error
- None of the above
Correct answer: 2
0 1 2 3 4 5
Explanation:
The above code will print "0 1 2 3 4 5" on the webpage because here continue statement is not doing anything extra in the flow of the execution of the loop, because continue statement is used to skip below statements in the body of the loop, but there is no any statement after continue statement in the loop body.
16) What is the correct output of the given code snippets in PHP?
<?php
$iLoop = 0;
while ($iLoop <= 5)
{
$iLoop = $iLoop + 1;
if ($iLoop == 2) continue;
echo $iLoop . " ";
}
?>
- 0 1 3 4 5
- 0 1 2 3 4 5
- 1 3 4 5 6
- Error
Correct answer: 3
1 3 4 5 6
Explanation:
The above code will print "1 3 4 5 6" on the web page because when the value of $iLoop will 2 then it will skip echo $iLoop statement then it will not 2 on the web page and then after print 6, condition get false and loop is terminated.
17) What is the correct output of the given code snippets in PHP?
<?php
for ($iLoop = 0;$iLoop < 5;$iLoop++)
{
echo $iLoop . " ";
}
?>
- 0 1 2 3 4
- 0 1 2 3 4 5
- Error
- None of the above
Correct answer: 1
0 1 2 3 4
Explanation:
The above code will print "0 1 2 3 4" on the web page, here we initialize counter variable $iLoop from 0 and loop condition is true till 4.
18) What is the correct output of the given code snippets in PHP?
<?php
for ($iLoop = 0;true;$iLoop++)
{
echo $iLoop . " ";
if ($iLoop == 4) break;
}
?>
- 0 1 2 3 4
- 0 1 2 3 4 5
- Error
- None of the above
Correct answer: 1
0 1 2 3 4
Explanation:
The above code will print "0 1 2 3 4" on the web page, here we pass true in place of loop condition explicitly, it is properly valid, so when $iLoop variable becomes 4 then break statement terminates the loop.
19) What is the correct output of the given code snippets in PHP?
<?php
for ($iLoop = 0;$iLoop < 5;)
{
echo $iLoop . " ";
$iLoop++;
}
?>
- 0 1 2 3 4
- 0 1 2 3 4 5
- Error
- None of the above
Correct answer: 1
0 1 2 3 4
Explanation:
The above code will print "0 1 2 3 4" on the web page, here we increase $iLoop variable in the body of loop instead of for(), it is properly valid.
20) What is the correct output of the given code snippets in PHP?
<?php
$iLoop = 0;
for (;$iLoop;)
{
echo $iLoop . " ";
$iLoop++;
if ($iLoop == 4) break;
}
?>
- 0 1 2 3 4
- 0 1 2 3
- Error
- None of the above
Correct answer: 4
None of the above
Explanation:
The above code will not print anything. Because the initial value of $iLoop is 0, so condition gets false, that is why it will not print anything.
21) What is the correct output of the given code snippets in PHP?
<?php
$iLoop = 0;
for (;1;)
{
echo $iLoop . " ";
$iLoop++;
if ($iLoop == 4) break;
}
?>
- 0 1 2 3
- 0 1 2 3 4
- Error
- None of the above
Correct answer: 1
0 1 2 3
The above code will print "0 1 2 3" on the web page, here we use "1" in place of loop condition explicitly which is correct, so loop will terminate when it becomes 4 using break statement.
22) What is the correct output of the given code snippets?
<?php
$i = 0;
for ($i = 0;$i < 3;$i++)
{
for ($j = 0;$j < 2;$j++) echo $i . " ";
}
?>
- 0 0 1 1 2 2
- Error
- Infinite loop
- None of the above
Correct answer: 1
0 0 1 1 2 2
The above code will print "0 0 1 1 2 2" on the web page, here we used nested loop, so that execution of above code is given below step by step:
$i = 0, $j=0 then it print 0
$i = 0, $j=1 then it print 0
$i = 1, $j=0 then it print 1
$i = 1, $j=1 then it print 1
$i = 2, $j=0 then it print 2
$i = 2, $j=1 then it print 2
23) What is the correct output of the given code snippets?
<?php
$days = array(
"sun",
"mon",
"tue"
);
foreach ($dayin $days)
{
echo "$day ";
} ?>
- sun mon tue
- infinite lop
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code generates an error, there is no "in" keyword used in the foreach loop. We need to use "as" keyword in foreach loop.
Learn: PHP Arrays
24) What is the correct output of the given code snippets in PHP?
<?php
$days = array(
"sun",
"mon",
"tue"
);
foreach ($days as $day)
{
echo "$day ";
} ?>
- sun mon tue
- infinite lop
- Error
- None of the above
Correct answer: 1
sun mon tue
Explanation:
The above code will print "sun mon tue" on the webpage, here we access each value of array one by one using the foreach loop.
25) What is the correct output of the given code snippets?
<?php
$days = array(
"sun",
"mon",
"tue"
);
foreach ($days as $day)
{
if ($day == "mon") echo "monday "
else echo "$day ";
}
?>
- sun mon tue
- sun monday tue
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate an error because we did not use the semicolon(;) in the below line.
echo "monday "