Exercise: Control Instructions

Questions for: Control Instructions

Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int i = 10, j = 15;
    if(i % 2 = j % 3)
        printf("ExamAdept\n");
    return 0;
}
A:
Error: Expression syntax
B:
Error: Lvalue required
C:
Error: Rvalue required
D:
The Code runs successfully
Answer: B

if(i % 2 = j % 3) This statement generates "LValue required error". There is no variable on the left side of the expression to assign (j % 3).

Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int i = 10, j = 20;
    if(i = 5) && if(j = 10)
        printf("Have a nice day");
    return 0;
}
A:
Output: Have a nice day
B:
No output
C:
Error: Expression syntax
D:
Error: Undeclared identifier if
Answer: C

"Expression syntax" error occur in this line if(i = 5) && if(j = 10).

It should be like if((i == 5) && (j == 10)).

Point out the error, if any in the program.
#include<stdio.h> 
int main()
{
    int a = 10, b;
    a >=5 ? b=100: b=200;
    printf("%d\n", b);
    return 0;
}
A:
100
B:
200
C:
Error: L value required for b
D:
Garbage value
Answer: C

Variable b is not assigned.

It should be like:

b = a >= 5 ? 100 : 200;

Point out the error, if any in the while loop.
#include<stdio.h>
int main()
{
    void fun();
    int i = 1;
    while(i <= 5)
    {
        printf("%d\n", i);
        if(i>2)
            goto here;
    }
return 0;
}
void fun()
{
    here:
    printf("It works");
}
A:
No Error: prints "It works"
B:
Error: fun() cannot be accessed
C:
Error: goto cannot takeover control to other function
D:
No error
Answer: C

A label is used as the target of a goto statement, and that label must be within the same function as the goto statement.

Syntax: goto <identifier> ;
Control is unconditionally transferred to the location of a local label specified by <identifier>.
Example:


#include <stdio.h>
int main()
{
    int i=1;
    while(i>0)
    {
        printf("%d", i++);
        if(i==5)
          goto mylabel;
    }
    mylabel:
    return 0;
}
 

Output: 1,2,3,4

Point out the error, if any in the program.
#include<stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        case 1:
           printf("Case1");
           break;
        case 1*2+4:
           printf("Case2");
           break;
    }
return 0;
}
A:
Error: in case 1*2+4 statement
B:
Error: No default specified
C:
Error: in switch statement
D:
No Error
Answer: D

Constant expression are accepted in switch

It prints "Case1"

Ad Slot (Above Pagination)
Quiz