C++ Programming Questions and Answers – Declaration

This section on C++ interview questions and answers focuses on “Declaration”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Declaration” along with answers, explanations and/or solutions:

1. Choose the correct option.

    extern int i;
    int i;

a) both 1 and 2 declare i
b) 1 declares the variable i and 2 defines i
c) 1 declares and defines i, 2 declares i
d) 1 declares i,2 declares and defines i
View Answer

Answer: d
Explanation: The keyword extern is not a definition and is not allocated storage until it is initialized.
advertisement
advertisement

2. Pick the right option.

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

a) Statement 1 is true, Statement 2 is false
b) Statement 2 is true, Statement 1 is false
c) Both are false
d) Both are true
View Answer

Answer: a
Explanation: When we define a variable, we automatically declare its type and assigns a memory location for it. So, statement1 is true. An identifier can be declared many times as long as they have different scopes. So, statement2 is false.

3. Which of the given statements are false?

i. extern int func;
ii. extern int func2(int,int);
iii. int func2(int,int);
iv. extern class foo;
advertisement

a) iii and iv only
b) ii and iii only
c) only iv
d) ii, iii and iv
View Answer

Answer: c
Explanation: No extern are allowed for class declarations.

4. Pick the right option.

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

a) Statement 1 is true, Statement 2 is false
b) Statement 2 is true, Statement 1 is false
c) Both are false
d) Both are true
View Answer

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

5. What will be the output of the following C++ code?

  1.     #include <iostream>
  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 << b << a << g;
  13.         }
  14.         a = 50;
  15.         cout << a << g;
  16.         return 0;
  17.     }

a) 2035655065
b) 2035655035
c) 2035635065
d) 2035645065
View Answer

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

6. Can two functions declare variables(non static) with the same name?
a) No
b) Yes
c) Yes, but not a very efficient way to write programs
d) No, it gives a runtime error
View Answer

Answer: c
Explanation: We can declare variables with the same name in two functions because their scope lies within the function.

7. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     void addprint()
  4.     {
  5.         static int s = 1;
  6.         s++;
  7.         cout << s;
  8.     }
  9.     int main()
  10.     {
  11.         addprint();
  12.         addprint();
  13.         addprint();
  14.         return 0;
  15.     }

a) 234
b) 111
c) 123
d) 235
View Answer

Answer: a
Explanation: The variable that is declared as static has a file scope.
Output:

$ g++ dec2.cpp
$ a.out
234

8. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 10;
  6.         if (a < 10) 
  7.         {
  8.             for (i = 0; i < 10; i++)
  9.                cout << i;
  10.         }
  11.         else 
  12.         {
  13.             cout << i;
  14.         }
  15.         return 0;
  16.     }

a) 0123456789
b) 123456789
c) 0
d) error
View Answer

Answer: d
Explanation: We will get compilation error because ‘i’ is an undeclared identifier.

9. Identify the incorrect statements.

    int var = 10;
    int *ptr = &(var + 1); //statement 1
    int *ptr2 = &var; //statement 2
    &&var = 40; //statement 3

a) Statement 1 and 2 are wrong
b) Statement 2 and 3 are wrong
c) Statement 1 and 3 are wrong
d) Statement 1, 2 and 3 are wrong
View Answer

Answer: c
Explanation: In statement 1 lvalue is required as unary ‘&’ operand and in statement 3 lvalue is required as left operand.

10. Identify the type of variables.

    typedef char* CHAR;
    CHAR p,q;

a) char*
b) char
c) CHAR
d) unknown
View Answer

Answer: a
Explanation: The statement makes CHAR a synonym for char*.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.