Find Output Of Program

Question 11 :

What will be the output of the program?

#include
int main()
{
    int x=55;
    printf("%d, %d, %d\n", x<=55, x=40, x>=10);
    return 0;
}


A). 1, 40, 1
B). 1, 55, 1
C). 1, 55, 0
D). 1, 1, 1
Answer : Option A

Explanation :

Step 1: int x=55; here variable x is declared as an integer type and initialized to '55'.
Step 2: printf("%d, %d, %d\n", x<=55, x=40, x>=10);
In printf the execution of expressions is from Right to Left.
here x>=10 returns TRUE hence it prints '1'.
x=40 here x is assigned to 40 Hence it prints '40'.
x<=55 returns TRUE. hence it prints '1'.
Step 3: Hence the output is "1, 40, 1".


Question 12 :

What will be the output of the program?

#include
int main()
{
    int i=2;
    printf("%d, %d\n", ++i, ++i);
    return 0;
}


A). 3, 4
B). 4, 3
C). 4, 4
D). Output may vary from compiler to compiler
Answer : Option D

Explanation :

The order of evaluation of arguments passed to a function call is unspecified.
Anyhow, we consider ++i, ++i are Right-to-Left associativity. The output of the program is 4, 3.
In TurboC, the output will be 4, 3.
In GCC, the output will be 4, 4.


Question 13 :

What will be the output of the program?

#include
int main()
{
    int k, num=30;
    k = (num>5 ? (num <=10 ? 100 : 200): 500);
    printf("%d\n", num);
    return 0;
}


A). 200
B). 30
C). 100
D). 500
Answer : Option B

Explanation :

Step 1: int k, num=30; here variable k and num are declared as an integer type and variable num is initialized to '30'.
Step 2: k = (num>5 ? (num <=10 ? 100 : 200): 500); This statement does not affect the output of the program. Because we are going to print the variable num in the next statement. So, we skip this statement.
Step 3: printf("%d\n", num); It prints the value of variable num '30'
Step 3: Hence the output of the program is '30'


Question 14 :

What will be the output of the program?

#include
int main()
{
    char ch;
    ch = 'A';
    printf("The letter is");
    printf("%c", ch >= 'A' && ch <= 'Z' ? ch + 'a' - 'A':ch);
    printf("Now the letter is");
    printf("%c\n", ch >= 'A' && ch <= 'Z' ? ch : ch + 'a' - 'A');
    return 0;
}


A). The letter is a
Now the letter is A
B). The letter is A
Now the letter is a
C). Error
D). None of above
Answer : Option A

Explanation :

Step 1: char ch; ch = 'A'; here variable ch is declared as an character type an initialized to 'A'.

Step 2: printf("The letter is"); It prints "The letter is".

Step 3: printf("%c", ch >= 'A' && ch <= 'Z' ? ch + 'a' - 'A':ch);

The ASCII value of 'A' is 65 and 'a' is 97.

Here
=> ('A' >= 'A' && 'A' <= 'Z') ? (A + 'a' - 'A'):('A')
=> (TRUE && TRUE) ? (65 + 97 - 65) : ('A')
=> (TRUE) ? (97): ('A')
In printf the format specifier is '%c'. Hence prints 97 as 'a'.
Step 4: printf("Now the letter is"); It prints "Now the letter is".
Step 5: printf("%c\n", ch >= 'A' && ch <= 'Z' ? ch : ch + 'a' - 'A');
Here => ('A' >= 'A' && 'A' <= 'Z') ? ('A') : (A + 'a' - 'A')
=> (TRUE && TRUE) ? ('A') :(65 + 97 - 65)
=> (TRUE) ? ('A') : (97)
It prints 'A'

Hence the output is

The letter is a
Now the letter is A


Question 15 :

What will be the output of the program?

#include
int main()
{
    int i=2;
    int j = i + (1, 2, 3, 4, 5);
    printf("%d\n", j);
    return 0;
}


A). 4
B). 7
C). 6
D). 5
Answer : Option B

Explanation :

Because, comma operator used in the expression i (1, 2, 3, 4, 5). The comma operator has left-right associativity. The left operand is always evaluated first, and the result of evaluation is discarded before the right operand is evaluated. In this expression 5 is the right most operand, hence after evaluating expression (1, 2, 3, 4, 5) the result is 5, which on adding to i results into 7.