Exam 200-710 All QuestionsBrowse all questions from this exam
Question 16

What is the output of the following code?

    Correct Answer: D

    In the provided code, the 'MyTrait' trait declares a private property '$abc' initialized to 1. The trait also contains two public methods: 'increment()' which increments '$abc' by 1, and 'getValue()' which returns the current value of '$abc'. The 'MyClass' class uses the 'MyTrait' trait and adds a method 'incrementBy2()', which calls 'increment()' and then increments '$abc' again. When 'MyClass' is instantiated and 'incrementBy2()' is called, the '$abc' property is incremented twice, going from 1 to 3. Therefore, the output of 'var_dump($c->getValue())' is int(3).

Discussion
ZhukovPeterOption: D

Program to check: <?php trait MyTrait { private $abc = 1; public function increment(){ $this->abc++; } public function getValue(){ return $this->abc; } } class MyClass { use MyTrait; public function incrementBy2(){ $this->increment(); $this->abc++; } } $c = new MyClass(); $c->incrementBy2(); var_dump($c->getValue()); Output: 3