PHP OOP Practice Quiz

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Gadhavitechnolog
G
Gadhavitechnolog
Community Contributor
Quizzes Created: 1 | Total Attempts: 4,808
Questions: 38 | Attempts: 4,808

SettingsSettingsSettings
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. In 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!


Questions and Answers
  • 1. 

    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?

    • A.

      Static class bindings

    • B.

      Static bindings

    • C.

      Late static bindings

    • D.

      Late bindings

    • E.

      Class bindings

    Correct Answer
    C. Late static bindings
    Explanation
    This is feedback Explanation of question

    Rate this question:

  • 2. 

    Which of the following statements accurately describes the concept of inheritance in pHP object-oriented programming (OOP)?

    • A.

      Inheritance allows a child class to inherit properties and methods from multiple parent classes simultaneously.

    • B.

      Inheritance is a mechanism by which a class can inherit properties and methods from one parent class only.

    • C.

      Inheritance is primarily used to restrict access to certain properties and methods within a class hierarchy.

    • D.

      Inheritance facilitates the creation of entirely new properties and methods in child classes, distinct from those in parent classes.

    Correct Answer
    B. Inheritance is a mechanism by which a class can inherit properties and methods from one parent class only.
    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.

    Rate this question:

  • 3. 

    Which function activates the circular reference collector?

    • A.

      Gc_rc()

    • B.

      Gc_enable()

    • C.

      Crc_enable()

    • D.

      Gc_activate()

    • E.

      Crc_activate()

    Correct Answer
    B. Gc_enable()
    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.

    Rate this question:

  • 4. 

    Does anonymous functions allow creation of functions without specifying their name?

    • A.

      Yes

    • B.

      No

    • C.

      There is nothing like 'anonymouss functions'

    Correct Answer
    A. Yes
    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.

    Rate this question:

  • 5. 

    Mysql_connect() takes parameters:

    • A.

      (string, string, string, bool, int)

    • B.

      (string, string)

    • C.

      (string, string, string, bool)

    • D.

      (string, string, string, int, bool)

    • E.

      (string, string, string, bool, bool)

    Correct Answer
    A. (string, string, string, bool, int)
    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.

    Rate this question:

  • 6. 

    What does pHP stand for?

    • A.

      PHP: Hypertext Processor

    • B.

      Private Home Page

    • C.

      Personal Home Page

    • D.

      Personal Hypertext Processor

    • E.

      PHP: Personal Hypertext Processor

    Correct Answer
    A. pHP: Hypertext Processor
    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.

    Rate this question:

  • 7. 

    Is it possible to run several versions of pHP at the same time?

    • A.

      Yes

    • B.

      No

    • C.

      Yes, but only commercial editions of PHP

    Correct Answer
    A. Yes
    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.

    Rate this question:

  • 8. 

    pHP scripts are surrounded by delimiters, which?

    • A.

    • B.

      ...

    • C.

      or

    • D.

    • E.

    Correct Answer
    C. or
    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.

    Rate this question:

  • 9. 

    All variables in pHP are prefixed with special symbol, which?

    • A.

      @

    • B.

      #

    • C.

      $

    • D.

      \

    • E.

      %

    Correct Answer
    C. $
    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.

    Rate this question:

  • 10. 

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

    • A.

      Request.QueryString;

    • B.

      Request.Form;

    • C.

      Get_request();

    • D.

      $_GET[];

    • E.

      Request(GET);

    Correct Answer
    D. $_GET[];
    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.

    Rate this question:

  • 11. 

    In pHP You can use both single quotes (' ') and double quotes (" ") for strings:

    • A.

      True

    • B.

      False

    • C.

      None from above

    Correct Answer
    A. True
    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.

    Rate this question:

  • 12. 

    Include files must have file extension *.inc

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    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.

    Rate this question:

  • 13. 

    What is the correct way to connect to MySQL database?

    • A.

      Connect_mysql("localhost");

    • B.

      Mysql_open("localhost");

    • C.

      Dbopen("localhost");

    • D.

      Mysql_connect("localhost");

    • E.

      Depends on MySQL server version

    Correct Answer
    D. Mysql_connect("localhost");
    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.

    Rate this question:

  • 14. 

    What is the correct way to add 1 to the variable?

    • A.

      $variable =+ 1;

    • B.

      Variable =+1;

    • C.

      Variable++;

    • D.

      $variable++;

    • E.

      Variable +=1;

    Correct Answer
    D. $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++.

    Rate this question:

  • 15. 

    What is the correct way to add comments to pHP code?

    • A.

      */.../*

    • B.

      *\...\*

    • C.

    • D.

      #...

    • E.

      …

    Correct Answer
    D. #...
    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.

    Rate this question:

  • 16. 

    Which one of theese variables has illegal name?

    • A.

      $myVar

    • B.

      $my_Var

    • C.

      $my-Var

    • D.

      All of them

    • E.

      None of them

    Correct Answer
    C. $my-Var
    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.

    Rate this question:

  • 17. 

    Can pHP be executed in Command line?

    • A.

      Yes

    • B.

      No

    • C.

      Only on linux

    • D.

      Only on Windows

    Correct Answer
    A. Yes
    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.

    Rate this question:

  • 18. 

    What Value $bodytag contains?

    • A.

      Body text='%black%'

    • B.

      Body text='black'

    • C.

      Body text="black"

    • D.

      None

    Correct Answer
    A. Body text='%black%'
    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".

    Rate this question:

  • 19. 

    $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?

    • A.

      Foo:*1023

    • B.

      Foo*

    • C.

      *foo

    • D.

      Foo

    • E.

      *

    Correct Answer
    B. Foo*
    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.

    Rate this question:

  • 20. 

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

    • A.

      Fruits[0] = lemon fruits[1] = orange fruits[2] = banana fruits[3] = apple

    • B.

      Apple banana lemon orange

    • C.

      Fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange

    • D.

      Fruits[0] = apple fruits[1] = banana fruits[2] = orange fruits[3] = lemon

    • E.

      None

    Correct Answer
    C. Fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange
    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".

    Rate this question:

  • 21. 

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

    • A.

      Set autocommit on

    • B.

      Set autocommit off

    • C.

      Error in code

    • D.

      Nothing

    Correct Answer
    A. Set autocommit on
    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.

    Rate this question:

  • 22. 

    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"; } ?>

    • A.

      Apple banana lemon a = orange

    • B.

      C = apple b = banana d = lemon a = orange

    • C.

      A = apple b = banana c = lemon d = orange

    • D.

      Apple banana lemon orange

    • E.

      Apple banana lemon orange

    Correct Answer
    B. C = apple b = banana d = lemon a = orange
    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.

    Rate this question:

  • 23. 

    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.

    • A.

      The above sentense is true

    • B.

      The above sentense is false

    • C.

      Described situation will result in error

    Correct Answer
    A. The above sentense is true
    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.

    Rate this question:

  • 24. 

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

    • A.

      Isset returns true

    • B.

      Isset returns false

    • C.

      Error in code

    Correct Answer
    B. Isset returns false
    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".

    Rate this question:

  • 25. 

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

    Correct Answer
    D. @example.com
    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".

    Rate this question:

  • 26. 

    In pHP, which of the following statements about visibility and inheritance in OOP is true?

    • A.

      A private method in a parent class can be accessed directly by its child class.

    • B.

      A protected property in a parent class can be accessed directly by its child class.

    • C.

      A public method in a parent class cannot be overridden by a child class.

    • D.

      A static property cannot be inherited by a child class.

    Correct Answer
    B. A protected property in a parent class can be accessed directly by its child class.
    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.

    Rate this question:

  • 27. 

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

    • A.

      12345

    • B.

      1

    • C.

      0

    • D.

      Displays nothing

    • E.

      Error in code

    Correct Answer
    C. 0
    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

    Rate this question:

  • 28. 

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

    • A.

      12345678910

    • B.

      1234567891011

    • C.

      None

    Correct Answer
    A. 12345678910
    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.

    Rate this question:

  • 29. 

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

    • A.

      NULL

    • B.

      0

    • C.

      8

    • D.

      Error in code

    Correct Answer
    C. 8
    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.

    Rate this question:

  • 30. 

    Which function gets information about the last error that occurred?

    • A.

      Getlasterror()

    • B.

      Get_error()

    • C.

      Get_last_error()

    • D.

      Error_get_last()

    Correct Answer
    D. Error_get_last()
    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.

    Rate this question:

  • 31. 

    Which function sets the error_reporting directive at runtime?

    • A.

      Set_error()

    • B.

      Error_reporting()

    • C.

      Set_error_reporting()

    • D.

      Set_reporting_errors()

    Correct Answer
    B. Error_reporting()
    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.

    Rate this question:

  • 32. 

    Which function generates a user-level error/warning/notice message?

    • A.

      Trigger_error()

    • B.

      Error_log()

    • C.

      Report_error()

    • D.

      Error_report()

    • E.

      Log_error()

    Correct Answer
    A. Trigger_error()
    Explanation
    The trigger_error() function generates a user-level error/warning/notice message.

    Rate this question:

  • 33. 

    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.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 34. 

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

    • A.

      E

    • B.

      A

    • C.

      F

    • D.

      D

    • E.

      C

    Correct Answer
    D. D
    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".

    Rate this question:

  • 35. 

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

    • A.

      Outputs: Hello World!

    • B.

      Outputs: "earth" not found in string

    • C.

      No output

    • D.

      Gives error in code

    Correct Answer
    B. Outputs: "earth" not found in string
    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".

    Rate this question:

  • 36. 

    India's National Animal?

    • A.

      Dog

    • B.

      Line

    • C.

      Elephant

    Correct Answer
    B. Line
  • 37. 

    True or False: In pHP, a class can have multiple constructors.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    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.

    Rate this question:

  • 38. 

    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 ______________.

    Correct Answer
    parent, Parent
    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().

    Rate this question:

Quiz Review Timeline +

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

  • Current Version
  • Aug 02, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Feb 01, 2012
    Quiz Created by
    Gadhavitechnolog
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.