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
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
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
Explanation: classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class class0
{
public:
virtual ~class0(){}
protected:
char p;
public:
char getChar();
};
class class1 : public class0
{
public:
void printChar();
};
void class1::printChar()
{
cout << "True" << endl;
}
int main()
{
class1 c;
c.printChar();
return 1;
}
a) True
b) error
c) no output
d) runtime error
View Answer
Explanation: In this program, We are passing the values and inheriting it to the other class and printing the result.
$ g++ dert.cpp $ a.out True
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template<typename T>class clsTemplate
{
public:
T value;
clsTemplate(T i)
{
this->value = i;
}
void test()
{
cout << value << endl;
}
};
class clsChild : public clsTemplate<char>
{
public:
clsChild(): clsTemplate<char>( 0 )
{
}
clsChild(char c): clsTemplate<char>( c )
{
}
void test2()
{
test();
}
};
int main()
{
clsTemplate <int> a( 42 );
clsChild b( 'A' );
a.test();
b.test();
return 0;
}
a) 42
b) A
c)
42 A
d)
A 42View Answer
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?
#include <iostream>
using namespace std;
template <class T>
class A
{
public:
A(int a): x(a) {}
protected:
int x;
};
template <class T>
class B: public A<char>
{
public:
B(): A<char>::A(100)
{
cout << x * 2 << endl;
}
};
int main()
{
B<char> test;
return 0;
}
a) 100
b) 200
c) error
d) runtime error
View Answer
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?
#include <iostream>
using namespace std;
template <class type>
class Test
{
public:
Test();
~Test();
type Data(type);
};
template <class type>
type Test<type>::Data(type Var0)
{
return Var0;
}
template <class type>
Test<type>::Test()
{
}
template <class type>
Test<type>::~Test()
{
}
int main(void)
{
Test<char> Var3;
cout << Var3.Data('K') << endl;
return 0;
}
a) k
b) l
c) error
d) runtime error
View Answer
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?
#include <iostream>
using namespace std;
class Base
{
public:
Base ( )
{
cout << "1" << endl;
}
~Base ( )
{
cout << "2" << endl;
}
};
class Derived : public Base
{
public:
Derived ( )
{
cout << "3" << endl;
}
~Derived ( )
{
cout << "4" << endl;
}
};
int main( )
{
Derived x;
}
a) 1234
b) 4321
c) 1423
d) 1342
View Answer
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
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
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.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Apply for Computer Science Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Practice Programming MCQs
- Check Programming Books