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

Control Instructions Questions and Answers

1.

Consider the following C program:

#include \(\lt\)stdio.h\(\gt\)
int main(){
int a = 6;
int b = 0;
while(a \(\lt\) 10) {
a = a / 12 + 1;
a += b;}
printf(”%d”, a);
return 0;}

Which one of the following statements is CORRECT?

Exam Year(s)

GATE CS - 2024,

Answer: C

The program gets stuck in an infinite loop.

Trace it. a = 6, b = 0, and 6 \(\lt\) 10, so the loop runs: a = 6 / 12 + 1 = 0 + 1 = 1 (integer division), then a += 0 leaves a = 1. Now 1 \(\lt\) 10, so the loop runs again: a = 1 / 12 + 1 = 1, and a stays 1.

From then on a is 1 on every pass, so the condition a \(\lt\) 10 never becomes false and printf is never reached. The program prints nothing, so 9, 10 and 6 are all wrong; 6 would require the loop body never to run.

Please log in or register to report this question.

2.

foreach instruction is used in :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: B

The foreach statement is used in Perl.

Perl's foreach loop runs through each element of a list or array in turn, setting a loop variable to each element and executing the block once for it. It is the natural way to process every item of an array or every key of a hash.

Pascal has for ... to and while loops, FORTRAN has the DO loop, and COBOL uses the PERFORM statement for repetition. None of them has a foreach instruction.

Please log in or register to report this question.

3.

A do-while loop always executes its loop body at least ..................

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

A do-while loop executes its body at least once.

In do { ... } while (condition); the condition is tested after the body has run. So the body runs once before the condition is ever checked, even if the condition is false from the start. A while loop tests first and may execute zero times, which is the key difference between the two.

Nothing in the construct forces a second, third or fourth pass: if the condition is false after the first pass, the loop ends.

Please log in or register to report this question.

4.

Consider the following 'C' program fragment :

int i = 5;
do
{
    putchar (i + 100);
    printf ("%d", i...);
} while (i);

results in the following output :

Exam Year(s)

MH SET - 2023, SET Exam - 2023,

Answer: C

The fragment results in an error message.

The argument to printf is written i..., which is not a valid C expression, so the compiler reports a syntax error and no output is produced.

The other options are the trap. If the argument were i--, the loop would print the character with code i + 100 and then i: 105 is 'i', so it would print i5h4g3f2e1 (option A) and stop when i reached 0. Option B would need the decrement before printing, and an infinite loop (D) would need i never to reach 0.

Please log in or register to report this question.

5.

The two statements that can change the flow of control are :

Exam Year(s)

MH SET - 2023, SET Exam - 2023,

Answer: A

The keyed answer is break and continue.

break and continue are jump statements: their only effect is to transfer control. break leaves the innermost loop or switch immediately, and continue skips the rest of the current loop iteration and goes to the next one. They change the flow of control inside a structure that is already running.

if, switch, while and do-while are selection and iteration constructs: they define the structure of the flow, choosing which block runs or repeating it, rather than being statements that break the normal sequence. The key takes the question in this sense, so the pairs in A, B and C are not accepted.

Please log in or register to report this question.

6.

Which loop is guaranteed to execute at least once ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: C

The loop guaranteed to run at least once is do-while.

A do-while loop is exit-controlled: the body is written first and the condition is tested after it, so the body always executes once before the condition is checked for the first time.

for and while loops are entry-controlled. They test the condition before the first pass, so if it is false at the start the body never runs at all. Since do-while does guarantee one pass, 'None of these' is wrong.

Please log in or register to report this question.

7.

The following code 'for(;;)' represents an infinite loop. It can be terminated by

Exam Year(s)

Tripura Public Service Commission - 2019,

Answer: A

The key's answer is break.

The break statement terminates the innermost loop and passes control to the statement after it, so it is the way to end a for(;;) loop and carry on with the program.

exit(0) and abort() also stop the loop, but only by ending the whole program; they terminate the process, not the loop. The question bank this is taken from treats only break as terminating the loop itself, which is why 'All of the mentioned above' is not the key.

Please log in or register to report this question.

8.

The keyword ‘break’ cannot be simply used within:

Answer: B

Correct answer: if-else

break belongs to loops and switch: it exits the nearest enclosing for, while, do-while or switch. An if is not one of those, so a break written directly inside an if either escapes the loop containing the if or, if there is no such loop, fails to compile.

Please log in or register to report this question.

9.

#include <stdio.h>
 
int main()
{
    unsigned int i = 65000;
    while (i++ != 0);
    printf("%d", i);
    return 0;
}

 

Answer: B

Correct answer: 1

i++ yields the old value and then increments, so the loop keeps going while the old value is non-zero. Starting at 65000 the variable climbs to the maximum for an unsigned int and then wraps round to 0 - wrapping is defined behaviour for unsigned types. On that pass the test sees 0 and stops, but i has already been incremented once more, to 1.

Please log in or register to report this question.

10.

The output of the code below is

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

 

Answer: C

Correct answer: Compile time error

Two things are wrong, and either alone is fatal. The controlling expression ch is a pointer, not an integer type; and the case labels "1" and "2" are string literals, which are not integer constants. (The scanf into an uninitialised pointer would also be undefined behaviour, but the program never gets that far.)

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