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

Control Instructions Questions and Answers

Answer: B

Correct answer: GeeksQuiz GeeksQuiz GeeksQuiz .... infinite times

i starts at 1 and rises by 2, so it takes only the odd values 1, 3, 5, 7, 9, 11, … and never equals 10. The test i != 10 is therefore never false. This is why < is safer than != in a loop condition whose variable moves by more than one.

Please log in or register to report this question.

32.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}

 

Answer: A

Correct answer: This is default This is case 1

i is 4, which matches no case, so control jumps to default - and default need not be last. Having no break, execution then falls through into case 1, whose break finally ends the switch. So two lines are printed.

Please log in or register to report this question.

33.

What will be the output of the following C program segment?

char inchar = 'A';
switch (inchar)
{
case 'A' :
    printf ("choice A \n") ;
case 'B' :
    printf ("choice B ") ;
case 'C' :
case 'D' :
case 'E' :
default:
    printf ("No Choice") ;
}

 

Answer: C

Correct answer: Choice A Choice B No Choice

There is no break after the first printf, so execution falls through every following label. Matching case 'A' prints “choice A”, then “choice B”, then - passing through the empty labels C, D and E - the default prints “No Choice”. A case label marks an entry point, not a block.

Please log in or register to report this question.

34.

Which of the following for loops covers the same range of values of i as for (i = 0; i < n; i++)?

Answer: D

Correct answer: for (i = n-1; i>-1; i--)

for (i = 0; i < n; i++) visits 0 to n-1. Running the same range backwards means starting at n-1 and continuing while i is still at least 0 - that is, while i > -1. The alternatives either start at n, which is out of range, or stop at i > 0, which misses the element at index 0.

Please log in or register to report this question.

35.

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

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

 

Answer: A

Correct answer: There should be a condition in the while loop

Unlike for, a while requires a controlling expression - while() is a syntax error. The equivalent infinite loop is written while(1), or for(;;), where an omitted condition is defined to be true.

Please log in or register to report this question.

36.

A labelled statement consists of an identifier followed by

Answer: B

Correct answer: :

A label is an identifier followed by a colon - here: - marking a point that goto can jump to. The same colon form is used by case and default labels inside a switch. Labels have function scope, so a goto can only reach one in the same function.

Please log in or register to report this question.

37.

goto can be used to jump from main into another function.

Answer: B

Correct answer: FALSE

goto can only reach a label in the same function; labels have function scope and are invisible outside it. Jumping between functions would leave the stack in an inconsistent state. Where a non-local jump really is needed, setjmp and longjmp provide a disciplined version of it.

Please log in or register to report this question.

38.

What is the output of this C code?

int main()

    {

        int *p = NULL;

        for (foo(); p; p = 0)

            printf("In for loop\n");

            printf("After loop\n");

    }

 

Answer: B

Correct answer: Compile time error

foo is never declared or defined anywhere in this program. Under the current C rules an undeclared function is an error, and even where it is only a warning the missing definition would fail at link time. So the program does not run at all - the value of p is beside the point.

Please log in or register to report this question.

39.

In the loop construct for(exp1; exp2; exp3), which part is always executed exactly once?

Answer: A

Correct answer: exp1

exp1, the initialisation, is evaluated once before the loop starts and never again. exp2, the condition, is evaluated before every pass - at least once, and once more than the body runs. exp3, the update, runs after each pass, so it may not run at all if the condition fails immediately.

Please log in or register to report this question.

40.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int x = 3;
    float y = 3.0;
    if(x == y)
        printf("x and y are equal");
    else
        printf("x and y are not equal");
    return 0;
}

 

Answer: A

Correct answer: x and y are equal

When an int and a float are compared, the int is converted to floating point first. Both values are then exactly 3.0 - and 3 is one of the values a float can represent exactly - so the comparison succeeds. It is only fractional values such as 0.1 that make such comparisons unreliable.

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