Exercise

1.What is the output of this program?
Note:Includes all required header files

using namespace std; template <class T>
T max (T& p, T& q)
{
return (p>q?p:q);
}
int main ()
{
int x = 55, y = 60, k;
long a = 105, b = 53, n;
m = max(x, y);
n = max(a, b);
cout << m << endl;
cout << n << endl;
return 0;
}

A. 60
B. 60
    105
C. 50
    105
D. 60
    53

View Answer


2.What is the output of this program?
Note:Includes all required header files

using namespace std;
template <typename T, int num>
void loopIt(T x)
{
T find[num];
for(int p = 0; p < num; p++)
{
find[p] = x++;
cout << find[p] << endl;
}
};
int main()
{
float q = 2.1;
loopIt<float, 3>(q);
}

A. 2.1
B. 3.1
C. 4.1
D. 2.1
    3.1
    4.1

View Answer


3.What is the output of this program?
Note:Includes all required header files

using namespace std;
template <typename T>
T maximum(T x, T y)
{
return (x > y)? x : y;
}
int main()
{
cout << maximum(3, 7) << std::endl;
cout << maximum(3.0, 7.0) << std::endl;
cout << maximum(3, 7.0) << std::endl;
return 0;
}

A. Compiler Error in last cout statement as call to maximum is ambiguous
B. Compiler Error in all cout statements as data type is not specified
C. 7
    7.0
    7.0
D. None of the above

View Answer


4.Which of the following statement is correct about the program given below?
Note:Includes all required header files

template<class T, class U>
class A {
T x;
U y;
static int count;
};
int main() {
A<char, char> p;
A<int, int> q;
cout << sizeof(p) << endl;
cout << sizeof(q) << endl;
return 0;
}

A. Compiler Error: There can not be more than one template arguments.
B. 6
    12
C. 8
    8
D. 2
    8

View Answer


5.Which of the following is true about the following program
Note:Includes all required header files
using namespace std;
template&t;class P, class Q, class Q=double>
class A {
P x;
Q y;
R z;
static int count;
};
int main()
{
A&t;int, int> m;
A&t;double, double> n;
cout << sizeof(m) << endl;
cout << sizeof(n) << endl;
return 0;
}

A. Compiler Error: template parameters cannot have default values
B. 16
    24
C. 8
    16
D. 20
    28

View Answer


6.What will be the output of this program?
Note:Includes all required header files

using namespace std;
template <int i>
void fun()
{
i = 20;
cout << i;
}
int main()
{
fun<10>();br/> return 0;br/> }br/>

A. Compile Error
B. 10
C. 20
D. 15

View Answer


7.What will be the output of this program?
Note:Includes all required header files

using namespace std;
template<int n> struct funStruct
{
static const int val = 2*funStruct<n-1>::val;
};
template<> struct funStruct<0>
{
static const int val = 1 ;
};
int main()
{
cout << funStruct<10>::val << endl;
return 0;
}

A. Compiler Error
B. 1
C. 2
D. 1024

View Answer


8. What will be the output of the following program?
Note:Includes all required header files

using namespace std;
template < typename T >
void print_mydata(T find)
{
cout << find << endl;
}
int main()
{
double p = 17.5;
string s("Hi to all");
print_mydata( p );
print_mydata( s );
return 0;
}

A. 5.5 Hi to all
B. 17.5
C. Hi to all
D. None of the above

View Answer



Discussion

* You must be logged in to add comment.