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

Control Instructions Questions and Answers

21.

Which keyword is used to come out of a loop only for that iteration?

Answer: B

Correct answer: continue

continue abandons the current iteration and jumps to the next one - to the update expression in a for, or to the condition test in a while. break leaves the loop altogether, and return leaves the function.

Please log in or register to report this question.

22.

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.

23.

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.

24.

What is the output of this C code?

void main()

    {

        int i = 0;

        if (i == 0)

        {

            printf("Hello");

            continue;

        }

    }

 

Answer: D

Correct answer: Compile time error

continue is meaningful only inside a loop: it asks for the next iteration, and here there is no loop to iterate. Written inside a bare if with no enclosing loop, it is a compile-time error.

Please log in or register to report this question.

25.

What is the output of this C code?

int main()

    {

        printf("before continue ");

        continue;

        printf("after continue\n");

    }

 

Answer: D

Correct answer: Compile time error

continue must appear inside a loop. In a function body with no loop around it there is no next iteration to jump to, so the compiler rejects the program - neither printf ever runs.

Please log in or register to report this question.

26.

The following code ‘for(;;)’ represents an infinite loop. Which statement exits the loop itself?

Answer: A

Correct answer: break

break is the statement that ends the loop: control passes to the statement after it and the program carries on. exit(0) and abort() would also stop the loop running, but only by ending the whole program, which is a different thing from leaving the loop.

Please log in or register to report this question.

Answer: B

Correct answer: for (i = 0, j = 0;i < n, j < n; i++, j += 5)

The comma operator lets a single for carry more than one initialisation and more than one update, so both variables advance together in one loop. Note the trap in the condition: i < n, j < n evaluates both but yields only the last, so it is really j < n that controls the loop - a genuine condition on both would need &&.

Please log in or register to report this question.

28.

Which of the following for loops covers the same range of values of i as for (i = 0; i < n; i++)?

Answer: D

Correct answer: for (i = n-1; i>-1; i--)

for (i = 0; i < n; i++) visits 0 to n-1. Running the same range backwards means starting at n-1 and continuing while i is still at least 0 - that is, while i > -1. The alternatives either start at n, which is out of range, or stop at i > 0, which misses the element at index 0.

Please log in or register to report this question.

29.

Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3) ?

Answer: D

Correct answer: macros

The three parts of a for are ordinary expressions, so anything assignable can appear on the left of an assignment in them - a variable, or the result of a function returning a modifiable lvalue. A macro is only text, replaced before compilation, so what appears in the code is whatever it expands to; it is not itself an object and cannot be assigned to.

Please log in or register to report this question.

30.

What is the output of this C code?

void main()

    {

        int k;

        for (k = -3; k < -5; k++)

            printf("Hello");

    }

 

Answer: D

Correct answer: Nothing

A for tests its condition before the first pass. With k at -3, the test k < -5 is already false, so the body never runs and nothing is printed. (Had it been a do-while, the body would have run 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