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

Control Instructions Questions and Answers

31.

Which keyword is used to come out of a loop only for that iteration?

Answer: B

Correct answer: continue

continue abandons the current iteration and jumps to the next one - to the update expression in a for, or to the condition test in a while. break leaves the loop altogether, and return leaves the function.

Please log in or register to report this question.

32.

Point out the error, if any in the program.

#include <stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        case 1:
           printf("Case1");
           break;
        case 1*2+4:
           printf("Case2");
           break;
    }
return 0;
}

 

Answer: D

Correct answer: No Error

case 1*2+4 is a constant expression, which the compiler evaluates to 6 - that is allowed, and 6 does not clash with case 1. A default label is optional. So the program compiles and, with i equal to 1, prints Case1.

Please log in or register to report this question.

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.

34.

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.

35.

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.

36.

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.

37.

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.

38.

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.

39.

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.

40.

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.

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