Condition MCQ
Q 1) Choose a C Conditional Operator from the list.
- ?:
- :?
- :<
- <:
Answer:- (A).
Explanations :?: = Question Mark Colon is also called C Ternary Operator.
Explanations :?: = Question Mark Colon is also called C Ternary Operator.
- Comparison Operator
- If-Else Operatorg
- Binary Operator
- Ternary Operator
Answer:- (C).
- condition ? expression1 : expression2
- condition : expression1 ? expression2
- condition ? expression1 < expression2
- condition < expression1 ? expression2
Answer:- (A).
Explanations :If the condition is true, expression 1 is evaluated. If the condition is false, expression 2 is evaluated.
Explanations :If the condition is true, expression 1 is evaluated. If the condition is false, expression 2 is evaluated.
int main() { int a=0; a = 5<2 ? 4 : 3; printf("%d",a); return 0; }
- 4
- 3
- 5
- 2
Answer:- (B).
Explanations :5<2 is false. So 3 will be picked and assigned to the variable a.
Explanations :5<2 is false. So 3 will be picked and assigned to the variable a.
int main() { int a=0; a = printf("4"); printf("%d",a); return 0; }
- 04
- compiler error
- 40
- 41
Answer:- (D).
Explanations :a = printf("4"); First printf prints 4. printf() returns 1. Now the variable a=1; So 1 is printed next.
Explanations :a = printf("4"); First printf prints 4. printf() returns 1. Now the variable a=1; So 1 is printed next.
int main() { int a=0; a = 5>2 ? printf("4"): 3; printf("%d",a); return 0; }
- compiler error
- 14
- 41
- 0
Answer:- (C).
Explanations :5>2 is true. So expression1 i.e printf("4) is executed printing 4. Function printf() returns 1. So a value is 1.
Explanations :5>2 is true. So expression1 i.e printf("4) is executed printing 4. Function printf() returns 1. So a value is 1.
int main(); { int a=0; a = (5>2) ? : 8; printf("%d",a); return 0; }
- 1
- 0
- 8
- compiler error
Answer:- (A).
Explanations :expression1 = empty
expression2 = 8
If no expression is specified, it will be treated as 1.
Explanations :expression1 = empty
expression2 = 8
If no expression is specified, it will be treated as 1.
int main(); { int a=0, b; a = (5>2) ? b=6: b=8; printf("%d %d",a, b); return 0; }
- 6 6
- 0 6
- 0 8
- compiler errore
Answer:- (D).
Explanations :Compiler error. a = (5>2) ? b=6: b=8; should be written as a = (5>2) ? b=6: (b=8); main.c: In function ‘main’: main.c:14:23: error: lvalue required as left operand of assignment a = (5>2) ? b=6: b=8;
Explanations :Compiler error. a = (5>2) ? b=6: b=8; should be written as a = (5>2) ? b=6: (b=8); main.c: In function ‘main’: main.c:14:23: error: lvalue required as left operand of assignment a = (5>2) ? b=6: b=8;
#include <stdio.h> int main() { int i; for(i=0; i < 5, i++) { int a = i; } printf("%d",a); return 0; }
- (x == y) Is x really equal to y. (x != y) Is x not equal to y.
- (x < y) Is x less than y (x > y) Is x greater than y
- (x <= y) Is x less than or equal to y. (x >= y) Is x greater than or equal to y
- All the above
Answer:- (D).
- else if is compulsory to use with if statement.
- else is compulsory to use with if statement.
- else or else if is optional with if statement.
- None of the above
Answer:- (C)
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.