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

Expressions Questions and Answers

1.

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.

2.

What is the output of this C code?    

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

 

Answer: B

Correct answer: -1

In C the result of % takes the sign of the dividend, and the language requires that (a/b)*b + a%b == a. Since -5 / 4 truncates toward zero to \(-1\), we need \(-1 \times 4 + r = -5\), giving \(r = -1\). So the program prints -1.

Please log in or register to report this question.

3.

What is the value of the below assignment expression
      (x = foo()) in the statement (x = foo()) != 1, considering that foo() returns 2?

Answer: A

Correct answer: 2

The value of an assignment expression is the value assigned. foo() returns 2, so x becomes 2 and the expression (x = foo()) itself has the value 2. That value is then what the surrounding != 1 compares against - and the comparison, being a separate expression, would yield 1.

Please log in or register to report this question.

4.

What will be the output of the following program?
 

#include <stdio.h>
int main()
{
int num = 0, z = 3;
if ( ! (num <= 0) || ++z )
printf( "%d %d ", ++num + z++, ++z );
else
printf( "%d %d", --num + z--, --z);
return 0;
}

 

Answer: B

Correct answer: 6 5

num is 0, so num <= 0 is true and !(num <= 0) is 0. The || must therefore evaluate its right operand: ++z takes z to 4 and is non-zero, so the if branch runs.

In printf("%d %d ", ++num + z++, ++z) the arguments are evaluated right to left on the usual compilers: ++z makes z 5 and yields 5; then ++num gives 1 and z++ yields 5, so the first argument is 6. Output: 6 5. Note that the order of argument evaluation is unspecified, so this particular pairing is compiler-dependent.

Please log in or register to report this question.

5.

for c = 2, value of c after c <<= 1;

Answer: D

Correct answer: c = 4;

<<= shifts left and assigns. Shifting left by one position doubles the value, because each bit moves to the next higher power of two:

\[ 2 = 10_2 \;\rightarrow\; 100_2 = 4 \]

So c becomes 4.

Please log in or register to report this question.

6.

What will be the output of the following program?

#include <stdio.h>

int main()
{
static unsigned int a = 23;
register unsigned char c = 'R';
auto long unsigned q = 345L;
static long signed p = 345L;
printf("a = %u c = %c", a ,c);
printf("\nq = %ld p = %ld", q, p);
return 0;
}

 

Answer: A

Correct answer: a=23 c=R q = 345 p = 345

Each variable prints its own value, and the storage classes affect only lifetime and placement, not the values: a is 23, and c printed with %c shows the character R. The two long variables both hold 345 and print with %ld as 345. The only real trap is the case of the letters in the options - the format string writes a lower-case a.

Please log in or register to report this question.

7.

Relational operators cannot be used on:

Answer: A

Correct answer: structure

Relational operators work on scalars - arithmetic types and pointers - because comparison needs a single value to order. A struct is an aggregate of several members, so s1 < s2 has no meaning and is rejected; even == is not defined for structures, since padding bytes would make a byte-wise comparison unreliable. Comparing them means comparing their members one by one.

Please log in or register to report this question.

8.

Are logical operators in C evaluated with short-circuit evaluation?

Answer: A

Correct answer: True

Both && and || short-circuit, and the standard requires it rather than leaving it to the compiler. If the left operand of && is false, or of || is true, the result is already determined and the right operand is not evaluated at all - side effects in it simply do not happen.

Please log in or register to report this question.

9.

What is the output of this C code?    

int main()
    {
        int a = 10, b = 10;
        if (a = 5)
        b--;
        printf("%d, %d", a, b--);
    }

 

Answer: C

Correct answer: a = 5, b = 9

if (a = 5) assigns 5 to a, and the value of that assignment, 5, is non-zero and so true. The body runs, taking b from 10 to 9. The printf then shows a as 5 and, because b-- is a post-decrement, the value passed is the current 9 - the decrement to 8 happens after the value has been handed over, and nothing prints afterwards.

Please log in or register to report this question.

10.

Which of the following data type will throw an error on modulus operation(%)?

Answer: C

Correct answer: float

% requires integer operands. char, short and int are all integer types and are promoted to int before the operation, so they are fine; float is not, and using it gives a compile-time error. Use fmod() for a floating-point remainder.

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