MCQs On Class 2
PHP Basics - Syntax and Data Types
1. Which of the following is the correct
syntax for declaring a variable in PHP? a) var $variable_name = value; Answer: d) $variable_name = value; 2. How do you output "Hello,
World!" using PHP? a) print("Hello, World!"); Answer: d) Both a) and b) 3. What will be the output of the following
PHP code? $number = 10; echo $number + 5; a) 10 Answer: b) 15 4. Which of the following is NOT a valid PHP
data type? a) Integer Answer: d) Character 5. What is the result of the following PHP
code? $float = 3.14; $integer = (int)$float; echo $integer; a) 3.14 Answer: b) 3 6. Which operator is used for concatenating
strings in PHP? a) + Answer: c) . 7. How do you declare a constant in PHP? a) const CONSTANT_NAME = value; Answer: b) define('CONSTANT_NAME', value); 8. What will be the output of the following
PHP code? $var = "10.5"; $intVar = (int)$var; echo $intVar; a) 10.5 Answer: b) 10 9. Which of the following PHP data types is
used to represent true/false values? a) Integer Answer: d) Boolean 10. What is the default data type of a
variable in PHP if not explicitly declared? a) Integer Answer: d) PHP automatically determines the data type based on the value
assigned. 11. What will be the result of the following
PHP code? $x = "Hello"; $y = "World"; echo $x . " " . $y; a) HelloWorld Answer: b) Hello World 12. How do you cast a string to a float in
PHP? a) (float)$string; Answer: a) (float)$string; 13. Which of the following is the correct way
to initialize an array in PHP? a) array = [1, 2, 3]; Answer: c) $array = array(1, 2, 3); 14. What is the output of the following PHP
code? $var = "5 apples"; $integerVar = (int)$var; echo $integerVar; a) 5 Answer: a) 5 15. Which function is used to get the length
of a string in PHP? a) length() Answer: b) strlen() 16. What does the print function do in PHP? a) Prints data to the screen. Answer: a) Prints data to the screen. 17. How do you convert an integer to a string
in PHP? a) (string)$integer; Answer: d) Both a) and c) 18. What will be the result of the following
code? $var1 = 10; $var2 = 20; $var1 += $var2; echo $var1; a) 10 Answer: c) 30 19. What is the output of the following PHP
code? php Copy code $number = 5; echo $number * 2; a) 10 Answer: a) 10 20. How do you define a variable in PHP that
holds a floating-point number? a) $float = 1.5; Answer: a) $float = 1.5; 21. What will be the result of the following
PHP code? php Copy code $var1 = "10"; $var2 = "20"; echo $var1 + $var2; a) 30 Answer: a) 30 22. Which of the following is used to check
the data type of a variable in PHP? a) data_type() Answer: b) gettype() 23. How do you declare a variable in PHP that
is an array? a) $array = array(1, 2, 3); Answer: a) $array = array(1, 2, 3); 24. What will be the result of the following
PHP code? $var = "Hello" . " " .
"World"; echo $var; a) HelloWorld Answer: b) Hello World 25. How do you convert a string
"123" to an integer in PHP? a) intval("123"); Answer: d) Both a) and b) 26. Which of the following operators is used
to compare two values in PHP? a) = Answer: d) Both b) and c) 27. What will be the result of the following
PHP code? $var = "12.34"; $intVar = (int)$var; echo $intVar; a) 12.34 Answer: b) 12 28. How do you declare a boolean variable in
PHP? a) $bool = true; Answer: a) $bool = true; 29. What will be the output of the following
PHP code? $x = 3; $y = 4; echo $x ** $y; a) 12 Answer: c) 81 30. How do you check if a variable is an
integer in PHP? a) is_numeric($variable); Answer: b) is_int($variable); 31. What is the result of the following PHP
code? php Copy code $number = 12; echo $number % 5; a) 2 Answer: a) 2 32. Which function would you use to convert a
float to a string in PHP? a) strval($float); Answer: a) strval($float); 33. What is the default value of an
uninitialized variable in PHP? a) null Answer: a) null 34. What will be the result of the following
PHP code? $var1 = "123abc"; $intVar = (int)$var1; echo $intVar; a) 123 Answer: a) 123 35. How can you ensure that a variable is
treated as a string in PHP? a) Use (string)$variable; Answer: d) Both a) and b) 36. What is the result of the following PHP
code? $val = "7.89"; echo $val + 2; a) 9.89 Answer: a) 9.89 37. How do you concatenate multiple strings
in PHP? a) Use + operator. Answer: b) Use . operator. 38. What will be the output of the following
PHP code? $var1 = 10; $var2 = 3; echo $var1 / $var2; a) 3.333 Answer: a) 3.333 39. Which PHP function is used to find the
length of an array? a) length() Answer: c) count() 40. How do you convert a boolean value to an
integer in PHP? a) (int)$boolean; Answer: d) Both a) and b) 41. What is the result of the following PHP
code? $var = "50.75"; echo $var - 20; a) 30.75 Answer: a) 30.75 42. Which function is used to check if a
variable is a string in PHP? a) is_string($variable); Answer: a) is_string($variable); 43. What will be the result of the following
PHP code? $val1 = 10; $val2 = "5"; echo $val1 * $val2; a) 50 Answer: a) 50 44. How do you properly declare a variable
with a floating-point value in PHP? a) $floatVar = 3.14; Answer: a) $floatVar = 3.14; 45. What will be the result of the following
PHP code? $val = (float) "12.34abc"; echo $val; a) 12.34 Answer: a) 12.34 46. How do you cast an integer to a string in
PHP? a) (string)$integer; Answer: d) Both a) and b) 47. What will be the result of the following
PHP code? $var = "100"; $var *= 2; echo $var; a) 200 Answer: a) 200 48. What is the output of the following PHP
code? $val = "Hello"; $val .= " World"; echo $val; a) HelloWorld Answer: b) Hello World 49. How can you ensure a variable is treated
as a boolean in PHP? a) boolval($variable); Answer: d) Both a) and b) 50. What will be the result of the following
PHP code? $var = "5.5"; $intVar = (int)round($var); echo $intVar; a) 5 Answer: b) 6 51. Which of the following statements about
PHP arrays is true? a) Arrays in PHP are ordered collections of
key-value pairs. Answer: a) Arrays in PHP are ordered collections of key-value pairs. 52. What will be the output of the following
PHP code? $var = "abc"; $intVar = (int)$var; echo $intVar; a) 0 Answer: a) 0 53. How do you convert a string
"true" to a boolean in PHP? a) (bool)"true"; Answer: d) Both a) and b) 54. What will be the result of the following
PHP code? $val = "Hello"; $val .= " there!"; echo $val; a) Hello there! Answer: a) Hello there! 55. Which of the following is a valid way to
check if a variable is an array in PHP? a) is_array($variable); Answer: a) is_array($variable); 56. What will be the output of the following
PHP code? $var1 = 5; $var2 = "5"; echo $var1 == $var2; a) true Answer: a) true 57. How can you get the absolute value of a
number in PHP? a) abs($number); Answer: a) abs($number); 58. What will be the result of the following
PHP code? $var = "Hello"; $var += 5; echo $var; a) 5 Answer: d) 0 59. Which of the following functions is used
to get the type of a variable in PHP? a) var_type($variable); Answer: c) gettype($variable); 60. What will be the result of the following
PHP code? $val = 3; $val = $val ** 3; echo $val; a) 27 Answer: a) 27 |