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

Control Instructions Questions and Answers

91.

Which of the following statements are correct about the below program?

#include <stdio.h>
int main()
{
    int i = 0;
    i++;
    if(i <= 5)
    {
        printf("IndiaBIX\n");
        exit(0);
        main();
    }
    return 0;
}

 

Answer: B

Correct answer: The program prints 'IndiaBIX' one time

exit(0) terminates the program immediately. The recursive main() call written after it is never reached, so the message is printed exactly once. (Calling main recursively is legal in C, which is why the last option is wrong - it just never happens here.)

Please log in or register to report this question.

92.

Which of the following sentences are correct about a for loop in a C program?

1. for loop works faster than a while loop.

2. All things that can be done using a for loop can also be done using a while loop

3. for(;;); implements an infinite loop.

4. for loop can be used if we want statements in a loop get executed at least once.

Answer: D

Correct answer: 2, 3, 4

2 is true - anything a for can do a while can do, since the for is only a compact way of writing initialisation, test and update. 3 is true: an omitted condition counts as true, so for(;;) loops forever.

1 is false. Both compile to the same test-at-the-top loop, so neither is faster.

4 is the weakest of the four: the construct designed to run its body at least once is the do-while, though a for can of course be written so that its condition is true on the first test.

Please log in or register to report this question.

93.

Which of the following statements are correct about the below program?

#include <stdio.h>
int main()
{
    int n = 0, y = 1;
    y == 1 ? n=0 : n=1;
    if(n)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

 

Answer: C

Correct answer: Error: Lvalue required

Assignment binds more loosely than ?:, so the line parses as (y == 1 ? n = 0 : n) = 1;, and the left-hand side is a value rather than an object. The compiler reports that an lvalue is required. Written as n = (y == 1) ? 0 : 1; it compiles and does what was meant.

Please log in or register to report this question.

94.

Which of the following sentences are correct about a switch loop in a C program?

1. switch is useful when we wish to check the value of variable against a particular set of values.

2. switch is useful when we wish to check whether a value falls in different ranges.

3. Compiler implements a jump table for cases used in switch.

4. It is not necessary to use a break in every switch statement.

Answer: B

Correct answer: 1,3,4

1 is true - a switch compares the controlling expression against a set of discrete constants. 3 is true: where the labels are dense the compiler typically builds a jump table, which is why a switch can beat a chain of ifs. 4 is true: omitting break is legal and produces deliberate fall-through.

2 is false. A switch cannot test a range - each value in the range needs its own label - which is what if-else if is for.

Please log in or register to report this question.

95.

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.

96.

If scanf() is used to store a value in a char variable then along with the value a carriage return also gets stored with it.

Answer: B

Correct answer: False

Nothing extra is stored. scanf("%c", &ch) reads exactly one character into the variable; the newline you typed afterwards is simply left in the input buffer, where it will be picked up by the next read. That is why a second %c so often appears to read nothing - it consumes the leftover newline.

Please log in or register to report this question.

97.

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.

98.

A char variable can store either an ASCII character or a Unicode character.

Answer: A

Correct answer: False

In C a char is one byte - sizeof(char) is 1 by definition - and on ordinary implementations that is eight bits, giving 256 possible values. That is ample for ASCII, which uses only the codes 0 to 127, but Unicode defines over a million code points, so a single char cannot hold an arbitrary Unicode character. C provides other types for that work: wchar_t for wide characters, and since C11 char16_t and char32_t for UTF-16 and UTF-32 code units, declared in <uchar.h>. What a char array can hold is a Unicode character encoded in UTF-8, but that takes one to four char elements per character, not one - the character is spread across several bytes rather than stored in a single variable. The confusion usually comes from Java, where char is a 16-bit UTF-16 code unit and the statement would be much closer to true. In C it is false.

Please log in or register to report this question.

99.

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.

100.

Can we use a switch statement to switch on strings?

Answer: B

Correct answer: No

A switch requires an integer controlling expression, and a string in C is an array of characters - in an expression, a pointer. Comparing strings means comparing their contents, which == does not do, so a chain of if (strcmp(s, "...") == 0) is the usual alternative. (Java added string switches; C has not.)

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