PHP OOP Practice Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Gadhavitechnolog
G
Gadhavitechnolog
Community Contributor
Quizzes Created: 1 | Total Attempts: 5,191
| Attempts: 5,191 | Questions: 38
Please wait...
Question 1 / 38
0 %
0/100
Score 0/100
1. In PHP You can use both single quotes (' ') and double quotes (" ") for strings:

Explanation

In PHP, both single quotes (' ') and double quotes (" ") can be used to define strings. This allows for flexibility in writing and manipulating strings in PHP code.

Submit
Please wait...
About This Quiz
PHP OOP Practice Quiz - Quiz

Are you a PHP language expert? What is PHP OOP? Is it hard? Take this PHP practice test and check how easily you can solve basic PHP questions.... see moreIn this quiz, we’ll be analyzing your knowledge of all the different functions of the widely used programming and web development language PHP! What can you tell us about these functions and how they are used? Give this quiz a try and test your knowledge of this language. Let’s find out! see less

2. All variables in PHP are prefixed with special symbol, which?

Explanation

In PHP, all variables are prefixed with the special symbol "$". This symbol is used to indicate that a variable is being declared or used in the code. It helps differentiate variables from other types of data and allows PHP to recognize and interpret them correctly. By using the "$" symbol, developers can easily identify and work with variables in their PHP scripts.

Submit
3. How do You get information from a form that is submitted using the "get" method?

Explanation

When a form is submitted using the "get" method, the information from the form is appended to the URL as query parameters. To retrieve this information in the server-side code, the correct way is to use the $_GET[] array. This array allows access to the values of the query parameters by specifying their names within the square brackets.

Submit
4. Can PHP be executed in Command line?

Explanation

PHP can be executed in the command line. This allows developers to run PHP scripts directly from the command line interface without the need for a web server. It is a useful feature for tasks such as testing and debugging scripts, running cron jobs, and performing command-line tasks with PHP. It is not limited to any specific operating system and can be executed on both Linux and Windows platforms.

Submit
5. What is the correct way to connect to MySQL database?

Explanation

The correct way to connect to a MySQL database is by using the function mysql_connect("localhost"). This function establishes a connection between the PHP script and the MySQL server located on the localhost.

Submit
6. Include files must have file extension *.inc

Explanation

The given statement is false. Include files do not necessarily have to have the file extension "*.inc". The file extension for include files can vary depending on the programming language or framework being used. It is common to see include files with extensions like ".h" for C/C++ or ".php" for PHP. Therefore, the statement is incorrect.

Submit
7. When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references.

Explanation

When an object is cloned in PHP 5, a shallow copy of all the object's properties is performed. This means that the cloned object will have its own copies of the properties, but if any of these properties are references to other variables, they will still remain as references. In other words, changes made to the referenced variables will be reflected in both the original object and the cloned object.

Submit
8. What does PHP stand for?

Explanation

PHP stands for "PHP: Hypertext Processor". This is the correct answer because PHP is a server-side scripting language that is widely used for web development. It is designed to process and generate dynamic web pages, allowing users to interact with websites in real-time. The term "Hypertext Processor" accurately describes the functionality of PHP, as it processes and interprets the code embedded within web pages to generate dynamic content.

Submit
9. What is the correct way to add 1 to the variable?

Explanation

The correct way to add 1 to the variable is by using the increment operator "++" after the variable name. This operator increases the value of the variable by 1. In this case, the variable is represented as $variable, so the correct answer is $variable++.

Submit
10. Which one of theese variables has illegal name?

Explanation

The variable name "$my-Var" is illegal because it contains a hyphen, which is not allowed in variable names. Variable names can only contain letters, numbers, and underscores. Therefore, all the other variable names ("$myVar", "$my_Var") are legal.

Submit
11. $string = 'Hello World!'; if(stristr($string, 'earth') === FALSE) { echo '"earth" not found in string'; } ?> The above code:

Explanation

The code checks if the string "earth" is present in the given string "Hello World!". Since "earth" is not found in the string, the condition is true and the code inside the if statement is executed. It prints the message "earth" not found in string. Therefore, the correct answer is "Outputs: "earth" not found in string".

Submit
12. True or False: In PHP, a class can have multiple constructors.

Explanation



In PHP, a class can only have one constructor method, which is defined using the __construct keyword. Unlike some other programming languages, PHP does not support method overloading, which means you cannot define multiple constructors with different parameters within a single class. Instead, you can use default parameter values or variadic functions to achieve similar functionality.
Submit
13. Does anonymous functions allow creation of functions without specifying their name?

Explanation

Anonymous functions do allow the creation of functions without specifying their name. These functions are also known as lambda functions or function literals. They are defined without a name and are typically used when a function is needed only once or when a function is passed as an argument to another function.

Submit
14. for ($i = 1; ; $i++) { if ($i > 10) { break; } echo $i; } ?> What result will it give?

Explanation

The given code is a loop that starts with a variable $i equal to 1. It continues to iterate indefinitely until a certain condition is met. In this case, the condition is $i > 10. Once $i becomes greater than 10, the loop is terminated using the break statement. Inside the loop, the value of $i is echoed, which means it is printed on the screen. Therefore, the code will output the numbers from 1 to 10, one after another, resulting in the answer 12345678910.

Submit
15. If only one array is given in array_merge() as parameter and the array is numerically indexed, the keys get reindexed in a continuous way.

Explanation

When using the array_merge() function with only one array parameter, if the array is numerically indexed, the keys will be reindexed in a continuous way. This means that the keys will be reassigned starting from 0 and incrementing by 1 for each element in the array. Therefore, the statement "The above sentence is true" is correct.

Submit
16. Is it possible to run several versions of PHP at the same time?

Explanation

Yes, it is possible to run several versions of PHP at the same time. This can be achieved by using tools such as virtualization or containerization, which allow different versions of PHP to coexist on the same system. By isolating each version in its own environment, conflicts between different versions can be avoided, enabling multiple versions to be used simultaneously for different projects or applications.

Submit
17. $fruits = array("lemon", "orange", "banana", "apple"); sort($fruits); foreach ($fruits as $key => $val) { echo "fruits[" . $key . "] = " . $val . "\n"; } ?> The above code will result in:

Explanation

The code first sorts the array of fruits in alphabetical order using the sort() function. Then, it iterates over the sorted array using a foreach loop. In each iteration, the key and value of the current element are printed using the echo statement. The result will be the elements of the array printed in alphabetical order, with the corresponding keys. Therefore, the correct answer is "fruits[0] = apple, fruits[1] = banana, fruits[2] = lemon, fruits[3] = orange".

Submit
18. What's correct result of below code? $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); asort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; } ?>

Explanation

The correct result of the given code is "c = apple b = banana d = lemon a = orange". The code creates an associative array called $fruits with keys and values. The asort() function is then used to sort the array by its values in ascending order, while maintaining the key-value association. The foreach loop is used to iterate through the sorted array and print each key-value pair. Therefore, the output will be the keys and values of the sorted array, separated by "=" and with each pair on a new line.

Submit
19. $email = '[email protected]'; $domain = strstr($email, '@'); echo $domain; ?> Whats the result of above code?

Explanation

The code uses the strstr() function to find the first occurrence of the "@" symbol in the email string. It then assigns the substring starting from the "@" symbol to the $domain variable. Finally, it prints the value of the $domain variable, which is "@example.com".

Submit
20. Which function sets the error_reporting directive at runtime?

Explanation

The function error_reporting() sets the error_reporting directive at runtime. This function allows you to specify which errors should be reported and displayed. By calling this function and passing the desired error reporting level as a parameter, you can control the types and levels of errors that are displayed in your application. This function is commonly used to suppress or display specific types of errors based on the needs of the application or the preferences of the developer.

Submit
21. $rest = substr("abcdef", -3, 1); ?> What dalue does $rest contain?

Explanation

The substr() function is used to extract a portion of a string. In this case, the string "abcdef" is being passed to the function. The second parameter, -3, indicates that the extraction should start from the third character from the end of the string. The third parameter, 1, specifies that only one character should be extracted. Therefore, the value of $rest will be the character "d".

Submit
22. In PHP, the keyword used to ensure a method in a child class calls the method with the same name in the parent class is ______________.

Explanation

In PHP OOP, the parent keyword is used within a method in a child class to call a method with the same name from the parent class. This allows the child class to extend or modify the behavior of the parent class method while still utilizing the functionality defined in the parent class. For example: parent::methodName().

Submit
23. $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data); echo $user; echo $pass; ?> What set of value it will give?

Explanation

The given code uses the explode function to split the string stored in the variable $data using ":" as the delimiter. The resulting values are assigned to the variables $user, $pass, $uid, $gid, $gecos, $home, and $shell. The echo statements then display the values of $user and $pass. Therefore, the code will output "foo" for the value of $user and "*" for the value of $pass.

Submit
24. $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } echo $value; unset($value); ?> What is the result?

Explanation

The code initializes an array $arr with values 1, 2, 3, and 4. Then, it enters a foreach loop where it multiplies each value in the array by 2. Since the variable $value is passed by reference using the "&" symbol, the original values in the array are modified. After the loop, the value of $value is echoed, which is the last value in the array after being multiplied by 2, resulting in 8. Finally, the unset() function is used to remove the reference to $value.

Submit
25. What is the correct way to add comments to PHP code?

Explanation

The correct way to add comments to PHP code is by using the "#" symbol. Comments are used to add notes or explanations to the code that are not executed by the interpreter. In PHP, the "#" symbol is used to start a single-line comment. Anything after the "#" symbol on the same line is considered a comment and is ignored by the interpreter.

Submit
26. Mysql_connect() takes parameters:

Explanation

The correct answer is (string, string, string, bool, int). The mysql_connect() function in PHP is used to establish a connection to a MySQL database. It takes five parameters: the first parameter is the server name or IP address, the second parameter is the username, the third parameter is the password, the fourth parameter is a boolean value indicating whether to use a new link or an existing one, and the fifth parameter is an optional integer value specifying the client flags.

Submit
27. In PHP, which of the following statements about visibility and inheritance in OOP is true?

Explanation

In PHP OOP, a protected property in a parent class can be accessed directly by its child class. Protected members are accessible within their own class and any class that inherits from it, enabling child classes to use and modify these properties and methods. This promotes encapsulation while allowing inheritance hierarchies to share and manipulate internal state and behavior safely.

Submit
28. $array = array('first' => null, 'second' => 4); isset($array['first']); ?> Choose the correct answer.

Explanation

The code is checking if the key 'first' exists in the array $array using the isset() function. Since the value of 'first' is null, isset() will return false because null is considered as not set. Therefore, the correct answer is "isset returns false".

Submit
29. Which function activates the circular reference collector?

Explanation

The correct answer is gc_enable(). This function activates the garbage collector in PHP. The garbage collector is responsible for identifying and freeing up memory that is no longer in use by the program. By enabling the garbage collector, circular references, which are references between objects that prevent them from being garbage collected, can be detected and resolved. This helps to prevent memory leaks and optimize memory usage in PHP programs.

Submit
30. Which of the following keywords is used to declare a class in PHP?

Explanation

The class keyword is used to define a class in PHP, which serves as a blueprint for creating objects. Classes encapsulate data (properties) and methods (functions) that operate on that data. The other options are incorrect because they serve different purposes in PHP:

function: Used to define a function or method.

object: An instance of a class.

new: Used to create a new object from a class.

Submit
31. As of PHP 5.3.0, PHP implements a feature which can be used to reference the called class in a context of static inheritance. How is it called?

Explanation

This is feedback Explanation of question

Submit
32. Which of the following statements accurately describes the concept of inheritance in PHP object-oriented programming (OOP)?

Explanation

In PHP OOP, inheritance is a fundamental concept where a child class can inherit properties and methods from a single parent class. This allows for code reusability and the creation of class hierarchies. Child classes can extend the functionality of the parent class by adding new methods or overriding existing ones, but they can inherit from only one parent class at a time, adhering to single inheritance principle.

Submit
33. $Link_ID = odbc_connect("DSN", "user", "pass"); $Return = odbc_autocommit($Link_ID, FALSE); ?> will result in:

Explanation

The given code is establishing a connection to a database using the ODBC driver and then setting the autocommit mode to FALSE. This means that any changes made to the database will not be automatically committed, and the developer will have to explicitly commit the changes. Therefore, the correct answer is "set autocommit on", indicating that the autocommit mode is turned on.

Submit
34. PHP scripts are surrounded by delimiters, which?

Explanation

PHP scripts are surrounded by delimiters, which are . These delimiters are used to indicate the beginning and end of PHP code within an HTML document. The opening delimiter is used to end the PHP code. The PHP code within these delimiters is then processed by the server before the HTML is sent to the client's browser.

Submit
35. Which function gets information about the last error that occurred?

Explanation

The function "error_get_last()" is used to obtain information about the last error that occurred. It returns an associative array with four elements: "type" (the type of the error), "message" (the error message), "file" (the file where the error occurred), and "line" (the line number where the error occurred). This function is commonly used in error handling to retrieve and log error information for debugging purposes.

Submit
36. $i = 0; while ($i <= 0): echo $i; $i++; endwhile; ?> What value will it give?

Explanation

In this script, the while loop will execute as long as the condition $i <= 0 is true. However, since $i is initially set to 0, and the condition is <= 0, the loop will execute only once because after the first iteration $i becomes 1, and the condition becomes false.

So, the output of this script will be: 0

Submit
37. What Value $bodytag contains?

Explanation



The % signs before and after the word "black" suggest that the value is meant to be used as a pattern or placeholder in a search query. This indicates that the actual text within the body tag could include any characters before and after the word "black".
Submit
38. Which function generates a user-level error/warning/notice message?

Explanation

The trigger_error() function generates a user-level error/warning/notice message.

Submit
View My Results

Quiz Review Timeline (Updated): Nov 25, 2024 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Nov 25, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Feb 01, 2012
    Quiz Created by
    Gadhavitechnolog
Cancel
  • All
    All (38)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
In PHP You can use both single quotes (' ') and double quotes...
All variables in PHP are prefixed with special symbol, which?
How do You get information from a form that is submitted using the...
Can PHP be executed in Command line?
What is the correct way to connect to MySQL database?
Include files must have file extension *.inc
When an object is cloned, PHP 5 will perform a shallow copy of all of...
What does PHP stand for?
What is the correct way to add 1 to the variable?
Which one of theese variables has illegal name?
$string = 'Hello World!';...
True or False: In PHP, a class can have multiple constructors.
Does anonymous functions allow creation of functions without...
For ($i = 1; ; $i++) {...
If only one array is given in array_merge() as parameter and the array...
Is it possible to run several versions of PHP at the same time?
$fruits = array("lemon", "orange",...
What's correct result of below code?...
$email = '[email protected]';...
Which function sets the error_reporting directive at runtime?
$rest = substr("abcdef", -3, 1);...
In PHP, the keyword used to ensure a method in a child class calls the...
$data = "foo:*:1023:1000::/home/foo:/bin/sh";...
$arr = array(1, 2, 3, 4);...
What is the correct way to add comments to PHP code?
Mysql_connect() takes parameters:
In PHP, which of the following statements about visibility and...
$array = array('first' => null, 'second' => 4);...
Which function activates the circular reference collector?
Which of the following keywords is used to declare a class in PHP?
As of PHP 5.3.0, PHP implements a feature which can be used to...
Which of the following statements accurately describes the concept of...
$Link_ID = odbc_connect("DSN", "user",...
PHP scripts are surrounded by delimiters, which?
Which function gets information about the last error that occurred?
$i = 0; ...
What Value $bodytag contains?
Which function generates a user-level error/warning/notice message?
Alert!

Advertisement