Exercise: Control Instructions
Questions for: Control Instructions
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
int P = 10;
switch(P)
{
case 10:
printf("Case 1");
case 20:
printf("Case 2");
break;
case P:
printf("Case 2");
break;
}
return 0;
}
A:
Error: No default value is specified
B:
Error: Constant expression required at line case P:
C:
Error: There is no break statement in each case.
D:
No error will be reported.
Answer: B
The compiler will report the error "Constant expression required" in the line case P: . Because, variable names cannot be used with case statements.
The case statements will accept only constant expression.
Which of the following errors would be reported by the compiler on compiling the program given below?
#include<stdio.h>
int main()
{
int a = 5;
switch(a)
{
case 1:
printf("First");
case 2:
printf("Second");
case 3 + 2:
printf("Third");
case 5:
printf("Final");
break;
}
return 0;
}
A:
There is no break statement in each case.
B:
Expression as in case 3 + 2 is not allowed.
C:
Duplicate case case 5:
D:
No error will be reported.
Answer: C
Because, case 3 + 2: and case 5: have the same constant value 5.
Discuss About this Question.
Point out the error, if any in the while loop.
#include<stdio.h>
int main()
{
int i=1;
while()
{
printf("%d\n", i++);
if(i>10)
break;
}
return 0;
}
A:
There should be a condition in the while loop
B:
There should be at least a semicolon in the while
C:
The while loop should be replaced with for loop.
D:
No error
Answer: A
The while() loop must have conditional expression or it shows "Expression syntax" error.
Example: while(i > 10){ ... }
Discuss About this Question.
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
int i = 1;
switch(i)
{
printf("This is c program.");
case 1:
printf("Case1");
break;
case 2:
printf("Case2");
break;
}
return 0;
}
A:
Error: No default specified
B:
Error: Invalid printf statement after switch statement
C:
No Error and prints "Case1"
D:
None of above
Answer: C
switch(i) becomes switch(1), then the case 1: block is get executed. Hence it prints "Case1".
printf("This is c program."); is ignored by the compiler.
Hence there is no error and prints "Case1".
Discuss About this Question.
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
int a = 10;
switch(a)
{
}
printf("This is c program.");
return 0;
}
A:
Error: No case statement specified
B:
Error: No default specified
C:
No Error
D:
Error: infinite loop occurs
Answer: C
There can exists a switch statement, which has no case.
Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.