The number of 3's in the output of the program main() { printf("1"); fork();…

2018

The number of 3's in the output of the program

main()

{

printf("1"); fork();

printf("2"); fork();

fork(); printf("3");

}

  1. A.

    16

  2. B.

    8

  3. C.

    4

  4. D.

    2

Attempted by 119 students.

Show answer & explanation

Correct answer: B

The program starts with one process printing '1'. Each fork doubles the processes: 2 after first, 4 after second, and 8 after third. All eight processes execute the final printf("3"), resulting in '3' appearing exactly 8 times.

Explore the full course: Up Lt Grade Assistant Teacher 2025