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

C Constants Questions and Answers

71.

What is the output of this C code?

#include <stdio.h>
    #include <string.h>
    int main()
    {
        char *str = "x";
        char c = 'x';
        char ary[1];
        ary[0] = c;
        printf("%d %d", strlen(str), strlen(ary));
        return 0;
    }

 

Answer: D

Correct answer: 1 (undefined value)

strlen(str) is 1, since the literal "x" is two bytes - the character and its terminating null.

The second call is undefined behaviour. ary is a one-element array holding 'x' with no room for a terminator, so strlen reads past the end of the array looking for a null and returns whatever count it reaches. The value is unpredictable, and the program may crash. This is why a character array intended as a string must always be declared one element longer than the text it holds.

Please log in or register to report this question.

72.

The variables which can be accessed by all modules in a program, are called

Answer: D

Correct answer: global variables

Variables accessible to all modules of a program are global variables - declared at file scope with external linkage, so that any translation unit can refer to them by declaring them extern.

"External variable" is a near synonym and describes the same thing from the linkage side, but the term the question asks for is global. Local and internal variables are the opposite case: confined to the block or the translation unit in which they are declared.

Please log in or register to report this question.

73.

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.

74.

The declaration "unsigned u" indicates u is a/an

Answer: B

Correct answer: unsigned integer

In C the type specifiers may be abbreviated, and unsigned alone means unsigned int. Similarly signed alone, short alone and long alone all mean the corresponding int type.

An unsigned character would have to be written unsigned char, which is a distinct type - and one that must be written out in full, since char is the only type whose signedness is implementation-defined and which therefore has three distinct spellings.

Please log in or register to report this question.

75.

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.

76.

We cannot use the way to declare the constant.

Answer: C

Correct answer: int constant a=1

There is no keyword constant in C. The qualifier is const, so int constant a=1; would be read as declaring a variable named constant of type int, followed by a stray a=1, and would not compile.

The first two forms are both valid and equivalent: a type qualifier may appear on either side of the type specifier, so const int a = 1; and int const a = 1; declare exactly the same thing.

Please log in or register to report this question.

77.

What is the output of this C code?

#include <stdio.h>
    #define MAX 2
    enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
    int main()
    {
        enum bird b = PARROT;
        printf("%d\n", b);
        return 0;
    }

 

Answer: B

Correct answer: 5

The preprocessor substitutes MAX textually before the compiler sees the enumeration, so it reads:

enum bird {SPARROW = 2 + 1, PARROT = SPARROW + 2};

giving SPARROW the value 3 and PARROT the value 5. Enumerator values must be constant expressions, and both of these are - an earlier enumerator may be used in a later one's value, which is what makes the second definition legal.

Please log in or register to report this question.

Answer: D

Correct answer: both (b) & (c)

Coercion is implicit conversion - the compiler converting a value automatically under the usual arithmetic conversions, as when an int is turned into a float to be added to one. Casting is explicit conversion, written by the programmer as (float)n.

The first statement reverses the terms and is therefore false. The distinction matters because implicit conversions happen silently and can lose information, which is why compilers warn about narrowing ones and why an explicit cast is read as the programmer asserting the conversion is intended.

Please log in or register to report this question.

Answer: C

Correct answer: which may require less storage than normal integers

short int requests an integer type that occupies no more storage than int, and typically less - commonly 2 bytes against 4. The standard guarantees only a minimum range and the ordering short ≤ int ≤ long, not any particular size.

The name refers to storage, not to the variable's lifetime or the length of its identifier. It is used where memory matters - in large arrays or in structures mapped onto a hardware or file format - at the cost of a narrower range.

Please log in or register to report this question.

80.

The value of ab if ab & 0 x 3f equals 0 x 27 is

Answer: D

Correct answer: 0 x 27

The mask 0x3f is 00111111 in binary, so ab & 0x3f keeps the low six bits of ab and clears the rest. For the result to be 0x27, those six bits must be 100111.

Of the values offered, 0x27 itself satisfies this, since 0x27 & 0x3f = 0x27. Note that the condition does not determine ab uniquely - any value agreeing in the low six bits, such as 0x67 or 0xa7, would also satisfy it - but only 0x27 appears among the options.

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