Machine-translated by Google. Questions are authored in English.
Top

Expressions Questions and Answers

81.

What is the output of this C code?    

int main()
    {
        int a = 20, b = 15, c = 5;
        int d;
        d = a == (b + c);
        printf("%d", d);
    }

 

Answer: A

Correct answer: 1

= has lower precedence than ==, so the statement is d = (a == (b + c)). Since \(15 + 5 = 20\) and a is 20, the comparison is true, and a true relational expression in C has the value 1.

Please log in or register to report this question.

82.

What is the output of this C code?   

 int main()
    {
        int x = 2;
        x = x << 1;
        printf("%d\n", x);
    }

 

Answer: A

Correct answer: 4

<< shifts the bits left, and each place shifted left doubles the value:

\[ 2 = 10_2 \;\rightarrow\; 100_2 = 4 \]

So x becomes 4. Shifting a small positive int left is well defined, so the result does not depend on the compiler or the machine.

Please log in or register to report this question.

83.

Which of the following is an invalid assignment operator?

Answer: D

Correct answer: None of the mentioned

All three are valid compound assignment operators: %= for remainder, /= for division and |= for bitwise OR. So none of them is invalid, and the answer is none of the mentioned.

Please log in or register to report this question.

84.

What will be the output of the following program?

#include <stdio.h>

static int y = 1;
int main()
{
static int z;
printf("%d %d", y, z);
return 0;
}

 

Answer: C

Correct answer: 1 0

y is a file-scope static int explicitly initialised to 1. z is a static local with no initialiser, and every object with static storage duration is zero-initialised - unlike an auto variable, which would hold junk. So the output is 1 0.

Please log in or register to report this question.

85.

What is the output of this C code?    

int main()
    {
        int a = 10, b = 5, c = 3;
        b != !a;
        c = !!a;
        printf("%d\t%d", b, c);
    }

 

Answer: A

Correct answer: 5 1

b != !a; is an expression statement whose value is computed and thrown away - it does not assign anything, so b remains 5. c = !!a applies logical NOT twice: !10 is 0 and !0 is 1, so the double negation reduces any non-zero value to 1. That idiom is the usual way of normalising a value to 0 or 1.

Please log in or register to report this question.

86.

What is the output of this C code?    

void main()



    {
        1 < 2 ? return 1: return 2;
    }

 

Answer: D

Correct answer: Compile time error

return is a statement, not an expression, so it cannot appear as a branch of the conditional operator. 1 < 2 ? return 1: return 2; is therefore a syntax error, and the program never runs. The valid way to write it is return 1 < 2 ? 1 : 2;.

Please log in or register to report this question.

87.

What will be the output of the following program?

#include <stdio.h>

int main()
{
register int i = 2;
static char ch = 'A';
auto float j;
int k;
k = ++ch && i;
k = ++ch;
j = i-- + ++k * 2;
printf("%d %f", k, j);
return 0;
}

 

Answer: C

Correct answer: 68 138.000000

Follow the four statements, with ch starting at 'A' = 65.

  1. k = ++ch && i; - ch becomes 66; both operands are non-zero, so k is 1.
  2. k = ++ch; - ch becomes 67 and k takes that value.
  3. j = i-- + ++k * 2; - i-- contributes 2 (leaving i at 1); ++k makes k 68, and \(68 \times 2 = 136\); so \(j = 2 + 136 = 138\).

Printed with %d and %f: 68 138.000000.

Please log in or register to report this question.

88.

Which of the following statement is correct about the code snippet given below?

#include <stdio.h>

int main()
{
int a = 10, b = 2, c;
a = !( c = c == c) && ++b;
c += ( a + b--);
printf( " %d %d %d", b, c, a);
return 0;

}

 

Answer: A

Correct answer: The program will print the output 1 3 0

Take the statements in order.

  1. a = !(c = c == c) && ++b; - c == c is 1, so c becomes 1 and !1 is 0. The && short-circuits, so ++b never runs and b stays 2. a becomes 0.
  2. c += (a + b--); - a + b is \(0 + 2 = 2\), so c becomes \(1 + 2 = 3\), and the post-decrement leaves b at 1.

Printing b, c, a gives 1 3 0.

Please log in or register to report this question.

89.

What will be the output of the following code?

static int i = 5;

main()
{
int sum = 0;
do
{
sum += (1/i);
}while(0 < i--);
printf("sum of the series is %d", sum);
}

 

Answer: C

Correct answer: It will produce a run time error.

The loop is do { sum += 1/i; } while (0 < i--);. Because the test is 0 < i--, the last successful test happens when i is 1, and the post-decrement then leaves i at 0 - so the body runs once more and evaluates 1/0. Integer division by zero is a run-time fault, not something the compiler can catch, so the program crashes. (Had it survived, integer division would have made every term but 1/1 zero, so sum would have been 1, not the series in the first option.)

Please log in or register to report this question.

90.

What is the difference between the following 2 codes?

//Program 1
    int main()
    {
        int d, a = 1, b = 2;
        d =  a++ + ++b;
        printf("%d %d %d", d, a, b);
    }

 //Program 2
    int main()
    {
        int d, a = 1, b = 2;
        d =  a++ + ++b;
        printf("%d %d %d", d, a, b);
    }

 

Answer: A

Correct answer: No difference as space doesn’t make any difference, values of a, b, d are same in both the case

The two programs are character for character the same, so naturally they produce the same values: a++ contributes 1 and leaves a as 2, ++b makes b 3 and contributes 3, so d is 4 and the output is 4 2 3. The point the question is driving at is that whitespace between tokens is insignificant - though it is worth knowing the limit of that rule: a+++++b would not compile, because the compiler takes the longest possible token at each step and reads it as a++ ++ +b.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Expressions Questions and Answers || Expressions MCQ Questions and Answers || Expressions GK Questions and Answers || Expressions GK MCQ Questions || Expressions Multiple Choice Questions and Answers || Expressions GK || GK on Expressions || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming