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

Monday, September 2, 2024

Class 2: PHP Basics - Syntax and Data Types(MCQ)

MULTIPLE CHOICE QUESTIONS 

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;
b) $variable_name := value;
c) variable_name = value;
d) $variable_name = value;

Answer: d) $variable_name = value;

2. How do you output "Hello, World!" using PHP?

a) print("Hello, World!");
b) echo "Hello, World!";
c) display("Hello, World!");
d) Both a) and b)

Answer: d) Both a) and b)

3. What will be the output of the following PHP code?

$number = 10;

echo $number + 5;

a) 10
b) 15
c) 105
d) Error

Answer: b) 15

4. Which of the following is NOT a valid PHP data type?

a) Integer
b) String
c) Boolean
d) Character

Answer: d) Character

5. What is the result of the following PHP code?

$float = 3.14;

$integer = (int)$float;

echo $integer;

a) 3.14
b) 3
c) 4
d) Error

Answer: b) 3

6. Which operator is used for concatenating strings in PHP?

a) +
b) &
c) .
d) =

Answer: c) .

7. How do you declare a constant in PHP?

a) const CONSTANT_NAME = value;
b) define('CONSTANT_NAME', value);
c) constant CONSTANT_NAME = value;
d) set 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
b) 10
c) 5
d) Error

Answer: b) 10

9. Which of the following PHP data types is used to represent true/false values?

a) Integer
b) Float
c) String
d) Boolean

Answer: d) Boolean

10. What is the default data type of a variable in PHP if not explicitly declared?

a) Integer
b) String
c) Float
d) PHP automatically determines the data type based on the value assigned.

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
b) Hello World
c) Hello, World
d) Error

Answer: b) Hello World

12. How do you cast a string to a float in PHP?

a) (float)$string;
b) (string)$float;
c) float(string);
d) convert_to_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];
b) $array = (1, 2, 3);
c) $array = array(1, 2, 3);
d) $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
b) 5 apples
c) Error
d) 0

Answer: a) 5

15. Which function is used to get the length of a string in PHP?

a) length()
b) strlen()
c) str_length()
d) string_length()

Answer: b) strlen()

16. What does the print function do in PHP?

a) Prints data to the screen.
b) Saves data to a file.
c) Returns the data length.
d) Converts data to uppercase.

Answer: a) Prints data to the screen.

17. How do you convert an integer to a string in PHP?

a) (string)$integer;
b) (int)$string;
c) strval($integer);
d) Both a) and c)

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
b) 20
c) 30
d) Error

Answer: c) 30

19. What is the output of the following PHP code?

php

Copy code

$number = 5;

echo $number * 2;

a) 10
b) 52
c) 25
d) Error

Answer: a) 10

20. How do you define a variable in PHP that holds a floating-point number?

a) $float = 1.5;
b) $float = "1.5";
c) float $float = 1.5;
d) var $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
b) 1020
c) "10 20"
d) Error

Answer: a) 30

22. Which of the following is used to check the data type of a variable in PHP?

a) data_type()
b) gettype()
c) type()
d) checktype()

Answer: b) gettype()

23. How do you declare a variable in PHP that is an array?

a) $array = array(1, 2, 3);
b) array $array = [1, 2, 3];
c) $array[] = 1, 2, 3;
d) var 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
b) Hello World
c) "Hello World"
d) Error

Answer: b) Hello World

25. How do you convert a string "123" to an integer in PHP?

a) intval("123");
b) (int)"123";
c) string_to_int("123");
d) Both a) and b)

Answer: d) Both a) and b)

26. Which of the following operators is used to compare two values in PHP?

a) =
b) ==
c) !=
d) Both b) and c)

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
b) 12
c) 34
d) Error

Answer: b) 12

28. How do you declare a boolean variable in PHP?

a) $bool = true;
b) $bool = "true";
c) boolean $bool = true;
d) bool $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
b) 7
c) 81
d) Error

Answer: c) 81

30. How do you check if a variable is an integer in PHP?

a) is_numeric($variable);
b) is_int($variable);
c) check_integer($variable);
d) type_of($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
b) 7
c) 12
d) 1

Answer: a) 2

32. Which function would you use to convert a float to a string in PHP?

a) strval($float);
b) floatval($string);
c) to_string($float);
d) convert_to_string($float);

Answer: a) strval($float);

33. What is the default value of an uninitialized variable in PHP?

a) null
b) 0
c) ""
d) false

Answer: a) null

34. What will be the result of the following PHP code?

$var1 = "123abc";

$intVar = (int)$var1;

echo $intVar;

a) 123
b) 123abc
c) 0
d) Error

Answer: a) 123

35. How can you ensure that a variable is treated as a string in PHP?

a) Use (string)$variable;
b) Use strval($variable);
c) Use string($variable);
d) Both a) and b)

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

Answer: a) 9.89

37. How do you concatenate multiple strings in PHP?

a) Use + operator.
b) Use . operator.
c) Use concat() function.
d) Use append() method.

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
b) 3
c) 10
d) Error

Answer: a) 3.333

39. Which PHP function is used to find the length of an array?

a) length()
b) array_length()
c) count()
d) size()

Answer: c) count()

40. How do you convert a boolean value to an integer in PHP?

a) (int)$boolean;
b) intval($boolean);
c) boolval($integer);
d) Both a) and b)

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
b) 50.75
c) 70.75
d) Error

Answer: a) 30.75

42. Which function is used to check if a variable is a string in PHP?

a) is_string($variable);
b) check_string($variable);
c) string_type($variable);
d) typeof_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
b) 105
c) 10
d) Error

Answer: a) 50

44. How do you properly declare a variable with a floating-point value in PHP?

a) $floatVar = 3.14;
b) float $floatVar = 3.14;
c) $floatVar = "3.14";
d) var 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
b) 12
c) 0
d) 12.34abc

Answer: a) 12.34

46. How do you cast an integer to a string in PHP?

a) (string)$integer;
b) strval($integer);
c) convert_to_string($integer);
d) Both a) and b)

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
b) 100100
c) 50
d) Error

Answer: a) 200

48. What is the output of the following PHP code?

$val = "Hello";

$val .= " World";

echo $val;

a) HelloWorld
b) Hello World
c) Hello
d) Error

Answer: b) Hello World

49. How can you ensure a variable is treated as a boolean in PHP?

a) boolval($variable);
b) (bool)$variable;
c) boolean($variable);
d) Both a) and b)

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
b) 6
c) 5.5
d) Error

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.
b) Arrays in PHP are strictly typed, meaning they can only hold elements of the same type.
c) Arrays in PHP are unordered collections of elements.
d) Arrays in PHP can only store integers and strings.

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
b) abc
c) 1
d) Error

Answer: a) 0

53. How do you convert a string "true" to a boolean in PHP?

a) (bool)"true";
b) boolval("true");
c) convert_to_bool("true");
d) Both a) and b)

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!
b) Hello there
c) Hello
d) Error

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);
b) check_array($variable);
c) array_type($variable);
d) typeof_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
b) false
c) 5
d) Error

Answer: a) true

57. How can you get the absolute value of a number in PHP?

a) abs($number);
b) absolute($number);
c) get_absolute($number);
d) number_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
b) Hello5
c) Error
d) 0

Answer: d) 0

59. Which of the following functions is used to get the type of a variable in PHP?

a) var_type($variable);
b) type_of($variable);
c) gettype($variable);
d) variable_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
b) 9
c) 3
d) Error

Answer: a) 27

 

 

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

 


No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template