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

Expressions Questions and Answers

21.

A function cannot be defined inside another function

Answer: A

Correct answer: True

C has no nested functions: every function is defined at file scope, and a function definition inside another is a syntax error. (GCC offers nested functions as a non-standard extension, but that is not C.) What can be nested is a block, and a block may declare its own variables.

Please log in or register to report this question.

22.

What is the output of this C code?    

int main()
    {
        int x = 1, y = 0, z = 3;
        x > y ? printf("%d", z) : return z;
    }

 

Answer: C

Correct answer: Compile time error

The conditional operator needs expressions in both branches, and return z is a statement. x > y ? printf("%d", z) : return z; is therefore a syntax error. Written properly it would be if (x > y) printf("%d", z); else return z;.

Please log in or register to report this question.

23.

What is the output of this C code?    

void main()
    {
        int x = 97;
        int y = sizeof(x++);
        printf("x is %d", x);
    }

 

Answer: A

Correct answer: x is 97

sizeof is evaluated at compile time from the type of its operand, and its operand is not executed. So x++ never runs, y becomes sizeof(int), and x is still 97. (Variable-length arrays are the one exception where the operand is evaluated.)

Please log in or register to report this question.

24.

What is the output of this C code?    

void main()
    {
        int a = -5;
        int k = (a++, ++a);
        printf("%d\n", k);
    }

 

Answer: D

Correct answer: -3

The comma operator evaluates its left operand, discards the result, then evaluates the right operand and yields that. So a++ takes a to -4, then ++a takes it to -3 and produces -3. k is therefore -3. There is a sequence point at the comma, so this is well defined.

Please log in or register to report this question.

25.

What will be the output of the following program?

#include <stdio.h>

void fun();
int main()
{
fun();
return 0;
}
void fun()
{
auto int i = 1;
register char a = 'D';
static int p = 0;
printf("%d %d %ld", i, a, p);
}

 

Answer: D

Correct answer: 1 68 0

Each declaration in fun behaves as its storage class says:

  • auto int i = 1; prints as 1.
  • register char a = 'D'; is printed with %d, not %c, so it comes out as the character’s code - 68 in ASCII.
  • static int p = 0; prints as 0.

So the output is 1 68 0. The trap is the format specifier: a char is an integer type, and %d shows its numeric value.

Please log in or register to report this question.

26.

What is the output of this C code?

int main()
    {
        int i = 5;
        int l = i / -4;
        int k = i % -4;
        printf("%d %d\n", l, k);
        return 0;
    }

 

Answer: B

Correct answer: -1 1

Integer division truncates toward zero, so \(5 / (-4) = -1\) rather than \(-2\). The remainder then follows from \((a/b) \times b + a\%b = a\):

\[ (-1) \times (-4) + r = 5 \;\Rightarrow\; r = 1 \]

The remainder takes the sign of the dividend, which is positive here, so the output is -1 1.

Please log in or register to report this question.

27.

What is the output of this C code?

int main()
    {
        int i = 2;
        int j = ++i + i;
        printf("%d\n", j);
    }

 

Answer: A

Correct answer: 6

++i takes i to 3 and contributes 3, and the second operand then reads 3, giving \(3 + 3 = 6\). As with any expression that both modifies and reads the same variable without an intervening sequence point, this is formally undefined behaviour; 6 is simply what the common compilers give.

Please log in or register to report this question.

28.

What is the output of this C code?

void main()
    {
        int y = 3;
        int x = 7 % 4 * 3 / 2;
        printf("Value of x is %d", x);
    }

 

Answer: A

Correct answer: Value of x is 4

%, * and / all have the same precedence and associate left to right, so the expression is grouped as ((7 % 4) * 3) / 2:

\[ 7 \bmod 4 = 3, \quad 3 \times 3 = 9, \quad 9 / 2 = 4 \]

The final division is integer division, so the 0.5 is discarded and x is 4. The declaration of y is never used.

Please log in or register to report this question.

29.

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.

30.

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.

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