Interview Questions on C - Control Statements
1. How would you make decisions in your C Programme?
By Using Control statements :
If, if else, switch, while(), do while(), for()
2. Explain if else statement?
Syntax :-
if(expr1){ if-block-content }
else{ else-block-content }
Explanation :- here expr1 is condition. If expression1 is true then if-block-content will execute. If expression1 is false then else block.
3. Explain Ternary operator (or) ? : ; operator (or) Conditional Operator?
Syntax : - Expression1? Expression2 : Expression3;
Exanple :- x<3? Printf(“X is less than 3”); : Printf(“X is greater than or equal to 3”);
Explanation :- here Expression1 is condition. If Expression1 is true then expression2 will execute. If Expression1 is false then Expression3 will execute.
4. Rewrite below programme using ternary operator?
if(a<b){
Printf(“ a is less than b”);
}
else{
Printf(“a is greater than or equal to b”);
}
a<b? Printf(“ a is less than b”) : Printf(“ a is less than b”) ;
5. Can you print “Hello World” without using semicolon?
Yes, write printf statement as condtion in conditional statements.
Ex:- Main(){
if(printf(“Hello world”))
}
Note : Here condition will be true as it is not null or non-zero.
6. Explain switch statement?
switch statement allows to execute perticular block for list of cases.
Syntax :-
switch(expression){
case const-value: {
}
case const-value: {
}
case const-value: {
}
Default: {
}
}
Explanation :-
here Expression may be intger, character or Boolean.
Const-value is just value.
If expression value matches const-value then that block code will execute.
7. Can be the switch statement float or double values?
No, only integers,characters or Boolean.
8. Can we write conditions in switch statement?
Yes, it will take as Boolean.
9. Why would we keep ‘break;’ for every case?
To avoid execution of multiple cases.
If we don’t use break then all matched cases will execute including default case.
10. Is it necessary to keep break statement for default case?
Actually no need. But In future if you want to add more cases then there is a chance to miss the break statement. So it would be better to keep break statement for default also.
11. What are all the iterative statements or looping statements?
1. while()
2. do while()
3. for()
12. What is the difference b/w while() and do while loop?
1. In while loop first condition evaluates and then body will wxecute.
2. In do while first body executes and then condition will be executed.
13. How to write infinite for loop?
for(;;){
body;
}
14. Re write the programm using for loop?
i =0;
while(i< 5){
Printf(“%d”,i);
i++
}
?
for(i=0; i<5;i++){
printf(“%d”,i);
}
15. What are all jumping statements?
1. Break
2. Continue
3. Goto
16. What is the difference between break and continue (when we use those in loops)?
1. break :-
when we use break inside loop then execution will stops and control goes to next line of the loop statement.
2. continue :-
if we use continue, then execution will stop for that particular condition only. And control continues the execution for next condition.
17. Can we use break out side loop or switch?
No. it should be within loop or switch.
18. Can be continue be out side the loop?
No it should be inside the loop statements only.