PHP Programming 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 Woanleng
W
Woanleng
Community Contributor
Quizzes Created: 1 | Total Attempts: 6,085
Questions: 40 | Attempts: 6,085

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. 

    Complete the PHP statement to retrieve data from a FORM with an input named ‘weight’ that is submitted using the “post” method.$myData = __________ ;

    Correct Answer
    $_POST['weight']
    $_POST["weight"]
    $_POST ['weight']
    $_POST ["weight"]
    Explanation
    The correct answer is $_POST['weight'],$_POST["weight"],$_POST ['weight'],$_POST ["weight"]. These are all valid ways to retrieve data from a form with an input named 'weight' that is submitted using the "post" method in PHP. The $_POST superglobal variable is used to access form data that is submitted using the "post" method. By using the input name as the key in the $_POST array, we can retrieve the value that was entered in the 'weight' input field of the form.

    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. 

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

    Correct Answer
    submit
    Explanation
    The given lines of code are part of an HTML document and the missing word is "submit". This suggests that the code is creating a button or input element with the label "submit".

    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. 

    Complete the for-loop that prints all multiples of 3 between 10 and 51 (both inclusive), in descending order (that is, starting from 51).  

    Correct Answer
    $i%3 == 0
    $i%3==0
    $i % 3 == 0
    Explanation
    The correct answer is $i % 3 == 0. This is the correct syntax for checking if a number is a multiple of 3. The modulus operator (%) returns the remainder when the number is divided by 3. If the remainder is 0, then the number is divisible by 3 and therefore a multiple of 3. This condition will be used in the for-loop to print all the multiples of 3 between 10 and 51 in descending order.

    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. 

    Complete the PHP statement to retrieve data from a FORM with an input named ‘durationInHour’ that is submitted using the “post” method; convert it to seconds.$ durationInSeconds = $_POST['_______________;

    Correct Answer
    durationInHour'] * 3600
    durationInHour']*3600
    durationInHour' ] * 3600
    Explanation
    The correct answer is $_POST['durationInHour'] * 3600. This PHP statement retrieves the value submitted from a form input named 'durationInHour' using the "post" method. It then multiplies this value by 3600 to convert it to seconds and assigns the result to the variable $durationInSeconds.

    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. 

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

    Correct Answer
    $rowData [2]
    $rowData[2]
    $rowData [2 ]
    $rowData[2 ]
    Explanation
    The correct answer is $rowData[2],$rowData[2],$rowData[2],$rowData[2]. In PHP, square brackets are used to access elements in an array. In this case, $rowData is an array and [2] is used to retrieve the value at index 2. The spaces before or after the square brackets do not affect the code execution, as PHP ignores whitespace. Therefore, all four options are valid ways to access the value at index 2 in the $rowData array.

    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
  • Aug 11, 2023
    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.