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

Expressions Questions and Answers

41.

What is the output of the following program?

#include <stdio.h>

int main()
{
static int a = 3;
printf("%d", a--);
return 0;
}

 

Answer: C

Correct answer: 3

a-- is a post-decrement: the current value is what the expression yields, and the decrement happens afterwards. So printf receives 3; a becomes 2 immediately after, but nothing prints it. The static qualifier matters only if the function is called again.

Please log in or register to report this question.

Answer: A

Correct answer: The duration for which the variable retains a given value during the execution of a program.

Longevity (or lifetime, or storage duration) is about time - how long the variable’s storage exists and keeps its value. That is a different question from scope, which is about place: the part of the program in which the name is visible. A static local has a short scope but the longest possible lifetime.

Please log in or register to report this question.

43.

Which of the following statement are correct?

(i) The maximum value a variable can hold depends upon its storage class.
(ii) By default all variables enjoy a static storage class.

Answer: D

Correct answer: Both I & II are incorrect

(i) is false: the range of a variable depends on its type - char, int, long - not on where it is stored. A static int and an auto int hold exactly the same range. (ii) is false: the default for a variable declared inside a function is auto, not static; only file-scope variables have static storage duration.

Please log in or register to report this question.

44.

What is the final value of j in the below code?

int main()
    {
        int i = 0, j = 0;
        if (i && (j = i + 10))
            //do something
            ;
    }

 

Answer: D

Correct answer: 0

&& short-circuits: since i is 0 the whole condition is already false, so the right operand (j = i + 10) is never evaluated and the assignment never happens. j keeps its initial value, 0. This guarantee is what makes idioms such as if (p && p->next) safe.

Please log in or register to report this question.

45.

What is the output of this C code?    

void main()
    {
        int k = 8;
        int x = 0 == 1 && k++;
        printf("%d%d\n", x, k);
    }

 

Answer: B

Correct answer: 0 8

&& short-circuits: if the left operand is false the right is never evaluated. 0 == 1 is false, so k++ never runs and k stays 8, while x gets the value 0. The output is therefore 0 8 - printed as 08, since the format string has no separator.

Please log in or register to report this question.

46.

What is the output of this C code  

void main()
    {
        unsigned int x = -5;
        printf("%d", x);
    }

 

Answer: C

Correct answer: -5

The bits assigned to x are the two’s-complement pattern of \(-5\); storing them in an unsigned int changes the interpretation, not the bits. %d then asks printf to read those same bits back as a signed int, so -5 is what appears. Using %u would print 4294967291 on a 32-bit int.

Please log in or register to report this question.

47.

What is the output of this C code?    

void main()
    {
        int x = 0, y = 2, z = 3;
        int a = x & y | z;
        printf("%d", a);
    }

 

Answer: A

Correct answer: 3

The bitwise operators have different precedences: & binds tighter than |, so the expression is (x & y) | z:

\[ (0 \;\&\; 2) = 0, \quad 0 \;|\; 3 = 3 \]

So a is 3. Note this differs from the logical operators only in that no short-circuiting occurs.

Please log in or register to report this question.

48.

What is the output of this C code?    

int main()
    {
        int x = 1, y = 0;
        x &&= y;
        printf("%d\n", x);
    }

 

Answer: A

Correct answer: Compile time error

There is no &&= operator in C. The compound assignment operators are += -= *= /= %= &= |= ^= <<= >>= - the bitwise &= exists, but the logical && has no assignment form. The program fails to compile.

Please log in or register to report this question.

49.

Operation “a = a * b + a” can also be written as:

Answer: D

Correct answer: All of the mentioned

All three compute \(a \times b + a\).

  • a *= b + 1; is a = a * (b + 1) = a b + a.
  • a = (b + 1) * a; is the same product written the other way round.
  • (c = a * b) != (a = c + a); stores \(ab\) in c and then \(c + a\) in a.

The third form is worth a warning: != does not sequence its operands, so relying on c being set before it is read is not something real code should do.

Please log in or register to report this question.

50.

What is the output of this C code?   

 void main()
    {
        int x = 4;
        int *p = &x;
        int *k = p++;
        int r = p - k;
        printf("%d", r);
    }

 

Answer: C

Correct answer: 1

Pointer subtraction returns the number of elements between the two pointers, not the number of bytes. k holds the value p had before the increment, and p++ moved p on by one int, so p - k is 1. The compiler divides the 4-byte gap by sizeof(int) for you.

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