C Technical Interview Questions – Operators

These C Technical Interview Questions on Operators  post providing basic, intermediate and Hard level examples in C Operators. The practice of these examples makes you good in C programming.


What is the Output of following Code?
#include<stdio.h>
main()
{
int x=5 , y = 0 , a,b;
a = ++y || ++x ;
b = –y && ++x ;
printf(“%d,%d,%d,%d\n”,x,y,a,b);
}

  1. 5,0,1,0
  2. 5,2,1,0
  3. 5,0,1,1
  4. 7,0,1,0

Answer :

Ans : 1; In case of logical-OR operator, second expression will not execute if the first expression is true. In the sameway, in case of logical-AND operator, second expression will not execute if first expression is false.

What is the Output of following Code?
#include <stdio.h>
main()
{
int a = -1, b = 5, c = 0, d;
d = ++a && ++b || ++c;
printf(“%d %d %d %d”, a, b, c, d);
}

  1. 0,5,0,0
  2. 0,5,1,1
  3. 0,6,1,0
  4. 0,6,1,1

Answer :

Ans : 2; ++b will not execute because ++a value is 0.

What is the Output of following Code?
#include<stdio.h>
main()
{
int a=5,b=3,c=7,d=5,e,f ;
e = a++ != d–;
f = ++b == d++;
printf(“%d,%d”, b,e);
}

  1. 3, 1
  2. 24, 18
  3. 4, 1
  4. 4, 0

Answer :

Ans : 4; First expression is e = 5 != 5 , Second expression f = 4 == 4

What is the Output of following Code?
#include<stdio.h>
main()
{
int a=5,b=0,c=3,d;
d = a && b + c;
printf(“%d\n”,d);
}

  1. 3
  2. 1
  3. 0
  4. Compile time error

Answer :

Ans : 1; Arithmetic oeprators having higher priority than logical operators in C

What is the Output of following Code?
#include<stdio.h>
main()
{
int a=5,b=3,c=2,d=3,e,f,g,h;
e = (b++ == ++c)|| (–b != d–);
f = (b– != d++)|| (–c <= –a);
g = (c++ >= –d)&& (++b != –c);
h = (e++ <= –f)|| (++g != –c);
printf(“%d,%d,%d” , a,c,e);
}

  1. 4,1,3
  2. 5,3,1
  3. 5,2,2
  4. 4,2,1

Answer :

Ans : 3; Analyze expression by expression by printing values after each expression

What is the Output of following Code?
#include<stdio.h>
main()
{
int x;
x = 4%5 + 6%5;
printf (“x = %d”,x);
}

  1. 5
  2. Error : L value required
  3. 1
  4. 10

Answer :

Ans : 1; 4%5 value is 4 and 6%5 value is 1

What is the Output of following Code?
#include<stdio.h>
main()
{
int a=2,b=3,c=4,d;
d = a && b++ – 3 || ++c ;
printf(“%d,%d,%d,%d”,a,b,c,d);
}

  1. 2,3,4,1
  2. 2,4,4,1
  3. 2,4,5,1
  4. 2,3,5,1

Answer :

Ans : 3; a&&b++ result is 0, hence ++c value also increases

What is the Output of following Code?
#include<stdio.h>
main()
{
int a = 2;
a = !!++a;
printf(“%d\n”,a);
}

  1. 2
  2. 1
  3. 0
  4. Compile time error

Answer :

Ans : 2; Expression will evaluate like !!3 –> !0 –> 1

What is the Output of following Code?
#include<stdio.h>
int main(void)
{
int a=0, b=10 ;
a = b | a++ ;
printf(“a : %d \n”,a);
return 0;
}

  1. Error : Invalid operator
  2. 10
  3. 0
  4. 11

Answer :

Ans : 4; Bitwise-OR operation of a = 10 | 0 is 10, and value will be post incremented using a++, hence value is 11

What is the Output of following Code?
#include<stdio.h>
main()
{
int a = 5, b = 6, c = 4;
a = a & b ^ c;
printf(“a = %d”,a);
}

  1. 0
  2. 1
  3. 5
  4. 4

Answer :

Ans : 1; Check the priority of bitwise operators and evaluate

What is the Output of following Code?
#include<stdio.h>
int main(void)
{
int a, b=0, c ;
printf(“Enter a val : “);
scanf(“%d”,&a);
c = a == b || a ;
printf(“c : %d \n”,c);
return 0;
}

  1. 0
  2. 1
  3. input value
  4. garbage value

Answer :

Ans : 2; Expression result will be true with any input value either true or false

What is the Output of following Code?
#include <stdio.h>
main()
{
int a = 10, b = 5, c = 3;
b != !a;
c = !!a;
printf(“%d\t%d”, b, c);
}

  1. Compile time error
  2. 0,1
  3. 5,1
  4. 5,0

Answer :

Ans : 3;  First expression is dummy expression, hence evaluate only second expression

What is the Output of following Code?
#include <stdio.h>
int main()
{
int i = 5;
int j = i / -4;
int k = i % -4;
printf(“%d %d\n”, j, k);
return 0;
}

  1. -1 , 1
  2. -1, -1
  3. 1, -1
  4. 1, 1

Answer :

Ans : 1;

C Objective Interview Questions – Operators – 1

Share this