C++ Programming Questions and Answers – Derivation and Templates

This section on advanced C++ interview questions focuses on “Derivation and Templates”. One shall practice these advanced C++ 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 advanced C++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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

1. Which is dependant on template parameter?
a) base class
b) abstract class
c) method
d) static class
View Answer

Answer: a
Explanation: Base class is dependant on template parameter.

2. Which value is placed in the base class?
a) derived values
b) default type values
c) both default type & derived values
d) null value
View Answer

Answer: b
Explanation: We can place the default type values in a base class and overriding some of them through derivation.

3. How many bits of memory needed for internal representation of class?
a) 1
b) 2
c) 4
d) no memory needed
View Answer

Answer: d
Explanation: classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time.
advertisement

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     class class0 
  4.     {
  5.         public:
  6.         virtual ~class0(){}
  7.         protected:
  8.         char p;
  9.         public:
  10.         char getChar();
  11.     };
  12.     class class1 : public class0 
  13.     {
  14.         public:
  15.         void printChar();
  16.     };
  17.     void class1::printChar()
  18.     {
  19.         cout  << "True" << endl;
  20.     }
  21.     int main() 
  22.     {
  23.         class1 c;
  24.         c.printChar();
  25.         return 1;
  26.     }

a) True
b) error
c) no output
d) runtime error
View Answer

Answer: a
Explanation: In this program, We are passing the values and inheriting it to the other class and printing the result.

Free 30-Day Python Certification Bootcamp is Live. Join Now!
$ g++ dert.cpp
$ a.out
True

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

  1.     #include <iostream>
  2.     using namespace std; 
  3.     template<typename T>class clsTemplate
  4.     {
  5.         public:
  6.         T value;
  7.         clsTemplate(T i)
  8.         {
  9.             this->value = i;
  10.         }
  11.     void test()
  12.     {
  13.         cout << value << endl;
  14.     }
  15.     };
  16.     class clsChild : public clsTemplate<char>
  17.     {
  18.         public:
  19.         clsChild(): clsTemplate<char>( 0 )
  20.         {
  21.         }
  22.         clsChild(char c): clsTemplate<char>( c )
  23.         {    
  24.         }
  25.     void test2()
  26.     {
  27.         test();
  28.     }
  29.     };
  30.     int main()
  31.     {
  32.         clsTemplate <int> a( 42 );
  33.         clsChild b( 'A' );
  34.         a.test();
  35.         b.test();
  36.         return 0;
  37.     }

a) 42
b) A
c)

42
A
advertisement

d)

A
42
View Answer
Answer: c
Explanation: In this program, We are passing the values by using the template inheritance and printing it.
Output:

$ g++ dert.cpp
$ a.out
42
A
 
 

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class T>
  4.     class A
  5.     {
  6.         public:
  7.         A(int a): x(a) {}
  8.         protected:
  9.         int x;
  10.     };
  11.     template <class T>
  12.     class B: public A<char>
  13.     {
  14.         public:
  15.         B(): A<char>::A(100) 
  16.         {
  17.             cout << x * 2 << endl;
  18.         }
  19.     };
  20.     int main()
  21.     {
  22.         B<char> test;
  23.         return 0;
  24.     }

a) 100
b) 200
c) error
d) runtime error
View Answer

Answer: b
Explanation: In this program, We are passing the values and manipulating it by using the template inheritance.
Output:

$ g++ dert2.cpp
$ a.out
200

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class type>
  4.     class Test
  5.     {
  6.         public:
  7.         Test();
  8.         ~Test();
  9.         type Data(type);
  10.     };
  11.     template <class type>
  12.     type Test<type>::Data(type Var0)
  13.     {
  14.         return Var0;
  15.     }
  16.     template <class type>
  17.     Test<type>::Test()
  18.     {
  19.     }
  20.     template <class type>
  21.     Test<type>::~Test()
  22.     {
  23.     }
  24.     int main(void)
  25.     {
  26.         Test<char> Var3;
  27.         cout << Var3.Data('K') << endl;
  28.         return 0;
  29.     }

a) k
b) l
c) error
d) runtime error
View Answer

Answer: a
Explanation: In this program, We are passing the values and printing it by using template inheritance.
Output:

$ g++ dert3.cpp
$ a.out
k

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         public:
  6.         Base ( )
  7.         {
  8.             cout << "1" << endl;
  9.         }
  10.         ~Base ( )
  11.         {
  12.             cout << "2" << endl;
  13.         }
  14.     };
  15.     class Derived : public Base
  16.     {
  17.         public:
  18.         Derived  ( )
  19.         {
  20.             cout << "3" << endl;
  21.         }
  22.         ~Derived ( )
  23.         {
  24.             cout << "4" << endl;
  25.         }    
  26.     }; 
  27.     int main( )
  28.     {
  29.         Derived x;
  30.     }

a) 1234
b) 4321
c) 1423
d) 1342
View Answer

Answer: d
Explanation: In this program, We are printing the order of execution of constructor and destructor in the class.
Output:

$ g++ dert4.cpp
$ a.out
1342

9. How many kinds of entities are directly parameterized in c++?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: C++ allows us to parameterize directly three kinds of entities through templates: types, constants, and templates.

10. How many kinds of parameters are there in C++?
a) 1
b) 2
c) 3
d) 5
View Answer

Answer: c
Explanation: There are three kinds of parameters are there in C++. They are type, non-type, template.

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.

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.