C Programming - Control Instructions
Exercise : Control Instructions - Point Out Errors
1.
Point out the error, if any in the for loop.
#include<stdio.h>
int main()
{
int i=1;
for(;;)
{
printf("%d\n", i++);
if(i>10)
break;
}
return 0;
}
Answer: Option
Explanation:
Step 1: for(;;) this statement will genereate infinite loop.
Step 2: printf("%d\n", i++); this statement will print the value of variable i and increement i by 1(one).
Step 3: if(i>10) here, if the variable i value is greater than 10, then the for loop breaks.
Hence the output of the program is
1
2
3
4
5
6
7
8
9
10
2.
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;
}
Answer: Option
Explanation:
There can exists a switch statement, which has no case.
3.
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;
}
Answer: Option
Explanation:
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".
4.
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;
}
Answer: Option
Explanation:
The while() loop must have conditional expression or it shows "Expression syntax" error.
Example: while(i > 10){ ... }
5.
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;
}
Answer: Option
Explanation:
Because, case 3 + 2: and case 5: have the same constant value 5.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers