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

Control Instructions Questions and Answers

51.

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

#include <stdio.h>
int main()
{
    int i = 0;
    i++;
    if(i <= 5)
    {
        printf("IndiaBIX\n");
        exit(0);
        main();
    }
    return 0;
}

 

Answer: B

Correct answer: The program prints 'IndiaBIX' one time

exit(0) terminates the program immediately. The recursive main() call written after it is never reached, so the message is printed exactly once. (Calling main recursively is legal in C, which is why the last option is wrong - it just never happens here.)

Please log in or register to report this question.

52.

Can we use a switch statement to switch on strings?

Answer: B

Correct answer: No

A switch requires an integer controlling expression, and a string in C is an array of characters - in an expression, a pointer. Comparing strings means comparing their contents, which == does not do, so a chain of if (strcmp(s, "...") == 0) is the usual alternative. (Java added string switches; C has not.)

Please log in or register to report this question.

53.

How many lines of output does the following C code produce?

#include <stdio.h>
float i=2.0;
float j=1.0;
float sum = 0.0;
main()
{
    while (i/j > 0.001)
    {
        j+=j;
        sum=sum+(i/j);
        printf("%f\n", sum);
    }
}

 

Answer: D

Correct answer: 11

Each pass doubles j and prints, so the tested quantity \(i/j\) starts at 2 and halves every time: 2, 1, 0.5, 0.25, … The loop continues while it exceeds 0.001. Since \(2/2^{11} = 0.000977\) is the first value at or below the threshold, the test succeeds for \(j = 1, 2, 4, \ldots, 1024\) - eleven passes, and eleven lines.

Please log in or register to report this question.

54.

Point out the error, if any in the for loop.

#include <stdio.h>
int main()
{
    int i=1;
    for(;;)
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

 

Answer: D

Correct answer: No error

for(;;) is legal and idiomatic: all three clauses may be omitted, and an omitted condition is taken as true, giving an infinite loop. The two semicolons are required, since the header always has three parts. The loop here terminates properly through its break.

Please log in or register to report this question.

55.

What will be the output of the program?

#include <stdio.h>
int main()
{
    char str[]="C-program";
    int a = 5;
    printf(a >10?"Ps\n":"%s\n", str);
    return 0;
}

 

Answer: A

Correct answer: C-program

The conditional operator chooses between the two format strings, not between two outputs. Since \(5 > 10\) is false, the format used is "%s\n", and str is the argument it consumes - so the program prints C-program. Had the condition been true, "Ps\n" would have been the format and str an unused extra argument.

Please log in or register to report this question.

56.

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.

57.

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.

58.

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.

59.

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.

60.

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.

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