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

Friday, August 16, 2024

MCQ 6-Variables, Constants, and Data Types

 

Variables, Constants, and Data Types-1

1.     Which of the following is the correct way to declare a variable in PHP?
a) var $name = "John";
b) $name = "John";
c) variable $name = "John";
d) let $name = "John";

Answer: b) $name = "John";

2.     What will be the result of this PHP code?

<?php

$a = 10;

$b = "20";

$c = $a + $b;

echo $c;

?>

a) 30
b) 1020
c) Error
d) 10 + 20

Answer: a) 30

3.     Which function is used to define a constant in PHP?
a) define()
b) constant()
c) set_const()
d) create_const()

Answer: a) define()

4.     What will be the output of the following PHP code if PI is defined as 3.14?

<?php

define("PI", 3.14);

echo PI;

?>

a) 3.14
b) PI
c) Error
d) 3.14 (note: it’s already correct)

Answer: a) 3.14

5.     Which of the following is not a valid PHP data type?
a) integer
b) float
c) string
d) decimal

Answer: d) decimal

6.     What is the default value of an uninitialized PHP variable?
a) 0
b) null
c) "" (empty string)
d) false

Answer: b) null

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

<?php

$num1 = 5.5;

$num2 = (int) $num1;

echo $num2;

?>

a) 5.5
b) 5
c) 6
d) null

Answer: b) 5

8.     Which of the following statements is true about PHP constants?
a) Constants can be redefined
b) Constants are case-insensitive
c) Constants are defined using const keyword
d) Constants are global and cannot be changed once defined

Answer: d) Constants are global and cannot be changed once defined

9.     What will be the output of the following PHP code if $value is true?

<?php

$value = true;

echo $value;

?>

a) 1
b) true
c) Error
d) null

Answer: a) 1

10.Which of the following is the correct way to check the data type of a variable in PHP?
a) checktype($var)
b) gettype($var)
c) typeof($var)
d) type($var)

Answer: b) gettype($var)

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

<?php

$value = "123abc";

$intValue = (int)$value;

echo $intValue;

?>

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

Answer: a) 123

12.Which PHP data type is used to represent true/false values?
a) boolean
b) int
c) string
d) array

Answer: a) boolean

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

<?php

$x = 10;

$y = "10";

$result = $x == $y;

echo $result;

?>

a) 1
b) true
c) 0
d) false

Answer: a) 1

14.Which of the following data types is used to represent a sequence of characters in PHP?
a) array
b) boolean
c) string
d) object

Answer: c) string

15.What will be the output of this PHP code?

<?php

$x = 12;

$y = "12.34";

$result = $x + $y;

echo $result;

?>

a) 24.34
b) 12.34
c) Error
d) 12

Answer: a) 24.34

Variables, Constants, and Data Types-2

1.     What will the following PHP code output if $x is null?

<?php

$x = null;

$result = (bool)$x;

echo $result;

?>

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

Answer: b) false

2.     Which of the following is the correct way to define a constant with a case-insensitive name in PHP?
a) define("MY_CONSTANT", "value", true);
b) define("MY_CONSTANT", "value");
c) const MY_CONSTANT = "value";
d) const MY_CONSTANT = "value", caseInsensitive = true;

Answer: a) define("MY_CONSTANT", "value", true);

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

<?php

$a = "Hello";

$b = "World";

$c = $a . $b;

echo $c;

?>

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

Answer: a) HelloWorld

4.     Which of the following data types in PHP can store multiple values?
a) int
b) string
c) array
d) boolean

Answer: c) array

5.     What is the output of the following PHP code if $x is 3.14?

<?php

$x = 3.14;

$y = (int)$x;

echo $y;

?>

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

Answer: a) 3

6.     What will the following PHP code output?

<?php

$x = "100";

$y = 50;

$z = $x + $y;

echo $z;

?>

a) 150
b) 10050
c) Error
d) 50

Answer: a) 150

7.     Which of the following is a valid way to check if a variable is of type integer?
a) is_integer($var)
b) is_int($var)
c) check_int($var)
d) type_of($var)

Answer: b) is_int($var)

8.     What is the result of this PHP code?

<?php

$string = "123abc";

$integer = (int)$string;

echo $integer;

?>

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

Answer: a) 123

9.     Which of the following is true about PHP variables?
a) Variables are case-insensitive
b) Variables must start with a dollar sign ($)
c) Variables can start with a digit
d) Variables are automatically declared

Answer: b) Variables must start with a dollar sign ($)

10.What will be the result of the following PHP code if $num is set to 15.6?

<?php

$num = 15.6;

$intNum = intval($num);

echo $intNum;

?>

a) 15
b) 15.6
c) 16
d) 0

Answer: a) 15

11.Which of the following is the correct way to declare a constant in PHP that cannot be changed once defined?
a) define('CONST_NAME', 'value');
b) const CONST_NAME = 'value';
c) constant('CONST_NAME', 'value');
d) Both a and b

Answer: d) Both a and b

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

<?php

$x = true;

$y = false;

$result = $x && $y;

echo $result;

?>

a) 1
b) 0
c) true
d) false

Answer: b) 0

13.What type of value is null in PHP?
a) Integer
b) String
c) Boolean
d) Special type representing no value

Answer: d) Special type representing no value

14.Which of the following is not a valid PHP data type?
a) object
b) resource
c) date
d) float

Answer: c) date

15.What is the output of this PHP code if $number is -42?

<?php

$number = -42;

echo abs($number);

?>

a) 42
b) -42
c) 0
d) Error

Answer: a) 42

 

Variables, Constants, and Data Types-3

1.     What will be the result of the following PHP code if $value is set to "100abc"?

<?php

$value = "100abc";

$number = floatval($value);

echo $number;

?>

a) 100
b) 100.0
c) 100abc
d) 0

Answer: b) 100.0

2.     Which of the following statements is true about PHP constants?
a) Constants can be defined with a default value
b) Constants can be changed once defined
c) Constants can be arrays
d) Constants must be scalar values (integer, float, string, boolean)

Answer: d) Constants must be scalar values (integer, float, string, boolean)

3.     What is the output of this PHP code if $x is null?

<?php

$x = null;

$y = (string)$x;

echo $y;

?>

a) null
b) "" (empty string)
c) 0
d) Error

Answer: b) "" (empty string)

4.     What type of value is returned by the gettype() function in PHP?
a) Integer
b) Boolean
c) String
d) Array

Answer: c) String

5.     How does PHP handle variable types when performing arithmetic operations?
a) PHP automatically converts variables to the required type
b) PHP requires explicit type conversion before arithmetic operations
c) Arithmetic operations on different types result in an error
d) PHP always converts variables to strings before arithmetic operations

Answer: a) PHP automatically converts variables to the required type

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

<?php

$x = "10";

$y = 5;

$result = $x * $y;

echo $result;

?>

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

Answer: a) 50

7.     What will be the result of the following PHP code if $array is an empty array?

<?php

$array = [];

$result = count($array);

echo $result;

?>

a) 0
b) null
c) 1
d) Error

Answer: a) 0

8.     Which PHP function is used to check if a variable is of type array?
a) is_array()
b) check_array()
c) is_list()
d) type_array()

Answer: a) is_array()

9.     What is the output of the following PHP code if $text is "hello"?

<?php

$text = "hello";

$length = strlen($text);

echo $length;

?>

a) 5
b) hello
c) null
d) Error

Answer: a) 5

10.How can you convert a string to an integer in PHP?
a) intval($string)
b) convert_to_int($string)
c) string_to_int($string)
d) int($string)

Answer: a) intval($string)

11.Which of the following will convert a floating-point number to an integer?
a) (int)$float
b) floatval($int)
c) intval($float)
d) convert_int($float)

Answer: a) (int)$float

12.What is the result of this PHP code?

<?php

$a = "3.5";

$b = 2;

$result = $a / $b;

echo $result;

?>

a) 1.75
b) 3.5
c) 7
d) Error

Answer: a) 1.75

13.What does the gettype() function return when used on a boolean variable?
a) boolean
b) bool
c) int
d) string

Answer: a) boolean

14.What is the result of the following code snippet if $x is 100 and $y is "100"?

<?php

$x = 100;

$y = "100";

$result = $x === $y;

echo $result;

?>

a) true
b) false
c) 1
d) 0

Answer: b) false

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

<?php

$str = "abc123";

$int = (int)$str;

echo $int;

?>

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

Answer: a) 123

Variables, Constants, and Data Types-4

1.     What will be the output of the following PHP code if $val is "hello"?

<?php

$val = "hello";

$result = isset($val);

echo $result;

?>

a) true
b) false
c) 1
d) 0

Answer: c) 1

2.     Which of the following statements is true about PHP arrays?
a) Arrays in PHP are always indexed numerically
b) Arrays in PHP can have both numeric and associative keys
c) Arrays in PHP cannot have mixed types of keys
d) Arrays in PHP can only hold scalar values

Answer: b) Arrays in PHP can have both numeric and associative keys

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

<?php

$x = "123";

$y = (float)$x;

echo gettype($y);

?>

a) string
b) integer
c) float
d) boolean

Answer: c) float

4.     How can you convert a string "12.34" to an integer in PHP?
a) intval("12.34")
b) floatval("12.34")
c) (int)"12.34"
d) convert_int("12.34")

Answer: a) intval("12.34")

5.     What is the default value of a constant defined using the define() function if no value is assigned?
a) null
b) false
c) 0
d) Constants must always have a value assigned

Answer: d) Constants must always have a value assigned

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

<?php

$a = 10;

$b = 20;

$result = $a . $b;

echo $result;

?>

a) 30
b) 1020
c) 10
d) 20

Answer: b) 1020

7.     What is the output of the following PHP code if $x is -1.5?

<?php

$x = -1.5;

$result = abs($x);

echo $result;

?>

a) 1.5
b) -1.5
c) 0
d) Error

Answer: a) 1.5

8.     Which of the following functions will return true if the variable is a boolean type?
a) is_bool()
b) is_boolean()
c) check_bool()
d) type_bool()

Answer: a) is_bool()

9.     What will be the output of this PHP code if $x is 0 and $y is false?

<?php

$x = 0;

$y = false;

$result = ($x == $y);

echo $result;

?>

a) true
b) false
c) 1
d) 0

Answer: a) true

10.Which of the following is the correct way to check if a variable is an integer in PHP?
a) is_int()
b) is_integer()
c) check_int()
d) is_number()

Answer: a) is_int()

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

<?php

$a = "123abc";

$b = (int)$a;

echo $b;

?>

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

Answer: a) 123

12.How does PHP handle type juggling when comparing an integer with a string?
a) PHP converts both to strings before comparing
b) PHP converts both to integers before comparing
c) PHP converts the string to an integer before comparing
d) PHP compares the string with the integer directly

Answer: c) PHP converts the string to an integer before comparing

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

<?php

$x = 10;

$y = "10.5";

$result = $x + $y;

echo $result;

?>

a) 20.5
b) 10.5
c) 10
d) Error

Answer: a) 20.5

14.What will be the result of the following PHP code if $arr is an array with values [1, 2, 3]?

<?php

$arr = [1, 2, 3];

$result = count($arr);

echo $result;

?>

a) 3
b) null
c) 1
d) Error

Answer: a) 3

15.What is the output of the following PHP code if $x is an empty string?

<?php

$x = "";

$result = empty($x);

echo $result;

?>

a) true
b) false
c) 1
d) 0

Answer: c) 1

Variables, Constants, and Data Types-5

1.     What will be the result of the following PHP code if $x is "hello"?

<?php

$x = "hello";

$y = (boolean)$x;

echo $y;

?>

a) true
b) false
c) 1
d) 0

Answer: c) 1

2.     Which function is used to get the length of a string in PHP?
a) strlen()
b) str_length()
c) length()
d) get_length()

Answer: a) strlen()

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

<?php

$a = "10";

$b = 10;

$result = $a === $b;

echo $result;

?>

a) true
b) false
c) 1
d) 0

Answer: b) false

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

<?php

$x = "5 apples";

$y = intval($x);

echo $y;

?>

a) 5
b) 5 apples
c) 0
d) Error

Answer: a) 5

5.     Which of the following is a correct way to declare a floating-point constant in PHP?
a) define("PI", 3.14);
b) const PI = 3.14;
c) float PI = 3.14;
d) Both a and b

Answer: d) Both a and b

6.     What is the result of the following PHP code if $x is true?

<?php

$x = true;

$result = (int)$x;

echo $result;

?>

a) 1
b) 0
c) true
d) false

Answer: a) 1

7.     Which of the following statements is true about PHP arrays?
a) Arrays can only hold scalar values
b) Arrays can have both numeric and string keys
c) Arrays must be indexed numerically
d) Arrays can only be associative

Answer: b) Arrays can have both numeric and string keys

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

php

 

<?php

$x = "123";

$y = (float)$x;

$result = $y * 2;

echo $result;

?>

a) 246
b) 123.0
c) 123
d) Error

Answer: a) 246

9.     What does the is_null() function check for?
a) If a variable is of type null
b) If a variable is empty
c) If a variable is not set
d) If a variable is of type boolean

Answer: a) If a variable is of type null

10.What is the output of the following PHP code if $x is [] (an empty array)?

php

 

<?php

$x = [];

$result = count($x);

echo $result;

?>

a) 0
b) 1
c) null
d) Error

Answer: a) 0

11.How can you check if a variable is a string in PHP?
a) is_string()
b) is_text()
c) check_string()
d) typeof_string()

Answer: a) is_string()

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

php

 

<?php

$a = "123.45abc";

$result = (float)$a;

echo $result;

?>

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

Answer: a) 123.45

13.Which of the following functions can be used to check if a variable is not set?
a) isset()
b) empty()
c) is_null()
d) check_set()

Answer: a) isset()

14.What will be the result of the following PHP code if $x is 5.5 and $y is 3.5?

php

 

<?php

$x = 5.5;

$y = 3.5;

$result = $x + $y;

echo $result;

?>

a) 9
b) 9.0
c) 5.5
d) 3.5

Answer: b) 9.0

15.Which of the following is a correct way to declare a boolean constant in PHP?
a) define("IS_ACTIVE", true);
b) const IS_ACTIVE = true;
c) boolean IS_ACTIVE = true;
d) Both a and b

Answer: d) Both a and b

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template