Here is a listing of C programming questions on “If-then-else Statements” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include -
void main()
-
{ -
int x = 5;
-
if (x < 1)
-
printf("hello");
-
if (x == 5)
-
printf("hi");
-
else -
printf("no");
-
}
a) hi
b) hello
c) no
d) error
Answer: a
Clarification: None.
2. What will be the output of the following C code?
-
#include -
int x;
-
void main()
-
{ -
if (x)
-
printf("hi");
-
else -
printf("how are u");
-
}
a) hi
b) how are you
c) compile time error
d) error
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include -
void main()
-
{ -
int x = 5;
-
if (true);
-
printf("hello");
-
}
a) It will display hello
b) It will throw an error
c) Nothing will be displayed
d) Compiler dependent
Answer: b
Clarification: None.
4. What will be the output of the following C code?
-
#include -
void main()
-
{ -
int x = 0;
-
if (x == 0)
-
printf("hi");
-
else -
printf("how are u");
-
printf("hello");
-
}
a) hi
b) how are you
c) hello
d) hihello
Answer: d
Clarification: None.
5. What will be the output of the following C code?
-
#include -
void main()
-
{ -
int x = 5;
-
if (x < 1);
-
printf("Hello");
-
-
}
a) Nothing
b) Run time error
c) Hello
d) Varies
Answer: c
Clarification: None.
6. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)
-
#include -
void main()
-
{ -
double ch;
-
printf("enter a value between 1 to 2:");
-
scanf("%lf", &ch);
-
switch (ch)
-
{ -
case 1:
-
printf("1");
-
break;
-
case 2:
-
printf("2");
-
break;
-
} -
}
a) Compile time error
b) 1
c) 2
d) Varies
Answer: a
Clarification: None.
7. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)
-
#include -
void main()
-
{ -
char *ch;
-
printf("enter a value between 1 to 3:");
-
scanf("%s", ch);
-
switch (ch)
-
{ -
case "1":
-
printf("1");
-
break;
-
case "2":
-
printf("2");
-
break;
-
} -
}
a) 1
b) 2
c) Compile time error
d) No Compile time error
Answer: c
Clarification: None.
8. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)
-
#include -
void main()
-
{ -
int ch;
-
printf("enter a value between 1 to 2:");
-
scanf("%d", &ch);
-
switch (ch)
-
{ -
case 1:
-
printf("1n");
-
default:
-
printf("2n");
-
} -
}
a) 1
b) 2
c) 1 2
d) Run time error
Answer: c
Clarification: None.
9. What will be the output of the following C code? (Assuming that we have entered the value 2 in the standard input)
-
#include -
void main()
-
{ -
int ch;
-
printf("enter a value between 1 to 2:");
-
scanf("%d", &ch);
-
switch (ch)
-
{ -
case 1:
-
printf("1n");
-
break;
-
printf("Hi");
-
default:
-
printf("2n");
-
} -
}
a) 1
b) Hi 2
c) Run time error
d) 2
Answer: d
Clarification: None.
10. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)
-
#include -
void main()
-
{ -
int ch;
-
printf("enter a value between 1 to 2:");
-
scanf("%d", &ch);
-
switch (ch, ch + 1)
-
{ -
case 1:
-
printf("1n");
-
break;
-
case 2:
-
printf("2");
-
break;
-
} -
}
a) 1
b) 2
c) 3
d) Run time error
Answer: b
Clarification: None.
