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.

Predict the output of the following C source code :

int x = 10;
int main( )
{
  int x = 20;
  {
    int x = 30;
    {extern x; printf("%d", x);}
  }
  return 0;
}

Exam Year(s)

MH SET - 2024, SET Exam - 2024,

Answer: A

The program prints 10.

The innermost block declares extern x;, which (with the old implicit int) declares that x refers to an object with external linkage. That is the global int x = 10, not either local x, so within that block printf prints 10.

Without the extern declaration, the innermost local x = 30 would be printed. The value 20 belongs to the outer local that is hidden here, and only one printf runs, so '10 20 30' is impossible.

Please log in or register to report this question.

3.

In C, which statement about static inside a function is correct ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: A

The correct statement is that its lifetime is the entire program.

A static variable declared inside a function has static storage duration: it is created once, before the program's main work begins, and lasts until the program ends. Its scope is still limited to the function, but it keeps its value between calls, which makes it useful for counters.

It is initialised only once, not on every call, so option B is wrong. It lives in the data (or BSS) segment, not on the stack, which is where ordinary local variables go. And it can be modified; a variable that cannot be changed is const, not static.

Please log in or register to report this question.

4.

What is the purpose of the 'volatile' keyword in programming languages like C/C++ ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: A

The volatile keyword prevents variable optimization by the compiler.

Declaring a variable volatile tells the compiler its value can change outside the program's visible flow, for example through a memory-mapped hardware register, an interrupt handler or a signal. The compiler must then read and write the variable in memory every time, instead of keeping it in a register or removing 'redundant' accesses.

It does not make a variable constant (that is const), and it does not decide where the variable is stored. It does not enable multi-threading, and in C and C++ it is not a substitute for proper thread synchronisation.

Please log in or register to report this question.

5.

What is the correct way to declare an integer variable in C ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: D

The correct declaration is int x;.

A C declaration gives the type first and then the variable name, ending with a semicolon. int is the keyword for the integer type, so int x; reserves storage for an integer called x.

C has no 'integer' keyword, putting the name before the type (x int;) is not C syntax, and var is used for declarations in JavaScript and C#, not in C.

Please log in or register to report this question.

6.

What is the size of int in C on a 32-bit system ?

Exam Year(s)

Tripura Public Service Commission - 2026,

Answer: C

On a 32-bit system an int is usually 4 bytes.

The C standard only requires int to be at least 16 bits, leaving the exact size to the compiler. On 32-bit (and most 64-bit) platforms, compilers make int 32 bits, which is 4 bytes, matching the natural word size of the processor.

2 bytes was the size of int on old 16-bit compilers such as Turbo C under DOS. 1 byte is the size of a char, and 8 bytes is typical of long long or of double.

Please log in or register to report this question.

7.

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.

8.

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.

Loading…

Other Subjects

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