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

Control Instructions Questions and Answers

Answer: C

Correct answer: “switch” body may contain more than one “case” labels where the label value of these “case” is same and it would still compile. If “switch” controlling expression results in this “case” label value, the “case” which is placed first would be executed.

That statement is the incorrect one: duplicate case labels are a compile-time error. Each label must be a distinct constant, precisely so that the jump is unambiguous - there is no “first one wins” rule.

The other three are all true. A switch with no case at all compiles (and does nothing); default is optional, and control simply skips the switch if nothing matches; and omitting break is legal, producing fall-through.

Please log in or register to report this question.

82.

What is the output of this C code?

void main()

    {

        int i = 0;

        int j = 0;

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

        {

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

            {

                if (i > 1)

                    continue;

                    printf("Hi \n");

            }

        }

    }

 

Answer: B

Correct answer: Hi is printed 8 times

Here the printf is inside the inner loop, and continue skips it. For i equal to 0 and 1 the condition i > 1 is false, so all four inner passes print - \(2 \times 4 = 8\) lines. From i = 2 onwards continue fires on every inner pass and nothing more is printed.

Please log in or register to report this question.

83.

Consider the following program fragment

if(a > b)
if(b > c)
s1;
else s2;

s2 will be executed if

Answer: D

Correct answer: a > b and b <= c

An else attaches to the nearest unmatched if, whatever the indentation suggests - the dangling-else rule. So the else here belongs to if (b > c), not to the outer one. Reaching it therefore needs the outer condition to be true, \(a > b\), and the inner one false, \(b \le c\). If \(a \le b\) nothing at all is executed.

Please log in or register to report this question.

84.

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.

85.

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.

86.

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.

87.

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.

88.

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.

90.

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.

Loading…
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