Sample questions
Php Concurrency AsyncDifficulty 1
In PHP, what does calling a function whose body contains a yield statement return?
- aA
Generator object✓ - bThe first yielded value
- cAn array of all yielded values
- d
null, until the function is used inside a foreach loop
Explanation:Any function containing yield anywhere in its body automatically becomes a generator function. Calling it never runs the body immediately — it returns a Generator object that implements Iterator, and the body only executes as the generator is advanced.
Php Concurrency AsyncDifficulty 2
function numbers() {
yield 1;
yield 2;
yield 3;
}
foreach (numbers() as $n) {
echo $n;
}
What is printed?
- a0 1 2
- b123✓
- c1, 2, 3
- dNothing is printed because generator functions cannot be used directly in a
foreach loop
Explanation:numbers() returns a Generator, which is directly iterable with foreach. Each iteration yields the next value (1, then 2, then 3), and echo $n prints them back to back with no separator: 123.
Php Concurrency AsyncDifficulty 1
When exactly does the code inside a generator function start executing?
- aImmediately when the function is called, before the caller does anything else
- bOnly after the entire script has completely finished running from start to end
- cWhen the generator is first advanced, e.g. via
foreach or current()/next()✓ - dWhen PHP parses and compiles the file that defines the generator function
Explanation:Calling a generator function only creates the Generator object; execution is lazy. The body runs up to the first yield only once something starts pulling values from it (foreach, ->current(), ->next(), ->send(), etc.).
Php Concurrency AsyncDifficulty 2
function pairs() {
yield 'a' => 1;
yield 'b' => 2;
}
foreach (pairs() as $k => $v) {
echo "$k=$v ";
}
What is printed?
- a0=1 1=2
- ba=1, b=2
- c1=a 2=b
- da=1 b=2 ✓
Explanation:yield 'a' => 1; yields the value 1 with the explicit key 'a', and likewise for 'b' => 2. foreach ($gen as $k => $v) receives exactly those key/value pairs, printing a=1 b=2 .
Php Concurrency AsyncDifficulty 1
By default, without any additional extension or library, how does a standard PHP script execute?
- aSynchronously, one statement after another, in a single thread✓
- bMultiple statements automatically run in parallel across CPU cores
- cEach function call automatically spawns a new OS thread
- dAll I/O operations are non-blocking by default
Explanation:Plain PHP is single-threaded and synchronous: statements execute in order, and a blocking call (like a slow HTTP request) stops everything else in that script until it returns. True parallelism or non-blocking I/O requires extra tools (pcntl_fork, Fibers plus an event-loop library, Swoole, etc.).
Php Concurrency AsyncDifficulty 2
function slowTask($seconds) {
sleep($seconds);
return 'done';
}
$start = microtime(true);
slowTask(1);
slowTask(1);
$elapsed = microtime(true) - $start;
Approximately what is
$elapsed, and why?
- a~1 second, because PHP automatically runs both
slowTask() calls concurrently by default - b~2 seconds, because each blocking
sleep() call must fully finish before the next one starts✓ - c~0 seconds, because PHP schedules
sleep() calls to run in the background automatically - dIt cannot be measured with
microtime(), since PHP hides timing details from userland code
Explanation:sleep() blocks the current (single) thread of execution. The second slowTask(1) only starts after the first one has fully returned, so the two 1-second sleeps add up sequentially to roughly 2 seconds.