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.
C++ interview questions on “Derivation and Templates” along with answers, explanations and/or solutions:
Q 1. Which is dependant on template parameter?
A. method
B. abstract class
C. base class
D. static class
Show Answer
Answer:-C. base classExplanation
Base class is dependant on template parameter.
Q 2. Which value is placed in the base class?
A. derived values
B. default type values
C. null value
D. both default type & derived values
Show Answer
Answer:-B. default type valuesExplanation
We can place the default type values in a base class and overriding some of them through derivation.
Q 3. How many bits of memory needed for internal representation of class?
A. 1
B. 2
C. 4
D. no memory needed
Show Answer
Answer:-D. no memory neededExplanation
classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time.Q 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
Show Answer
Answer:-A. TrueExplanation
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
Q 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 42
Show Answer
Answer:-C. 42 AExplanation
In this program, We are passing the values by using the template inheritance and printing it. Output: $ g++ dert.cpp $ a.out 42 A
Q 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
Show Answer
Answer:-B. 200Explanation
In this program, We are passing the values and manipulating it by using the template inheritance. Output: $ g++ dert2.cpp $ a.out 200
Q 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
Show Answer
Answer:-A. kExplanation
In this program, We are passing the values and printing it by using template inheritance. Output: $ g++ dert3.cpp $ a.out k
Q 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
Show Answer
Answer:-D. 1342Explanation
In this program, We are printing the order of execution of constructor and destructor in the class. Output: $ g++ dert4.cpp $ a.out 1342
Q 9. How many kinds of entities are directly parameterized in c++?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-C. 3Explanation
C++ allows us to parameterize directly three kinds of entities through templates: types, constants, and templates.
Q 10. How many kinds of parameters are there in C++?
A. 1
B. 2
C. 3
D. 4
Leave a Reply