PHP - MCQ Questions and Answers

Test your knowledge of PHP with these interactive multiple-choice questions.

« First 6 7 8 9 Last »
« Previous Page Next Page »

121. Which PDO method prepares a statement for execution?

  • a) prepare()
  • b) query()
  • c) execute()
  • d) statement()
Answer: A - prepare() creates a statement object for parameter binding.

122. What is the purpose of database connection pooling in PHP?

  • a) Reuses existing connections
  • b) Encrypts connections
  • c) Load balances queries
  • d) Caches query results
Answer: A - Reduces overhead by reusing connections (implemented in Swoole/ReactPHP).

123. Which SQL injection defense technique is most effective?

  • a) Prepared statements with bound parameters
  • b) Escaping special characters
  • c) Stored procedures
  • d) Input validation
Answer: A - Parameterized queries separate SQL logic from data.

124. What is the purpose of the Repository pattern?

  • a) Abstracts data access layer
  • b) Caches database results
  • c) Manages database connections
  • d) Generates SQL queries
Answer: A - Mediates between domain and data mapping layers.

125. Which PHP function prevents XSS attacks?

  • a) htmlspecialchars()
  • b) strip_tags()
  • c) filter_var()
  • d) All of the above
Answer: D - All help prevent cross-site scripting (context-dependent).

126. What is the purpose of Doctrine's QueryBuilder?

  • a) Creates type-safe DQL queries
  • b) Generates raw SQL
  • c) Both A and B
  • d) Manages database migrations
Answer: C - Builds both DQL (Doctrine Query Language) and SQL queries.

127. Which header prevents MIME-sniffing attacks?

  • a) X-Content-Type-Options: nosniff
  • b) Content-Security-Policy
  • c) X-XSS-Protection
  • d) Strict-Transport-Security
Answer: A - Prevents browsers from interpreting files as different MIME types.

128. What is the purpose of the Unit of Work pattern?

  • a) Tracks object changes for database synchronization
  • b) Isolates database transactions
  • c) Measures code performance
  • d) Manages worker threads
Answer: A - Core of ORMs like Doctrine (tracks new/dirty/removed entities).

129. Which Laravel feature provides database query logging?

  • a) DB::listen()
  • b) QueryLog facade
  • c) Eloquent::logQueries()
  • d) Database::enableQueryLog()
Answer: A - DB::listen() registers a callback for query logging.

130. What is the purpose of the N+1 query problem?

  • a) Excessive database queries from lazy loading
  • b) Recursive query execution
  • c) Database connection leaks
  • d) Race conditions in transactions
Answer: A - Occurs when iterating over results and fetching related data separately.

131. Which PHP function securely compares strings to prevent timing attacks?

  • a) hash_equals()
  • b) strcmp()
  • c) secure_compare()
  • d) password_verify()
Answer: A - hash_equals() uses constant-time comparison.

132. What is the purpose of the Data Mapper pattern?

  • a) Separates domain objects from database
  • b) Maps JSON to objects
  • c) Converts database types
  • d) Generates SQL schemas
Answer: A - Unlike Active Record, domain objects don't know about persistence.

133. Which header enables CORS in PHP?

  • a) Access-Control-Allow-Origin
  • b) Cross-Origin-Resource-Sharing
  • c) Allow-External-Requests
  • d) X-CORS-Enable
Answer: A - Specifies which origins can access the resource.

134. What is the purpose of the Identity Map pattern?

  • a) Ensures each object is loaded only once
  • b) Maps database IDs to objects
  • c) Both A and B
  • d) Generates primary keys
Answer: C - Prevents duplicate objects and maintains consistency.

135. Which Laravel method prevents mass assignment vulnerabilities?

  • a) $fillable property
  • b) $guarded property
  • c) fill() method
  • d) Both A and B
Answer: D - $fillable (allowlist) and $guarded (denylist) control mass assignment.

136. What is the purpose of database indexing?

  • a) Improves query performance
  • b) Enforces data uniqueness
  • c) Both A and B
  • d) Compresses database storage
Answer: C - Speeds up searches and can enforce constraints.

137. Which PHP function securely generates random integers?

  • a) random_int()
  • b) rand()
  • c) mt_rand()
  • d) uniqid()
Answer: A - Cryptographically secure (unlike rand()/mt_rand()).

138. What is the purpose of the Lazy Loading pattern?

  • a) Defers resource initialization until needed
  • b) Loads all dependencies upfront
  • c) Caches database results
  • d) Queues background tasks
Answer: A - Improves performance by delaying object construction.

139. Which security measure prevents CSRF attacks?

  • a) CSRF tokens
  • b) SameSite cookies
  • c) Both A and B
  • d) Input validation
Answer: C - Tokens verify form submissions, SameSite restricts cookie usage.

140. What is the purpose of database sharding?

  • a) Horizontally partitions data across servers
  • b) Compresses database files
  • c) Creates read replicas
  • d) Backs up data incrementally
Answer: A - Distributes data to improve scalability (e.g., by user region).
« First 6 7 8 9 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.