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

Control Instructions Questions and Answers

71.

We want to test whether a value lies in the range 2 to 4 or 5 to 7. Can we do this using a switch?

Answer: A

Correct answer: Yes

A switch cannot test a range directly, but the ranges here are small and the values are integers, so each can be listed as its own label with fall-through:

case 2: case 3: case 4: /* first range */ break;
case 5: case 6: case 7: /* second range */ break;

This works because a label with no statements falls through to the next. For a wide or non-integer range, an if is the only option.

Please log in or register to report this question.

72.

Which of the following errors would be reported by the compiler on compiling the program given below?

#include <stdio.h>
int main()
{
    int a = 5;
    switch(a)
    {
    case 1:
    printf("First");

    case 2:
    printf("Second");

    case 3 + 2:
    printf("Third");

    case 5:
    printf("Final");
    break;

    }
    return 0;
}

 

Answer: C

Correct answer: Duplicate case case 5:

case 3 + 2 is perfectly legal - a label may be any constant expression, and the compiler evaluates it to 5. That is exactly the problem: it then collides with case 5, and duplicate labels are an error, since the jump would be ambiguous. Missing break statements are legal (they produce fall-through).

Please log in or register to report this question.

73.

Which keyword can be used for coming out of recursion?

Answer: A

Correct answer: return

Recursion is unwound by return: the base case returns instead of calling again, and each frame then returns in turn. break applies to loops and switches, not to function calls, and exit() would abandon the whole program rather than come back.

Please log in or register to report this question.

74.

Which of the following statements are correct about the below program?

#include <stdio.h>
int main()
{
    int n = 0, y = 1;
    y == 1 ? n=0 : n=1;
    if(n)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

 

Answer: C

Correct answer: Error: Lvalue required

Assignment binds more loosely than ?:, so the line parses as (y == 1 ? n = 0 : n) = 1;, and the left-hand side is a value rather than an object. The compiler reports that an lvalue is required. Written as n = (y == 1) ? 0 : 1; it compiles and does what was meant.

Please log in or register to report this question.

75.

The continue statement cannot be used with

Answer: D

Correct answer: switch

continue means “begin the next iteration”, so it belongs to the three loops. A switch has no iterations, so a continue written inside one is either applied to an enclosing loop or, if there is none, rejected. break, by contrast, works in all four.

Please log in or register to report this question.

76.

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.

77.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int a = 500, b = 100, c;
    if(!a >= 400)
        b = 300;
    c = 200;
    printf("b = %d c = %d\n", b, c);
    return 0;
}

 

Answer: D

Correct answer: b = 100 c = 200

! binds far tighter than >=, so the condition is (!a) >= 400, not !(a >= 400). With a non-zero, !a is 0, and \(0 \ge 400\) is false - so b keeps its initial 100. The assignment c = 200 is a separate statement, outside the if altogether, so it always runs.

Please log in or register to report this question.

78.

By default, the data type of a constant without a decimal point is int, whereas the one with a decimal point is a double.

Answer: A

Correct answer: Yes

An unsuffixed integer literal such as 10 has type int, and one with a decimal point such as 3.14 has type double - not float, which catches many people out. Suffixes override this: 10L, 10U, 3.14f.

Please log in or register to report this question.

79.

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.

80.

Predict the output of the below program:

#include <stdio.h>
#define EVEN 0
#define ODD 1
int main()
{
    int i = 3;
    switch (i & 1)
    {
        case EVEN: printf("Even");
                break;
        case ODD: printf("Odd");
                break;
        default: printf("Default");
    }
    return 0;
}

 

Answer: B

Correct answer: Odd

i & 1 isolates the lowest bit, which is 1 for an odd number and 0 for an even one. With i equal to 3 the expression is 1, matching ODD, so Odd is printed. This bitwise test is a common alternative to i % 2.

Please log in or register to report this question.

Loading…
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