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

Expressions Questions and Answers

91.

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.

92.

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.

93.

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.

94.

What is the output of this C code?   

 int main()
    {
        int a = 4, n, i, result = 0;
        scanf("%d", n);
        for (i = 0; i < n; i++)
            result += a;
    }

 

Answer: C

Correct answer: Multiplication of a and n.

The loop runs n times and each pass adds a to result, so result ends up as \(a + a + \cdots + a = a \times n\) - repeated addition standing in for multiplication. (The scanf call is written scanf("%d", n) without the &, which is a bug in its own right; the question is about what the loop computes.)

Please log in or register to report this question.

95.

What is the output of this C code?    

void main()
    {
        int x = 1, z = 3;
        int y = x << 3;
        printf(" %d\n", y);
    }

 

Answer: D

Correct answer: 8

<< shifts the bits of x left by z places, and each place doubles the value:

\[ 1 \ll 3 = 1 \times 2^3 = 8 \]

So y is 8. Shifting left by \(n\) is multiplication by \(2^n\), which is why compilers turn such multiplications into shifts.

Please log in or register to report this question.

96.

What is the output of this C code?    

int main()
    {
        int c = 2 ^ 3;
        printf("%d\n", c);
    }

 

Answer: A

Correct answer: 1

^ is bitwise exclusive OR, not exponentiation - a common trap for anyone coming from a language where ^ means “to the power of”:

\[ 2 = 010_2, \quad 3 = 011_2, \quad 010 \oplus 011 = 001_2 = 1 \]

So c is 1, not 8.

Please log in or register to report this question.

97.

What is the output of this C code?    

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

 

Answer: C

Correct answer: 2 1

Assignment is right-associative, so a += b -= a; groups as a += (b -= a). The inner assignment runs first: b = 2 - 1 = 1, and its value is 1. Then a += 1 gives a = 2. The output is 2 1.

Please log in or register to report this question.

98.

What is the output of this C code?

 void main()
    {
        int a = 5, b = -7, c = 0, d;
        d = ++a && ++b || ++c;
        printf("\n%d%d%d%d", a, b, c, d);
    }

 

Answer: D

Correct answer: 6 -6 0 1

Evaluate left to right, remembering that any non-zero value is true.

  • ++a makes a 6, which is true.
  • ++b makes b -6 - non-zero, so also true.
  • The && is therefore true, so || short-circuits and ++c never runs: c stays 0.
  • d gets the value of the whole expression, 1.

Output: 6 -6 0 1.

Please log in or register to report this question.

99.

What is the correct and fully portable way to obtain the most significant byte of an unsigned integer x?

Answer: D

Correct answer: x > > ( CHAR_BIT * (sizeof(int) - 1))

The most significant byte must be shifted down by the width of all the bytes below it. The portable way to say that is CHAR_BIT * (sizeof(int) - 1) - the number of bits in a byte times the number of bytes below the top one.

x >> 24 assumes a 4-byte int and an 8-bit char. x & 0xFF00 masks the second byte, not the top one, and does not shift it down. The variant with sizeof(int) - 3 is simply the wrong count.

Please log in or register to report this question.

Answer: B

Correct answer: The local variable is given a priority.

The inner declaration shadows the outer one: within the block where the local is declared, the name refers to the local, and the global is simply not visible. It is not an error, and it does not depend on declaration order. The global can still be reached in C++ with ::name, but plain C offers no such escape.

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