• edited by
20,195 views
64 64 votes

Consider the C program shown below:

#include<stdio.h>
#define print(x) printf("%d", x)

int x;
void Q(int z)
{
        z+=x;
        print(z);
}

void P(int *y)
{
        int x = *y + 2;
        Q(x);
        *y = x - 1;
        print(x);
}
main(void) {
        x = 5;
        P(&x);
        print(x);
}

The output of this program is:

  1. $12 \ 7 \ 6$
  2. $22 \ 12 \ 11$
  3. $14 \ 6 \ 6$
  4. $7 \ 6 \ 6$

6 Answers

Best answer
80 80 votes
main: x = 5; //Global x becomes 5
P: int x = *y + 2; //local x in P becomes 5+2 = 7
Q: z+=x; //local z in Q becomes 7 + 5 = 12
Q: print(z); //prints 12
P:  *y  = x - 1; 
//content of address of local variable y 
(same as global variable x) becomes 7 - 1 = 6
P: print(x); //prints local variable x in P = 7
main: print(x); //prints the global variable x = 6

Correct Answer: $A$

• edited by
5 5 votes
The answer for this is A).. Can get the answer by a simple dry run..
3 3 votes
void P(int *y)
{
        int x = *y + 2; // x=5+2=7
        Q(x); // Q(7)
        void Q(int z) // z=7 
           { 
               z+=x; // z=z+x=7+5=12 as here x is global and its value is 5 define in main() section print(z); // 12 is printed 
           } 
        *y = x - 1; // 7-1=6
        print(x);  // here value of x is 7 as it is local so 7 is printed   
}                     
 print(x); // here value of y is printed i.e 6 as it represents the final value of global x

so output is 12 7 6

                    
 
Answer:
Position:
Show:

Related questions

61 61 votes
5 answers 5 answers
14.7k
14.7k views
Kathleen asked Sep 17, 2014
14,680 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
116 116 votes
7 answers 7 answers
43.5k
43.5k views
Kathleen asked Sep 16, 2014
43,491 views
Assume the following C variable declaration:int *A[10], B[10][10];Of the following expressions:$A $$A [3]$$B $$B [3]$which will not give compile-time errors if used as le...
2 2 votes
2 2 answers
7.7k
7.7k views
Kathleen asked Sep 17, 2014
7,715 views
Consider the following class definitions in a hypothetical Object Oriented language that supports inheritance and uses dynamic binding. The language should not be assumed...
61 61 votes
6 answers 6 answers
16.9k
16.9k views
Kathleen asked Sep 16, 2014
16,850 views
Which of the following statements is FALSE?In statically typed languages, each variable in a program has a fixed typeIn un-typed languages, values do not have any typesIn...