=== PHP MOTIVATION TEST === Total Questions: 10 Instructions: Answer all questions to the best of your ability. Question 1 (1 points): Which of the following is the correct way to start a PHP script? A) <php> B) <?php C) <script language="php"> D) <%php%> -------------------------------------------------- Question 2 (2 points): What will be the output of the following code? Code: A) 53 B) 8 C) Error D) 5 + 3 -------------------------------------------------- Question 3 (1 points): Which function is used to count the number of elements in an array? A) length() B) size() C) count() D) sizeof() -------------------------------------------------- Question 4 (3 points): Complete the function to return the factorial of a number: Code: function factorial($n) { if ($n <= 1) { return 1; } return $n * _________; } -------------------------------------------------- Question 5 (1 points): Which keyword is used to create a class in PHP? A) create B) class C) object D) define -------------------------------------------------- Question 6 (2 points): What will happen when this code runs? Code: getMessage(); } echo " - Done"; ?> A) Fatal error B) Caught: Test error - Done C) Test error D) Nothing will be displayed -------------------------------------------------- Question 7 (2 points): Which is the modern recommended way to connect to MySQL in PHP? A) mysql_connect() B) mysqli or PDO C) db_connect() D) sql_connect() -------------------------------------------------- Question 8 (3 points): Which function should be used to prevent SQL injection? A) mysql_escape_string() B) addslashes() C) Prepared statements D) strip_tags() -------------------------------------------------- Question 9 (3 points): What design pattern is demonstrated in this code? Code: class Database { private static $instance = null; private function __construct() {} public static function getInstance() { if (self::$instance === null) { self::$instance = new Database(); } return self::$instance; } } A) Factory Pattern B) Singleton Pattern C) Observer Pattern D) Strategy Pattern -------------------------------------------------- Question 10 (5 points): Write a PHP function to check if a string is a palindrome (ignoring case and spaces) -------------------------------------------------- === ANSWER KEY === Q1: B (1 points) Q2: B (2 points) Q3: C (1 points) Q4: factorial($n - 1) (3 points) Q5: B (1 points) Q6: B (2 points) Q7: B (2 points) Q8: C (3 points) Q9: B (3 points) Q10: See sample solution below (5 points) === SAMPLE SOLUTIONS === Question 4 - Complete Factorial Function: Answer: factorial($n - 1) Complete function: function factorial($n) { if ($n <= 1) { return 1; } return $n * factorial($n - 1); } Question 10 - Palindrome Function: function isPalindrome($str) { // Remove spaces and convert to lowercase $clean = strtolower(str_replace(' ', '', $str)); // Compare with reverse return $clean === strrev($clean); } Alternative palindrome solution: function isPalindrome($str) { $clean = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $str)); $len = strlen($clean); for ($i = 0; $i < $len / 2; $i++) { if ($clean[$i] !== $clean[$len - 1 - $i]) { return false; } } return true; } === SCORING EXAMPLE === Sample Score: 18 points Analysis: Good! You have solid PHP fundamentals with room for advanced concepts. Recommendations: - Study design patterns and advanced OOP concepts - Focus on security best practices - Practice algorithm and problem-solving skills - Build practical projects to apply your knowledge - Contribute to open-source PHP projects