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

C Constants Questions and Answers

81.

If abc is the input, then the following program fragment
char x, y, z ;
printf ("% d" , scanf ("%c%c%c" , &x , &y , &z )) ;
results in

Answer: D

Correct answer: printing of 3

scanf returns the number of input items successfully assigned. With the input abc and three %c conversions, all three characters are read and assigned, so it returns 3, and the outer printf prints that.

Note that %c does not skip whitespace as most conversions do, so the three characters read are exactly the next three in the stream. Checking this return value is the correct way to detect a failed or partial scanf, and neglecting it is a common source of bugs.

Please log in or register to report this question.

82.

The constants are also called as _____________

Answer: C

Correct answer: literals

Constants written directly in source are called literals - the value appears literally in the text rather than being referred to by a name.

The categories are integer literals, floating-point literals, character literals and string literals. const is a type qualifier applied to an object, which is a different idea: a const object has a name and an address, whereas a literal is a value in an expression. A variable is by definition the thing a constant is not.

Please log in or register to report this question.

Answer: C

Correct answer: Enumeration feature increase the power of C

Enumerations add expressive power: they let a set of related integer constants be given a named type, so the intent is visible in the source, the values appear symbolically in a debugger, and the compiler can warn where a switch fails to handle every enumerator.

The other statements are false as written. Enum variables are ordinary integer objects - they can be assigned new values and compared freely, and C does not restrict them to their declared enumerators. That looseness is the price of the compatibility C keeps with plain integers.

Please log in or register to report this question.

84.

The minimum number of temporary variables needed to swap the contents of two variables is

Answer: D

Correct answer: 0

No temporary is needed. Two values can be exchanged in place with arithmetic:

b = s + b; s = b - s; b = b - s;

or with the exclusive-or trick, a ^= b; b ^= a; a ^= b;.

Both work, but neither is recommended in practice: the arithmetic version can overflow, the XOR version fails silently when both operands are the same object, and a compiler generates better code from the obvious three-line swap with a temporary than from either. The question asks what is possible, not what is wise.

Please log in or register to report this question.

85.

The statement 

printf( " % f ", ( float )9/5) ;

prints

Answer: A

Correct answer: 1.8

A cast binds more tightly than division, so the expression is ((float)9) / 5. The left operand is now floating point, so the usual arithmetic conversions promote 5 to floating point too and the division is performed in floating point:

\[ 9.0 / 5 = 1.8 \]

Without the cast, 9/5 would be integer division giving 1, and printing that with %f would be undefined behaviour rather than merely wrong. Casting one operand is the standard way to force a floating point division.

Please log in or register to report this question.

86.

Comment on the output of this C code?

#include <stdio.h>
    void main()
    {
        int k = 4;
        int *const p = &k;
        int r = 3;
        p = &r;
        printf("%d", p);
    }

 

Answer: C

Since the pointer p is declared to be constant, trying to assign it with a new value results in an error.

Output:
$ cc pgm11.c
pgm11.c: In function ‘main’:
pgm11.c:7: error: assignment of read-only variable ‘p’
pgm11.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int * const’

Please log in or register to report this question.

Answer: A

Correct answer: The first one refers to a variable whose identifier is x and the second one refers to the character constant x

Without quotes, x is an identifier - the name of a variable, function or other declared entity. In single quotes, 'x' is a character constant, the integer value of the character x in the execution character set, 120 in ASCII.

Double quotes would give a third thing again: "x" is a string literal, an array of two characters - the x and a terminating null. The three are entirely distinct, and confusing the single-quoted and double-quoted forms is among the commonest beginner errors.

Please log in or register to report this question.

88.

------not a secondry constant.

Answer: A

Correct answer: Character

A character constant is a primary constant, alongside integer and real constants - one of the three fundamental kinds of value written directly in source.

The secondary constants are those built from the primary ones: arrays, pointers, structures, unions and enumerations. The division parallels the distinction between primitive and derived types, so the odd one out here is the only item that is not composed of anything more basic.

Please log in or register to report this question.

89.

What is the output of this C code?

#include <stdio.h>
    #define a 10
    int main()
    {
        const int a = 5;
        printf("a = %d\n", a);
    }

 

Answer: C

The #define substitutes a with 10 leaving no identifier and hence compilation error.

Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:5: error: expected identifier or ‘(’ before numeric constant

Please log in or register to report this question.

90.

C programming language provides operations which deal directly with objects such as

Answer: C

Correct answer: characters, integers, and floating point numbers

C provides operations directly on the fundamental machine types - characters, integers and floating point numbers - because those are what the hardware supports.

It provides nothing built in for strings, sets, lists or dynamic arrays. A C string is only a convention: an array of characters terminated by a null, manipulated through library functions rather than through operators. This minimalism is deliberate, and it is what allows C to compile to efficient code on machines with very different capabilities.

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