PHP Programming Quiz

Approved & Edited by ProProfs 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 Woanleng
W
Woanleng
Community Contributor
Quizzes Created: 1 | Total Attempts: 6,097
Questions: 40 | Attempts: 6,097

SettingsSettingsSettings
PHP Programming Quiz - Quiz

Republic Polytechnic Students: You need to set aside 60 mins to complete this quiz. There will be 30 questions in the quiz; the purpose is to test your ability to apply your problem solving and PHP programming skills.


Questions and Answers
  • 1. 

    Rearrange these steps in the correct order. 1. Connect to Database Server2. Close database connection3. Execute SQL query4. Retrieve HTML form data5. Build SQL statement

    • A.

      1,4,3,2,5

    • B.

      5,4,3,2,1

    • C.

      3,4,5,2,1

    • D.

      1,4,5,3,2

    • E.

      4,5,3,1,2

    Correct Answer
    D. 1,4,5,3,2
    Explanation
    The correct order of the steps is as follows:
    1. Connect to Database Server - This step should come first as it establishes a connection to the database server.
    2. Retrieve HTML form data - After connecting to the database server, the next step is to retrieve the data entered in the HTML form.
    3. Build SQL statement - Once the form data is retrieved, it needs to be used to construct the SQL statement that will be executed.
    4. Execute SQL query - After building the SQL statement, it is executed to perform the desired operation on the database.
    5. Close database connection - Finally, the database connection is closed to free up resources and ensure proper termination of the connection.

    Rate this question:

  • 2. 

    In PHP, an ______ is a data structure that can store multiple values of the same data type, such as a list of numbers or strings.

    Correct Answer
    array, Array
    Explanation
    Arrays are versatile data structures in PHP that allow you to store collections of values under a single variable name. This is useful for organizing and managing related data, such as a list of products, user names, or test scores. Arrays can be accessed and manipulated using their index, which represents the position of each element within the array.

    Rate this question:

  • 3. 

    Consider the following scenario and answer the related questions:• XAMPP is in the c:\xampp\ folder of your local machine• The c:\xampp\htdocs\myProject\ folder contains a HTML document called postItem.htmlWhat is the URL to display postItem.html in your browser in the same machine?____________________________________________________

    Correct Answer
    http://localhost/myProject/postItem.html/
    http://localhost/myProject/postItem.html
    Explanation
    The correct answer is "http://localhost/myProject/postItem.html". This is because XAMPP is installed in the "c:\xampp\" folder, and the "postItem.html" file is located in the "c:\xampp\htdocs\myProject\" folder. To access the file through the browser on the same machine, we use the "localhost" address followed by the relative path to the file, which in this case is "/myProject/postItem.html".

    Rate this question:

  • 4. 

    Retrieve data from a FORM with an input named ‘distance’ that is submitted using the “post” method. Write ONE PHP statement to store the data in variable $gap.

    Correct Answer
    $gap = $_POST['distance'];
    $gap = $_POST['distance']
    $gap = $_POST ['distance'] ;
    $gap=$_POST['distance'];
    $gap=$_POST["distance"];
    Explanation
    The correct answer is $gap = $_POST['distance'];, $gap = $_POST['distance'], $gap = $_POST ['distance'] ;, $gap=$_POST['distance'];, $gap=$_POST["distance"];

    The given PHP statements are used to retrieve data from a form that is submitted using the "post" method. The input in the form is named 'distance'. The data from this input is stored in the variable $gap using the $_POST superglobal array. The different variations of the statement all achieve the same result of storing the submitted data in the variable $gap.

    Rate this question:

  • 5. 

    In PHP, a ______ is a block of code that performs a specific task and can be reused multiple times within a program.

    Correct Answer
    function, Function
    Explanation
    Functions are essential for organizing and modularizing PHP code. They encapsulate a set of instructions that perform a specific task, allowing you to break down complex programs into smaller, more manageable units. Functions can be called multiple times within a program, promoting code reusability and reducing redundancy.

    Rate this question:

  • 6. 

    The following 2 lines are part of a HTML document, fill in the blank: 

    Correct Answer
    form
    FORM
    Form
    Explanation
    The blank should be filled with "form" because HTML tags are case-insensitive, meaning that "form" and "FORM" are both valid tags. However, conventionally, HTML tags are written in lowercase, so "form" is the most appropriate choice. "Form" is also a valid tag, but it is less commonly used and may cause confusion.

    Rate this question:

  • 7. 

    The following lines are part of a HTML document for login screen:Fill in the blank so that user will not see the password being typed in:

    Correct Answer
    password
    PASSWORD
    Password
    Explanation
    To ensure that the password being typed in is not visible to the user, the "type" attribute of the input field should be set to "password". By setting this attribute, the characters entered by the user will be masked with asterisks or bullets, preventing anyone from seeing the actual characters. In this case, the lines "password", "PASSWORD", and "Password" should all have the "type" attribute set to "password" to hide the password as it is being typed.

    Rate this question:

  • 8. 

    Write ONE PHP statement to show this output using variable $gap (need to pay attention to spacing in the output):

    Correct Answer
    echo 'Gap: ' . $gap . ' cm
    ';
    echo "Gap: " . $gap . " cm
    ";
    echo 'Gap: '.$gap.' cm
    ';
    echo "Gap: ".$gap." cm
    ";
    echo "Gap: ".$gap." cm
    " ;
    Explanation
    The correct answer is to use any of the provided PHP statements to display the output "Gap: [value of $gap] cm". The statements concatenate the string "Gap: " with the value of the variable $gap and the string " cm" using the concatenation operator ".". The spacing between the strings and the variable is properly maintained in all the statements to ensure the correct output format.

    Rate this question:

  • 9. 

    In PHP, the ______ statement is used to output data to the browser, such as text or HTML code.

    Correct Answer
    echo, Echo
    Explanation
    The echo statement is a fundamental tool in PHP for displaying information to the user. It allows you to send data, such as text strings, variables, or the results of calculations, to the web browser, where it is rendered as part of the webpage.

    Rate this question:

  • 10. 

    Complete the while-loop that terminates (stops) when $myData reaches 33 (not inclusive).

    Correct Answer
    $myData < 33
    $myData<33
    $myData <= 32
    $myData<=32
    Explanation
    The while-loop will continue executing as long as the value of $myData is less than 33. Once $myData reaches 33, the condition $myData < 33 will evaluate to false, causing the loop to terminate.

    Rate this question:

  • 11. 

    Examine the loop below and write down the value of $x printed on the screen. Value of $x:

    Correct Answer
    32
    Explanation
    The value of $x is 32 because there is no code provided that would modify the value of $x. Therefore, the initial value of $x, which is 32, remains unchanged and is printed on the screen.

    Rate this question:

  • 12. 

    Examine the loop below and write down the value of $y printed on the screen. Value of $y:

    Correct Answer
    34
    Explanation
    The value of $y is 34 because there is no code provided to modify or update the value of $y within the loop. Therefore, the initial value of $y, which is 34, remains unchanged and is printed on the screen.

    Rate this question:

  • 13. 

    What is the return type of strcmp() function?

    Correct Answer
    int
    integer
    INT
    Integer
    INTEGER
    Explanation
    The return type of the strcmp() function is "int".

    Rate this question:

  • 14. 

    How many mandatory arguments (parameters) does fopen() function require?

    Correct Answer
    2
    TWO
    two
    Explanation
    The fopen() function requires two mandatory arguments (parameters) to be passed. The answer options provided, "2", "TWO", and "two", all indicate the same number of required arguments.

    Rate this question:

  • 15. 

    What is the required data type of the first argument (parameter) of fopen() function?

    Correct Answer
    string
    STRING
    str
    STR
    String
    Explanation
    The first argument (parameter) of the fopen() function should be a string data type.

    Rate this question:

  • 16. 

    What is the name of the PHP code that is executed when the Calculate button of this form is pressed?Answer:

    Correct Answer
    trainSvc.php
    Explanation
    The PHP code that is executed when the Calculate button of this form is pressed is trainSvc.php.

    Rate this question:

  • 17. 

    Complete the code according to comment provided: Answer:

    Correct Answer
    $cnter
    Explanation
    The code is expected to be completed by adding the variable `$cnter` in the designated location. This variable could be assigned any value or used in any way as required by the code.

    Rate this question:

  • 18. 

    In PHP, a ______ is a named container that stores a value, such as a number, string, or array.  

    Correct Answer
    variable, Variable
    Explanation
    Variables are fundamental building blocks in PHP programming. They allow you to store and manipulate data within your code, making it more dynamic and adaptable. By assigning values to variables, you can perform calculations, display information, and control the flow of your program.

    Rate this question:

  • 19. 

    The ‘c203project’ database has a table called ‘keyContacts’, with the following schema:What is the primary key field name?

    Correct Answer
    contact_id
    CONTACT_ID
    contact_ID
    Explanation
    The primary key field name in the 'keyContacts' table of the 'c203project' database is contact_id, CONTACT_ID, or contact_ID.

    Rate this question:

  • 20. 

    The ‘c203project’ database has a table called ‘keyContacts’, with the following schema:Complete the PHP statement to store the SQL statement in variable $query1 - Inserting ONE record into the table. _________________________ (contact_name, phone, email) VALUES ($name, $contactno, $eMail)";

    Correct Answer
    $query1 = "INSERT INTO keyContacts
    $query1="INSERT INTO keyContacts
    $query1 = "insert into keyContacts
    $query1="insert into keyContacts
    $query1= "INSERT INTO keyContacts
    Explanation
    $query1 = "INSERT INTO keyContacts (contact_name, phone, email) VALUES ($name, $contactno, $eMail)";

    Rate this question:

  • 21. 

    The ‘c203project’ database has a table called ‘keyContacts’, with the following schema:Complete the PHP statement to store the SQL statement in variable $queryCount - Find out total number of contacts.___________________________________________  FROM keyContacts";

    Correct Answer
    $queryCount = "SELECT COUNT(contact_id)
    $queryCount= "SELECT COUNT(contact_id)
    $queryCount ="SELECT COUNT(contact_id)
    $queryCount ="SELECT COUNT(*)
    $queryCount = "SELECT COUNT(*)
    Explanation
    $queryCount = "SELECT COUNT(*) FROM keyContacts";

    Rate this question:

  • 22. 

    The ‘c203project’ database is residing in server “sit.rp.sg”; it has a table called ‘keyContacts’. Complete the PHP statement to connect to the MySQL Server using user “admin” with password “pWd!”.$conn = __________________________________________________;

    Correct Answer
    mysqli_connect("sit.rp.sg", "admin", "pWd!", "c203project")
    mysqli_connect('sit.rp.sg', 'admin', 'pWd!', 'c203project')
    mysqli_connect("sit.rp.sg","admin","pWd!","c203project")
    mysqli_connect('sit.rp.sg','admin','pWd!','c203project')
    mysqli_connect ('sit.rp.sg', 'admin', 'pWd!', 'c203project')
    Explanation
    The correct answer is to use the PHP statement:

    mysqli_connect("sit.rp.sg", "admin", "pWd!", "c203project")

    This statement connects to the MySQL Server on the server "sit.rp.sg" using the username "admin" and password "pWd!". It also specifies the database "c203project" to be used.

    Rate this question:

  • 23. 

    Complete the PHP statement to execute the SQL query in variable $selectStr. Database connection is stored in variable $conn. $allRows = ______________________________;

    Correct Answer
    mysqli_query($conn,$selectStr)
    mysqli_query ($conn, $selectStr)
    mysqli_query($conn, $selectStr)
    mysqli_query ($conn,$selectStr)
    mysqli_query($conn, $selectStr )
    Explanation
    The correct answer is to use the mysqli_query() function to execute the SQL query stored in the variable $selectStr using the database connection stored in the variable $conn. This function is used to send a query to the database and returns a result set if the query is successful.

    Rate this question:

  • 24. 

    What is the result of execution of a failed INSERT SQL query?

    • A.

      0 (zero)

    • B.

      1

    • C.

      True

    • D.

      False

    • E.

      Result object

    Correct Answer
    D. False
    Explanation
    The result of executing a failed INSERT SQL query is False. This means that the query was not successful in inserting the data into the database. The False value indicates that the operation did not complete as intended and no changes were made to the database.

    Rate this question:

  • 25. 

    What is the result of execution of a successful SELECT SQL query?

    • A.

      0 (zero)

    • B.

      1

    • C.

      True

    • D.

      False

    • E.

      Result object

    Correct Answer
    E. Result object
    Explanation
    The result of executing a successful SELECT SQL query is a result object. This object contains the retrieved data from the database based on the conditions specified in the query. It can be used to access and manipulate the returned data in the application code.

    Rate this question:

  • 26. 

    Choose the most appropriate statement that will check for the successful execution of a SELECT SQL query.

    • A.

      $link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die (mysqli_connect_error());

    • B.

      If ($row=mysqli_fetch_assoc($result))

    • C.

      If (mysqli_query($link, "SELECT * FROM keyContacts"))

    • D.

      $sql = "SELECT * FROM keyContacts";

    • E.

      $status = mysqli_query($link,$query);

    Correct Answer
    C. If (mysqli_query($link, "SELECT * FROM keyContacts"))
    Explanation
    The given answer is the most appropriate statement to check for the successful execution of a SELECT SQL query. It uses the mysqli_query() function to execute the query "SELECT * FROM keyContacts" using the connection variable $link. If the query is executed successfully, it will return a result object, indicating that the query was successful.

    Rate this question:

  • 27. 

    Which function will get a result row as a numerically indexed array after the successful execution of a SELECT SQL query?

    • A.

      Mysqli_fetch_row($allRows)

    • B.

      While (mysqli_fetch_rows($allRows))

    • C.

      While (mysqli_fetch_assoc($allRows))

    • D.

      If (mysqli_fetch_assoc($allRows))

    • E.

      Mysqli_fetch_data($allRows)

    Correct Answer
    A. Mysqli_fetch_row($allRows)
    Explanation
    The function mysqli_fetch_row($allRows) will return a numerically indexed array containing the result row after executing a successful SELECT SQL query. This function fetches the next row from the result set and returns it as an array, where the array indices correspond to the column numbers.

    Rate this question:

  • 28. 

    The ‘c203project’ database has a table called ‘keyContacts’, with the following schema:Complete the PHP statement to store the SQL statement in a string variable that will List out the name of all contacts with email containing “rp.sg” at the end.$querySel = "SELECT contact_name _______________________________ ;

    Correct Answer
    FROM keyContacts WHERE email LIKE '%rp.sg'"
    FROM keyContacts WHERE email LIKE '%rp.sg' "
    from keyContacts where email like '%rp.sg' "
    FROM KEYCONTACTS WHERE email LIKE '%rp.sg' "
    FROM KEYCONTACTS WHERE email LIKE '%rp.sg'"
    Explanation
    $querySel = "SELECT contact_name FROM keyContacts WHERE email LIKE '%rp.sg'";

    Rate this question:

  • 29. 

    The following lines are part of a PHP code to retrieve information from database; the result of execution of a successful SELECT SQL query is stored in a variable $allRows:Fill in the blank:

    Correct Answer
    mysqli_fetch_array($allRows)
    mysqli_fetch_row($allRows)
    mysqli_fetch_array ($allRows)
    mysqli_fetch_row ($allRows)
    mysqli_fetch_row($allRows )
    Explanation
    The correct answer is mysqli_fetch_array($allRows), mysqli_fetch_row($allRows), mysqli_fetch_array ($allRows), mysqli_fetch_row ($allRows), mysqli_fetch_row($allRows).

    Rate this question:

  • 30. 

    In a numerically indexed array, what is the index number to access the 2nd item?

    • A.

      2

    • B.

      0

    • C.

      3

    • D.

      1

    • E.

      '2nd'

    Correct Answer
    D. 1
    Explanation
    The index number to access the 2nd item in a numerically indexed array is 1. In programming, arrays are typically zero-indexed, meaning that the first item in the array is accessed using an index of 0. Therefore, the 2nd item would be accessed using an index of 1.

    Rate this question:

  • 31. 

    Complete the code according to comment provided:

    Correct Answer
    array
    Explanation
    The correct answer is "array" because the prompt asks to complete the code according to the provided comment, which is "array". This suggests that the missing code should be the word "array" to fulfill the requirement.

    Rate this question:

  • 32. 

    The three blanks in the code have the same answer: The missing code is :

    Correct Answer
    =>
    Explanation
    The missing code is the arrow operator (=>) in JavaScript. This operator is used in arrow function expressions to define anonymous functions. It allows for a more concise syntax compared to traditional function expressions.

    Rate this question:

  • 33. 

    The following lines are part of a PHP code to retrieve information from database; complete the code according to comment provided:

    Correct Answer
    $record['contact_name']
    $record ['contact_name']
    $record["contact_name"]
    $record ["contact_name"]
    Explanation
    The given answer provides all possible correct ways to access the value of the "contact_name" field from the $record array in PHP. The code can be completed by using any of these four options: $record['contact_name'], $record ['contact_name'], $record["contact_name"], or $record ["contact_name"]. These options demonstrate the flexibility of PHP in allowing different syntaxes for accessing array values.

    Rate this question:

  • 34. 

    In PHP, ______ is the process of combining two or more strings together to create a new string.

    Correct Answer
    concatenation, Concatenation
    Explanation
    Concatenation is a common operation in PHP used to join strings together. This is often used to create dynamic output, such as combining a user's name with a greeting or constructing a file path. The concatenation operator in PHP is the period (.), which is used to join two or more strings into a single string.

    Rate this question:

  • 35. 

    The following lines are part of a PHP code to register new user into a database; complete the code according to comment provided:

    Correct Answer
    sha1($passw)
    sha1 ($passw)
    sha1($passw )
    sha1 ($passw )
    sha1 ( $passw )
    Explanation
    The code is using the sha1 function to encrypt the password before storing it in the database. The function takes the variable $passw as input and returns the encrypted value. The correct answer includes all variations of the sha1 function calls with different spacing between the function name and the variable.

    Rate this question:

  • 36. 

    The following lines are part of a PHP code to authenticate user; complete the code according to comment provided:

    Correct Answer
    mysqli_num_rows($ans
    mysqli_num_rows ($ans
    mysqli_num_rows($ans)
    mysqli_num_rows ($ans)
    mysqli_num_rows ( $ans
    Explanation
    The correct answer is mysqli_num_rows($ans) because the mysqli_num_rows function is used to get the number of rows returned from a SELECT statement. In this case, the variable $ans is likely to hold the result of a database query, and mysqli_num_rows($ans) will return the number of rows in that result.

    Rate this question:

  • 37. 

    The following lines are part of a PHP code to authenticate user; complete the code according to comment provided:

    Correct Answer
    session_start()
    session_start ( )
    session_start ()
    Explanation
    The correct answer is "session_start(), session_start ( ), session_start ()".

    The code is incomplete and requires the session_start() function to be called in order to start or resume a session. The function is called with empty parentheses as it does not require any parameters. The code includes three variations of calling the session_start() function, with and without spaces between the function name and parentheses. All three variations are valid and will achieve the same result of starting the session.

    Rate this question:

  • 38. 

    The following lines are part of a PHP code to set session information for an authenticated user; complete the code according to comment provided:

    Correct Answer
    $_SESSION
    Explanation
    The given correct answer is $_SESSION because it is the superglobal variable in PHP that is used to store session data. In this code, the $_SESSION variable is being used to set session information for an authenticated user. It allows the user's data to be stored and accessed across multiple pages during their session on the website.

    Rate this question:

  • 39. 

    The following lines are part of a PHP code to retrieve session information for an authenticated user; complete the code according to comment provided:

    Correct Answer
    isset
    Explanation
    The "isset" function in PHP is used to check if a variable is set and not null. In this context, it is being used to check if the session information for an authenticated user exists. By using the "isset" function, the code can determine whether the session information is available or not, allowing the program to proceed accordingly.

    Rate this question:

  • 40. 

    The following 2 lines are part of a HTML document , fill in the blank:

    Correct Answer
    enctype
    Explanation
    The "enctype" attribute is used in HTML forms to specify how the form data should be encoded before it is sent to the server for processing. It is typically used in conjunction with the "method" attribute, which specifies the HTTP method to be used for submitting the form. The "enctype" attribute can take different values, such as "application/x-www-form-urlencoded" or "multipart/form-data", depending on the encoding type required for the form data.

    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
  • Jan 13, 2025
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 06, 2009
    Quiz Created by
    Woanleng
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.