Declaration & Initialisation Questions with Answers

15 practice questions on Declaration & Initialisation (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Medium

Who is father of C Language?

  1. Dennis Ritchie
  2. James A. Gosling
  3. Dr. E.F. Codd
  4. Bjarne Stroustrup

Answer: A. Dennis Ritchie

Explanation

Dennis Ritchie created the C language at Bell Labs in about 1972.

Step-by-step solution

  1. C was created by Dennis Ritchie at Bell Labs.
  2. The other names belong to other languages: Gosling created Java, Stroustrup created C++, and Codd proposed the relational model.
  3. So the answer is Dennis Ritchie.

Why the other options are wrong

  • B. James Gosling created Java, not C.
  • C. Dr. E.F. Codd proposed the relational database model, not C.
  • D. Bjarne Stroustrup created C++, which came after C.

Tip: C: Dennis Ritchie; C++: Stroustrup; Java: Gosling.

Question 2Medium

Which of the following cannot be a variable name in C...?

  1. volatile
  2. true
  3. friend
  4. export

Answer: A.

Explanation

volatile is a reserved C keyword.so it cannot be as a Identifier and Variable name, Function name and Array name are the Identifiers.

Step-by-step solution

  1. Volatile is a reserved keyword in C, so it cannot be used as a variable name.
  2. True is not a keyword in C, since it is only a macro in stdbool.h, so it can be used as a name.
  3. Friend and export are not C keywords either, so they are also valid names.
  4. So the answer is volatile.

Why the other options are wrong

  • B. True is not a keyword in C, so it can be used as a variable name.
  • C. Friend is a keyword in C++ only, not in C.
  • D. Export is not a keyword in C, so it is a valid name.

Tip: Keywords like volatile, register and typedef cannot be used as variable names.

Question 3Medium

Which of the following is allowed in a C Arithmetic instruction...?

  1. [ ]
  2. { }
  3. ( )
  4. None of the above

Answer: C. ( )

Explanation

() operator has highest priority in c language.We use this operator to execute lower priority operators before highest.
For example:
consider expression 3*5+3 compiler give result 18. If we want to add 3 and 5, after that multiply result with 3, then we can use () operator like 3*(5+3) compiler will give result 24.
We never use [],{} this operator in C Arithmetic instruction.[] is Array subscript operator and {} operator used to define blocks in C language.

Step-by-step solution

  1. C arithmetic expressions use operators and grouping with parentheses.
  2. Square brackets are for array subscripts, not for arithmetic.
  3. Curly braces are for blocks and initializers, not for arithmetic.
  4. The answer is ( ).

Why the other options are wrong

  • A. Square brackets are for array subscripts, not arithmetic grouping.
  • B. Curly braces define blocks, not arithmetic grouping.
  • D. Parentheses are allowed, so none of the above is wrong.

Tip: Parentheses are the only grouping symbol used in arithmetic expressions.

Question 4Medium

Why it is Invalid Variable Declaration...?

float 3Bedroom-Hall-Kitchen?;
  1. The variable name begins with an integer
  2. The special character " -"
  3. The special character ?
  4. All of the mentioned

Answer: D. All of the mentioned

Explanation

A variable name cannot start with an integer, along with that the C compiler interprets the - and? as a minus operator and a question mark operator respectively.

Step-by-step solution

  1. A variable name cannot start with a digit, so 3Bedroom is invalid.
  2. A name cannot contain a minus sign, so Bedroom-Hall-Kitchen is invalid.
  3. A name cannot contain the? character.
  4. All three problems are present, so the answer is All of the mentioned, which is option D.

Why the other options are wrong

  • A. Starting with a digit is one error, but the minus and? are also errors.
  • B. The minus sign is one error, but the digit and? are also errors.
  • C. The question mark is one error, but the digit and minus are also errors.

Tip: Identifiers use letters, digits and underscores, and cannot start with a digit.

Question 5Medium

What will happen if the below program is executed...?

#include <stdio.h>
int main()
 {
    int main = 3;
    printf("%d", main);
    return 0;
 }
  1. It will cause a compile-time error
  2. It will cause a run-time error
  3. It will run without any error and prints 3
  4. It will experience infinite looping

Answer: C. It will run without any error and prints 3

Explanation

A C program can have same function name and same variable name.

Step-by-step solution

  1. Main is an ordinary identifier in C, so a local int variable can be named main.
  2. Inside main, the local variable named main hides the function name.
  3. ```c printf("%d", main) ``` prints the value of the variable, which is 3.
  4. So the answer is It will run without any error and prints 3.

Why the other options are wrong

  • A. Valid C allows a variable named main, so there is no compile error.
  • B. No run-time error occurs, because the variable is read normally.
  • D. There is no loop; the program runs once and ends.

Tip: A name like main is legal as a variable in C, though it is poor style.

Question 6Medium

Which operators are known as Ternary Operator...?

  1. ::, ?
  2. ?, :
  3. ?, ;;
  4. None of the above

Answer: B. ?, :

Explanation

Valid Ternary operator syntax is? , : and Syntax is: (Expression )? truth_part: false_part
Ternary Operator is also called Tertiary Operator.

Step-by-step solution

  1. The ternary operator has three operands and uses two symbols.
  2. The two symbols are? and :, written as condition? a: b.
  3. So the answer is ?, :.

Why the other options are wrong

  • A. :: is the scope resolution operator, not part of the ternary operator.
  • C. ;; is not a ternary symbol; the second symbol is a colon.
  • D. The ternary operator does have symbols, so none of the above is wrong.

Tip: Ternary form: condition? value1: value2.

Question 7Medium

Which of the following is not a valid variable name declaration...?

  1. float PI = 3.14;
  2. double PI = 3.14;
  3. int PI = 3.14;
  4. #define PI 3.14

Answer: D.

Explanation

#define PI 3.14

is a macro preprocessor,it is a textual substitution. So we cant say it as a Variable Declaration

Step-by-step solution

  1. Float PI, double PI and int PI are all valid declarations of a variable named PI.
  2. Int PI = 3.14 is legal C, and it just stores 3 in the int.
  3. #define PI 3.14 is a preprocessor macro, not a variable declaration.
  4. So the answer is #define PI 3.14.

Why the other options are wrong

  • A. Float PI = 3.14; is a valid variable declaration.
  • B. Double PI = 3.14; is a valid variable declaration.
  • C. Int PI = 3.14; is valid and simply stores the value 3.

Tip: A #define line is a macro, not a variable, so it is never a declaration.

Question 8Medium

Which is the right way to declare constant in C...?

  1. int constant var =10;
  2. int const var = 10;
  3. const int var = 10;
  4. B & C Both

Answer: D. B & C Both

Explanation

const can come before or after the type, so int const var = 10; and const int var = 10; are both valid. The word constant is not a C keyword, so option A is wrong.

Step-by-step solution

  1. Option A has two identifiers, constant and var, in a row, so it is not a valid declaration.
  2. Option B, int const var = 10, is valid because const can follow the type.
  3. Option C, const int var = 10, is valid because const can come before the type.
  4. Options B and C are both correct, so the answer is B & C Both.

Why the other options are wrong

  • A. Two identifiers in a row make this an invalid declaration.
  • B. This is correct on its own, but option D covers both B and C.
  • C. This is correct on its own, but option D covers both B and C.

Tip: Const can go before or after the type name in C.

Question 9Medium

What is the output of this C code?

 #include <stdio.h>
  int main()
  {
     printf("Hello World! %d ", x);
     return 0;
  }
  1. Hello World! x;
  2. Hello World! followed by a junk value
  3. Compile time error
  4. Hello World! 0

Answer: C. Compile time error

Explanation

It results in an error since x is used without declaring the variable x.Error is: Undefined Symbol x;
A variable should be declared before we use it in program.

Step-by-step solution

  1. X is used in printf but is never declared in the program.
  2. C requires a variable to be declared before it is used.
  3. So the compiler reports an error, and the answer is Compile time error, which is option C.

Why the other options are wrong

  • A. The code does not compile, so nothing is printed.
  • B. The compile error stops the program, so no junk value is printed.
  • D. The code does not compile, so no output is printed.

Tip: Undeclared variables are compile errors in C.

Question 10Medium

C programs are converted into machine language with the help of...?

  1. An Editor
  2. A compiler
  3. An operating system
  4. None of the above

Answer: B. A compiler

Explanation

C is middle level language, Computer understand only machine level language like assembly language, to convert C program into machine level language Compiler and Interpreter are used.
Compiler is system program that's convert high level language program into machine level language.

Step-by-step solution

  1. A C program is written as text and must be translated into machine language.
  2. A compiler does this translation.
  3. An editor only writes the text, and an operating system runs programs.
  4. So the answer is A compiler.

Why the other options are wrong

  • A. An editor only writes the source text.
  • C. An operating system loads and runs programs but does not translate C.
  • D. A compiler is the correct tool, so None of the above is wrong.

Tip: The compiler turns C source into machine code.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics