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.

Which of the following has compilation error in C ?

Exam Year(s)

UGC NET - 2013,

Answer: D

None of the declarations has a compilation error, so 'none of the above' is correct. int n = 32; is a plain initialisation. char ch = 65; is allowed because 65 fits in a char and is the code of 'A'. float f = (float) 3.2; is a valid cast from a double constant to float.

Each of the first three lines is therefore legal C, and no option other than the last can be the answer to a 'which has an error' question.

Please log in or register to report this question.

8.

A variable type 'char' indicates the range and the no. of bytes.

1. 1 byte 2. 2 bytes
3. -127 to + 128 4. -128 to +127

Exam Year(s)

UGC NET - 2013,

Answer: C

The signed char type takes 1 byte and ranges from -128 to +127, which is option (C), statements 1 and 4. One byte holds 8 bits, giving 256 values, and in two's complement these run from -128 to +127. Statement 2 is wrong because 2 bytes is the size of an int on a 16-bit compiler. Statement 3, from -127 to +128, is not a valid two's complement range, since +128 cannot be stored in a signed byte. Options (A), (B) and (D) each contain one of those two wrong statements.

Please log in or register to report this question.

9.

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.

10.

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.

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