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

Expressions Questions and Answers

71.

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.

72.

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.

73.

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.

74.

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.

Answer: B

Correct answer: 0 or 1

A relational or logical expression in C yields an int that is exactly 1 for true and 0 for false. That is stricter than the rule for testing a value, where any non-zero counts as true. So (3 > 2) is 1, never 3 and never some other positive number.

Please log in or register to report this question.

77.

What is the value of the following expression?

i = 1;
i = ( i <<= 1 % 2)

Answer: A

Correct answer: 2

% binds tighter than <<=, so the right-hand side is \(1 \bmod 2 = 1\), and the statement is i <<= 1. Shifting 1 left by one place gives 2, and the value of the assignment expression is that same 2.

Please log in or register to report this question.

78.

Expression x % y is equivalent to____?

Answer: B

Correct answer: (x – (x/y) * y)

The standard guarantees that integer division and remainder satisfy

\[ (x / y) \times y + (x \% y) = x \]

Rearranging gives \(x \% y = x - (x / y) \times y\). The multiplication by \(y\) is essential - it is what converts the quotient back into the part of \(x\) that divides exactly.

Please log in or register to report this question.

79.

What is the output of this C code?   

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

 

Answer: B

Correct answer: a = 2, b = 1

c = a++ + b; uses the old value of a, so c becomes 2, and the side effect then makes a equal to 2. b is only read, so it stays 1. The printed values are those of a and b, not of c.

Please log in or register to report this question.

80.

What is the output of this C code?  

int main()
    {
        int x = -2;
        if (!0 == 1)
            printf("yes\n");
        else
            printf("no\n");
    }

 

Answer: A

Correct answer: yes

! is logical negation: it maps zero to 1 and any non-zero value to 0. So !0 is exactly 1, the comparison !0 == 1 holds, and the program prints yes. Contrast ~0, which flips bits and gives -1.

Please log in or register to report this question.

Loading…
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