Full-Course Mock Papers

5 mock papers of 41 questions each – one question from every chapter and the project. They are the same papers as Part VII of the e-book. Answer all questions and press Submit Test to see your score; each answer has an explanation and a link to the chapter it comes from.

41 questions · 1 mark each Time taken: 00:00 Answered: 0 / 41 Printable papers (PDF)
Mock Paper 4

Q1. MySQL error 1062 means:

Show Answer

Correct Answer : B. Duplicate entry
A UNIQUE value already exists. (From Chapter 35 : PHP and MySQLi – Object-Oriented Style)

Q2. PHP: Hypertext Preprocessor is an example of a:

Show Answer

Correct Answer : A. Recursive acronym
The acronym PHP appears inside its own expansion, so it is recursive. (From Chapter 1 : AMP Basics – Apache, MySQL and PHP)

Q3. Which is a valid variable name?

Show Answer

Correct Answer : A. $_first_name
A variable starts with $ followed by a letter or underscore; hyphens are not allowed. (From Chapter 4 : Constants, Variables and Data Types)

Q4. Which readyState value means the response is complete?

Show Answer

Correct Answer : D. 4
4 = Complete/Loaded. (From Chapter 39 : PHP and AJAX)

Q5. Which is an arrow function?

Show Answer

Correct Answer : A. fn($x) => $x * 2
Arrow functions use the fn keyword and =>. (From Chapter 10 : User-Defined Functions)

Q6. $link->insert_id is the OOP equivalent of:

Show Answer

Correct Answer : C. mysql_insert_id()
It returns the last AUTO_INCREMENT id. (From Chapter 27 : MySQL Functions and their MySQLi Equivalents)

Q7. Which statement imports a function from a namespace?

Show Answer

Correct Answer : A. use function MyProject\connect;
Functions are imported with use function. (From Chapter 16 : PHP Namespaces)

Q8. Which tag correctly starts a portable PHP block?

Show Answer

Correct Answer : D. <?php
<?php always works; <? needs short_open_tag enabled. (From Chapter 3 : PHP – Hypertext Preprocessor: Introduction and Syntax)

Q9. Which function protects output against XSS?

Show Answer

Correct Answer : D. htmlspecialchars()
It converts special HTML characters into entities. (From Chapter 11 : PHP Form Handling)

Q10. Which function deletes all session data on the server?

Show Answer

Correct Answer : B. session_destroy()
session_destroy() destroys the session. (From Chapter 31 : State Management – Sessions)

Q11. A class can implement:

Show Answer

Correct Answer : A. Many interfaces
implements accepts a comma-separated list. (From Chapter 18 : Object-Oriented PHP – Abstraction, Overloading, Iteration and Patterns)

Q12. Which function splits a string into an array by a separator?

Show Answer

Correct Answer : B. explode()
explode() returns an array of pieces. (From Chapter 29 : String, Variable and Miscellaneous Functions)

Q13. The default port of MySQL/MariaDB in XAMPP is:

Show Answer

Correct Answer : B. 3306
MariaDB listens on TCP port 3306; Apache uses 80 and 443. (From Chapter 2 : XAMPP – Installation, Configuration and Use)

Q14. A protected property can be accessed from:

Show Answer

Correct Answer : C. The declaring class and its parent/child classes
protected allows access within the class hierarchy. (From Chapter 17 : Object-Oriented PHP – Classes and Objects)

Q15. The PHP 8 match expression compares values using:

Show Answer

Correct Answer : A. ===
match uses strict identity comparison. (From Chapter 6 : Conditional Statements)

Q16. What is the index of "BMW" in array("Saab", "Volvo", "BMW")?

Show Answer

Correct Answer : A. 2
Numeric arrays start at index 0. (From Chapter 9 : PHP and Arrays)

Q17. Which format character prints the full name of the day?

Show Answer

Correct Answer : D. l
Lowercase L gives Sunday … Saturday. (From Chapter 24 : Date, Time and Calendar Functions)

Q18. In PHP 8, both Exception and Error implement:

Show Answer

Correct Answer : B. Throwable
Catching Throwable catches both. (From Chapter 20 : Error and Exception Handling)

Q19. In XAMPP, mailToDisk saves messages in:

Show Answer

Correct Answer : B. C:\xampp\mailoutput
Messages are written as files to the mailoutput folder. (From Chapter 15 : PHP and E-Mail)

Q20. $name = $_GET['name'] ?? 'Guest'; uses the:

Show Answer

Correct Answer : D. Null coalescing operator
?? returns the right value when the left one is missing or null. (From Chapter 5 : Operators and Expressions)

Q21. What does continue do?

Show Answer

Correct Answer : C. Skips to the next iteration
continue skips the rest of the current iteration. (From Chapter 7 : Looping Statements)

Q22. What prevents a comment from being posted twice when the page is refreshed?

Show Answer

Correct Answer : D. The Post/Redirect/Get pattern
After the POST the browser is redirected to a GET page. (From Project 1 : Project – A Mini Content Management System with PHP and MySQLi)

Q23. In MVC, the component that contains business logic and database access is the:

Show Answer

Correct Answer : C. Model
The model handles data and rules. (From Chapter 19 : PHP and MVC (Model–View–Controller))

Q24. Which mode opens a file for writing and erases its existing content?

Show Answer

Correct Answer : D. w
w truncates the file (or creates it). (From Chapter 14 : PHP and the File System)

Q25. Which function moves to the next row of an ODBC result?

Show Answer

Correct Answer : C. odbc_fetch_row()
It returns true while rows remain. (From Chapter 37 : Database Support using ODBC)

Q26. Which of these is a PHP-based CMS?

Show Answer

Correct Answer : B. WordPress
WordPress is written in PHP and uses MySQL/MariaDB. (From Chapter 40 : Content Management System (CMS) – Concepts)

Q27. Which function parses a line of a CSV file into an array?

Show Answer

Correct Answer : A. fgetcsv()
fgetcsv() reads and parses one line from a file handle. (From Chapter 25 : Class/Object, Function Handling, Directory and File Functions)

Q28. In the procedural style, the connection is passed to mysqli_query() as the:

Show Answer

Correct Answer : C. First argument
mysqli_query($conn, $sql). (From Chapter 34 : PHP and MySQLi – Procedural Style)

Q29. array_diff([1,2,3], [2,3,4]) returns:

Show Answer

Correct Answer : C. [1]
Only 1 is not present in the second array. (From Chapter 23 : Apache and Array Functions)

Q30. Which function returns the buffer contents and deletes the buffer?

Show Answer

Correct Answer : B. ob_get_clean()
ob_get_clean() = get contents + end clean. (From Chapter 28 : Network, Output Buffer, Configuration and Session Functions)

Q31. Which string syntax does NOT expand variables?

Show Answer

Correct Answer : D. Nowdoc
Nowdoc behaves like a single-quoted string. (From Chapter 8 : PHP and Strings)

Q32. In page2.php?d1=23&d2=45, the value of d2 is read with:

Show Answer

Correct Answer : A. $_GET['d2']
Query-string values are in $_GET (and $_REQUEST). (From Chapter 30 : State Management – Cookies and Query Strings)

Q33. Which statement stops the script when the file cannot be loaded?

Show Answer

Correct Answer : C. require
require raises a fatal error. (From Chapter 13 : Server Side Includes – include and require)

Q34. What does preg_match() return when no match is found?

Show Answer

Correct Answer : C. 0
It returns 0 (or false on error). (From Chapter 12 : Form Validation and Regular Expressions)

Q35. Which document is NOT well-formed?

Show Answer

Correct Answer : C. <a><b>x</a></b>
The tags are not properly nested. (From Chapter 38 : XML with PHP)

Q36. dechex(255) returns:

Show Answer

Correct Answer : D. "ff"
255 decimal is ff in hexadecimal. (From Chapter 26 : Error, HTTP, Mail and Mathematical Functions)

Q37. Which clause sorts the result of a SELECT?

Show Answer

Correct Answer : B. ORDER BY
ORDER BY field [ASC|DESC]. (From Chapter 32 : Database Support and MySQL Fundamentals)

Q38. Which filter calls a user-defined function?

Show Answer

Correct Answer : D. FILTER_CALLBACK
FILTER_CALLBACK with "options" => "functionName". (From Chapter 21 : PHP Filters)

Q39. Require valid-user allows:

Show Answer

Correct Answer : A. Any user listed in the AuthUserFile
Any authenticated user from the password file may enter. (From Chapter 22 : .htaccess Security)

Q40. Which method undoes all statements of the current transaction?

Show Answer

Correct Answer : D. rollback()
rollback() cancels the uncommitted changes. (From Chapter 36 : MySQLi Prepared Statements, Transactions and Security)

Q41. Which loop is normally used to read all rows of a result set?

Show Answer

Correct Answer : B. while ($row = …fetch_array($result))
Fetching returns false after the last row, ending the loop. (From Chapter 33 : PHP and MySQL – The Classic mysql API)