main(){ fork(); fork(); fork(); printf("Hello World!"); } How many times "Hello World!" will be printed?

  1. 2

  2. 4

  3. 6

  4. 8


Correct Option: D

AI Explanation

To determine how many times "Hello World!" will be printed, let's analyze the given code.

In the code, the fork() function is called three times consecutively. Each time the fork() function is called, it creates a new process by duplicating the existing process. So, the first fork() call creates one child process, the second fork() call creates two child processes (one from the original process and one from the child process of the first fork() call), and the third fork() call creates four child processes (one from the original process, two from the child processes of the second fork() call, and one from the child process of the first fork() call).

Therefore, a total of 1 + 2 + 4 = 7 child processes are created, in addition to the original process. Since each process executes the printf("Hello World!"); statement, it will be printed 8 times in total.

So, the correct answer is D) 8.

Find more quizzes: