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

Declarations and Initializations Questions and Answers

1.

Consider the following three ANSI-C programs, P1, P2, and P3.

P1P2P3
#include \(\lt\)stdio.h\(\gt\)
int a=5;
int main(){
 int a=7;
 return(0);
}
#include \(\lt\)stdio.h\(\gt\)
int main(){
 int a=5;
 int a=7;
 return(0);
}
#include \(\lt\)stdio.h\(\gt\)
int main(){
 int a=5;
 float a=7;
 return(0);
}

Which one of the following statements is true?

Exam Year(s)

GATE CS - 2026,

Answer: A

Only P1 will compile without any error.

P1 declares a global int a=5; and a local int a=7; inside main. These are in different scopes, so the local declaration simply hides the global one inside main. That is legal C.

P2 declares int a twice in the same block scope. C does not allow two definitions of the same identifier in one block, so this is a redeclaration error.

P3 declares a as an int and then as a float in the same block: a conflicting-types error. So only P1 compiles, which rules out the other three options.

Please log in or register to report this question.

2.

What is the output of this C code?

void main()
{
int k = 4;
float k = 4;
printf("%d", k)
}

Answer: A

Correct answer: Compile time error

The same identifier is declared twice in one scope with different types, which the language forbids: k cannot be both an int and a float. The error is diagnosed at compile time, so no output is produced at all.

Please log in or register to report this question.

3.

Does this compile without error?

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

Answer: A

Correct answer: Yes

The inner braces open a new block, and a declaration in an inner block may reuse a name from an outer one - the inner k simply shadows the outer for the extent of that block. It is legal, though the shadowing is a common source of confusion and most compilers will warn about it.

Please log in or register to report this question.

Answer: D

Correct answer: char[] str = “...”;

C writes the brackets after the identifier, not after the type: char str[]. The form with the brackets before the name belongs to Java. The other three are all valid ways of holding a string, though the third truncates if the literal exceeds the declared size.

Please log in or register to report this question.

Answer: B

Correct answer: External, Internal and None

Three linkages exist. External linkage makes a name visible across translation units, internal linkage confines it to one file - which is what static at file scope gives - and no linkage belongs to block-scope names, each declaration of which denotes a distinct object.

Please log in or register to report this question.

6.

How would you round off a value from 1.66 to 2.0?

Answer: A

Correct answer: ceil(1.66)

ceil returns the smallest integer not less than its argument, so 1.66 becomes 2.0. floor would give 1.0, and neither roundup nor roundto exists in the standard library - the standard rounding function is round.

Please log in or register to report this question.

7.

Global variable are available to all functions. Does there exist a mechanism by way of which it available to some and not to others.

Answer: B

Correct answer: No

A name with external linkage is visible to every function that declares it, and nothing distinguishes one function from another. What can be restricted is the file: declaring the variable static at file scope confines it to that translation unit. The granularity is the file, not the function.

Please log in or register to report this question.

8.

Which of the following format identifier can never be used for the variable var?

int main()
{
char *var = "Advanced Training in C by AllIndiaExams.com";
}

Answer: A

Correct answer: %f

var is a pointer to char, so %s prints the string, and %d or %c could at least be applied to the pointer value or to a dereferenced character. %f expects a double and could never be given anything derived from this variable, which is what makes it the answer.

Please log in or register to report this question.

9.

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 is defined only for integer operands and will not compile with doubles. fmod from math.h is the floating point equivalent and returns 1.04 here. modf is unrelated - it splits a value into its integer and fractional parts.

Please log in or register to report this question.

10.

Is it true that a function may have several declarations, but only one definition?

Answer: A

Correct answer: Yes

A prototype may appear in every file that calls the function, and usually does by way of a header, but the body is written once. The rule is the same as for objects: declarations may be repeated, and the definition may not.

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