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

Control Instructions Questions and Answers

111.

A do-while loop terminates when its controlling expression evaluates to

Answer: B

Correct answer: Zero

A loop continues while its condition is true, that is non-zero, and stops when it evaluates to zero. The do-while differs from the others only in when the test happens - after the body rather than before - so the body always runs at least once.

Please log in or register to report this question.

Answer: A

Correct answer: if(n){c = a;}else{c = b;}

The conditional operator evaluates its first operand as a condition: if it is true the result is the second operand, otherwise the third. So c = n ? a : b; assigns a when n is non-zero and b when it is zero - exactly what the first if-else does.

Please log in or register to report this question.

113.

The output of the code below is

#include <stdio.h>
    int x;
    void main()
    {
        if (x)
            printf("hi");
        else
            printf("how are u");
    }

 

Answer: B

Correct answer: how are u

x is declared at file scope, so it has static storage duration and is zero-initialised - not left holding junk, as an uninitialised local would be. The condition is therefore false and the else branch runs.

Please log in or register to report this question.

114.

Comment on the code below

#include <stdio.h>
    void main()
    {
        int x = 5;
        if (true);
            printf("hello");
    }

 

Answer: B

Correct answer: It will throw an error

true is not a keyword in C. It is a macro defined in <stdbool.h>, which this program does not include, so the identifier is undeclared and the program fails to compile. (Had it compiled, the semicolon after the if would have been the empty statement forming its branch, and hello would have printed unconditionally.)

Please log in or register to report this question.

115.

The output of the code below is

#include <stdio.h>
    void main()
    {
        int x = 0;
        if (x == 0)
            printf("hi");
        else
            printf("how are u");
            printf("hello");
    }

 

Answer: D

Correct answer: hihello

The condition is true, so the if branch prints hi. The second printf is outside the if-else altogether - there are no braces, so the else takes only the single statement that follows it - and therefore runs whichever branch was taken. The output is hihello, with no separator since neither string has a newline.

Please log in or register to report this question.

116.

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.

117.

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.

118.

When 1 is entered, The output of the code below is?

#include <stdio.h>
    void main()
    {
        int ch;
        printf("enter a value btw 1 to 2:");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("1\n");
        default:
            printf("2\n");
        }
    }

 

Answer: C

Correct answer: 1 2

With no break after case 1, execution falls through into the default label - a label marks an entry point, not the boundary of a block. So both printf calls run, giving 1 and then 2 on the next line.

Please log in or register to report this question.

119.

When 2 is entered, The output of the code below is?

#include <stdio.h>
    void main()
    {
        int ch;
        printf("enter a value btw 1 to 2:");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("1\n");
            break;
            printf("Hi");
        default:
            printf("2\n");
        }
    }

 

Answer: D

Correct answer: 2

ch is 2, which matches no case, so control goes straight to default and prints 2. The printf("Hi") is unreachable in any event: it sits immediately after a break and before the next label, so no path can reach it.

Please log in or register to report this question.

120.

When 1 is entered, The output of the code below is?

#include <stdio.h>
    void main()
    {
        int ch;
        printf("enter a value btw 1 to 2:");
        scanf("%d", &ch);
        switch (ch, ch + 1)
        {
        case 1:
            printf("1\n");
            break;
        case 2:
            printf("2");
            break;
        }
    }

 

Answer: B

Correct answer: 2

The controlling expression uses the comma operator: ch, ch + 1 evaluates ch, discards it, and yields ch + 1. With 1 entered, the switch is therefore performed on 2, so case 2 runs and prints 2.

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