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

Control Instructions Questions and Answers

31.

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.

32.

Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3) ?

Answer: D

Correct answer: macros

The three parts of a for are ordinary expressions, so anything assignable can appear on the left of an assignment in them - a variable, or the result of a function returning a modifiable lvalue. A macro is only text, replaced before compilation, so what appears in the code is whatever it expands to; it is not itself an object and cannot be assigned to.

Please log in or register to report this question.

Answer: B

Correct answer: Both int and char can be used in “switch” control expression.

The controlling expression must have an integer type. char qualifies - it is a small integer type and is promoted to int - as do short, long and enumerations. float and double do not, because equality against a case label would be unreliable for values that cannot be represented exactly. The expression also cannot be omitted.

Please log in or register to report this question.

34.

The following code ‘for(;;)’ represents an infinite loop. Which statement exits the loop itself?

Answer: A

Correct answer: break

break is the statement that ends the loop: control passes to the statement after it and the program carries on. exit(0) and abort() would also stop the loop running, but only by ending the whole program, which is a different thing from leaving the loop.

Please log in or register to report this question.

35.

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.

36.

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.

38.

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.

39.

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.

40.

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.

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