C Questions and Answers Part-7

1. Comment on the output of the following C code.
#include <stdio.h>
int main()
{
int i, n, a = 4;
scanf("%d", &n);
for (i = 0; i < n; i++)
a = a * 2;
}
a) Logical Shift left
b) No output
c) Arithmetic Shift right
d) Bitwise exclusive OR

Answer: b

2. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int x = 97;
int y = sizeof(x++);
printf("x is %d", x);
}
a) X is 97
b) X is 98
c) X is 99
d) Run time error

Answer: a

3. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d%d%d", x, y, z);
}
a) 323
b) 223
c) 322
d) 233

Answer: d

4. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int x = 4;
int *p = &x;
int *k = p++;
int r = p - k;
printf("%d", r);
}
a) 4
b) 8
c) 1
d) Run time error

Answer: c

5. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int a = 5, b = -7, c = 0, d;
d = ++a && ++b || ++c;
printf("\n%d%d%d%d", a, b, c, d);
}
a) 6 -6 0 0
b) 6 -5 0 1
c) -6 -6 0 1
d) 6 -6 0 1

Answer: d

6. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int a = -5;
int k = (a++, ++a);
printf("%d\n", k);
}
a) -3
b) -5
c) 4
d) Undefined

Answer: a

7. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = 2;
x = x << 1;
printf("%d\n", x);
}
a) 4
b) 1
c) Depends on the compiler
d) Depends on the endianness of the machine

Answer: a

8. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = -2;
x = x >> 1;
printf("%d\n", x);
}
a) 1
b) -1
c) 231 – 1 considering int to be 4 bytes
d) Either -1 or 1

Answer: b

9. What will be the output of the following C code?
#include <stdio.h>
int main()
{
if (~0 == 1)
printf("yes\n");
else
printf("no\n");
}
a) Yes
b) No
c) compile time error
d) undefined

Answer: b

10. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = -2;
if (!0 == 1)
printf("yes\n");
else
printf("no\n");
}
a) yes
b) no
c) run time error
d) undefined

Answer: a