SE(Comp)

MCQ on Templates and Exception Handling

Topic- Templates 1. 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 2. 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. 3. 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. 4. What is the output of this program? #include using namespace std; template 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: $ g++ ftemp.cpp Prof. A. V. Yenkikar

Page 1

SE(Comp)

MCQ on Templates and Exception Handling

$ a.out 200 5. What is the output of this program? #include using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; cout << Var1.Funct1(200) << endl; cout << Var2.Funct2(3.123) << endl; return 0; } a) 200 3.123 b) 3.123 200 c) 200 d) 3.123 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 6. What is the output of this program? #include using namespace std; template class TestVirt Prof. A. V. Yenkikar

Page 2

SE(Comp)

MCQ on Templates and Exception Handling

{ public: virtual type TestFunct(type Var1) { return Var1 * 2; } }; int main() { TestVirt 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: $ g++ ftemp3.cpp $ a.out 200 7. What is the output of this program? #include using namespace std; template 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 Prof. A. V. Yenkikar

Page 3

SE(Comp)

MCQ on Templates and Exception Handling

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: $ g++ ftemp4.cpp $ a.out 24 2.2 4.84 8. What is the output of this program? #include using namespace std; template 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(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 9. What can be passed by non-type template parameters during compile time? a) int b) float c) constant expression d) none of the mentioned

Prof. A. V. Yenkikar

Page 4

SE(Comp)

MCQ on Templates and Exception Handling

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. 10. 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 11. 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

12. Which keyword can be used in template? a) class b) typename c) both a & b d) function

13. What is the validity of template parameters? a) inside that block only b) inside the class c) whole program d) any of the mentioned

14.What is the output of this program? #include using namespace std; template class mysequence { T memblock [N]; public: void setmember (int x, T value); T getmember (int x); }; template void mysequence :: setmember (int x, T value) { memblock[x] = value; } template T mysequence :: getmember (int x) Prof. A. V. Yenkikar

Page 5

SE(Comp)

MCQ on Templates and Exception Handling

{ return memblock[x]; } int main () { mysequence myints; mysequence 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: $ g++ farg.cpp $ a.out 100 3.1416 15. What is the output of this program? #include using namespace std; template 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 Prof. A. V. Yenkikar

Page 6

SE(Comp)

MCQ on Templates and Exception Handling

10 d) 6 5 Answer:b Explanation:In this program, We are using the ternary operator on the template function. Output: $ g++ farg.cpp $ a.out 6 10 16. What is the output of this program? #include using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; cout << Var1.Funct1(200); cout << Var2.Funct2(3.123); return 0; } a) 100 b) 200 c) 3.123 d) 200 3.123 Answer:d Explanation:In this program, We are passing the value and returning it from template. Output: $ g++ farg3.cpp $ a.out Prof. A. V. Yenkikar

Page 7

SE(Comp)

MCQ on Templates and Exception Handling

2003.123 17. What is the output of this program? #include using namespace std; template 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(xx); } a) 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 18. 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. 19. Which parameter is legal for non-type template? a) pointer to member b) object c) class d) none of the mentioned

Prof. A. V. Yenkikar

Page 8

SE(Comp)

MCQ on Templates and Exception Handling

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. 20. 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. 21. 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. 22. Which keyword can be used in template? a) class b) typename c) both a & b d) function Answer:c

23. 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

24.What is the output of this program? #include using namespace std; template class mysequence { T memblock [N]; public: void setmember (int x, T value); T getmember (int x); }; template void mysequence :: setmember (int x, T value) Prof. A. V. Yenkikar

Page 9

SE(Comp)

MCQ on Templates and Exception Handling

{ memblock[x] = value; } template T mysequence :: getmember (int x) { return memblock[x]; } int main () { mysequence myints; mysequence 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: $ g++ farg.cpp $ a.out 100 3.1416 25. What is the output of this program? #include using namespace std; template 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 Prof. A. V. Yenkikar

Page 10

SE(Comp)

MCQ on Templates and Exception Handling

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: $ g++ farg.cpp $ a.out 6 10 26. What is the output of this program? #include using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; cout << Var1.Funct1(200); cout << Var2.Funct2(3.123); return 0; } a) 100 b) 200 c) 3.123 d) 200 3.123 Answer:d Explanation:In this program, We are passing the value and returning it from template. Output: $ g++ farg3.cpp Prof. A. V. Yenkikar

Page 11

SE(Comp)

MCQ on Templates and Exception Handling

$ a.out 2003.123 27. What is the output of this program? #include using namespace std; template 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(xx); } a) 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 28. 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. 29. Which parameter is legal for non-type template? a) pointer to member b) object c) class d) none of the mentioned Answer:d Explanation:The compiler does not generate definitions for functions, non virtual member functions, class Prof. A. V. Yenkikar

Page 12

SE(Comp)

MCQ on Templates and Exception Handling

or member class because it does not require instantiation. 30. 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 31. 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. 32. 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 33. What is the output of this program? #include using namespace std; template 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: $ g++ ftemp.cpp $ a.out 200 34. What is the output of this program? Prof. A. V. Yenkikar

Page 13

SE(Comp)

MCQ on Templates and Exception Handling

#include using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; cout << Var1.Funct1(200) << endl; cout << Var2.Funct2(3.123) << endl; return 0; } a) 200 3.123 b) 3.123 200 c) 200 d) 3.123 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 35. What is the output of this program? #include using namespace std; template class TestVirt { public: virtual type TestFunct(type Var1) { Prof. A. V. Yenkikar

Page 14

SE(Comp)

MCQ on Templates and Exception Handling

return Var1 * 2; } }; int main() { TestVirt 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: $ g++ ftemp3.cpp $ a.out 200 36. What is the output of this program? #include using namespace std; template 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

37. What is the output of this program? #include using namespace std; Prof. A. V. Yenkikar

Page 15

SE(Comp)

MCQ on Templates and Exception Handling

template 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(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 38. 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. 39. 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 40. Which is dependant on template parameter? a) base class b) abstract class c) method d) None of the mentioned Answer:a Prof. A. V. Yenkikar

Page 16

SE(Comp)

MCQ on Templates and Exception Handling

41. 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. 42. How many bits of memory needed for internal representation of class? a) 1 b) 2 c) 4 d) no memory neededAnswer:d Explanation:classes that contain only type members, nonvirtual function members, and static data members do not require memory at run time. 43. What is the output of this program? #include 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 errorAnswer:a 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 Prof. A. V. Yenkikar

Page 17

SE(Comp)

MCQ on Templates and Exception Handling

True 44. What is the output of this program? #include using namespace std; templateclass clsTemplate { public: T value; clsTemplate(T i) { this->value = i; } void test() { cout << value << endl; } }; class clsChild : public clsTemplate { public: clsChild(): clsTemplate( 0 ) { } clsChild(char c): clsTemplate( c ) { } void test2() { test(); } }; int main() { clsTemplate 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

Prof. A. V. Yenkikar

Page 18

SE(Comp)

MCQ on Templates and Exception Handling

45. What is the output of this program? #include using namespace std; template class A { public: A(int a): x(a) {} protected: int x; }; template class B: public A { public: B(): A::A(100) { cout << x * 2 << endl; } }; int main() { B test; return 0; } a) 100 b) 200 c) error d) runtime error 46. What is the output of this program? #include using namespace std; template class Test { public: Test(); ~Test(); type Data(type); }; template type Test::Data(type Var0) { return Var0; } template Test::Test() { } template Test::~Test() { Prof. A. V. Yenkikar

Page 19

SE(Comp)

MCQ on Templates and Exception Handling

} int main(void) { Test 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: $ g++ dert3.cpp $ a.out k 47. What is the output of this program? #include 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 Prof. A. V. Yenkikar

Page 20

SE(Comp)

MCQ on Templates and Exception Handling

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 48. 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. 49. 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. 50.Which of the following is illegal: a)template func(T x) {} template func(T* x) {} b)template class myObject {}; c)template class myObj { template memFunc() {} }; 51.What problems can a templated member function cause? a) They're just not legal b) They're hard to use c) They allow violations of encapsulation 52.When must template functions have explicit template parameters? a) Always b) When the template types cannot be inferred c) Never, the template types can always be inferred 53.Are templates conceptually related to polymorphism? a) Nope b) Only when the template types are objects c) Yes, but compile-time polymorphism 54.In general, is it possible to completely hide the source code of a library written using templates? a) Yes, but using export feature b) No, pretty much never c) Yes, all the time

Prof. A. V. Yenkikar

Page 21

SE(Comp)

MCQ on Templates and Exception Handling

55.When are templates usually instantiated? a) At runtime b) At compile time c) At link time 56.Which of the following describes a potentially surprising result of using templates? a) Slower programs b) Poor variable naming in the debugger c) Increased executable size in comparison to the code base 57.Which of the following is an invalid template declaration: a) template int func() {return x;} b) template double func() {return x;} c) template void func(x t) {} 58.What is the result of trying to run this program: #include template struct X { enum val {v = T * X::v }; }; template <> struct X<0> { enum val {v = 1 }; }; int main() { std::cout<::v; } a) Compilation error b) Link error c) 120 59.Given the below code, what happens when a method invokes callFunc on an object of type obj? template func(X val) {} template <> func(double val) {} class obj { public: callFunc() { func(4.5); } private: func(int val) {} }; a) func(int val) b) template func(X val) c) template <> func(double val) Q61) What is the o/p of this programm #include using namespace std; template class mysequence { T memblock [N]; Prof. A. V. Yenkikar

Page 22

SE(Comp)

MCQ on Templates and Exception Handling

public: void setmember (int x, T value); T getmember (int x); }; template void mysequence :: setmember (int x, T value) { memblock[x] = value; } template T mysequence :: getmember (int x) { return memblock[x]; } int main () { mysequence myints; mysequence myfloats; myints.setmember (0, 100); myfloats.setmember (3, 3.1416); cout << myints.getmember(0) << '\n'; cout << myfloats.getmember(3) << '\n'; return 0; } a)101 b)102 C)103 d) none ans.d Q62) what is the o/p of this program #include using namespace std; template 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(xx); } a) 2.1 b) 3.1 c) 4.1 d)none ans.D Prof. A. V. Yenkikar

Page 23

SE(Comp)

MCQ on Templates and Exception Handling

Q63). 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 Q64). Which keyword can be used in template? a) class b) typename c) both a & b d) function Answer:c Q65). 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 Q66) Which parameter is legal for non-type template? a) pointer to member b) object c) class d) none of the mentioned Answer:a

Q67). 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 Q68) what is the o/p of the program #include using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; Prof. A. V. Yenkikar

Page 24

SE(Comp)

MCQ on Templates and Exception Handling

} type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; cout << Var1.Funct1(200); cout << Var2.Funct2(3.123); return 0; } a) 100 b) 200 c) 3.123 d) none ans. D Q69) What is the output of this programm #include using namespace std; template 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) 5 10 c) 6 5 d) none ans.D Q70) Which parameter is legal for non-type template? a) pointer to member b) object c) class d) none of the mentioned Answer:a

Prof. A. V. Yenkikar

Page 25

SE(Comp)

MCQ on Templates and Exception Handling

Q71). 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 Q72) A class template in C++ has the following structure template class TemplatedClass { ... }; What is the meaning of T in the above declaration? a) It is a placeholder for a pointer value b) It is a placeholder for a type name c) It is a string variable d) It must be an integer constant ans.B Q73 )Why we use :: template-template parameter? a) binding b) rebinding c) both a & b d) none of these ans C Q74) The members of a class a) by default are public b) by default are private c) are made private by declaring as private d) none ans.B Q75) what is the o/p of this program #include using namespace std; template void fun(const T& x) { static int i = 10; cout << ++i; return; } int main() { fun(1); // prints 11 cout << endl; fun(2); // prints 12 cout << endl; fun(1.1); // prints 11 Prof. A. V. Yenkikar

Page 26

SE(Comp)

MCQ on Templates and Exception Handling

cout << endl; getchar(); return 0; } a) 10,12,13 b)10,11,10 C) 11,10,12 d) 11,12,11 ans.D Q76) What is the o/p of this programm #include using namespace std; template class Test { private: T val; public: static int count; Test() { count++; } // some other stuff in class }; template int Test::count = 0; int main() { Test a; // value of count for Test is 1 now Test b; // value of count for Test is 2 now Test c; // value of count for Test is 1 now cout << Test::count << endl; // prints 2 cout << Test::count << endl; //prints 1 getchar(); return 0; } a) 1,2 b) 11,0 c) 2,1 d) any one of the above ans.C Q77.) What is the output of this program #include using namespace std; template class mysequence { T memblock [N]; public: void setmember (int x, T value); T getmember (int x); Prof. A. V. Yenkikar

Page 27

SE(Comp)

MCQ on Templates and Exception Handling

}; template void mysequence :: setmember (int x, T value) { memblock[x] = value; } template T mysequence :: getmember (int x) { return memblock[x]; } int main () { mysequence myints mysequence 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 Q78). What is the output of this program? #include using namespace std; template 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 Prof. A. V. Yenkikar

Page 28

SE(Comp)

MCQ on Templates and Exception Handling

d) 6 5 Answer:b Explanation:In this program, We are using the ternary operator on the template function. Q79).What is the output of the program #include using namespace std; template 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(xx); } a) 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. Q80). What is the output of this program? #include using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { Prof. A. V. Yenkikar

Page 29

SE(Comp)

MCQ on Templates and Exception Handling

return Var2; } }; int main() { Test Var1; Test Var2; cout << Var1.Funct1(200); cout << Var2.Funct2(3.123); return 0; } a) 100 b) 200 c) 3.123 d) 200 3.123 Answer:d Explanation:In this program, We are passing the value and returning it from template. 81.What is the output of this program? 1. #include 2. usingnamespacestd; 3. template 4. classmysequence 5. { 6. T memblock[N]; 7. public: 8. voidsetmember(int x, T value); 9. T getmember(int x); 10. }; 11. template 12. voidmysequence::setmember(int x, T value) 13. { 14. memblock[x]= value; 15. } 16. template 17. T mysequence::getmember(int x) 18. { 19. returnmemblock[x]; 20. } 21. int main () 22. { 23. mysequencemyints; 24. mysequencemyfloats; 25. myints.setmember(0, 100); 26. myfloats.setmember(3, 3.1416); 27. cout<
Page 30

SE(Comp)

MCQ on Templates and Exception Handling

3.1416 d) none of the mentioned

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.

82. What is the output of this program? #include usingnamespacestd; template 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 < usingnamespacestd; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() Prof. A. V. Yenkikar

Page 31

SE(Comp) 23. 24. 25. 26. 27. 28. 29.

MCQ on Templates and Exception Handling

{ Test Var1; Test Var2; cout<< Var1.Funct1(200); cout<< Var2.Funct2(3.123); return0; } a) 100 b) 200 c) 3.123 d) 200 3.123

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

84. What is the output of this program? #include usingnamespacestd; template voidloopIt(T x) { T val[count]; for(int ii =0; ii < count; ii++) { val[ii]= x++; cout<(xx); } a) 2.1 b) 3.1 c) 4.1 d) 2.1 3.1 4.1 85. Why we use :: template-template parameter? a) binding b) rebinding c) both a & b d) none of these 86.What is the output of this program? #include using namespace std; template classmysequence { T memblock [N]; public: voidsetmember (int x, T value); T getmember (int x); Prof. A. V. Yenkikar

Page 32

SE(Comp)

MCQ on Templates and Exception Handling

}; template voidmysequence :: setmember (int x, T value) { memblock[x] = value; } template T mysequence :: getmember (int x) { returnmemblock[x]; } int main () { mysequencemyints; mysequencemyfloats; myints.setmember (0, 100); myfloats.setmember (3, 3.1416); cout< using namespace std; template T min (T a, T b) { return (a>b?a:b); } int main () { int i = 5, j = 6, k; long l = 10, m = 5, n; k = min(i, j); n = min(l, m); cout<< k <
Prof. A. V. Yenkikar

Page 33

SE(Comp)

MCQ on Templates and Exception Handling

88. What is the output of this program? #include using namespace std; template void swap( T& x, T& y) { T temp = x; x = y; y = temp; } int main () { int i = 5, j = 6; cout<< swap(I,j)< T max(T,T); 2)template R max(T,S); 3)template<>const char *max( const char *s1, const char *s2); a)only 1 true b)1&2 true c)2&3 True d)1,2&3True 90. What is the output of this program? #include using namespace std; template int find(atype* array, atype value, int size) { for(int j=0; j
Page 34

SE(Comp)

MCQ on Templates and Exception Handling

cout<< “\n 4 in dubArray: index=” << find(dubArr, db, 6); cout<
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

1. 2. 3. 4. 5. 6. 7. 8. 9.

93. What is the output of this program? #include usingnamespacestd; template type Max(type Var1, type Var2) { return Var1 > Var2 ? Var1:Var2; } int main() { int p; p = Max(100, 200); cout<< p < usingnamespacestd; template class Test { public: Test() { }; Prof. A. V. Yenkikar

Page 35

SE(Comp) 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.

MCQ on Templates and Exception Handling ~Test()

{ }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; cout<< Var1.Funct1(200)<
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

94. What is the output of this program? #include usingnamespacestd; template classTestVirt { public: virtual type TestFunct(type Var1) { return Var1 *2; } }; int main() { TestVirt Var1; cout<< Var1.TestFunct(100)<
95. What is the output of this program? 1. #include 2. usingnamespacestd; Prof. A. V. Yenkikar

Page 36

SE(Comp) 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.

MCQ on Templates and Exception Handling

template 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 <
98. Which is dependant on template parameter? a) base class b) abstract class c) method d) None of the mentioned 99. Which value is placed in the base class? a) derived values b) default type values c) both a & b d) None of the mentioned

Prof. A. V. Yenkikar

Page 37

SE(Comp)

MCQ on Templates and Exception Handling

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

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26.

101. What is the output of this program? #include usingnamespacestd; class class0 { public: virtual ~class0(){} protected: char p; public: chargetChar(); }; class class1 :public class0 { public: voidprintChar(); }; void class1::printChar() { cout<<"True"<
102. What is the output of this program? 1. #include 2. usingnamespacestd; 3. templateclassclsTemplate 4. { 5. public: 6. T value; 7. clsTemplate(T i) 8. { 9. this->value = i; 10. } 11. void test() 12. { 13. cout<< value <
Page 38

SE(Comp) 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37.

MCQ on Templates and Exception Handling

}; classclsChild:publicclsTemplate { public: clsChild():clsTemplate(0) { } clsChild(char c):clsTemplate( c ) { } void test2() { test(); } }; int main() { clsTemplate a(42); clsChild b('A'); a.test(); b.test(); return0; } a) 42 b) A c) 42 A d) A 42

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.

103. What is the output of this program? #include usingnamespacestd; template class Test { public: Test(); ~Test(); type Data(type); }; template type Test::Data(type Var0) { return Var0; } template Test::Test() { } template Test::~Test() { Prof. A. V. Yenkikar

Page 39

SE(Comp) 23. 24. 25. 26. 27. 28. 29.

MCQ on Templates and Exception Handling

} int main(void) { Test Var3; cout<< Var3.Data('K')<
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

104. What is the output of this program? #include usingnamespacestd; class Base { public: Base () { cout<<"1"<
Prof. A. V. Yenkikar

Page 40

SE(Comp)

MCQ on Templates and Exception Handling

d) 4 106. How many kinds of parameters are there in C++? a) 1 b) 2 c) 3 d) None of the mentioned 107 What is the output of this program? #include using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; cout<< Var1.Funct1(200); cout<< Var2.Funct2(3.123); return 0; } a) 100 b) 200 c) 3.123 d) 200 3.123 Answer:d 1. 108. What is the output of this program? #include using namespace std; class class0 { public: virtual ~class0(){} protected: char p; Prof. A. V. Yenkikar

Page 41

SE(Comp)

MCQ on Templates and Exception Handling

public: chargetChar(); }; class class1 : public class0 { public: voidprintChar(); }; void class1::printChar() { cout<< "True" < using namespace std; template class Test { public: Test(); ~Test(); type Data(type); }; template type Test::Data(type Var0) { return Var0; } template Test::Test() { } template Test::~Test() { } int main(void) { Test Var3; cout<< Var3.Data('K') <
Page 42

SE(Comp)

MCQ on Templates and Exception Handling

} a) k b) l c) error d) runtime error Answer:a 110.

What is the output of this program? #include using namespace std; class Base { public: Base ( ) { cout<< "1" <
Page 43

SE(Comp)

MCQ on Templates and Exception Handling

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. 113.What is the output of this program? #include using namespace std; template classmysequence { T memblock [N]; public: voidsetmember (int x, T value); T getmember (int x); }; template voidmysequence :: setmember (int x, T value) { memblock[x] = value; } template T mysequence :: getmember (int x) { returnmemblock[x]; } int main () { mysequencemyints; mysequencemyfloats; myints.setmember (0, 100); myfloats.setmember (3, 3.1416); cout< using namespace std; template T max (T a, T b) { return (a>b?a:b); } int main () { int i = 5, j = 6, k; Prof. A. V. Yenkikar

Page 44

SE(Comp)

MCQ on Templates and Exception Handling

long l = 10, m = 5, n; k = max(i, j); n = max(l, m); cout<< k < using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; cout<< Var1.Funct1(200); cout<< Var2.Funct2(3.123); return 0; } a) 100 b) 200 c) 3.123 d) 200 3.123 Answer:d

Prof. A. V. Yenkikar

Page 45

SE(Comp)

MCQ on Templates and Exception Handling

116. Which parameter is legal for non-type template? a) pointer to member b) object c) class d) none of the mentioned Answer:a 117. In the template declaration, T stands for a) an integer data type b) a generic data type c) an arbitrary class d) a class defined earlier Answer:b 118. find error in programm #include usingnamespacestd; template voidfun(constT& x) { staticinti = 10; cout<< ++i; return; } intmain() { fun(1); // prints 11 cout<(2); // prints 12 cout<(1.1); // prints 11 cout< using namespace std; template class Test { private: T val; public: static int count; Test() Prof. A. V. Yenkikar

Page 46

SE(Comp)

MCQ on Templates and Exception Handling

{ count++; } // some other stuff in class }; template int Test::count = 0; int main() { Test a; // value of count for Test is 1 now Test b; // value of count for Test is 2 now Test c; // value of count for Test is 1 now cout<< Test::count <::count < using namespace std; class Box { public: staticintobjectCount; Box(double l=2.0, double b=2.0, double h=2.0) { cout<<"Constructor called." <
Page 47

SE(Comp)

MCQ on Templates and Exception Handling

{ Box Box1(3.3, 1.2, 1.5); Box Box2(8.5, 6.0, 2.0); cout<< "Total objects: " << Box::objectCount< #include using namespace std; template inline T const& Max (T const& a, T const& b) { return a < b ? b:a; } int main () { int i = 39; int j = 20; cout<< "Max(i, j): " << Max(i, j) <
Prof. A. V. Yenkikar

Page 48

SE(Comp)

MCQ on Templates and Exception Handling

123. What is the output of this program? #include using namespace std; template 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 < using namespace std; template class Test { public: Test() { }; ~Test() { }; type Funct1(type Var1) { return Var1; } type Funct2(type Var2) { return Var2; } }; int main() { Test Var1; Test Var2; Prof. A. V. Yenkikar

Page 49

SE(Comp)

MCQ on Templates and Exception Handling

cout<< Var1.Funct1(200); cout<< Var2.Funct2(3.123); return 0; } a) 100 b) 200 c) 3.123 d) 200 3.123 Answer:d 125.How many kinds of parameters are there in C++? a) 1 b) 2 c) 3 d) None of the mentioned Answer:c 126. What is the output of this program? #include using namespace std; class Base { public: Base ( ) { cout<< "1" <
Prof. A. V. Yenkikar

Page 50

SE(Comp) -

Explanation:In this program, We are using class to pass the value and then we .... #include using namespace std; template <class type> class Test.

526KB Sizes 21 Downloads 524 Views

Recommend Documents

No documents