Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3438536
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:11:58+00:00 2026-05-18T08:11:58+00:00

the question in the exam was : write the output of the following program

  • 0

the question in the exam was :

write the output of the following program :

int i = 2 ;
int main () {

    int j = 10, p ;
    while (i-- && p == fork())
        if ( p < 0 ) exit(1);
    j += 2;
    if (p == 0) {
        i *= 3;
        j *= 3;
    }
    else {
        i *= 2;
        j *= 2;
    }

    printf("i = %d, j = %d \n",i,j);

    return 0;
}

Output from the console using xcode with include this lines before int i = 2; :

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

Output:

i = 3, j = 36 
i = 0, j = 36 
i = -3, j = 36 

Note: I notice that the output is different if we use the Ubuntu.

i think this is the output of the Ubuntu :

i = 2 , j = 24
i = 2 , j = 24

any brief explain or trace would be great Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:11:59+00:00Added an answer on May 18, 2026 at 8:11 am

    Assuming that was a typo, and while (i-- && p == fork()) was really while (i-- && (p = fork())), then the output depends on the OS scheduler.

    The main process forks off processes with

    • i=1 p=0 <– process A, parent.p=A
    • i=0 p=0 <– process B, parent.p=B
    • i=-1 p=B <– parent process, parent.p = B

    Processes A and B do not continue the loop, because the p=fork() evaluates as false for them.

    Each process adds 2 to j (which might as well be j=12). In summary:

    A: i=+1 p=0    j=12
    B: i= 0 p=0    j=12
    P: i=-1 p=*B*  j=12
    

    The cases where p=0, have i and j multiplied by 3, the ones with p!=0 (the parent process) have them multiplied by 2. This yields the following perfectly reasonable output for me:

    i = -2, j = 24
    i = 3, j = 36
    i = 0, j = 36
    

    (The order is going to be somewhat random)

    As sharptooth pointed out, your code as written is simply producing random results, depending on a piece of uninitialized memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While studying for a Functional Programming exam, I came across the following question from
Whilst doing exam revision I am having trouble answering the following question from the
While studying for a Functional Programming exam, I came across the following questions from
Preparing for exam and got stuck at this question: Allowed operators are <<,+,& no
I just took an exam where I was asked the following: Write the function
This is a scjp mock exam question. Suppose I have the following two files:
I have faced the following interview question. Consider this function declaration: void quiz(int i)
So the exam question is this: Write a method using the method header below.
Consider the following exam question: Use the CRC polynomial generator x2 + 1 to
I found this exam question that I have to tell how the beq instruction

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.