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

Control Instructions Questions and Answers

91.

What will be the output of the program?

#include <stdio.h>
int main()
{
    float a = 0.7;
    if(0.7 > a)
        printf("Hi\n");
    else
        printf("Hello\n");
    return 0;
}

 

Answer: A

Correct answer: Hi

0.7 written in the source is a double, while a is a float, which holds fewer bits. Storing 0.7 in a float loses precision, and when a is promoted back to double for the comparison it is slightly less than the double 0.7. So the condition is true and Hi is printed - the classic warning against comparing floating-point values of different types for equality or order.

Please log in or register to report this question.

92.

Which of the following is not a logical operator?

Answer: A

Correct answer: &

& is the bitwise AND: it combines the operands bit by bit and yields an integer. The logical operators are &&, || and !, which treat their operands as true or false, yield 0 or 1, and - unlike & - short-circuit.

Please log in or register to report this question.

93.

Which of the following sentences are correct about a for loop in a C program?

1. for loop works faster than a while loop.

2. All things that can be done using a for loop can also be done using a while loop

3. for(;;); implements an infinite loop.

4. for loop can be used if we want statements in a loop get executed at least once.

Answer: D

Correct answer: 2, 3, 4

2 is true - anything a for can do a while can do, since the for is only a compact way of writing initialisation, test and update. 3 is true: an omitted condition counts as true, so for(;;) loops forever.

1 is false. Both compile to the same test-at-the-top loop, so neither is faster.

4 is the weakest of the four: the construct designed to run its body at least once is the do-while, though a for can of course be written so that its condition is true on the first test.

Please log in or register to report this question.

94.

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.

95.

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.

97.

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.

98.

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.

99.

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.

100.

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.

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