MCQs Of Class 29: Debugging CSS
1. Which of the following is a valid array in PHP?
A) $arr = (1, 2, 3);
B) $arr = array(1, 2, 3);
C) $arr = {1, 2, 3};
D) $arr = [1 => 'apple', 2 => 'banana', 'cherry'];
Answer: B
Explanation: The correct syntax for defining an array in PHP is array(1, 2, 3) or using the shorthand []. Option B is the correct way to define an indexed array.
2. What does the count() function in PHP return when applied to an array?
A) The first element of the array
B) The length of the array
C) The last element of the array
D) The total of all array elements
Answer: B
Explanation: The count() function returns the number of elements in an array.
3. How can you create an associative array in PHP?
A) $arr = array(1, 2, 3);
B) $arr = ['name' => 'John', 'age' => 25];
C) $arr = (1 => 'apple', 2 => 'banana');
D) $arr = array('apple', 'banana', 'cherry');
Answer: B
Explanation: An associative array uses named keys (e.g., 'name' => 'John') instead of numeric indices.
4. How would you remove the last element from an array in PHP?
A) array_pop()
B) array_shift()
C) unset()
D) array_reverse()
Answer: A
Explanation: The array_pop() function removes the last element from an array.
5. Which PHP function returns the value of the specified key in an associative array?
A) array_value()
B) array_key_exists()
C) array_search()
D) array_values()
Answer: C
Explanation: array_search() is used to search for a value in an array and return its corresponding key.
6. Which of the following is the correct syntax to define a function in PHP?
A) function myFunction[] {}
B) function myFunction() {}
C) def myFunction() {}
D) function myFunction[];
Answer: B
Explanation: The correct way to define a function in PHP is function myFunction() {}.
7. What does the return keyword do in a PHP function?
A) Stops the function execution and outputs a value
B) Outputs the value without stopping the function
C) Ends the function and returns a value
D) Creates a new function
Answer: C
Explanation: The return keyword stops function execution and sends a value back to the caller.
8. Which of the following is the correct way to pass an argument by reference in PHP?
A) function example(&$arg) {}
B) function example($arg&) {}
C) function example(){};
D) function example($&arg) {}
Answer: A
Explanation: To pass a variable by reference, you use the & symbol before the parameter in the function definition.
9. What does the isset() function do in PHP?
A) Checks if a variable is empty
B) Checks if a variable is null
C) Checks if a variable is set and is not null
D) Declares a variable
Answer: C
Explanation: isset() checks if a variable is set and is not null.
10. Which of the following statements is correct about the switch statement in PHP?
A) It compares all expressions sequentially
B) It compares values with strict equality (===)
C) It executes all cases in the block
D) It does not allow default case
Answer: B
Explanation: The switch statement in PHP uses strict comparison (===), meaning it checks both value and type.
11. What does the array_map() function do?
A) Maps array values to new values based on a given function
B) Combines two arrays into one
C) Returns a new array of keys
D) Sorts an array in ascending order
Answer: A
Explanation: array_map() applies a given function to each element of the array and returns a new array.
12. Which of the following functions is used to reverse the elements of an array in PHP?
A) array_reverse()
B) array_flip()
C) array_sort()
D) array_rotate()
Answer: A
Explanation: The array_reverse() function reverses the order of elements in an array.
13. Which of the following PHP loops will execute at least once?
A) for
B) while
C) do-while
D) foreach
Answer: C
Explanation: A do-while loop executes the block of code at least once before checking the condition.
14. What is the default value of the $GLOBALS array?
A) Null
B) All variables in the global scope
C) An empty array
D) All variables in the local scope
Answer: B
Explanation: The $GLOBALS array contains all global variables.
15. What does the array_merge() function do in PHP?
A) Merges two arrays by appending elements
B) Merges two arrays by removing duplicates
C) Merges two arrays into a single array
D) Reverses the order of elements in two arrays
Answer: C
Explanation: array_merge() merges two arrays into one array.
16. Which of the following is true for PHP arrays?
A) Arrays in PHP can only store values of the same data type.
B) Arrays in PHP are ordered and indexed.
C) Arrays in PHP can only store integer keys.
D) Arrays in PHP cannot be associative.
Answer: B
Explanation: PHP arrays are ordered and can have either numeric or associative keys.
17. What is the purpose of the continue statement in PHP?
A) Skip the current iteration of a loop and continue with the next iteration
B) Terminate the loop execution
C) Jump to the first iteration of the loop
D) Skip the loop entirely
Answer: A
Explanation: continue is used to skip the current iteration and continue to the next iteration of the loop.
18. What will the following code output?
php
CopyEdit
$arr = array(1, 2, 3);
echo count($arr);
A) 1
B) 2
C) 3
D) NULL
Answer: C
Explanation: The count() function returns the number of elements in the array, which is 3.
19. Which function is used to check if a variable is an array in PHP?
A) is_array()
B) is_array_type()
C) array_check()
D) is_array_val()
Answer: A
Explanation: is_array() checks whether a variable is an array.
20. Which of the following loops is not valid in PHP?
A) for
B) while
C) foreach
D) loop
Answer: D
Explanation: loop is not a valid loop in PHP. PHP supports for, while, and foreach.
21. What does the array_keys() function do in PHP?
A) Returns the keys of the array
B) Returns the values of the array
C) Removes keys from the array
D) Reverses the keys in the array
Answer: A
Explanation: The array_keys() function returns an array containing all the keys of the array.
22. Which PHP function is used to change the case of all characters in a string to uppercase?
A) strtoupper()
B) strtolower()
C) ucwords()
D) capitalize()
Answer: A
Explanation: strtoupper() converts all characters in a string to uppercase.
23. Which of the following is used to combine elements of two arrays in PHP?
A) combine()
B) merge()
C) array_merge()
D) concat()
Answer: C
Explanation: array_merge() combines two or more arrays into one.
24. What will this code output?
php
CopyEdit
$numbers = [1, 2, 3];
array_shift($numbers);
print_r($numbers);
A) [2, 3]
B) [1, 2, 3]
C) [1]
D) NULL
Answer: A
Explanation: array_shift() removes the first element of the array, resulting in [2, 3].
25. What is the default value returned by array_search() when the value is not found?
A) false
B) NULL
C) 0
D) empty string
Answer: A
Explanation: array_search() returns false when the value is not found in the array.
26. Which function is used to extract all values of an array in PHP?
A) array_values()
B) array_keys()
C) array_extract()
D) array_get()
Answer: A
Explanation: array_values() returns all values of the array, re-indexed numerically.
27. Which PHP function is used to check if a key exists in an array?
A) key_exists()
B) is_key()
C) array_key_exists()
D) exists()
Answer: C
Explanation: array_key_exists() checks if a specific key exists in an array.
28. Which of the following is not a valid control structure in PHP?
A) if
B) else
C) elif
D) while
Answer: C
Explanation: PHP uses elseif, not elif.
29. What is the output of the following PHP code?
php
CopyEdit
$fruits = ['apple', 'banana', 'cherry'];
echo $fruits[1];
A) banana
B) apple
C) cherry
D) NULL
Answer: A
Explanation: The code accesses the second element (index 1) of the array, which is 'banana'.
30. How would you write a PHP function that takes two arguments and returns their sum?
A) function sum($a, $b) { return $a + $b; }
B) function sum($a, $b) { return $a - $b; }
C) sum($a, $b) { return $a + b; }
D) function sum($a, b) { return a + b; }
Answer: A
Explanation: The function sum($a, $b) returns the sum of $a and $b.
31. What will the following PHP code output?
php
CopyEdit
$numbers = [2, 3, 4];
array_push($numbers, 5);
print_r($numbers);
A) [2, 3, 4, 5]
B) [5, 2, 3, 4]
C) [5]
D) [2, 3, 4]
Answer: A
Explanation: array_push() adds the element to the end of the array, so the array becomes [2, 3, 4, 5].
32. Which of the following is used to start a block of code for the foreach loop?
A) foreach($arr as $value)
B) foreach($arr => $value)
C) foreach($value in $arr)
D) foreach($value of $arr)
Answer: A
Explanation: The correct syntax for a foreach loop is foreach($arr as $value).
33. Which function in PHP is used to count the number of elements in an array?
A) count()
B) length()
C) num_elements()
D) size()
Answer: A
Explanation: The count() function is used to count the number of elements in an array.
34. How would you loop through an associative array using a foreach loop in PHP?
A) foreach($arr as $key => $value)
B) foreach($arr as $value => $key)
C) foreach($arr as $key, $value)
D) foreach($arr as $value)
Answer: A
Explanation: The correct syntax to loop through an associative array is foreach($arr as $key => $value).
35. How can you remove an element by key from an associative array in PHP?
A) array_remove()
B) unset()
C) array_pop()
D) array_shift()
Answer: B
Explanation: The unset() function is used to remove an element from an array by key.
36. Which PHP function is used to combine two arrays into one?
A) array_combine()
B) array_concat()
C) array_union()
D) merge_arrays()
Answer: A
Explanation: array_combine() combines two arrays by using the values of the first array as keys and the values of the second array as values.
37. What does the empty() function do in PHP?
A) Checks if a variable is empty or not set
B) Deletes a variable
C) Initializes a variable
D) Returns an empty array
Answer: A
Explanation: empty() checks if a variable is empty or not set.
38. What does the break statement do in PHP?
A) Terminates the entire script
B) Terminates the current loop or switch
C) Continues to the next iteration of the loop
D) Ends the current function
Answer: B
Explanation: The break statement exits from the current loop or switch statement.
39. Which of the following PHP functions is used to remove duplicate values from an array?
A) array_unique()
B) array_no_duplicates()
C) array_remove_duplicates()
D) array_distinct()
Answer: A
Explanation: The array_unique() function removes duplicate values from an array.
40. How would you define a default value for an argument in a PHP function?
A) function example($arg = 5) {}
B) function example($arg: 5) {}
C) function example($arg[5]) {}
D) function example($arg) => 5 {}
Answer: A
Explanation: You can assign a default value to an argument like this: function example($arg = 5).
41. What will this code output?
php
CopyEdit
$x = 10;
$y = 20;
echo ($x < $y) ? 'True' : 'False';
A) True
B) False
C) 10
D) 20
Answer: A
Explanation: The ternary operator checks if $x < $y, which is true, so it outputs 'True'.
42. How can you change the keys of an associative array in PHP?
A) array_change_key()
B) array_flip()
C) array_map_keys()
D) array_key_change()
Answer: B
Explanation: array_flip() swaps the keys and values of an array.
43. How do you declare a global variable in PHP?
A) global $var;
B) $var = global;
C) declare global $var;
D) global var;
Answer: A
Explanation: You declare a global variable inside a function using the global keyword.
44. What is the purpose of the isset() function in PHP?
A) To check if a variable is not null
B) To check if a variable is empty
C) To check if a variable is declared and not null
D) To check if a variable is an array
Answer: C
Explanation: isset() checks if a variable is both declared and not null.
45. What will this code output?
php
CopyEdit
$nums = [1, 2, 3, 4];
foreach($nums as $num) {
echo $num;
}
A) 1234
B) 1 2 3 4
C) 4 3 2 1
D) 1,2,3,4
Answer: A
Explanation: The foreach loop prints the numbers consecutively without spaces.
46. Which PHP function can be used to find the length of a string?
A) strlen()
B) length()
C) str_length()
D) char_count()
Answer: A
Explanation: strlen() is used to find the length of a string.
47. Which of the following will not terminate the script execution in PHP?
A) exit()
B) die()
C) break
D) return
Answer: C
Explanation: break terminates the loop or switch, but does not terminate the script.
48. What does the array_flip() function do in PHP?
A) Changes the order of elements
B) Reverses the values and keys of an array
C) Merges two arrays
D) Sorts an array in ascending order
Answer: B
Explanation: array_flip() swaps the keys and values of an array.
No comments:
Post a Comment