Multiple Choice Questions and Answers for Bitwise Operators in C Language
https://www.computersprofessor.com/2017/09/multiple-choice-questions-and-answers_23.html
List of C interview questions on “Bitwise Operators” along with answers, explanations and/or solutions:
1. What is the output of this C code?
#include
int main()
{
int c = 2 ^ 3;
printf("%d\n", c);
}
a) 1
b) 8
c) 9
d) 0
b) 8
c) 9
d) 0
Answer: a
2. What is the output of this C code?
#include
int main()
{
unsigned int a = 10;
a = ~a;
printf("%d\n", a);
}
a) -9
b) -10
c) -11
d) 10
b) -10
c) -11
d) 10
Answer:c
3. What is the output of this C code?
#include
int main()
{
if (7 & 8)
printf("Honesty");
if ((~7 & 0x000f) == 8)
printf("is the best policy\n");
}
a) Honesty is the best policy
b) Honesty
c) is the best policy
d) No output
b) Honesty
c) is the best policy
d) No output
Answer:c
4. What is the output of this C code?
#include
int main()
{
int a = 2;
if (a >> 1)
printf("%d\n", a);
}
a) 0
b) 1
c) 2
d) No Output.
b) 1
c) 2
d) No Output.
Answer:c
5. Comment on the output of this C code?
#include
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
b) No output
c) Arithmetic Shift right
d) bitwise exclusive OR
Answer:b
6. What is the output of this C code?
#include
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
b) x is 98
c) x is 99
d) Run time error
Answer:a
7. What is the output of this C code?
#include
void main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d%d%d", x, y, z);
}
a) 3 2 3
b) 2 2 3
c) 3 2 2
d) 2 3 3
b) 2 2 3
c) 3 2 2
d) 2 3 3
Answer:d
8. What is the output of this C code?
#include
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
b) 8
c) 1
d) Run time error
Answer:c
9. What is the output of this C code?
#include
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
b) 6 -5 0 1
c) -6 -6 0 1
d) 6 -6 0 1
Answer:d
10. What is the output of this C code?
#include
void main()
{
int a = -5;
int k = (a++, ++a);
printf("%d\n", k);
}
a) -3
b) -5
c) 4
d) Undefined
b) -5
c) 4
d) Undefined
Answer:a
11. What is the output of this C code?
#include
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
b) 1
c) Depends on the compiler
d) Depends on the endianness of the machine
Answer:a
12. What is the output of this C code?
#include
int main()
{
int x = -2;
x = x >> 1;
printf("%d\n", x);
}
a) 1
b) -1
c) 2 ^ 31 – 1 considering int to be 4 bytes
d) Either b or c
b) -1
c) 2 ^ 31 – 1 considering int to be 4 bytes
d) Either b or c
Answer:b
13. What is the output of this C code?
#include
int main()
{
if (~0 == 1)
printf("yes\n");
else
printf("no\n");
}
a) yes
b) no
c) Compile time error
d) Undefined
b) no
c) Compile time error
d) Undefined
Answer:b
14. What is the output of this C code?
#include
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
b) no
c) Run time error
d) Undefined
Answer:a
15. What is the output of this C code?
#include
int main()
{
int y = 0;
if (1 |(y = 1))
printf("y is %d\n", y);
else
printf("%d\n", y);
}
a) y is 1
b) 1
c) Run time error
d) Undefined
b) 1
c) Run time error
d) Undefined
Answer:a
16. What is the output of this C code?
#include
int main()
{
int y = 1;
if (y & (y = 2))
printf("true %d\n", y);
else
printf("false %d\n", y);
}
a) true 2
b) false 2
c) Either option a or option b
d) true 1
b) false 2
c) Either option a or option b
d) true 1
Answer:c