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

Control Instructions Questions and Answers

81.

Break takes control out of a switch. Does continue, used inside a switch, take control back to the beginning of that switch?

Answer: B

Correct answer: No

break does end a switch, but continue has no meaning inside one: it asks for the next iteration, and a switch has no iterations. A continue written inside a switch applies to the loop enclosing the switch, and if there is no such loop the program will not compile.

Please log in or register to report this question.

82.

What is the output?

#include <stdio.h>
int main()
{
   int n;
   for (n = 9; n!=0; n--)
     printf("n = %d", n--);
   return 0;
}

 

Answer: C

Correct answer: Infinite Loop

n is decremented twice on every pass - once by n-- inside the printf and once by the loop’s own n--. Starting from 9 it therefore takes only odd values: 9, 7, 5, 3, 1, -1, -3, … and never equals 0, so the test n != 0 never fails. It runs until n underflows, which is undefined behaviour.

Please log in or register to report this question.

83.

Point out the error, if any in the program.

#include <stdio.h>
int main()
{
    int P = 10;
    switch(P)
    {
       case 10:
       printf("Case 1");

       case 20:
       printf("Case 2");
       break;

       case P:
       printf("Case 2");
       break;
    }
    return 0;
}

 

Answer: B

Correct answer: Error: Constant expression required at line case P:

A case label must be a compile-time constant, because the compiler builds the jump table before the program runs. P is a variable, so case P: is rejected - even though its value happens to be known here. A const int would not help either in C; only a literal, an enumeration constant or a constant expression will do.

Please log in or register to report this question.

84.

In _______, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to each other.

Answer: D

Correct answer: Loop jamming

Loop jamming (loop fusion) merges two loops with the same trip count into one, which is safe only if neither depends on the other’s results. The gain is one set of loop overheads instead of two, and better use of the cache, since the data is walked once. Loop unrolling repeats the body to cut the number of tests; strength reduction replaces an expensive operation with a cheaper one.

Please log in or register to report this question.

85.

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.

86.

What is the output of this C code?

int main()

    {

        int i = 0;

        for (foo(); i == 1; i = 2)

            printf("In for loop\n");

            printf("After loop\n");

    }

    int foo()

    {

        return 1;

    }

 

Answer: A

Correct answer: After loop

The initialisation foo() is evaluated once and its value discarded, so the 1 it returns changes nothing. The condition i == 1 is then tested with i still 0, which is false, so the body never runs - only After loop is printed.

Please log in or register to report this question.

87.

#include <stdio.h>
int main()
{
    int i = 3;
    switch (i)
    {
        case 0+1: printf("Geeks");
                break;
        case 1+2: printf("Quiz");
                break;
        default: printf("GeeksQuiz");
    }
    return 0;
}

 

Answer: B

Correct answer: Quiz

A case label may be any constant expression, and the compiler evaluates it: 0+1 is 1 and 1+2 is 3. With i equal to 3 the second label matches, so Quiz is printed and the break ends the switch.

Please log in or register to report this question.

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.

89.

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.

90.

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.

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