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

C Constants Questions and Answers

91.

If s and b are integers then  purpose of the following program fragment is to 

b = s + b ;

s = b - s ;

b = b - s ;

Answer: C

Correct answer: exchange (swap) the contents of s and b

Trace the three assignments with initial values \( s \) and \( b \):

\[ b \leftarrow s+b, \qquad s \leftarrow (s+b)-s = b, \qquad b \leftarrow (s+b)-b = s \]

so the two values are exchanged without a temporary. The technique is a curiosity rather than good practice: the intermediate sum can overflow, and a compiler produces better code from an ordinary swap using a temporary variable.

Please log in or register to report this question.

92.

Whats is the output of this C code?

void main()
{
int const k = 11;
k++;
printf("k is %d", k);
}

 

Answer: D

Correct answer: Error, because a constant variable cannot be changed

The declaration int const k = 11; makes k a const-qualified object, and k++ attempts to modify it, which the compiler rejects.

Note that int const and const int are equivalent - the qualifier may be written on either side of the type - so there is nothing wrong with the combination itself. It is the increment that fails. The error is a compile-time diagnostic, not a run-time fault, since const-ness is enforced entirely by the compiler.

Please log in or register to report this question.

93.

Consider the statements
putchar ( getchar ( ));
putchar(getchar( ));
if
a
b
is the input, the output will be

Answer: B

Correct answer: a followed by a newline; b is never read

The trap is that the newline is itself a character in the input stream. Typing a, Enter, b, Enter puts four characters into stdin: ’a’, ’\n’, ’b’, ’\n’. getchar reads one character at a time and does not skip whitespace, so the first putchar(getchar()) reads and prints a, and the second reads the newline and prints it. The b is still sitting in the buffer when the program ends. The output is therefore the letter a followed by a line break - not ab, which would require the input to be typed as a single line ab, and not a b, which would need a space between them. This is the reason scanf("%d") followed by getchar() so often appears to skip an input: the newline left behind by the earlier read is consumed by the later one. The keyed option in this record read “this can’t be the input”, which describes nothing; its text has been corrected to the actual output.

Please log in or register to report this question.

Loading…
Tags: C Constants Questions and Answers || C Constants MCQ Questions and Answers || C Constants GK Questions and Answers || C Constants GK MCQ Questions || C Constants Multiple Choice Questions and Answers || C Constants GK || GK on C Constants || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming