Declaration(C++ Programming ) Questions and Answers

Question 1.

Identify the type of the variables.
 typedef char* CHAR;
 CHAR p,q;


  1.    char*
  2.    char
  3.    CHAR
  4.    unknown
Explanation:-
Answer: Option A. -> char*

The statement makes CHAR a synonym for char*.



Question 2.

Identify the incorrect statements.
 int var = 10;
 int *ptr = &(var + 1); //statement 1
 int *ptr2 = &var; //statement 2
 &var = 40; //statement 3


  1.    Statement 1 and 2 are wrong
  2.    Statement 2 and 3 are wrong
  3.    Statement 1 and 3 are wrong
  4.    All the three are wrong
Explanation:-
Answer: Option C. -> Statement 1 and 3 are wrong

In statement 1 lvalue is required as unary ‘&’ operand and in statement 3 lvalue is required as 

left operand.



Question 3.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int a = 10;
6.
if (a < 10) {
7.
for (i = 0; i < 10; i++)
8.
cout
  1.    0123456789
  2.    123456789
  3.    0
  4.    error
Explanation:-
Answer: Option D. -> error

We will get compilation error because â€˜i’ is an undeclared identifier.



Question 4.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
void addprint()
4.
{
5.
static int s = 1;
6.
s++;
7.
cout
  1.    234
  2.    111
  3.    123
  4.    235
Explanation:-
Answer: Option A. -> 234

The variable that is declared as static has a file scope.
Output:
$ g++ dec2.cpp
$ a.out
234



Question 5.

Can two functions declare variables(non static) with the same name.


  1.    No
  2.    Yes
  3.    Yes, but not a very efficient way to write programs.
  4.    No, it gives a runtime error.
Explanation:-
Answer: Option C. -> Yes, but not a very efficient way to write programs.

We can declare variables with the same name in two functions because their scope lies within the 

function.



Question 6.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
int g = 100;
4.
int main()
5.
{
6.
int a;
7.
{
8.
int b;
9.
b = 20;
10.
a = 35;
11.
g = 65;
12.
cout
  1.    2035655065
  2.    2035655035
  3.    2035635065
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> 2035655065

The local values of a and g within the block are more dominant than the global values.
Output:
$ g++ dec1.cpp
$ a.out
2035655065



Question 7.

Choose the correct option.
 extern int i;
 int i;


  1.    both 1 and 2 declare i
  2.    1 declares the variable i and 2 defines i
  3.    1 declares and defines i, 2 declares i
  4.    1 declares i,2 declares and defines i
Explanation:-
Answer: Option D. -> 1 declares i,2 declares and defines i

The keyword extern is not a definition and is not allocated storage until it is initialized.



Question 8.

Which of the given statements are false.
1. extern int func;
2. extern int func2(int,int);
3. int func2(int,int);
4. extern class foo;


  1.    3 and 4 only
  2.    2 and 3 only
  3.    only 4
  4.    2, 3 and 4
Explanation:-
Answer: Option C. -> only 4

No extern are allowed for class declarations.



Question 9.

Pick the right option
 Statement 1:Global values are not initialized by the stream.
 Statement 2:Local values are implicitly initialised to 0.


  1.    Statement 1 is true, Statement 2 is false.
  2.    Statement 2 is true, Statement 1 is false.
  3.    Both are false.
  4.    Both are true.
Explanation:-
Answer: Option C. -> Both are false.

Global values are implicitly initialised to 0, but local values have to be initialised by the system.



Question 10.

Pick the right option
 Statement 1:A definition is also a declaration.
 Statement 2:An identifier can be declared just once.


  1.    Statement 1 is true, Statement 2 is false.
  2.    Statement 2 is true, Statement 1 is false.
  3.    Both are false.
  4.    Both are true.
Explanation:-
Answer: Option B. -> Statement 2 is true, Statement 1 is false.

An identifier can be declared many times must be defined just once.