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

Control Instructions Questions and Answers

101.

We want to test whether a value lies in the range 2 to 4 or 5 to 7. Can we do this using a switch?

Answer: A

Correct answer: Yes

A switch cannot test a range directly, but the ranges here are small and the values are integers, so each can be listed as its own label with fall-through:

case 2: case 3: case 4: /* first range */ break;
case 5: case 6: case 7: /* second range */ break;

This works because a label with no statements falls through to the next. For a wide or non-integer range, an if is the only option.

Please log in or register to report this question.

102.

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.

103.

In the loop construct for(exp1; exp2; exp3), which part is always executed exactly once?

Answer: A

Correct answer: exp1

exp1, the initialisation, is evaluated once before the loop starts and never again. exp2, the condition, is evaluated before every pass - at least once, and once more than the body runs. exp3, the update, runs after each pass, so it may not run at all if the condition fails immediately.

Please log in or register to report this question.

104.

The continue statement cannot be used with

Answer: D

Correct answer: switch

continue means “begin the next iteration”, so it belongs to the three loops. A switch has no iterations, so a continue written inside one is either applied to an enclosing loop or, if there is none, rejected. break, by contrast, works in all four.

Please log in or register to report this question.

105.

Which keyword can be used for coming out of recursion?

Answer: A

Correct answer: return

Recursion is unwound by return: the base case returns instead of calling again, and each frame then returns in turn. break applies to loops and switches, not to function calls, and exit() would abandon the whole program rather than come back.

Please log in or register to report this question.

106.

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.

108.

A switch statement accepts a controlling expression of type

Answer: D

Correct answer: All of the above

The controlling expression must be of integer type, and int, char and long all qualify - as do short and enumerations, with the smaller types promoted to int. Only the floating-point types are excluded.

Please log in or register to report this question.

109.

Which loop is guaranteed to execute at least once?

Answer: C

Correct answer: do while

A do-while tests its condition after the body, so the body always runs at least once. for and while test first, and so may not run at all. This makes do-while the natural choice for reading input and then validating it.

Please log in or register to report this question.

110.

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.

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