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

Expressions Questions and Answers

41.

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

#include <stdio.h>
int main()
{
int n = 12, k;
printf("%d", (k = sizeof( n + 12.0))++);
return 0;
}

 

Answer: C

Correct answer: The code will result in a compile time error

(k = sizeof(n + 12.0))++ tries to increment the result of an assignment expression. That result is a value, not an object, so it is not a modifiable lvalue and the compiler rejects it. (Had the increment been dropped, sizeof(n + 12.0) would be 8 on a typical machine, since the int is promoted to double.)

Please log in or register to report this question.

42.

What is the output of this C code?    

int main()
    {
        int i = 10;
        int *p = &i;
        printf("%d\n", *p++);
    }

 

Answer: A

Correct answer: 10

++ binds tighter than *, but it is a post-increment, so *p++ means “dereference the current p, then advance p”. The value printed is therefore i itself, 10; the pointer moves on afterwards, to an address that is no longer valid.

Please log in or register to report this question.

43.

What is the output of this C code?    

void main()
    {
        int a = 5;
        int b = ++a + a++ + --a;
        printf("Value of b is %d", b);
    }

 

Answer: D

Correct answer: Undefined behaviour

b = ++a + a++ + --a; modifies a three times within one expression, with no sequence point between the changes. The standard leaves the result undefined - not merely unspecified - so any answer a particular compiler gives is accidental. This is why such expressions must be split into separate statements.

Please log in or register to report this question.

44.

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.

46.

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.

47.

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.

48.

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.

49.

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.

50.

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.

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