Quiz on Relational and Logical Operator in C

Quiz on Relational and Logical operator in C | Relational operators are used to check given condition or expression is true or false. The Combination of some operands and constants with relational operators is called a relational expression.

If the relation is true then the value of the relational expression is 1 and if the relation is false then the value of the expression is 0. In C language (with relational operator), every non-zero value is 1 i.e. true.

C language supports three logical operators:- AND (&&), OR(||), and NOT(!). An expression that combines two or more relational expressions is termed a logical expression. Like the simple relational expressions, a logical expression also yields a value of true (1) or false(0).

If you finding difficulties to solve these questions then first you should learn these:- Operators in CBitwise operators in C

Relational and Logical operator Quiz in C

Find the output of the programs given in Quiz on Relational and Logical operator in C?

Q1) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int x, y, z;
  x = 9 > 8 > 7;
  y = 9 > 8 > 0;
  z = 9 > 8 > 1;
  printf("%d %d %d", x, y, z);
  return 0;
}

a) 0 0 1
b) 0 1 0
c) 1 0 0
d) 0 1 1

View Answer Answer:- b) 0 1 0

For >, execution starts from left to right. In statement x = 9>8>7; first 9>8 will be evaluated and the result is 1. Now, x = 1>7; so, result will be 0. Similarly, y and z will be calculated.

Q2) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a=-1, b=1, c, d;
  c = !a && b;
  d = !a || b;
  printf("%d %d %d %d",a,b,c,d);
  return 0;
}

a) -1 1 1 0
b) -1 1 0 0
c) -1 1 1 1
d) -1 1 0 1

View Answer Answer:- d) -1 1 0 1

Q3) Find the output of the given C program.

#include<stdio.h>
int main()
{
  printf("%d ",5>2 && 1<2);
  printf("%d ", 4>5 || 2<1);
  printf("%d ",!(2>3));
  return 0;
}

a) 0 1 0
b) 0 1 1
c) 1 0 0
d) 1 0 1

View Answer Answer:- d) 1 0 1

Q4) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a;
  a = 'a' > 'A';
  printf("%d",a);
  return 0;
}

a) Garbage Value
b) 0
c) 1

View Answer Answer:- c) 1

The decimal value of ‘a’ and ‘A’ is 97 and 65 respectively. 97 > 65 gives true(1).

Q5) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int x=0, y=0;
  printf("%d ", x++ && ++y);
  printf("%d %d\t",x,y);
  
  x = 0, y=1;
  printf("%d ", ++x || ++y);
  printf("%d %d",x,y);
  return 0;
}

a) 0 1 0 1 1 2
b) 0 1 1 1 1 1
c) 0 1 0 1 1 1
d) 0 1 1 1 1 2

View Answer Answer:- c) 0 1 0 1 1 1

For the logical AND (&&) operator, if the left operand yields a false (0) value, then the compiler does not evaluate the right operand and directly gives the result false (0). For the logical OR ( || ) operator, if the left operand yields a true (1) value, then the compiler does not evaluate the right operand and directly gives result 1 (true).

Initially, x=0, y=0;

In the expression “x++ && ++y”, compiler starts evaluating from left to right. Here x++ means first use x value then increase 1, currently x = 0; In logical AND (&&) operation, if left side expression is false then the compiler doesn’t evaluate right-side expression because the overall expression will be always false. In this line “y” variable is not used.

Therefore “x++ && ++y” gives 0. Now, x=1, y=0;

Again x, and y values are updated,
Currently, x = 0, y=1;

In the expression “++x || ++y” compiler starts evaluating from left to right. Here ++x means first increase 1 then use x value, so x becomes 1. In the logical (||) operation, if the left-side expression is true then the compiler will not evaluate the right-side expression value because the overall result will be always true. In this line “y” variable is not used.

Hence “++x || ++y” gives 1.
Now, x=1, y=1.

Q6) Find the output of the given C program.

#include<stdio.h>
int main()
{
  printf("%d ", -9 && 9);
  printf("%d", -9 || 9);
  return 0;
}

a) 0 0
b) 0 1
c) 1 1
d) 1 0

View Answer Answer:- c) 1 1

In C, non-zero value treated as true (1). So, we can write -9 && 9 as 1 && 1, which yeilds 1. Similarly, 1 || 1 produces 1.

Q7) Find the output of the given C program.

#include< stdio.h>
int main()
{
  int x, y, z;
  x = 9;
  y = 10;
  z = x == y;
  printf("%d", z);
  return 0;
}

a) None of these
b) Garbage Value
c) 0
d) 1

View Answer Answer:- c) 0

9 is not equal to 10.

Q8) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a=5;
  int b= !9 && a;
  printf("%d",b);
  return 0;
}

a) 1
b) Garbage Value
c) 0

View Answer Answer:- c) 0

Any non-zero value can be treated as true so, !(true) && true can be wriiten as false && ture, which produces false(0).

Q9) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int i=10;
  i= !i > 14;
  printf("%d",i);
  return 0;
}

a) 0
b) 1
c) Garbage Value

View Answer Answer:- a) 0

Comparison of constant ‘14’ with boolean expression is always false.

Q10) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int x= 90;
  printf("%d %d %d", x>50, x=5, x>10);
  return 0;
}

a) 0 0 0
b) 0 1 1
c) 0 5 1
d) 0 18 1

View Answer Answer:- c) 0 5 1

The expression is evaluated from right to left. First, expression x>10 will be calculated. 90>10 so the result is 1. Now, x=5 is an assignment expression, not a relational expression. Here the value of 5 will print by printf() and the value of x updated to 5. Third expression x> 10 gives 0 or false because 5<10.

Q11) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a;
  a = !8;
  printf("%d",a);
  return 0;
}

a) Garbage Value
b) 0
c) 1

View Answer Answer:- b) 0

Any non-zero value can be treated as true so, !(true) gives false(0).

Q12) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a,b,c;
  a = b = 10;
  c = a == b;
  printf("%d %d %d",a,b,c);
  return 0;
}

a) 10 10 10
b) 10 10 1
c) 1 1 1
d) 10 10 0

View Answer Answer:- b) 10 10 1

a=b=10 so, value of a and b becomes 10. c= a==b; 10==10 gives 1, so value of c becomes 1.

Q13) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a,b;
  a = 9;
  b = 5;
  printf("%d ", a == b);
  printf("%d", a = b);
  return 0;
}

a) 1 0
b) 0 5
c) 1 5
d) 0 0

View Answer Answer:- b) 0 5

a==b gives 0 because 9 is not equal to 5. a=b gives 5 because the value of b is 5.

Q14) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int x=55, y=20, a;
  a = (x == 55 || y>20);
  printf("%d ", a);
  return 0;
}

a) 1
b) Garbage Value
c) 0

View Answer Answer:- a) 1

x==55 gives 1 because 55 is equal to 55. We are working with logical OR (||) operator and its left operand gives 1 so, final result 1 will stored in variable a.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

2 thoughts on “Quiz on Relational and Logical Operator in C”

Leave a Comment

Your email address will not be published. Required fields are marked *