"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Monday, September 2, 2024

Class 4: Control Structures - Part 1(MCQ)

 

 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
b) Tuesday
c) Wednesday
d) Weekend

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
b) Minor
c) Error
d) Nothing

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
b) B
c) C
d) D

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
b) Two
c) Five
d) Not Found

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
b) Twenty
c) Other
d) Error

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
b) Cold
c) Cool
d) Warm

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
b) Equal
c) Smaller
d) Error

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
b) Odd
c) Error
d) Nothing

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
b) Second
c) Third
d) None

Answer: b) Second

15. How many break statements are required in a switch statement if you want to execute only one case?

a) None
b) One
c) Two
d) Depends on the number of cases

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
b) Good
c) Pass
d) Fail

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
b) False
c) Error
d) Nothing

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
b) b is greater
c) a and b are equal
d) Error

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
b) The code block following continue
c) The code block following default
d) None

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
b) Teenager
c) Adult
d) Senior

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
b) Medium
c) High
d) Undefined

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
b) Divisible by 2 but not 3
c) Not divisible by 2
d) Error

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
b) Good
c) Excellent
d) Fail

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
b) Saturday
c) Sunday
d) Invalid day

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
b) Not a Teen
c) Error
d) Nothing

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
b) Two or Three
c) Four
d) Other

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
b) 10 or greater
c) 5 or less
d) Error

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
b) Medium
c) High
d) Undefined

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
b) False
c) Error
d) Nothing

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
b) At freezing
c) Below freezing
d) Error

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
b) Two
c) Three or Four
d) Other

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
b) Between 5 and 15
c) 15 or more
d) Error

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
b) Weekend
c) Invalid day
d) Error

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
b) Adult Non-Gold Member
c) Not an Adult
d) Error

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
b) Between 10 and 20
c) Greater than 20
d) Undefined

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
b) Very Good
c) Good
d) Needs Improvement

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
b) Odd
c) Unknown
d) Error

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
b) Fail
c) Error
d) Nothing

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
b) Hola Mundo
c) Bonjour le Monde
d) Language not supported

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
b) Adult
c) Senior
d) Error

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
b) B
c) C
d) D

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
b) Weekend
c) Invalid day
d) Error

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
b) False
c) Error
d) Nothing

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
b) y is greater
c) x and y are equal
d) Error

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
b) Two
c) Three or Four
d) Other

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
b) Second condition false
c) First condition false
d) Error

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
b) One is correct
c) Neither is correct
d) Error

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
b) Equal to 5
c) Greater than 5
d) Undefined

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
b) Barely Pass
c) Fail
d) Error

Answer: b) Barely Pass                          

 


***************************


 

 


No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template