MULTIPLE CHOICE QUSTION-SET 4
Class 4: Control Structures - Part 1(Implementing conditions and multiple case
scenarios ,scripts that use if, else if, else, and switch statements to
control the flow of their code based on different conditions) 1. Which of the following PHP code snippets
will print "Hello, World!" only if the variable $x is greater than
10? a) if ($x > 10) { echo "Hello,
World!"; } b) if ($x < 10) { echo "Hello,
World!"; } c) if ($x == 10) { echo "Hello,
World!"; } d) if ($x != 10) { echo "Hello,
World!"; } Answer: a) ```php if ($x > 10) { echo "Hello, World!"; } 2. What will be the output of the
following PHP code if `$day` is `3`? switch ($day) { case 1: echo
"Monday"; break; case 2: echo
"Tuesday"; break; case 3: echo
"Wednesday"; break; default: echo
"Weekend"; } a) Monday Answer: c) Wednesday 3. What will be the result of the following
PHP code? $age = 20; if ($age >= 18) { echo
"Adult"; } else { echo
"Minor"; } a) Adult Answer: a) Adult 4. Which of the following is the correct
syntax for an else if statement in PHP? a) else if (condition) { // code } b) else (condition) { // code } c) else if (condition): // code endif; d) else if (condition) { // code } else { // code } Answer: a) else if (condition) { // code } 5. What will be the output of the following
PHP code if `$score` is `85`? if ($score >= 90) { echo "A"; } elseif ($score >= 80) { echo "B"; } elseif ($score >= 70) { echo "C"; } else { echo "D"; } a) A Answer: b) B 6. What will be the output of the following
PHP code if $number is 5? switch ($number) { case 1: echo
"One"; break; case 2: echo
"Two"; break; case 5: echo
"Five"; break; default: echo
"Not Found"; } a) One Answer: c) Five 7. Which PHP statement will execute if $value
is neither equal to 10 nor 20 in the following code? switch ($value) { case 10: echo
"Ten"; break; case 20: echo
"Twenty"; break; default: echo
"Other"; } a) Ten Answer: c) Other 8. How do you handle multiple conditions in
an if statement? a) if (condition1 or condition2) { //
code } b)if (condition1 && condition2) { // code } c) if (condition1, condition2) { // code } d) ```php if (condition1 || condition2) { // code } Answer: b) if (condition1 && condition2) { // code } 9. What is the correct way to write an `else`
statement in PHP? a)else { // code } b) else if { // code } c)elseif { // code } d)else { // code } Answer a) else { // code } 10. What will be the output of the following
PHP code if $temperature is -5? if ($temperature < 0) { echo
"Freezing"; } elseif ($temperature < 10) { echo
"Cold"; } elseif ($temperature < 20) { echo
"Cool"; } else { echo
"Warm"; } a) Freezing Answer: a) Freezing 11. What will be the output of the following
PHP code if $x is 15? if ($x > 10) { echo
"Greater"; } elseif ($x == 10) { echo
"Equal"; } else { echo
"Smaller"; } a) Greater Answer: a) Greater 12. What is the correct syntax for a switch
statement in PHP? a) switch (condition) { case value1: // code
break; case value2: // code break; default: // code } b) switch (expression) { case value1: //
code break; case value2: //
code break; else: //
code } c)switch (expression) { case value1: // code
break; case value2: // code break; default: // code } d) switch (expression) { case value1: //
code case value2: //
code break; default: //
code } Answer: a) ```php switch (condition) { case value1: // code break; case
value2: // code break; default: // code } 13. What will be the output of the following
PHP code if `$num` is `7`? if ($num % 2 == 0) { echo
"Even"; } else { echo "Odd"; } a) Even Answer: b) Odd 14. What is the result of the following PHP
code if $value is 2? switch ($value) { case 1: echo
"First"; break; case 2: echo
"Second"; break; case 3: echo
"Third"; break; default: echo
"None"; } a) First Answer: b) Second 15. How many break statements are required in
a switch statement if you want to execute only one case? a) None Answer: b) One 16. What will be the output of the following
PHP code if $score is 55? if ($score >= 90) { echo
"Excellent"; } elseif ($score >= 70) { echo
"Good"; } elseif ($score >= 50) { echo
"Pass"; } else { echo
"Fail"; } a) Excellent Answer: c) Pass 17. What will be the output of the following
PHP code if $x is 0? if ($x) { echo
"True"; } else { echo
"False"; } a) True Answer: b) False 18. What is the correct syntax for using
multiple case statements in a switch block? a) case value1: case value2: // code break; b) case value1 || value2: // code break; c) case value1, value2: // code break; d)case value1, value2: // code continue; Answer: a) case value1: case value2: // code break; 19. What will be the output of the following
PHP code if `$a` is `5` and `$b` is `10`? if ($a > $b) { echo "a is
greater"; } elseif ($a < $b) { echo "b is
greater"; } else { echo "a and b
are equal"; } a) a is greater Answer: b) b is greater 20. Which statement will execute if none of
the case conditions match in a switch statement? a) The code block following break Answer: c) The code block following default 21. What will be the result of the following
PHP code if $age is 25? if ($age < 13) { echo
"Child"; } elseif ($age < 20) { echo
"Teenager"; } elseif ($age < 60) { echo
"Adult"; } else { echo
"Senior"; } a) Child Answer: c) Adult 22. What will be the output of the following
PHP code if $x is 5? php Copy code switch ($x) { case 1: case 2: case 3: echo
"Low"; break; case 4: case 5: echo
"Medium"; break; case 6: case 7: echo
"High"; break; default: echo
"Undefined"; } a) Low Answer: b) Medium 23. What will be the output of the following
PHP code if $number is 10? if ($number % 2 == 0) { if ($number % 3 == 0)
{ echo
"Divisible by 6"; } else { echo
"Divisible by 2 but not 3"; } } else { echo "Not
divisible by 2"; } a) Divisible by 6 Answer: b) Divisible by 2 but not 3 24. What will be the result of the following
PHP code if $score is 75? if ($score >= 50 && $score <
60) { echo
"Pass"; } elseif ($score >= 60 && $score
< 75) { echo
"Good"; } elseif ($score >= 75) { echo
"Excellent"; } else { echo
"Fail"; } a) Pass Answer: a) Good 25. Which PHP statement will be executed if
$day is 7 in the following code? switch ($day) { case 1: echo
"Monday"; break; case 2: echo
"Tuesday"; break; case 3: echo
"Wednesday"; break; case 4: echo
"Thursday"; break; case 5: echo
"Friday"; break; case 6: echo
"Saturday"; break; case 7: echo
"Sunday"; break; default: echo
"Invalid day"; } a) Monday Answer: c) Sunday 26. What will be the result of the following
PHP code if $age is 16? if ($age >= 13 && $age < 18) { echo
"Teen"; } else { echo "Not a
Teen"; } a) Teen Answer: a) Teen 27. What will be the output of the following
PHP code if $x is 3? switch ($x) { case 1: echo
"One"; break; case 2: case 3: echo
"Two or Three"; break; case 4: echo
"Four"; break; default: echo
"Other"; } a) One Answer: b) Two or Three 28. What will be the result of the following
PHP code if $number is 7? if ($number > 5) { if ($number < 10)
{ echo
"Between 6 and 9"; } else { echo
"10 or greater"; } } else { echo "5 or
less"; } a) Between 6 and 9 Answer: a) Between 6 and 9 29. What will be the output of the following
PHP code if $value is 4? switch ($value) { case 1: case 2: case 3: echo
"Low"; break; case 4: case 5: echo
"Medium"; break; case 6: echo
"High"; break; default: echo
"Undefined"; } a) Low Answer: b) Medium 30. What will be the result of the following
PHP code if $x is true? if ($x) { echo
"True"; } else { echo
"False"; } a) True Answer: a) True 31. What will be the output of the following
PHP code if $temperature is -1? if ($temperature > 0) { echo "Above
freezing"; } elseif ($temperature == 0) { echo "At
freezing"; } else { echo "Below
freezing"; } a) Above freezing Answer: c) Below freezing 32. Consider the following code. What will be
the output if $value is 3? switch ($value) { case 1: echo
"One"; break; case 2: echo
"Two"; break; case 3: case 4: echo
"Three or Four"; break; default: echo
"Other"; } a) One Answer: c) Three or Four 33. What will be the output of the following
PHP code? $x = 10; if ($x < 5) { echo "Less than
5"; } elseif ($x < 15) { echo "Between 5
and 15"; } else { echo "15 or
more"; } a) Less than 5 Answer: b) Between 5 and 15 34. What will be the result of the following
PHP code if $day is 6? switch ($day) { case 1: case 2: case 3: case 4: case 5: echo
"Weekday"; break; case 6: case 7: echo
"Weekend"; break; default: echo
"Invalid day"; } a) Weekday Answer: b) Weekend 35. How would you correctly handle multiple
conditions in a nested if statement? $age = 20; $membership = "Gold"; if ($age > 18) { if ($membership ==
"Gold") { echo
"Adult Gold Member"; } else { echo
"Adult Non-Gold Member"; } } else { echo "Not an
Adult"; } a) Adult Gold Member Answer: a) Adult Gold Member 36. What is the output of the following PHP
code if $x is 15? switch (true) { case ($x < 10): echo
"Less than 10"; break; case ($x >= 10
&& $x <= 20): echo
"Between 10 and 20"; break; case ($x > 20): echo
"Greater than 20"; break; default: echo
"Undefined"; } a) Less than 10 Answer: b) Between 10 and 20 37. What will be the result of this PHP code
if $score is 85? if ($score >= 90) { echo
"Excellent"; } elseif ($score >= 80) { echo "Very
Good"; } elseif ($score >= 70) { echo
"Good"; } else { echo "Needs
Improvement"; } a) Excellent Answer: b) Very Good 38. What will be the output of the following
PHP code if $value is 7? switch ($value % 2) { case 0: echo
"Even"; break; case 1: echo
"Odd"; break; default: echo
"Unknown"; } a) Even Answer: b) Odd 39. What will be the result of the following
PHP code if $score is 60? if ($score >= 50) { if ($score >= 60)
{ echo
"Pass"; } else { echo
"Fail"; } } else { echo
"Fail"; } a) Pass Answer: a) Pass 40. Which of the following PHP statements
will output "Hello World" based on $language? switch ($language) { case
"English": echo
"Hello World"; break; case
"Spanish": echo
"Hola Mundo"; break; case
"French": echo
"Bonjour le Monde"; break; default: echo
"Language not supported"; } a) Hello World Answer: a) Hello World (assuming $language is set to
"English") 41. What will be the output of the following
PHP code if $age is 18? if ($age < 18) { echo
"Minor"; } elseif ($age >= 18 && $age <=
65) { echo
"Adult"; } else { echo
"Senior"; } a) Minor Answer: b) Adult 42. What is the output of the following PHP
code if $score is 100? if ($score >= 90) { echo "A"; } elseif ($score >= 80) { echo "B"; } elseif ($score >= 70) { echo "C"; } elseif ($score >= 60) { echo "D"; } else { echo "F"; } a) A Answer: a) A 43. What will be the output of the following
PHP code if $day is 5? switch ($day) { case 1: case 2: case 3: case 4: case 5: echo
"Weekday"; break; case 6: case 7: echo
"Weekend"; break; default: echo
"Invalid day"; } a) Weekday Answer: a) Weekday 44. What will be the result of this PHP code
if $x is 0? if ($x) { echo
"True"; } else { echo
"False"; } a) True Answer: b) False 45. What will be the output of the following
PHP code? $x = 10; $y = 20; if ($x > $y) { echo "x is
greater"; } elseif ($x < $y) { echo "y is
greater"; } else { echo "x and y
are equal"; } a) x is greater Answer: b) y is greater 46. What will be the output of the following
PHP code if $value is 3? switch ($value) { case 1: echo
"One"; break; case 2: echo
"Two"; break; case 3: case 4: echo
"Three or Four"; break; default: echo
"Other"; } a) One Answer: c) Three or Four 47. What will be the output of the following
PHP code? $x = 10; $y = 20; $z = 30; if ($x < $y) { if ($y < $z) { echo
"All conditions true"; } else { echo
"Second condition false"; } } else { echo "First
condition false"; } a) All conditions true Answer: a) All conditions true 48. What will be the output if $x is 2 and $y
is 3? if ($x == 2 && $y == 3) { echo "Both are
correct"; } elseif ($x == 2 || $y == 3) { echo "One is
correct"; } else { echo "Neither is
correct"; } a) Both are correct Answer: a) Both are correct 49. What will be the result of the following
PHP code? $x = 5; switch (true) { case ($x < 5): echo
"Less than 5"; break; case ($x == 5): echo
"Equal to 5"; break; case ($x > 5): echo
"Greater than 5"; break; default: echo
"Undefined"; } a) Less than 5 Answer: b) Equal to 5 50. What will be the output of the following
PHP code if $score is 65? if ($score >= 70) { echo
"Pass"; } else { if ($score >= 60)
{ echo
"Barely Pass"; } else { echo
"Fail"; } } a) Pass Answer: b) Barely Pass |