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

Expressions Questions and Answers

31.

What is the value of the following expression?

i = 1;
i << 1 < 1>

Answer: A

Correct answer: 2

<< shifts the bits left, and one place left doubles the value:

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

The value of the expression is therefore 2. Shifting cannot produce a negative result here, since the sign bit of a small positive int is nowhere near the top.

Please log in or register to report this question.

32.

Which of the following is not an arithmetic operation?

Answer: D

Correct answer: a != 50;

!= is a relational operator: it compares and yields 1 or 0, and does not modify a at all. The other three are compound assignment operators - a *= 20 means a = a * 20, and likewise for /= and %=.

Please log in or register to report this question.

33.

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.

34.

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.

35.

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.

36.

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.

38.

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.

39.

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.

40.

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.

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