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.

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.

5.

#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.

6.

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.

7.

Which loop is guaranteed to execute at least once?

Answer: C

Correct answer: do while

A do-while tests its condition after the body, so the body always runs at least once. for and while test first, and so may not run at all. This makes do-while the natural choice for reading input and then validating it.

Please log in or register to report this question.

8.

Which of the following statements are correct about an if-else statements in a C-program?

1. Every if-else statement can be replaced by an equivalent statements using   ?: operators

2. Nested if-else statements are allowed.

3. Multiple statements in an if block are allowed.

4. Multiple statements in an else block are allowed.

Answer: D

Correct answer: 2, 3, 4

2, 3 and 4 are true: if-else statements nest freely, and either branch may hold as many statements as you like inside braces.

1 is false. The conditional operator ?: combines expressions, not statements, so it cannot replace an if-else whose branches contain declarations, loops, or several statements. It replaces only the special case where both branches produce a value.

Please log in or register to report this question.

9.

Which of the following is correct with respect to “Jump Statements” in C?

Answer: D

Correct answer: All of the above.

C has four jump statements, and three of them are listed: goto, which transfers to a label in the same function; continue, which starts the next iteration; and break, which leaves a loop or switch. The fourth is return.

Please log in or register to report this question.

10.

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

1. switch is useful when we wish to check the value of variable against a particular set of values.

2. switch is useful when we wish to check whether a value falls in different ranges.

3. Compiler implements a jump table for cases used in switch.

4. It is not necessary to use a break in every switch statement.

Answer: B

Correct answer: 1,3,4

1 is true - a switch compares the controlling expression against a set of discrete constants. 3 is true: where the labels are dense the compiler typically builds a jump table, which is why a switch can beat a chain of ifs. 4 is true: omitting break is legal and produces deliberate fall-through.

2 is false. A switch cannot test a range - each value in the range needs its own label - which is what if-else if is for.

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