Outputs Programming


What will be the output of the program?
#include<stdio.h>
int main()
{
    int a=0, b=1, c=3;
    *((a) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}
Answer: Option C
Explanation:
Step 1int a=0, b=1, c=3; here variable ab, and c are declared as integer type and initialized to 0, 1, 3 respectively.
Step 2*((a) ? &b : &a) = a ? b : c; The right side of the expression(a?b:c) becomes (0?1:3). Hence it return the value '3'.
The left side of the expression *((a) ? &b : &a) becomes *((0) ? &b : &a). Hence this contains the address of the variable a *(&a).
Step 3*((a) ? &b : &a) = a ? b : c; Finally this statement becomes *(&a)=3. Hence the variable a has the value '3'.
Step 4printf("%d, %d, %d\n", a, b, c); It prints "3, 1, 3".

12. 
What will be the output of the program?
#include<stdio.h>
int main()
{
    int a = 300, b, c;
    if(a >= 400)
        b = 300;
    c = 200;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}
Answer: Option C
Explanation:
Step 1int a = 300, b, c; here variable a is initialized to '300', variable b and c are declared, but not initialized.
Step 2if(a >= 400) means if(300 >= 400). Hence this condition will be failed.
Step 3c = 200; here variable c is initialized to '200'.
Step 4printf("%d, %d, %d\n", a, b, c); It prints "300, garbage value, 200". because variable b is not initialized.

16. 
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x = 10, y = 20;
    if(!(!x) && x)
        printf("x = %d\n", x);
    else
        printf("y = %d\n", y);
    return 0;
}
A.y =20
B.x = 0
C.
x = 10@
D.x = 1
Answer: Option C
Explanation:
The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression.
Step 1if(!(!x) && x)
Step 2if(!(!10) && 10)
Step 3if(!(0) && 10)
Step 3if(1 && 10)
Step 4if(TRUE) here the if condition is satisfied. Hence it prints x = 10.
20. 
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x, y, z;
    x=y=z=1;
    z = ++x || ++y && ++z;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}
A.
x=2, y=1, z=1@
B.x=2, y=2, z=1
C.x=2, y=2, z=2
D.x=1, y=2, z=1
Answer: Option A
Explanation:
Step 1x=y=z=1; here the variables x ,yz are initialized to value '1'.
Step 2z = ++x || ++y && ++z; becomes z = ( (++x) || (++y && ++z) ). Here ++x becomes 2. So there is no need to check the other side because ||(Logical OR) condition is satisfied.(z = (2 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '1'. So the value of variable z is '1'
Step 3printf("x=%d, y=%d, z=%d\n", x, y, z); It prints "x=2, y=1, z=1". here x is increemented in previous step. y and z are not increemented.

No comments:

Post a Comment