Bird
0
0

Given this class:

hard📝 Application Q9 of 15
PHP - Classes and Objects

Given this class:

class Point {
public int $x;
public int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
}

Which code correctly creates a new Point object at coordinates (5, 10)?

A$p = new Point(5, 10);
B$p = new Point(x:5, y:10);
C$p = new Point(10, 5);
D$p = Point(5, 10);
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor parameters order

    The constructor expects int $x first, then int $y.
  2. Step 2: Identify correct instantiation

    new Point(5, 10) matches the order and types.
  3. Final Answer:

    $p = new Point(5, 10); -> Option A
  4. Quick Check:

    Constructor parameters order matters [OK]
Quick Trick: Match constructor parameter order when instantiating [OK]
Common Mistakes:
  • Swapping parameter order
  • Using named arguments (PHP 8+) without syntax support
  • Omitting new keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes