Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : sizeof operator - 2Next Topic : Bitwise operator - 2

This section focuses on C interview questions, tricky questions, and puzzles, and viva questions based on “bitwise operator”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, viva exams and company interviews), placements, entrance exams and other competitive exams. These programming questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate, engineering student, bca student, mca student, mcs students or an experienced IT professional. It will improve their understanding of C Language. Here is a listing of C questions and puzzles on “bitwise operator” along with answers and/or explanations.

Q1: What will be the output of the following C code?
#include <stdio.h>
int main()
{
int a = ~0;
printf("%d ", a);
}
a)  -1
b)  1
c)  2
d)  -2
Correct Answer : a
Explanation : Learn bitwise operators from scratch


Q2: if a and b are two integer variables then what will the following code do?
c=(a+b+abs(a-b))/2;
a)  c will store the maximum between a and b
b)  c will store the mimimum between a and b
c)  c will store the average value of a and b
d)  none of these
Correct Answer : a
Explanation : if a=3 and b=2 then 3+2+1/2 results in 3
if a=3 and b=4 then 3+4+1/2 results in 4
so equation (a+b+abs(a-b))/2 gives the max value between a and b.


Q3: What will be the output of following code?

int main()
{
if(!-1 == ~-1)
printf("Good one");
else
printf("Will try improving");
return 0;
}
a)  Good one
b)  Will try improving
c)  Compile time error
d)  Runtime error
Correct Answer : a
Explanation : -1 is not zero value, so !-1 will yields 0 and ~-1 also yields 0, so result of 0==0 is 1.


Q4: What will be the output of this c code?
int main() 
{ int num=12;
  printf("%d", -~num); 
  return 0; 
} 
a)  13
b)  -13
c)  -12
d)  12
Correct Answer : a
Explanation : -~num increases the value of operand by 1.


Q5: Which of the following statement is correct, if we want to check whether two integers a and b have same sign or different sign.

1. printf((a^b)>=0?"number have same signs":"Number have different signs");

2. printf((a^b)<=0?"number have same signs":"Number have different signs");

3.printf((a-b)>=0?"number have different signs":"Number have same signs");

4. printf((a+b)>0?"number have same signs":"Number have different signs");
a)  Only 1
b)  Both 1 and 4
c)  Both 2 and 3
d)  All except 1
Correct Answer : a
Explanation : (a^b) always results a value that is greater than or equals to 0, when both a and b have same sign bit, if a and b have different sign bits then the result will be a negative value.
To learn c from scratch subscribe our youtube channel.


Q6: What will be the output of the following C code?

    #include <stdio.h>
    int main()
    {
        int n = 4;
        if (n >> 2)
           printf("%d", n);
        else
           printf("%d",n);
    }
a)  1
b)  4
c)  2
d)  None of these
Correct Answer : b
Explanation : n>>2 will results in 1, so the condition of if becomes true, but it will not change the value of n.
Learn working of bitwise operators


Q7: Predict the output.
#include <stdio.h> 
int main() 
{  int y = 0; 
   int x = (~y == 1); 
   printf("%d", x); 
   return 0; 
} 
a)  1
b)  0
c)  -1
d)  2
Correct Answer : b
Explanation : Here , precedence of ~ is higher than ==.
So statement is solved as x=((~y)==1).
~y is -1, so -1==1 results in 0.


Previous Topic : sizeof operator - 2Next Topic : Bitwise operator - 2