Q84: Fork behavior

Question 84: What will be the output of the following program:

if ( fork() )

{ printf(“Parent Process\n”);

wait(NULL);

printf(“Waited for child\n”);

}

else

{ printf(“Child Process\n”);

}

1. Parent Process

Child Process

Waited for child

2. Child Process

Parent Process

Waited for child

3. Parent Process

Waited for child

Child Process

4. Both A and B are possible

Solution: Since both parent and child process can execute first depending on the scheduling policy, it is indeterminate. The only thing is that child process has to be killed before parent can go ahead of wait(). The correct answer is option 4.

Q45: Fermat’s last theorem

This question is in continuation of Question #43.

Question #45: What is the output of the program discussed in Q43?

Options:

  • The program always prints “hello, world\n”.
  • The program prints “hello, world\n” for n = 2 only.
  • The program never prints “hello, world\n”.
  • We still don’t know what will be the output of this program.

Solution:

Fermat’s last theorem was proved recently and it took 300 years to prove that for n > 2, there is no solution to this equation. This was conjectured by Fermat but no proof was provided in 1637. Until Andrew Wiles proved it in 1995, nobody was able to prove this successfully. Hence, this program was thought to be undecidable before proof. So the correct option is second one.

Q43: What does the given C program do?

Question #43: What problem is the below C program trying to solve?

C-program

Options:

  • The program tries to find positive integer solutions to x^2 + y^2 = z^2, that is, Pythagoras theorem.
  • The program tries to find the value of ‘n’ for given values of ‘x’, ‘y’ and ‘z’ for the equation x^n + y^n = z^n by printing “hello, world\n”.
  • The program tries to find the values of ‘x’, ‘y’ and ‘z’ for a given ‘n’ by printing “hello, world\n”.
  • None of the above.

Solution: For the given input ‘n’ by the user, the program is trying to find the values of x, y and z which satisfy the equation x^n + y^n = z^n. In fact, this is the statement of Fermat’s last theorem. Hence third option is the correct one.