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

Control Instructions Questions and Answers

21.

A short integer is at least 16 bits wide and a long integer is at least 32 bits wide.

Answer: A

Correct answer: True

The standard sets minimum widths, not exact ones: short must be at least 16 bits and long at least 32, with int at least 16 and long long at least 64. It also requires the ordering char ≤ short ≤ int ≤ long ≤ long long. Actual widths vary by platform, which is why <stdint.h> offers fixed-width types.

Please log in or register to report this question.

22.

#include <stdio.h>
int i;
int main()
{
    if (i);
    else
        printf("Else");
    return 0;
}

What is correct about the above program?

Answer: B

Correct answer: Else block is executed

i is a global, so it is zero-initialised - not indeterminate, which is what makes the outcome predictable. The condition is therefore false. The semicolon after if (i) is the empty statement that forms the if branch, and the else attaches to it perfectly legally, so the else branch runs.

Please log in or register to report this question.

23.

Output?

#include <stdio.h>
int main()
{
    int c = 5, no = 10;
    do {
        no /= c;
    } while(c--);
  
    printf ("%d\n", no);
    return 0;
}

 

Answer: B

Correct answer: Runtime Error

The loop divides no by c and then tests c--, so c falls 5, 4, 3, 2, 1, 0. On the pass after the test that saw c equal to 1, c is 0 - and no /= 0 is integer division by zero, which the hardware traps. The compiler cannot see it, so it is a run-time fault.

Please log in or register to report this question.

24.

What is the output of the below program?

#include <stdio.h>
int main()
{
    int i = 0;
    switch (i)
    {
        case '0': printf("Geeks");
                break;
        case '1': printf("Quiz");
                break;
        default: printf("GeeksQuiz");
    }
    return 0;
} 

 

Answer: C

Correct answer: GeeksQuiz

'0' is a character constant, and its value is the ASCII code 48, not the number 0. Likewise '1' is 49. The controlling expression is 0, which matches neither, so control falls to default.

Please log in or register to report this question.

25.

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.

26.

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.

28.

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.

29.

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.

30.

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.

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