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

Declarations and Initializations Questions and Answers

21.

Does this compile without error?

int main()
{
for (int k = 0; k < 10> return 0;
}

Answer: C

Compilers implementing C90 does not allow this but compilers implementing C99 allow it.
Output:
$ cc pgm4.c
pgm4.c: In function ‘main’:
pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code

Please log in or register to report this question.

22.

Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ?

Answer: C

Correct answer: rem = fmod(3.14, 2.1);

The % operator requires integer operands, so it will not compile here. fmod is the floating point remainder function declared in math.h, and it is the only one of the four options that computes what the question asks for.

Please log in or register to report this question.

23.

Which of the following special symbol allowed in a variable name?

Answer: D

Correct answer: _ (underscore)

An identifier may contain letters, digits and the underscore, and may not begin with a digit. The underscore is therefore the only special character permitted, and it is how multi-word names are written where the language will not accept a hyphen or a space.

Please log in or register to report this question.

24.

Is the following statement a declaration or definition?
extern int i;

Answer: A

Correct answer: Declaration

extern int i; announces that an object named i of type int exists somewhere, without reserving storage for it. A definition is what allocates the storage, and there must be exactly one of those in the program however many declarations refer to it.

Please log in or register to report this question.

25.

Suppose a program is divided into three files f1, f2 and f3, and a variable is defined in the file f1 but used in files f2 and f3. In such a case would we need the extern declaration for the variables in the files f2 and f3?

Answer: A

Correct answer: Yes

The definition lives in one file and reserves the storage; the other two files must be told that the name exists and what type it has, or the compiler has nothing to check their uses against. That is precisely what extern does, and the declaration is conventionally put in a header included by both.

Please log in or register to report this question.

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