PHP - MCQ Questions and Answers

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

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

101. Which PHPUnit annotation marks a test method?

  • a) @test
  • b) @phpunit
  • c) @testMethod
  • d) Method name starting with "test"
Answer: D - Either prefix method name with "test" or use @test annotation.

102. What is the purpose of PHP 8's named arguments?

  • a) Pass arguments by parameter name instead of position
  • b) Name anonymous functions
  • c) Document function parameters
  • d) Create named parameter arrays
Answer: A - Named arguments allow function(foo: 'bar') syntax, improving readability.

103. Which PHP 8 feature replaces switch statements with more readable syntax?

  • a) match() expression
  • b) switch() enhancement
  • c) case/when blocks
  • d) pattern matching
Answer: A - match is stricter (returns value, no fallthrough) than switch.

104. How do you test private methods in PHPUnit?

  • a) Use Reflection to make them accessible
  • b) Test through public methods
  • c) Mark them @testable
  • d) Both A and B
Answer: D - Best practice is testing via public API, but Reflection can access privates when needed.

105. Which PHP 8.1 feature enables read-only properties?

  • a) readonly keyword
  • b) const properties
  • c) final properties
  • d) immutable classes
Answer: A - readonly properties can only be set once during initialization.

106. What is the purpose of PHP's async/await in frameworks like Swoole?

  • a) Non-blocking I/O operations
  • b) Parallel execution
  • c) Both A and B
  • d) Synchronous task queuing
Answer: C - Async PHP enables concurrent I/O and parallel processing.

107. Which PHP 8.0 feature ensures strict parameter type checking?

  • a) Union types
  • b) Strict mode
  • c) declare(strict_types=1)
  • d) Type assertions
Answer: C - declare(strict_types=1) enforces exact type matching in the file.

108. What is the purpose of PHP's fiber in 8.1+?

  • a) Lightweight cooperative multitasking
  • b) Database connection pooling
  • c) Parallel thread execution
  • d) CPU-intensive task scheduling
Answer: A - Fibers enable suspendable/resumable functions without full threading.

109. Which PHPUnit method asserts two variables are equal?

  • a) assertEquals()
  • b) assertSame()
  • c) assertEqual()
  • d) Both A and B
Answer: D - assertEquals() (loose) and assertSame() (strict) compare values.

110. What is the purpose of PHP 8's nullsafe operator (?->)?

  • a) Prevents null reference errors
  • b) Checks for null values
  • c) Both A and B
  • d) Throws NullPointerException
Answer: A - $obj?->method() safely returns null if $obj is null.

111. Which PHP 8.2 feature prevents dynamic property creation?

  • a) #[AllowDynamicProperties] attribute
  • b) readonly classes
  • c) declare(strict_properties=1)
  • d) final classes
Answer: A - Classes now forbid dynamic properties unless marked with this attribute.

112. How do you mock objects in PHPUnit?

  • a) createMock() method
  • b) MockBuilder class
  • c) @mock annotation
  • d) Both A and B
Answer: D - Both createMock() (simple) and MockBuilder (advanced) create test doubles.

113. Which PHP 8.0 attribute marks deprecated functionality?

  • a) #[Deprecated]
  • b) @deprecated
  • c) #[Deprecation]
  • d) #[Deprecate]
Answer: A - #[Deprecated] replaces PHPDoc's @deprecated in PHP 8+.

114. What is the purpose of PHP's WeakMap in 8.0+?

  • a) Holds weak references to objects
  • b) Memory-efficient array alternative
  • c) Thread-safe mapping
  • d) Immutable key-value storage
Answer: A - WeakMap doesn't prevent garbage collection of its keys (unlike SplObjectStorage).

115. Which PHPUnit annotation provides test data?

  • a) @dataProvider
  • b) @testWith
  • c) @dataset
  • d) Both A and B
Answer: D - @dataProvider references a method, @testWith provides inline data.

116. What is the purpose of PHP 8.1's enums?

  • a) Type-safe named constants
  • b) Alternative to classes
  • c) Database enum replication
  • d) Bitmask operations
Answer: A - Enums provide first-class, type-checked constant values with methods.

117. Which PHP 8.2 feature improves type system flexibility?

  • a) Disjunctive Normal Form (DNF) types
  • b) Dynamic typing
  • c) Type erasure
  • d) Generic types
Answer: A - DNF allows combining union/intersection types (e.g., (A&B)|C).

118. How do you test exceptions in PHPUnit?

  • a) expectException() method
  • b) @expectedException annotation
  • c) try/catch in test
  • d) Both A and B
Answer: D - Modern PHPUnit uses expectException(), but annotations still work.

119. Which PHP 8.0 constructor feature reduces boilerplate?

  • a) Constructor Property Promotion
  • b) Named constructors
  • c) Auto-injection
  • d) Constructor overloading
Answer: A - Combines property declaration and assignment in constructor parameters.

120. What is the purpose of PHP's #[SensitiveParameter] attribute?

  • a) Redacts sensitive data in stack traces
  • b) Encrypts parameter values
  • c) Validates input sanitization
  • d) Marks PII for GDPR compliance
Answer: A - Hides values (like passwords) in error logs (PHP 8.2+).
« First 5 6 7 8 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.