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

C Constants Questions and Answers

61.

To print out a and b given below, which printf() statement would you use?

float a = 3.14;
double b = 3.14;

Answer: A

Correct answer: printf("%f%f",a,b);

Both are printed with %f. In a call to a variadic function such as printf, the default argument promotions apply, and a float argument is promoted to double before it is passed - so printf receives two doubles and %f is correct for each.

There is therefore no %f for float and separate specifier for double; %f serves both. %Lf is for long double, and using it here would misread the argument and produce nonsense.

Please log in or register to report this question.

62.

When a variable of data type double is converted into float, then

Answer: A

Correct answer: Rounding takes place

Narrowing a double to a float is a value conversion, not a reinterpretation of bits. If the value cannot be represented exactly in single precision, the result is one of the two nearest representable values, chosen according to the current rounding mode - normally to nearest.

So it is rounding rather than truncation, and describing it as dropping the low-order bits is wrong in both senses: the exponent field differs too, and mantissa bits are rounded rather than discarded. If the magnitude is too large for float, the result is infinity.

Please log in or register to report this question.

63.

The library function sqrt operates on a double precision argument. If, i is an integer variable, then which of the following calls would correctly compute sqrt(i)?

Answer: A

Correct answer: sqrt((double)i)

The cast must be applied to the argument, before the call, so that the value passed has the type the function expects.

In practice a prototype from math.h would convert i automatically, so sqrt(i) also works when the header is included - but the explicit cast is correct regardless and states the intent. Casting the result, as the other options do, is too late: it converts a value the function has already computed, and without a prototype the argument would have been passed as an int and misinterpreted.

Please log in or register to report this question.

64.

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.

65.

The declaration
enum cities{ bethlehem, jericho, nazareth = 1, jerusalem }
assigns the value 1 to

Answer: D

Correct answer: jericho and nazareth

Enumerators count upward from zero unless given an explicit value, and an explicit value resets the count. So bethlehem is 0, jericho is 1, nazareth is explicitly 1, and jerusalem follows at 2.

Both jericho and nazareth therefore have the value 1. C permits duplicate enumerator values, which is occasionally useful for aliases but is more often an accident - as here, where an explicit assignment collides with the automatic sequence.

Please log in or register to report this question.

66.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
        printf("PEACH = %d\n", PEACH);
    }

 

Answer: C

Correct answer: PEACH = 5

Enumerators continue from the previous value, and an explicit value restarts the sequence from that point.

So ORANGE = 5, MANGO becomes 6, BANANA = 4 resets the count, and PEACH follows at 5. ORANGE and PEACH therefore share the value 5, which C permits. The trap is expecting PEACH to continue from MANGO rather than from the explicitly reset BANANA.

Please log in or register to report this question.

67.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        printf("C programming %s", "Class by\n%s Sanfoundry", "WOW");
    }

 

Answer: C

This program has only one %s within first double quotes, so it does not read the string “WOW”.
The %s along with the Sanfoundry is not read as a format modifier while new line character prints the new line.
Output:
$ cc pgm2.c
$ a.out
C programming Class by
%s Sanfoundry

Please log in or register to report this question.

68.

For the following code snippet:

char *str = "Sanfoundry.com\0" "training classes";
The character pointer str holds reference to string:

Answer: B

Correct answer: Sanfoundry.com\0training classes

Adjacent string literals are concatenated at translation time, so the two become one literal with the \0 retained as a byte inside it and a further terminating null appended at the end.

The pointer refers to that whole storage. Printing it with %s would show only Sanfoundry.com, since printf stops at the first null - but what str holds a reference to is the complete concatenated sequence, which is what the question asks about.

Please log in or register to report this question.

69.

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.

70.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        int var = 010;
        printf("%d", var);
    }

 

Answer: B

Correct answer: 8

A leading zero makes an integer literal octal, so 010 is 8 in decimal:

\[ 010_8 = 1 \times 8 + 0 = 8 \]

The convention is a frequent source of bugs where numbers are written with leading zeros for alignment - 08 and 09 are not valid octal and fail to compile, while 010 compiles and silently means something other than ten. Hexadecimal needs the explicit 0x; octal needs only the zero.

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