Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Quizzes  arrow Chapter 16

Chapter 16

Templates

This activity contains 10 questions.

Question 1.
What is a more generalizable and compact way to implement the following functions that compute the Absolute Value?

int AbsoluteValue(int i)
{
	if (i<0) return -1 * i;
	return i;
}

float AbsoluteValue(float f)
{
	if (f<0) return -1 * f;
	return f;
}

 
End of Question 1


Question 2.

What will the following program output when it is run?

template<class T>
void swapValues(T &var1, T &var2)
{
	T temp;

	temp = var1;
	var1 = var2;
	var2 = temp;
}

class MyInt
{
private:
	int *pInt;
public:
	MyInt();
	MyInt(int num);
	~MyInt();
	int getVal();
};

MyInt::MyInt()
{
	pInt = new int;
	*pInt = 0;
}

MyInt::MyInt(int num)
{
	pInt = new int;
	*pInt = num;
}

MyInt::~MyInt()
{
	delete pInt;
}

int MyInt::getVal()
{
	return *pInt;
}

int main()
{	
	MyInt myX(1), myY(2);

	swapValues(myX, myY);
	cout << myX.getVal() << " " << 
	   myY.getVal() << endl;
	
	return 0;
}
 
End of Question 2


Question 3.

What will the following program output when run?

template<class T>
void swapValues(T &var1, T &var2)
{
	T temp;

	temp = var1;
	var1 = var2;
	var2 = temp;
}

class MyInt
{
private:
	int *pInt;
public:
	MyInt();
	MyInt(int num);
	~MyInt();
	int getVal();
	MyInt& operator =(MyInt &myInt2);
};

MyInt::MyInt()
{
	pInt = new int;
	*pInt = 0;
}

MyInt::MyInt(int num)
{
	pInt = new int;
	*pInt = num;
}

MyInt::~MyInt()
{
	delete pInt;
}

int MyInt::getVal()
{
	return *pInt;
}

MyInt& MyInt::operator =(MyInt &myInt2)
{
	*pInt = *(myInt2.pInt);
	return *this;
}

// Main class
int main()
{	
	MyInt myX(1), myY(2);


	swapValues(myX, myY);
	cout << myX.getVal() << " " 
	   << myY.getVal() << endl;
	
	return 0;
}
 
End of Question 3


Question 4.
What is the difference between:

1. template<typename T>
and
2. template<class T>

 
End of Question 4


Question 5.
What is the difference between:

vector<int>* pVec1;
  and
vector<int *> pVec2;

 
End of Question 5


Question 6.
Is the following declaration valid?

vector<vector<double>> dVec;

 
End of Question 6


Question 7.
Which of the following is the simplest way to legally define a class to handle sets using class templates?

 
End of Question 7


Question 8.

Given the template below:

template<class T>
class Set
{
	private:
		vector<T> data;
	public:
		Set();
		void Add(T item);
		bool Member(T item);
};

template<class T>
Set<T>::Set()
{
}

template<class T>
void Set<T>::Add(T item)
{
	for (int i=0; i<data.size(); i++) {
		if (data[i]==item) return;
	}
	data.push_back(item);
}

template<class T>
bool Set<T>::Member(T item)
{
	for (int i=0; i<data.size(); i++) {
		if (data[i]==item) return true;
	}
	return false;
}

Which of the following is a valid main() function?
 
End of Question 8


Question 9.
How can we define a template with two type parameters?

 
End of Question 9


Question 10.
Given the following class definition:

template<class T>
class Set
{
	private:
		vector<T> data;
	public:
		Set();
		void Add(T item);
		bool Member(T item);
};

Which of the following is a correctly defined derived class with a constructor?

 
End of Question 10





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page