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

Control Instructions Questions and Answers

71.

By default, the data type of a constant without a decimal point is int, whereas the one with a decimal point is a double.

Answer: A

Correct answer: Yes

An unsuffixed integer literal such as 10 has type int, and one with a decimal point such as 3.14 has type double - not float, which catches many people out. Suffixes override this: 10L, 10U, 3.14f.

Please log in or register to report this question.

72.

When 1 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");
        default:
            printf("2\n");
        }
    }

 

Answer: C

Correct answer: 1 2

With no break after case 1, execution falls through into the default label - a label marks an entry point, not the boundary of a block. So both printf calls run, giving 1 and then 2 on the next line.

Please log in or register to report this question.

73.

Predict the output of the below program:

#include <stdio.h>
#define EVEN 0
#define ODD 1
int main()
{
    int i = 3;
    switch (i & 1)
    {
        case EVEN: printf("Even");
                break;
        case ODD: printf("Odd");
                break;
        default: printf("Default");
    }
    return 0;
}

 

Answer: B

Correct answer: Odd

i & 1 isolates the lowest bit, which is 1 for an odd number and 0 for an even one. With i equal to 3 the expression is 1, matching ODD, so Odd is printed. This bitwise test is a common alternative to i % 2.

Please log in or register to report this question.

74.

What will be the output of the program, if a short int is 2 bytes wide?

#include <stdio.h>
int main()
{
    short int i = 0;
    for(i<=5 && i>=-1; ++i; i>0)
        printf("%u,", i);
    return 0;
}

 

Answer: A

Correct answer: 1 ... 65535

The three clauses are not doing what they look like. The initialisation i<=5 && i>=-1 merely computes a value and throws it away; the condition is ++i, so the loop runs while the incremented value is non-zero; and the update i>0 changes nothing. So i simply counts up and is printed with %u, which reinterprets the negative half of the range as large positive numbers - ending when the value wraps back to 0. (Signed overflow is formally undefined, so this is a description of what happens in practice, not a guarantee.)

Please log in or register to report this question.

75.

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

#include <stdio.h>
int main()
{
    void fun();
    int i = 1;
    while(i <= 5)
    {
        printf("%d\n", i);
        if(i>2)
            goto here;
    }
return 0;
}
void fun()
{
    here:
    printf("It works");
}

 

Answer: C

Correct answer: Error: goto cannot takeover control to other function

A goto can only jump to a label within the same function. The label here: is inside fun(), so it is not visible in main and the compiler reports an undefined label. Transferring control between functions is what a call, or setjmp/longjmp, is for.

Please log in or register to report this question.

76.

The output of the code below is

#include <stdio.h>
    void main()
    {
        int x = 0;
        if (x == 0)
            printf("hi");
        else
            printf("how are u");
            printf("hello");
    }

 

Answer: D

Correct answer: hihello

The condition is true, so the if branch prints hi. The second printf is outside the if-else altogether - there are no braces, so the else takes only the single statement that follows it - and therefore runs whichever branch was taken. The output is hihello, with no separator since neither string has a newline.

Please log in or register to report this question.

77.

The modulus operator cannot be used with a long double.

Answer: A

Correct answer: True

The % operator requires integer operands, so it cannot be applied to float, double or long double. For a floating-point remainder the library provides fmodl() in <math.h>.

Please log in or register to report this question.

78.

Break takes control out of a switch. Does continue, used inside a switch, take control back to the beginning of that switch?

Answer: B

Correct answer: No

break does end a switch, but continue has no meaning inside one: it asks for the next iteration, and a switch has no iterations. A continue written inside a switch applies to the loop enclosing the switch, and if there is no such loop the program will not compile.

Please log in or register to report this question.

79.

What is the output?

#include <stdio.h>
int main()
{
   int n;
   for (n = 9; n!=0; n--)
     printf("n = %d", n--);
   return 0;
}

 

Answer: C

Correct answer: Infinite Loop

n is decremented twice on every pass - once by n-- inside the printf and once by the loop’s own n--. Starting from 9 it therefore takes only odd values: 9, 7, 5, 3, 1, -1, -3, … and never equals 0, so the test n != 0 never fails. It runs until n underflows, which is undefined behaviour.

Please log in or register to report this question.

80.

Point out the error, if any in the program.

#include <stdio.h>
int main()
{
    int P = 10;
    switch(P)
    {
       case 10:
       printf("Case 1");

       case 20:
       printf("Case 2");
       break;

       case P:
       printf("Case 2");
       break;
    }
    return 0;
}

 

Answer: B

Correct answer: Error: Constant expression required at line case P:

A case label must be a compile-time constant, because the compiler builds the jump table before the program runs. P is a variable, so case P: is rejected - even though its value happens to be known here. A const int would not help either in C; only a literal, an enumeration constant or a constant expression will do.

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