Exercise: Control Instructions

Questions for: Control Instructions

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;
}
A:
There should be a condition in the for loop
B:
The two semicolons should be dropped
C:
The for loop should be replaced with while loop.
D:
No error
Answer: D

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

What will be the output of the program?
#include<stdio.h>
int main()
{
    int x, y, z;
    x=y=z=1;
    z = ++x || ++y && ++z;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}
A:
x=2, y=1, z=1
B:
x=2, y=2, z=1
C:
x=2, y=2, z=2
D:
x=1, y=2, z=1
Answer: A

Step 1: x=y=z=1; here the variables x ,y, z are initialized to value '1'.

Step 2: z = ++x || ++y && ++z; becomes z = ( (++x) || (++y && ++z) ). Here ++x becomes 2. So there is no need to check the other side because ||(Logical OR) condition is satisfied.(z = (2 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '1'. So the value of variable z is '1'

Step 3: printf("x=%d, y=%d, z=%d\n", x, y, z); It prints "x=2, y=1, z=1". here x is increemented in previous step. y and z are not increemented.

What will be the output of the program?
#include<stdio.h>
int main()
{
    char j=1;
    while(j < 5)
    {
        printf("%d, ", j);
        j = j+1;
    }
    printf("\n");
    return 0;
}
A:
1 2 3 ... 127
B:
1 2 3 ... 255
C:
1 2 3 ... 127 128 0 1 2 3 ... infinite times
D:
1, 2, 3, 4
Answer: D
No answer description is available. Let's discuss.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        printf("Hello\n");
        case 1:
            printf("Hi\n");
            break;
        case 2:
            printf("\nBye\n");
            break;
    }
    return 0;
}
A:
Hello
Hi
B:
Hello
Bye
C:
Hi
D:
Bye
Answer: C

switch(i) has the variable i it has the value '1'(one).

Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement.

switch-case do not execute any statements outside these blocks case and default

Hence the output is "Hi".

What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}
A:
This is default
This is case 1
B:
This is case 3
This is default
C:
This is case 1
This is case 3
D:
This is default
Answer: A

In the very begining of switch-case statement default statement is encountered. So, it prints "This is default".

In default statement there is no break; statement is included. So it prints the case 1 statements. "This is case 1".

Then the break; statement is encountered. Hence the program exits from the switch-case block.

Ad Slot (Above Pagination)
Quiz