Find Output Of Program

Question 16 :

What will be the output of the program?

#include
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 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.


Question 17 :

What will be the output of the program?

#include
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}


A). This is default This is case 1
B). This is case 3 This is default
C). This is case 1 This is case 3
D). This is default Answer & Explanation
Answer : Option A

Explanation :

In the very begining of switch-case statement default statement is encountered. So, it prints "This is default".
In default statement there is no break; statement is included. So it prints the case 1 statements. "This is case 1".
Then the break; statement is encountered. Hence the program exits from the switch-case block.


Question 18 :

What will be the output of the program?

#include
int main()
{
    int i = 1;
    switch(i)
    {
        printf("Hello\n");
        case 1:
            printf("Hi\n");
            break;
        case 2:
            printf("\nBye\n");
            break;
    }
    return 0;
}


A). Hello Hi
B). Hello Bye
C). Hi
D). Bye
Answer : Option C

Explanation :

switch(i) has the variable i it has the value '1'(one).
Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement.
switch-case do not execute any statements outside these blocks case and default

Hence the output is "Hi".


Question 19 :

What will be the output of the program?

#include
int main()
{
    char j=1;
    while(j < 5)
    {
        printf("%d, ", j);
        j = j+1;
    }
    printf("\n");
    return 0;
}


A). 1 2 3 ... 127
B). 1 2 3 ... 255
C). 1 2 3 ... 127 128 0 1 2 3 ... infinite times
D). 1, 2, 3, 4
Answer : Option D

Question 20 :

What will be the output of the program?

#include
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 1: x=y=z=1; here the variables x ,y, z are initialized to value '1'.
Step 2: z = ++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 3: printf("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.