C Control Statements Test 1
C control statements test paper contains questions from decision statement: if-else and switch, loop statement: for loop, while loop & do-while loop and jump statement: break and continue.
1) Which data type cannot be checked in switch-case statement?
- enum
- character
- integer
- float
The correct option is (d).
Explanation:
In C-languageswitch/case statement is defined by the language specification to use an int value therefore we cannot use a float value in switch/case statement.
2) How many times “tutoraspire” is printed?
- 10 times
- 11 times
- 0 times
- Infinite times
The correct option is (c).
Explanation:
In program the x is initialized with -1. As x < 5 (since x is -1) it will start with continue statement.
Continue means “stop the current iteration and proceed to the next iteration”. Therefore x becomes 0 now. This will take place until x becomes 5.
Now if the value of x=5, it will enter the else part where it encounters the break statement, as a result it will come out of for loop. Hence it will not go to printf statement.
Therefore tutoraspire will be printed 0 times.
3) How many times while loop is executed if a short int is 2 byte wide?
- 154 times
- 155 times
- 156 times
- Infinite times
The correct option is (b).
Explanation:
The size of short int which is 2 byte wide does not affect the while() loop operation.
Therefore thewhile (i <= 155) loop will executed 155 times.
4) Which statement is correct about the below program?
- Welcome Programmer
- Error: Undeclared identifier if
- Error: Expression syntax
- No output
The correct option is (c).
Explanation:
In program 5th line i.e. if(i = 8) && if(j = 24) the “Expression syntax” error occur.
Hence the statement should be like if((i == 5) && (j == 10)).
Therefore on compiling the program Error: Expression syntax is occurring.
5) Find out the error, if any in the below program?
- No error in program and prints “Case1”
- Error: Invalid printf statement after switch statement
- Error: No default specified
- None of the above
The correct option is (a).
Explanation:
In program switch statement is used for switch(j) it becomes switch(1) because i is initialized with 1.
Therefore the case 1: block is get executed. Hence it prints “Case1”.
Printf (“Hello programmer!”); is ignored by the compiler.
Hence there is no error in program and prints “Case1”.