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

Control Instructions Questions and Answers

91.

What is the output of this C code?

void main()

    {

        int i = 0, j = 0;

        for (i = 0;i < 5; i++)

        {

            for (j = 0;j < 4; j++)

            {

                if (i > 1)

                    break;

            }

            printf("Hi \n");

        }

    }

 

Answer: A

Correct answer: Hi is printed 5 times

The printf is outside the inner loop but inside the outer one, so it runs once per outer iteration - five times in all. The break only ever escapes the inner loop, and even then it changes nothing visible: whether the inner loop runs four times or stops at once, the outer loop still completes its five passes.

Please log in or register to report this question.

92.

The output of the code below is

#include <stdio.h>
    void main()
    {
        double ch;
        printf("enter a value btw 1 to 2:");
        scanf("%lf", &ch);
        switch (ch)
        {
        case 1:
            printf("1");
            break;
        case 2:
            printf("2");
            break;
        }
    }

 

Answer: A

Correct answer: Compile time error

A switch requires an integer controlling expression, and ch is a double. Floating-point values are excluded precisely because equality against a case label would be unreliable for numbers that cannot be represented exactly in binary.

Please log in or register to report this question.

94.

What will be the output of the program?

#include <stdio.h>
int main()
{
    unsigned int i = 65536; /* Assume 2 byte integer*/
    while(i != 0)
        printf("%d",++i);
    printf("\n");
    return 0;
}

 

Answer: D

Correct answer: No output

An unsigned int two bytes wide holds 0 to 65535, so the initialiser 65536 wraps round to 0. The condition i != 0 is false on the very first test, the loop body never runs, and the only thing printed is the newline.

Please log in or register to report this question.

95.

If scanf() is used to store a value in a char variable then along with the value a carriage return also gets stored with it.

Answer: B

Correct answer: False

Nothing extra is stored. scanf("%c", &ch) reads exactly one character into the variable; the newline you typed afterwards is simply left in the input buffer, where it will be picked up by the next read. That is why a second %c so often appears to read nothing - it consumes the leftover newline.

Please log in or register to report this question.

96.

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.

97.

A do-while loop terminates when its controlling expression evaluates to

Answer: B

Correct answer: Zero

A loop continues while its condition is true, that is non-zero, and stops when it evaluates to zero. The do-while differs from the others only in when the test happens - after the body rather than before - so the body always runs at least once.

Please log in or register to report this question.

98.

#include <stdio.h>
int main()
{
    int i;
    if (printf("0"))
        i = 3;
    else
        i = 5;
    printf("%d", i);
    return 0;
} 

 

Answer: C

Correct answer: 03

printf returns the number of characters written. It prints 0 - that is one character - and returns 1, which is true, so i becomes 3. The output is therefore the 0 from the condition followed by the 3: 03.

Please log in or register to report this question.

99.

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.

100.

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.

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