C Programming Questions and Answers – Relational & Logical Operators
C Programming Questions and Answers – Relational & Logical Operators
1.What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 1, y = 0, z = 5;
int a = x && y || z++;
printf(“%d”, z);
}
a) 6
b) 5
c) 0
d) Varies
View Answer
Answer:a
2.What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 1, y = 0, z = 5;
int a = x && y && z++;
printf(“%d”, z);
}
a) 6
b) 5
c) 0
d) Varies
View Answer
Answer:b
3.What is the output of this C code?
#include <stdio.h>
int main()
{
int x = 1, y = 0, z = 3;
x > y ? printf(“%d”, z) : return z;
}
a) 3
b) 1
c) Compile time error
d) Run time error
View Answer
Answer:c
4.What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 1, z = 3;
int y = x << 3;
printf(” %d\n”, y);
}
a) -2147483648
b) -1
c) Run time error
d) 8
View Answer
Answer:d
5.What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 0, y = 2, z = 3;
int a = x & y | z;
printf(“%d”, a);
}
a) 3
b) 0
c) 2
d) Run time error
View Answer
Answer:a
6.What is the final value of j in the below code?
#include <stdio.h>
int main()
{
int i = 0, j = 0;
if (i && (j = i + 10))
//do something
;
}
a) 0
b) 10
c) Depends on the compiler
d) Depends on language standard
View Answer
Answer:a
7.What is the final value of j in the below code?
#include <stdio.h>
int main()
{
int i = 10, j = 0;
if (i || (j = i + 10))
//do something
;
}
a) 0
b) 20
c) Compile time error
d) Depends on language standard
View Answer
Answer:a
8.What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 1;
if (i++ && (i == 1))
printf(“Yes\n”);
else
printf(“No\n”);
}
a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer:b
9.Are logical operators sequence points?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer:a
10.Does logical operators in C language are evaluated with short circuit?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer:a
11.Result of a logical or relational expression in C is
a) True or False
b) 0 or 1
c) 0 if expression is false and any positive number if expression is true
d) None of the mentioned
View Answer
Answer:b
12.What will be the value of d in the following program?
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf(“%d”, d);
}
a) Syntax error
b) 1
c) 5
d) 10
View Answer
Answer:b
13.What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 3;
b != !a;
c = !!a;
printf(“%d\t%d”, b, c);
}
a) 5 1
b) 0 3
c) 5 3
d) 1 1
View Answer
Answer:a
14.Which among the following is NOT a logical or relational operator?
a) !=
b) ==
c) ||
d) =
View Answer
Answer:d
15.What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 10;
if (a == a–)
printf(“TRUE 1\t”);
a = 10;
if (a == –a)
printf(“TRUE 2\t”);
}
a) TRUE 1
b) TRUE 2
c) TRUE 1 TRUE 2
d) Compiler Dependent
View Answer
Answer:d
Explanation: This is a sequence point problem and hence the result will be implementation dependent
16.Relational operators cannot be used on:
a) structure
b) long
c) strings
d) float
View Answer
Answer: a