Machine-translated by Google. Questions are authored in English.
Top

Control Instructions Questions and Answers

71.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int a = 300, b, c;
    if(a >= 400)
        b = 300;
    c = 200;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}

 

Answer: C

Correct answer: 300, Garbage, 200

The condition \(300 \ge 400\) is false, so b = 300 never runs - and b, an uninitialised automatic variable, holds whatever was on the stack. c = 200 is a separate statement outside the if, so it always executes.

Please log in or register to report this question.

72.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int x=1, y=1;
    for(; y; printf("%d %d\n", x, y))
    {
        y = x++ <= 5;
    }
    printf("\n");
    return 0;
}

 

Answer: A

Correct answer: 2 1 3 1 4 1 5 1 6 1 7 0

The third clause of the for is the printf, so the values are printed after each pass through the body. The body sets y to the result of x++ <= 5, which is 1 while the old x is at most 5. So the pairs printed are 2 1, 3 1, 4 1, 5 1, 6 1 - and on the pass where x is 6 the comparison fails, giving 7 0, after which the condition y is false and the loop ends.

Please log in or register to report this question.

73.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int i = 5;
    while(i-- >= 0)
        printf("%d,", i);
    i = 5;
    printf("\n");
    while(i-- >= 0)
        printf("%i,", i);
    while(i-- >= 0)
        printf("%d,", i);
    return 0;
}

 

Answer: A

Correct answer: 4, 3, 2, 1, 0, -1 4, 3, 2, 1, 0, -1

i-- >= 0 tests the old value and then decrements, so the value printed is always one less than the value tested. Starting at 5 that gives 4, 3, 2, 1, 0 and finally -1 - the pass where the test saw 0. The loop then leaves i at -2. After i is reset to 5 the second loop prints the same line. The third loop then starts with i at -2, so its very first test fails and it prints nothing.

Please log in or register to report this question.

74.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int i=3;
    switch(i)
    {
        case 1:
            printf("Hello\n");
        case 2:
            printf("Hi\n");
        case 3:
            continue;
        default:
            printf("Bye\n");
    }
    return 0;
}

 

Answer: A

Correct answer: Error: Misplaced continue

continue asks for the next iteration of a loop, and a switch is not a loop - nor is there any loop around this one. The compiler therefore rejects it. break would have been legal, since break applies to a switch as well as to a loop.

Please log in or register to report this question.

75.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int x = 10, y = 20;
    if(!(!x) && x)
        printf("x = %d\n", x);
    else
        printf("y = %d\n", y);
    return 0;
}

 

Answer: C

Correct answer: x = 10

!x is 0 since x is non-zero, and !(!x) is therefore 1 - the double negation normalises any non-zero value to 1. The condition 1 && 10 is true, so the first branch runs and prints the value of x, which the logical operators never altered: 10.

Please log in or register to report this question.

76.

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;
}

 

Answer: A

Correct answer: This is default This is case 1

i is 4, which matches no case, so control jumps to default - and default need not be last. Having no break, execution then falls through into case 1, whose break finally ends the switch. So two lines are printed.

Please log in or register to report this question.

77.

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;
}

 

Answer: C

Correct answer: Hi

A switch jumps directly to a matching label, so the printf("Hello") written before any case is unreachable. i is 1, so control lands on case 1, prints Hi, and the break ends the switch.

Please log in or register to report this question.

78.

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;
}

 

Answer: A

Correct answer: x=2, y=1, z=1

&& binds tighter than ||, so the expression is (++x) || ((++y) && (++z)). ++x makes x 2, which is true - and || short-circuits, so the entire right-hand side, including both increments, is never evaluated. y and z keep their value 1, and z is then assigned the result of the expression, which is also 1.

Please log in or register to report this question.

79.

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: D

Correct answer: No error

for(;;) is legal and idiomatic: all three clauses may be omitted, and an omitted condition is taken as true, giving an infinite loop. The two semicolons are required, since the header always has three parts. The loop here terminates properly through its break.

Please log in or register to report this question.

80.

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: C

Correct answer: No Error

A switch body is just a compound statement, and an empty one is perfectly legal. Neither a case nor a default is required - the switch simply evaluates its controlling expression, finds no matching label, and does nothing. The program compiles and prints its message.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Control Instructions Questions and Answers || Control Instructions MCQ Questions and Answers || Control Instructions GK Questions and Answers || Control Instructions GK MCQ Questions || Control Instructions Multiple Choice Questions and Answers || Control Instructions GK || GK on Control Instructions || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming