



















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The CPP 001 Certified PHP Professional Certification Exam Preparation guide is designed for developers seeking validation of PHP programming expertise. This guide covers core PHP syntax, object-oriented programming, database integration, security practices, and web application development. Concepts are explained through examples, coding scenarios, and best-practice discussions. Exam-style questions and structured review sections help candidates prepare thoroughly for certification. Ideal for web developers and backend programmers aiming to demonstrate professional PHP proficiency.
Typology: Exams
1 / 91
This page cannot be seen from the preview
Don't miss anything!




















































































Question 1. Which tag is the standard way to embed PHP code in an HTML file? A) `
B) $2ndUser C) $_SESSION D) $UserName Answer: B Explanation: Variable names cannot start with a number; they must begin with a letter or underscore. Question 5. In PHP, what does the construct $$var represent? A) A constant named by $var. B) A variable whose name is the value stored in $var. C) An error, because double‑dollar signs are illegal. D) A reference to the variable $var. Answer: B Explanation: Variable variables allow the value of $var to be used as the name of another variable. Question 6. Which function is used to define a constant that is case‑insensitive? A) define('PI', 3.14, true) B) const PI = 3.14; C) define('PI', 3.14, false) D) constant('PI', 3.14); Answer: A Explanation: The third parameter of define() specifies case‑insensitivity; setting it to true makes the constant case‑insensitive. Question 7. Which of the following is not a scalar type in PHP? A) Boolean B) Integer C) Array D) Float
B) while C) do…while D) for Answer: C Explanation: do…while executes the block first, then evaluates the condition, guaranteeing at least one execution. **Question 11. How many times will the loop below execute?
for($i = 0; $i < 5; $i++) { if($i == 2) break; } ```** A) 2 B) 3 C) 5 D) 0 Answer: B Explanation: The loop runs for `$i = 0, 1, 2`. When `$i` becomes 2, `break` stops the loop, so three iterations occur. **Question 12. What does the `continue` statement do inside a `foreach` loop?** A) Terminates the entire script. B) Skips the rest of the current iteration and proceeds to the next element. C) Restarts the loop from the first element. D) Exits the loop and returns to the calling function. Answer: B Explanation: `continue` jumps to the next iteration, bypassing any remaining code in the current iteration. ## Exam Preparation **Question 13. Which of the following correctly declares a function with a default argument value?** A) `function add($a, $b = 0) { return $a + $b; }` B) `function add($a = 0, $b) { return $a + $b; }` C) `function add($a, $b) = 0 { return $a + $b; }` D) `function add($a, $b) { $b = 0; return $a + $b; }` Answer: A Explanation: Default values must be defined in the parameter list and can only appear after required parameters. **Question 14. When passing an argument by reference to a function, which symbol must be used in the function definition?** A) `&` before the function name. B) `&` before the variable name in the argument list. C) `*` before the variable name. D) No special symbol; PHP always passes by reference. Answer: B Explanation: Adding `&` before a parameter (e.g., `function foo(&$var)`) tells PHP to pass the variable by reference. **Question 15. What will be the output of the following code? ```php function test($x) { $x = $x * 2; } $a = 5; test($a); echo $a; ```** ## Exam Preparation C) Both A and B D) `length($data)` Answer: C Explanation: `count()` and `sizeof()` are aliases; both return the number of elements. **Question 19. Which function removes the last element from an array and returns it?** A) `array_shift()` B) `array_pop()` C) `array_slice()` D) `array_splice()` Answer: B Explanation: `array_pop()` pops the last element off the end of the array. **Question 20. What does `array_merge($a, $b)` do when both `$a` and `$b` have string keys?** A) Overwrites values from `$a` with those from `$b` for matching keys. B) Appends `$b` to `$a` preserving all keys. C) Throws a warning because string keys cannot be merged. D) Returns an empty array. Answer: A Explanation: For associative arrays, `array_merge()` overwrites values from the first array with those from later arrays when keys are identical. **Question 21. Which loop is most appropriate for iterating over every element of an associative array `$person`?** A) `for` loop with a counter. B) `while` loop with `list()`. C) `foreach` loop. D) `do…while` loop. ## Exam Preparation Answer: C Explanation: `foreach` directly accesses key/value pairs of an associative array without needing a counter. **Question 22. Which superglobal contains data sent via HTTP GET method?** A) `$_POST` B) `$_REQUEST` C) `$_GET` D) `$_SERVER` Answer: C Explanation: `$_GET` holds query string parameters; `$_REQUEST` merges GET, POST, and COOKIE data. **Question 23. Which function is **not** a proper way to sanitize a string for HTML output?** A) `htmlspecialchars()` B) `htmlentities()` C) `strip_tags()` D) `addslashes()` Answer: D Explanation: `addslashes()` escapes characters for SQL, not for HTML output; the other three are used to prevent XSS. **Question 24. To protect a form against Cross‑Site Request Forgery (CSRF), a common technique is to:** A) Use `$_GET` instead of `$_POST`. B) Include a hidden token that is verified on submission. C) Disable cookies in the browser. D) Encode the form data with `base64_encode()`. Answer: B ## Exam Preparation **Question 28. Which mode opens a file for **reading and writing**, placing the file pointer at the beginning of the file?** A) `'r'` B) `'w'` C) `'a'` D) `'r+'` Answer: D Explanation: `'r+'` opens the file for both reading and writing without truncating it. **Question 29. What does the PHP function `header('Location: page.php');` do?** A) Includes `page.php` as a PHP file. B) Sends a HTTP redirect to the client. C) Prints the string “Location: page.php”. D) Sets a cookie named “Location”. Answer: B Explanation: The `Location` header instructs the browser to request a different URL, causing a redirect. **Question 30. Which statement correctly defines a class named `Car` with a public property `$color`?** A) `class Car { public $color; }` B) `class Car { var $color; }` C) `class Car { private $color; }` D) `class Car { protected $color; }` Answer: A Explanation: `public` makes the property accessible from outside the class; `var` is deprecated, while `private` and `protected` restrict visibility. **Question 31. How do you create an object of class `Car` and assign it to `$myCar`?** A) `$myCar = Car();` ## Exam Preparation B) `$myCar = new Car;` C) `$myCar = new Car();` D) Both B and C are correct. Answer: D Explanation: Both syntaxes are valid; parentheses are optional when no constructor arguments are needed. **Question 32. Which visibility keyword restricts access to class members only within the class **and** its subclasses?** A) `public` B) `protected` C) `private` D) `static` Answer: B Explanation: `protected` members are accessible inside the class itself and any class that extends it. **Question 33. What is the purpose of a constructor method in a PHP class?** A) To destroy the object when it goes out of scope. B) To initialize object properties when the object is created. C) To define static methods. D) To overload other methods. Answer: B Explanation: The `__construct()` method runs automatically upon object instantiation, allowing property initialization. **Question 34. Which magic method is called automatically when an object is no longer referenced?** A) `__construct()` B) `__destruct()` C) `__clone()` ## Exam Preparation Explanation: The `static` keyword can appear before or after the visibility keyword; any visibility is allowed. **Question 38. Which syntax correctly calls a static method `getTotal()` of class `Item`?** A) `$item->getTotal();` B) `Item::getTotal();` C) `self::getTotal();` D) `static::getTotal();` Answer: B Explanation: Static methods are accessed with the scope resolution operator `::` on the class name. **Question 39. Which SQL statement creates a new database named `shop`?** A) `CREATE DATABASE shop;` B) `NEW DATABASE shop;` C) `CREATE SCHEMA shop;` D) `MAKE DATABASE shop;` Answer: A Explanation: `CREATE DATABASE` is the standard MySQL command for creating a database. **Question 40. Which MySQL data type is best suited for storing a price with two decimal places?** A) `INT` B) `VARCHAR(10)` C) `DECIMAL(10,2)` D) `FLOAT` Answer: C Explanation: `DECIMAL(10,2)` stores exact numeric values with two digits after the decimal point, ideal for monetary amounts. ## Exam Preparation **Question 41. Which PHP extension provides an object‑oriented interface for MySQL databases?** A) `mysqli` B) `PDO` C) Both A and B D) `mysql` (deprecated) Answer: C Explanation: Both `mysqli` and `PDO` support OOP; `mysql` is deprecated and removed in newer PHP versions. **Question 42. What does the following PDO code do? ```php $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); ```** A) Enables silent error handling. B) Throws exceptions on database errors. C) Disables all error reporting. D) Logs errors to a file. Answer: B Explanation: Setting `ERRMODE_EXCEPTION` makes PDO raise a `PDOException` whenever a database error occurs. **Question 43. Which method fetches a single row as an associative array using MySQLi?** A) `mysqli_fetch_assoc($result);` B) `mysqli_fetch_array($result, MYSQLI_NUM);` C) `mysqli_fetch_row($result);` D) `mysqli_fetch_object($result);` Answer: A Explanation: `mysqli_fetch_assoc()` returns an associative array where column names are keys. ## Exam Preparation A) First Normal Form (1NF) B) Second Normal Form (2NF) C) Third Normal Form (3NF) D) Boyce‑Codd Normal Form (BCNF) Answer: B Explanation: 2NF removes partial dependencies where a non‑key attribute depends on only part of a composite key. **Question 48. What will be the output of the following PHP code? ```php $var = null; var_dump(isset($var)); ```** A) `bool(true)` B) `bool(false)` C) `NULL` D) `Notice: Undefined variable` Answer: B Explanation: `isset()` returns false for variables that are null. **Question 49. Which function can be used to generate a cryptographically secure random integer in PHP 7+?** A) `rand()` B) `mt_rand()` C) `random_int()` D) `crc32()` Answer: C Explanation: `random_int()` provides a secure random integer suitable for security‑critical applications. ## Exam Preparation **Question 50. Which of the following will **not** trigger a fatal error?** A) Calling an undefined function. B) Including a file with `require_once` that does not exist. C) Accessing an undefined constant without quotes. D) Using `include` on a missing file. Answer: D Explanation: `include` generates a warning, not a fatal error; the script continues execution. **Question 51. Which PHP function returns the current Unix timestamp with microseconds?** A) `time()` B) `microtime(true)` C) `date('U')` D) `gettimeofday()` Answer: B Explanation: `microtime(true)` returns a float representing seconds and microseconds since the Unix epoch. **Question 52. Which of the following statements about namespaces is **true**?** A) Namespaces can be defined inside functions only. B) The `use` keyword imports classes, functions, or constants from a namespace. C) Namespaces replace the need for `require`/`include`. D) A file can contain multiple namespace blocks without braces. Answer: B Explanation: `use` imports symbols from a namespace into the current scope. **Question 53. How would you autoload a class named `User` located in `classes/User.php` using `spl_autoload_register`?** ## Exam Preparation D) Traits cannot define constructors. Answer: D Explanation: Traits can define constructors; the class using the trait can invoke them. **Question 57. Which PHP function can be used to serialize an object for storage?** A) `json_encode()` B) `serialize()` C) `var_export()` D) `dump()` Answer: B Explanation: `serialize()` converts a value (including objects) into a storable string representation. **Question 58. Which function restores a serialized string back into a PHP value?** A) `json_decode()` B) `unserialize()` C) `decode()` D) `restore()` Answer: B Explanation: `unserialize()` reconstructs the original value from its serialized form. **Question 59. What will be the output of the following code? ```php $a = [1,2,3]; $b = $a; $b[] = 4; print_r($a); ```** A) `Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )` ## Exam Preparation B) `Array ( [0] => 1 [1] => 2 [2] => 3 )` C) `Fatal error` D) `NULL` Answer: B Explanation: Assignment copies the array by value; modifying `$b` does not affect `$a`. **Question 60. Which function returns the type of a variable as a string, e.g., “integer”, “string”?** A) `gettype()` B) `type_of()` C) `var_type()` D) `type()` Answer: A Explanation: `gettype()` reports the type name of a variable. **Question 61. Which operator is used to concatenate strings in PHP?** A) `+` B) `.` C) `&` D) `||` Answer: B Explanation: The dot (`.`) operator joins strings. **Question 62. Which of the following statements about the `clone` keyword is correct?** A) It creates a shallow copy of an object. B) It creates a deep copy of an object automatically. C) It can only be used on arrays. D) It triggers the `__clone()` magic method after copying. Answer: D