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

Expressions Questions and Answers

21.

Which of the following is the better approach to do the operation i = i * 16?

Answer: B

Correct answer: Shift left by 4 bits

Shifting left by \(n\) places multiplies by \(2^n\), and \(16 = 2^4\), so i << 4 is the same as i * 16 for non-negative values - done in a single instruction rather than a multiply. In practice any modern compiler makes this substitution for you, so writing i * 16 is clearer and costs nothing.

Please log in or register to report this question.

22.

extern int s;
int t;
static int u;

main()
{

}

Which of s, t and u are available to a function present in another file?

Answer: A

Correct answer: s and t

The question is one of linkage: which of these names can be referred to from a different translation unit.

extern int s; declares s with external linkage without defining it. The name refers to a single object shared across the whole program, defined in some other file, so a function in another file names the same s. Available.

int t; at file scope also has external linkage. It is a tentative definition: if nothing else in the file defines t, the compiler emits a definition initialised to zero. Another file can reach it by declaring extern int t;. Available.

static int u; at file scope is the exception. The static keyword here has nothing to do with lifetime - all three of these objects live for the whole run of the program - it sets internal linkage, confining the name to this translation unit. No other file can reach u, and another file may define its own unrelated u without a clash. Not available.

So s and t are visible outside the file and u is not. This record was keyed to “Only s”, which overlooks that a bare file-scope definition is external by default; the option reading “S & u” has been corrected to “s and t” and the key moved to it.

Please log in or register to report this question.

23.

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.

24.

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.

25.

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.

26.

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.

27.

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.

28.

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.

30.

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.

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