Multiple choice technology programming languages

What will be printed? BEGIN { print 'a'; } END { print 'b'; } BEGIN { print 'c'; } END { print 'd'; }

  1. abcd

  2. acbd

  3. cadb

  4. code will fail

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

BEGIN blocks execute at compile time in declaration order, while END blocks execute at runtime in reverse declaration order (LIFO - last in, first out). The two BEGIN blocks print 'a' then 'c'. The two END blocks are declared in order (prints 'b' then 'd'), but execute in reverse order, so 'd' prints before 'b'. Final output: 'ac' from BEGIN blocks, then 'db' from END blocks, yielding 'cadb'.