C++ exam 34
What is the output of this program? #include <iostream> using namespace std; template <typename T, int count> void loopIt(T x) { T val[count]; for(int ii = 0; ii < count; ii++) { val[ii] = x++; cout << val[ii] << endl; } }; int main() { float xx = 2.1; loopIt<float, 3>(xx); } a) 2.1 b) 3.1 c) 4.1 d) 2.1 3.1 4.1
) 2.1 b) 3.1 c) 4.1 d) 2.1 3.1 4.1 Answer:d Explanation:In this program, We are using the non-type template parameter to increment the value in the function template. Output: $ g++ farg4.cpp $ a.out 2.1 3.1 4.1
What is meant by template parameter? a) It can be used to pass a type as argument b) It can be used to evaluate a type. c) It can of no return type d) None of the mentioned
Answer:a Explanation:A template parameter is a special kind of parameter that can be used to pass a type as argument.
What is meant by pure virtual function? a) Function which does not have definition of its own. b) Function which does have definition of its own. c) Function which does not have any return type. d) None of the mentioned
Answer:a Explanation:As the name itself implies, it have to depend on other class only.
Pick out the correct statement. a) you only need to write one function, and it will work with many different types. b) it will take a long time to execute c) duplicate code is increased d) none of the mentioned
Answer:a Explanation:Because of template type parameters, It will work with many types and saves a lot of time.
Which interface determines how your class will be used by other program? a) public b) private c) protected d) None of the mentioned
Answer:a Explanation:If we invoked the interface as public means, We can access the program from other programs also.
What is meant by template specialization? a) It will have certain data types to be fixed. b) It will make certain data types to be dynamic. c) Certain data types are invalid d) None of the mentioned
Answer:a Explanation:In the template specialization, it will make the template to be specific for some data types.
What is the output of this program? #include <iostream> #include <string> #include <cstring> using namespace std; template <class type> type MyMax(const type Var1, const type Var2) { cout << "no specialization"; return Var1 < Var2 ? Var2 : Var1; } template <> const char *MyMax(const char *Var1, const char *Var2) { return (strcmp(Var1, Var2)<0) ? Var2 : Var1; } int main() { string Str1 = "class", Str2 = "template"; const char *Var3 = "class"; const char *Var4 = "template"; const char *q = MyMax(Var3, Var4); cout << q << endl; return 0; } a) template b) class c) no specialization d) None of the mentioned
Answer:a Explanation:In this program, We are computing the result in the specalized block of the program. Output: $ g++ spec3.cpp $ a.out template
What is the output of this program? #include<iostream> using namespace std; class X { int m; public: X() : m(10) { } X(int mm): m(mm) { } int getm() { return m; } }; class Y : public X { int n; public: Y(int nn) : n(nn) {} int getn() { return n; } }; int main() { Y yobj( 100 ); cout << yobj.getm() << " " << yobj.getn() << endl; } a) 10 100 b) 100 10 c) 10 10 d) 100 100
Answer:a Explanation:In this program, We are passing the value and getting the result by derived class. Output: $ g++ der5.cpp $ a.out 10 100
What is the output of this program? #include <iostream> using namespace std; class A { public: A(int n ) { cout << n; } }; class B: public A { public: B(int n, double d) : A(n) { cout << d; } }; class C: public B { public: C(int n, double d, char ch) : B(n, d) { cout <<ch; } }; int main() { C c(5, 4.3, 'R'); return 0; } a) 54.3R b) R4.35 c) 4.3R5 d) None of the mentioned
Answer:a Explanation:In this program, We are passing the value and manipulating by using the derived class. Output: $ g++ der.cpp $ a.out 54.3R
What is the output of this program? #include <iostream> #include <string> using namespace std; template<typename T> void print_mydata(T output) { cout << output << endl; } int main() { double d = 5.5; string s("Hello World"); print_mydata( d ); print_mydata( s ); return 0; } a) 5.5 Hello World b) 5.5 c) Hello World d) none of the mentioned
Answer:a Explanation:In this program, We are passing the value to the template and printing it in the template. Output: 5.5 Hello World
What is the output of this program? #include <iostream> using namespace std; template<typename T> inline T square(T x) { T result; result = x * x; return result; }; int main() { int i, ii; float x, xx; double y, yy; i = 2; x = 2.2; y = 2.2; ii = square(i); cout << i << " " << ii << endl; yy = square(y); cout << y << " " << yy << endl; } a) 2 4 2.2 4.84 b) 2 4 c) error d) runtime error
Answer:a Explanation:In this program, We are passing the values and calculating the square of the value by using the function template. Output: 2.2 4.84
What is the output of this program? #include <iostream> using namespace std; template<typename type> class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test<int> Var1; Test<float> Var2; cout << Var1.Funct1(200) << endl; cout << Var2.Funct2(3.123) << endl; return 0; }
Answer:a Explanation:In this program, We are passing the values and getting it back from template. And we are using the constructor and destructor for the function template. Output: $ g++ ftemp1.cpp $ a.out 200 3.123
What is the output of this program? #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
Answer:a Explanation:In this program, We are passing the values and inheriting it to the other class and printing the result. True
What is the output of this program? #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
Answer:a Explanation:In this program, We are passing the values and printing it by using template inheritance. Output: k
What is the output of this program? #include <iostream> using namespace std; class Base { public: int m; Base(int n=0) : m(n) { cout << "Base" << endl; } }; class Derived: public Base { public: double d; Derived(double de = 0.0) : d(de) { cout << "Derived" << endl; } }; int main() { cout << "Instantiating Base" << endl; Base cBase; cout << "Instantiating Derived" << endl; Derived cDerived; return 0; } a) Instantiating Base Base Instantiating Derived Base Derived b) Instantiating Base Instantiating Derived Base Derived c) Instantiating Base Base Instantiating Derived Base d) None of the mentioned
Answer:a Explanation:In this program, We are printing the execution order of the program. Output: Instantiating Base Base Instantiating Derived Base Derived
What is the output of this program? #include <iostream> using namespace std; template <class T> inline T square(T x) { T result; result = x * x; return result; }; template <> string square<string>(string ss) { return (ss+ss); }; int main() { int i = 4, ii; string ww("A"); ii = square<int>(i); cout << i << ii; cout << square<string>(ww) << endl; } a) 416AA b) 164AA c) AA416 d) none of the mentioned
Answer:a Explanation:In this program, We are using two template to calculate the square and to find the addition. Output: 416AA
What is other name of full specialization? a) explicit specialization b) implicit specialization c) function overloading template d) None of the mentioned
Answer:a Explanation:None
Which is dependant on template parameter? a) base class b) abstract class c) method d) None of the mentioned
Answer:a Explanation:None
Pick out the correct option. a) We cannot make an instance of an abstract base class b) We can make an instance of an abstract base class c) Both a & b d) None of the mentioned
Answer:a Explanation:None.
Pick out the correct statement about override. a) Overriding refers to a derived class function that has the same name and signature as a base class virtual function. b) Overriding has different names. c) both a & b d) None of the mentioned
Answer:a Explanation:None.
What is a function template? a) creating a function without having to specify the exact type. b) creating a function with having a exact type. c) both a & b d) none of the mentioned
Answer:a Explanation:None.
What is a template? a) A template is a formula for creating a generic class b) A template is used to manipulate the class c) A template is used for creating the attributes d) none of the mentioned
Answer:a Explanation:None.
What is the validity of template parameters? a) inside that block only b) inside the class c) whole program d) any of the mentioned
Answer:a Explanation:None.
What may be the name of the parameter that the template should take? a) same as template b) same as class c) same as function d) none of the mentioned
Answer:a Explanation:None.
Where does the abstract class is used? a) base class only b) derived class c) both a & b d) None of the mentioned
Answer:a Explanation:None.
Which class is used to design the base class? a) abstract class b) derived class c) base class d) None of the mentioned
Answer:a Explanation:None.
What is the output of this program? #include <iostream> using namespace std; template <class T> inline T square(T x) { T result; result = x * x; return result; }; template <> string square<string>(string ss) { return (ss+ss); }; int main() { int i = 2, ii; string ww("A"); ii = square<int>(i); cout << i << ": " << ii; cout << square<string>(ww) << ":" << endl; } a) 2:4AA b) 2:4 c) AA d) 2:4A
Answer:a Explanation:Template specialization is used when a different and specific implementation is to be used for a specific data type. In this program, We are using integer and character. Output: 2:4AA
Which are done by compiler for templates? a) type-safe b) portability c) code elimination d) all of the mentioned
Answer:a Explanation:The compiler can determine at compile time whether the type associated with a template definition can perform all of the functions required by that template definition.
Which parameter is legal for non-type template? a) pointer to member b) object c) class d) none of the mentioned
Answer:a Explanation:The following are legal for non-type template parameters:integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.
What is the output of this program? #include <iostream> using namespace std; class BaseClass { public: virtual void myFunction() { cout << "1"; } }; class DerivedClass1 : public BaseClass { public: void myFunction() { cout << "2"; } }; class DerivedClass2 : public DerivedClass1 { public: void myFunction() { cout << "3"; } }; int main() { BaseClass *p; BaseClass ob; DerivedClass1 derivedObject1; DerivedClass2 derivedObject2; p = &ob; p -> myFunction(); p = &derivedObject1; p -> myFunction(); p = &derivedObject2; p -> myFunction(); return 0; } a) 123 b) 12 c) 213 d) 321
Answer:a Explanation:We are passing the objects and executing them in a certain order and we are printing the program flow. Output: $ g++ des3.cpp $ a.out 123
1. Where is the derived class is derived from? a) derived b) base c) both a & b d) None of the mentioned
Answer:b Explanation:Because derived inherits functions and variables from base.
Which constructor will initialize the base class data member? a) derived class b) base class c) class d) None of the mentioned
Answer:b Explanation:Because it is having the proper data set to initialize, Otherwise it will throw a error.
How many ways of reusing are there in class hierarchy? a) 1 b) 2 c) 3 d) 4
Answer:b Explanation:Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.
Which is also called as abstract class? a) virtual function b) pure virtual function c) derived class d) None of the mentioned
Answer:b Explanation:Classes that contain at least one pure virtual function are called as abstract base classes.
Pick out the correct statement. a) A derived class's constructor cannot explicitly invokes its base class's constructor. b) A derived class's destructor cannot invoke its base class's destructor. c) A derived class's destructor can invoke its base class's destructor. d) None of the mentioned
Answer:b Explanation:Destructors are automatically invoked when a object goes out of scope or when a dynamically allocated object is deleted. Inheritance does not change this behavior. This is the reason a derived destructor cannot invoke its base class destructor.
Which is used to describe the function using placeholder types? a) template parameters b) template type parameters c) template type d) none of the mentioned
Answer:b Explanation:During runtime, We can choose the appropriate type for the function and it is called as template type parameters.
Pick out the correct statement about string template. a) It is used to replace a string. b) It is used to replace a string with another string at runtime. c) It is used to delete a string. d) none of the mentioned
Answer:b Explanation:Every string template is used to replace the string with another string at runtime.
Which is used to create a pure virtual function ? a) $ b) =0 c) & d) !
Answer:b Explanation:For making a method as pure virtual function, We have to append '=0' to the class or method.
Pick out the correct statement about multiple inheritance. a) Deriving a class from one direct base class b) Deriving a class from more than one direct base class c) Deriving a class from more than one direct derived class d) None of the mentioned
Answer:b Explanation:In multiple inheritance, We are able to derive a class from more than one base class.
What is the output of this program? #include <iostream> using namespace std; template <typename T, typename U> void squareAndPrint(T x, U y) { cout << x << x * x << endl; cout << y << " " << y * y << endl; }; int main() { int ii = 2; float jj = 2.1; squareAndPrint<int, float>(ii, jj); } a) 23 2.1 4.41 b) 24 2.1 4.41 c) 24 2.1 3.41 d) none of the mentioned
Answer:b Explanation:In this multiple templated types, We are passing two values of different types and producing the result. Output: $ g++ tem1.cpp $ a.out 24 2.1 4.41
What is the output of this program? #include <iostream> using namespace std; class BaseClass { protected: int i; public: BaseClass(int x) { i = x; } ~BaseClass() { } }; class DerivedClass: public BaseClass { int j; public: DerivedClass(int x, int y): BaseClass(y) { j = x; } ~DerivedClass() { } void show() { cout << i << " " << j << endl; } }; int main() { DerivedClass ob(3, 4); ob.show(); return 0; } a) 3 4 b) 4 3 c) 4 d) 3
Answer:b Explanation:In this program, We are passing the values and assigning it to i and j and we are printing it. Output: 4 3
What is the output of this program? #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
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
What is the output of this program? #include <iostream> using namespace std; template<typename type> type Max(type Var1, type Var2) { return Var1 > Var2 ? Var1:Var2; } int main() { int p; p = Max(100, 200); cout << p << endl; return 0; } a) 100 b) 200 c) 300 d) 100200
Answer:b Explanation:In this program, We are returning the maximum value by using function template. Output: 200
What is the output of this program? #include <iostream> using namespace std; template<typename type> class TestVirt { public: virtual type TestFunct(type Var1) { return Var1 * 2; } }; int main() { TestVirt<int> Var1; cout << Var1.TestFunct(100) << endl; return 0; } a) 100 b) 200 c) 50 d) none of the mentioned
Answer:b Explanation:In this program, We are using class to pass the value and then we are manipulating it. Output: 200
What is the output of this program? #include <iostream> using namespace std; template <class T> T max (T& a, T& b) { return (a>b?a:b); } int main () { int i = 5, j = 6, k; long l = 10, m = 5, n; k = max(i, j); n = max(l, m); cout << k << endl; cout << n << endl; return 0; } a) 6 b) 6 10 c) 5 10 d) 6 5
Answer:b Explanation:In this program, We are using the ternary operator on the template function. Output: 6 10
What is the output of this program? #include <iostream> using namespace std; class Parent { public: Parent (void) { cout << "Parent()\n"; } Parent (int i) { cout << "Parent("<< i << ")\n"; }; Parent (void) { cout << "~Parent()\n"; }; }; class Child1 : public Parent { }; class Child2 : public Parent { public: Child2 (void) { cout << "Child2()\n"; } Child2 (int i) : Parent (i) { cout << "Child2(" << i << ")\n"; } ~Child2 (void) { cout << "~Child2()\n"; } }; int main (void) { Child1 a; Child2 b; Child2 c(42); return 0; } a) Parent() Parent() Child2() Parent(42) Child2(42) ~Child2() ~Parent() ~Child2() ~Parent() ~Parent() b) error c) runtime error d) None of the mentioned
Answer:b Explanation:In this program, We got an error in overloading because we didn't invoke the destructor of parent.
What does inheriatance allows you to do? a) create a class b) create a hierarchy of classes c) access methods d) None of the mentioned
Answer:b Explanation:None.
Which operator is used to declare the destructor? a) # b) ~ c) @ d) $
Answer:b Explanation:None.
How many types of templates are there in c++? a) 1 b) 2 c) 3 d) 4
Answer:b Explanation:There are two types of templates. They are function template and class template.
How many types of specialization are there in c++? a) 1 b) 2 c) 3 d) 4
Answer:b Explanation:There are two types specialization. They are full specialization and partial specialization.
Which value is placed in the base class? a) derived values b) default type values c) both a & b d) None of the mentioned
Answer:b Explanation:We can place the default type values in a base class and overriding some of them through derivation.
Which is called on allocating the memory for array of objects? a) destructor b) constructor c) method d) None of the mentioned
Answer:b Explanation:When you allocate memory for an array of objects, the default constructor must be called to construct each object. If no default constructor exists, you're stuck needing a list of pointers to objects.
How many kinds of entities are directly parameterized in c++? a) 1 b) 2 c) 3 d) 4
Answer:c Explanation:C++ allows us to parameterize directly three kinds of entities through templates: types, constants, and templates.
What is the output of this program? #include <iostream> using namespace std; class sample { public: virtual void example() = 0; }; class Ex1:public sample { public: void example() { cout << "ubuntu"; } }; class Ex2:public sample { public: void example() { cout << " is awesome"; } }; int main() { sample* arra[2]; Ex1 e1; Ex2 e2; arra[0]=&e1; arra[1]=&e2; arra[0]->example(); arra[1]->example(); } a) ubuntu b) is awesome c) ubuntu is awesome d) None of the mentioned
Answer:c Explanation:In this program, We are combining the two statements from two classes and printing it by using abstract class. Output: $ g++ abs3.cpp $ a.out ubuntu is awesome
What is the output of this program? #include <iostream> using namespace std; class MyInterface { public: virtual void Display() = 0; }; class Class1 : public MyInterface { public: void Display() { int a = 5; cout << a; } }; class Class2 : public MyInterface { public: void Display() { cout <<" 5" << endl; } }; int main() { Class1 obj1; obj1.Display(); Class2 obj2; obj2.Display(); return 0; } a) 5 b) 10 c) 5 5 d) None of the mentioned
Answer:c Explanation:In this program, We are displaying the data from the two classes by using abstract class. Output: 5 5
What is the output of this program? #include <iostream> using namespace std; class BaseClass { int i; public: void setInt(int n); int getInt(); }; class DerivedClass : public BaseClass { int j; public: void setJ(int n); int mul(); }; void BaseClass::setInt(int n) { i = n; } int BaseClass::getInt() { return i; } void DerivedClass::setJ(int n) { j = n; } int DerivedClass::mul() { return j * getInt(); } int main() { DerivedClass ob; ob.setInt(10); ob.setJ(4); cout << ob.mul(); return 0; } a) 10 b) 4 c) 40 d) None of the mentioned
Answer:c Explanation:In this program, We are multiplying the value 10 and 4 by using inheritance. Output: $ g++ des.cpp $ a.out 40
What is the output of this program? #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
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
What is the output of this program? #include <iostream> using namespace std; class BaseClass { int x; public: void setx(int n) { x = n; } void showx() { cout << x ; } }; class DerivedClass : private BaseClass { int y; public: void setxy(int n, int m) { setx(n); y = m; } void showxy() { showx(); cout << y << '\n'; } }; int main() { DerivedClass ob; ob.setxy(10, 20); ob.showxy(); return 0; } a) 10 b) 20 c) 1020 d) None of the mentioned
Answer:c Explanation:In this program, We are passing the values from the main class and printing it on the inherited classes. Output: $ g++ des2.cpp $ a.out 1020
What is the output of this program? #include <iostream> using namespace std; template <class T, int N> class mysequence { T memblock [N]; public: void setmember (int x, T value); T getmember (int x); }; template <class T, int N> void mysequence<T,N> :: setmember (int x, T value) { memblock[x] = value; } template <class T, int N> T mysequence<T,N> :: getmember (int x) { return memblock[x]; } int main () { mysequence <int, 5> myints; mysequence <double, 5> myfloats; myints.setmember (0, 100); myfloats.setmember (3, 3.1416); cout << myints.getmember(0) << '\n'; cout << myfloats.getmember(3) << '\n'; return 0; } a) 100 b) 3.1416 c) 100 3.1416 d) none of the mentioned
Answer:c Explanation:In this program, We are printing the integer in the first function and float in the second function. Output: 100 3.1416
Why we use :: template-template parameter? a) binding b) rebinding c) both a & b d) none of these
Answer:c Explanation:It is used to adapt a policy into binary ones.
What can be passed by non-type template parameters during compile time? a) int b) float c) constant expression d) none of the mentioned
Answer:c Explanation:Non-type template parameters provide the ability to pass a constant expression at compile time. The constant expression may also be an address of a function, object or static class member.
Which is similar to template specialization? a) template b) function overloading c) function template overloading d) None of the mentioned
Answer:c Explanation:None
From where does the template class derived? a) regular non-templated C++ class b) templated class c) a or b d) none of the mentioned
Answer:c Explanation:None.
How to declare a template? a) tem b) temp c) template<> d) none of the mentioned
Answer:c Explanation:None.
What is the syntax of inheritance of class? a) class name b) class name : access specifer c) class name : access specifer class name d) None of the mentioned
Answer:c Explanation:None.
Which keyword can be used in template? a) class b) typename c) both a & b d) function
Answer:c Explanation:None.
Which of the following can derived class inherit? a) members b) functions c) both a & b d) None of the mentioned
Answer:c Explanation:None.
How many kinds of parameters are there in C++? a) 1 b) 2 c) 3 d) None of the mentioned
Answer:c Explanation:There are three kinds of parameters are there in C++. They are type, non-type, template.
How many types of class are there in c++? a) 1 b) 2 c) 3 d) 4
Answer:c Explanation:There are three types of classes. They are abstract base classes, concrete derived classes, standalone classes.
What is the output of this program? #include <iostream> using namespace std; class p { protected: int width, height; public: void set_values (int a, int b) { width = a; height = b; } virtual int area (void) = 0; }; class r: public p { public: int area (void) { return (width * height); } }; class t: public p { public: int area (void) { return (width * height / 2); } }; int main () { r rect; t trgl; p * ppoly1 = ▭ p * ppoly2 = &trgl; ppoly1->set_values (4, 5); ppoly2->set_values (4, 5); cout << ppoly1 -> area() ; cout << ppoly2 -> area(); return 0; } a) 1020 b) 20 c) 10 d) 2010
Answer:d Explanation:In this program, We are calculating the area of rectangle and triangle by using abstract class. Output: 2010
What is the output of this program? #include <iostream> using namespace std; template <class type> class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test<int> Var1; Test<double> Var2; cout << Var1.Funct1(200); cout << Var2.Funct2(3.123); return 0; } a) 100 b) 200 c) 3.123 d) 2003.123
Answer:d Explanation:In this program, We are passing the value and returning it from template. Output: $ g++ farg3.cpp $ a.out 2003.123
What is the output of this program? #include <iostream> using namespace std; template <class T> class XYZ { public: void putPri(); static T ipub; private: static T ipri; }; template <class T> void XYZ<T>::putPri() { cout << ipri++ << endl; } template <class T> T XYZ<T>::ipub = 1; template <class T> T XYZ<T>::ipri = 1.2; int main() { XYZ<int> a; XYZ<float> b; a.putPri(); cout << a.ipub << endl; b.putPri(); } a) 1 b) 1.2 c) 1 1.2 d) 1 1 1.2
Answer:d Explanation:In this program, We are passing the value of specified type and printing it by specialization. Output: $ g++ spec2.cpp $ a.out 1 1 1.2
What is the output of this program? #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
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
What is the output of this program? #include <iostream> using namespace std; template<typename T> void loopIt(T x) { int count = 3; T val[count]; for (int ii=0; ii < count; ii++) { val[ii] = x++; cout << val[ii] << endl; } }; int main() { float xx = 2.1; loopIt(xx); } a) 2.1 b) 3.1 c) 3.2 d) 2.1 3.1 4.1
Answer:d Explanation:In this program, We are using the for loop to increment the value by 1 in the function template. Output: $ g++ ftemp5.cpp $ a.out 2.1 3.1 4.1
Which of the things does not require instantiation? a) functions b) non virtual member function c) member class d) all of the mentioned
Answer:d Explanation:The compiler does not generate definitions for functions, non virtual member functions, class or member class because it does not require instantiation.
How many parameters are legal for non-type template? a) 1 b) 2 c) 3 d) 4
Answer:d Explanation:The following are legal for non-type template parameters: integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.
How many bits of memory needed for internal representation of class? a) 1 b) 2 c) 4 d) no memory needed
Answer:d Explanation:classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time.
What is the output of this program? #include <iostream> using namespace std; template<class T = float, int i = 5> class A { public: A(); int value; }; template<> class A<> { public: A(); }; template<> class A<double, 10> { public: A(); }; template<class T, int i> A<T, i>::A() : value(i) { cout << value; } A<>::A() { cout << "default"; } A<double, 10>::A() { cout << "10" << endl; } int main() { A<int, 6> x; A<> y; A<double, 10> z; } ) 6 b) 10 c) 6default10 d) None of the mentioned
Explanation:In this program, We are defining three templates and specializing it and passing the values to it and printing it. Output: $ g++ spec5.cpp $ a.out 6default10
What is the output of this program? #include <iostream> using namespace std; template <typename T = float, int count = 3> T multIt(T x) { for(int ii = 0; ii < count; ii++) { x = x * x; } return x; }; int main() { float xx = 2.1; cout << xx << ": " << multIt<>(xx) << endl; } a) 2.1 b) 378.228 c) 2.1: 378.228 d) None of the mentioned
a) 2.1 b) 378.228 c) 2.1: 378.228 d) None of the mentioned Answer:c Explanation:In this program, We specifed the type in the template function. We need to compile this program by adding -std=c++0x. Output: $ g++ -std=c++0x spec1.cpp $ a.out 2.1: 378.228