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 10

Chapter 10

Pointers and Dynamic Arrays

This activity contains 10 questions.

Question 1.
Which of the following is not a valid way to dereference pointer p to access the num field?

class MyInt {
  public: 
    int num;
};

MyInt *p;

 
End of Question 1


Question 2.
Which line of the code below will cause a compiler error?

int* p1,p2;
int x = 3;
p1 = &x;
p2 = &x;

 
End of Question 2


Question 3.
What will the following program output?

class MyInt {
  public: 
    int num;
};

int main()
{
	MyInt *p1;
	MyInt *p2;

	p1->num = 10;
	p1 = p2;
	cout << p1->num << " " << p2->num << endl;
	return 0;
}

 
End of Question 3


Question 4.

What will the following code output?

class MyInt {
  public: 
    int num;
	MyInt();
	MyInt(int n);
};

MyInt::MyInt() 
{
}

MyInt::MyInt(int n)
{
	num=n;
}

void DoSomething(MyInt *ptrA, MyInt *ptrB)
{
	int x = ptrA->num;
	ptrA->num = ptrB->num;
	ptrB->num = x;
}

int main()
{

	MyInt *p1;
	MyInt *p2;

	p1 = new MyInt(3);
	p2 = new MyInt(4);
	
	DoSomething(p1,p2);

	cout << p1->num << " " << p2->num << endl;

	return 0;
}
 
End of Question 4


Question 5.

What will the following code output?

class MyInt {
  public: 
    int num;
	MyInt();
	MyInt(int n);
};

MyInt::MyInt() 
{
}

MyInt::MyInt(int n)
{
	num=n;
}

void DoSomething(MyInt *ptrA, MyInt *ptrB)
{
	ptrA = new MyInt(103);
	ptrB = new MyInt(104);
	cout << ptrA->num << " " << ptrB->num << endl;
}

int main()
{

	MyInt *p1;
	MyInt *p2;

	p1 = new MyInt(3);
	p2 = new MyInt(4);
	
	DoSomething(p1,p2);

	cout << p1->num << " " << p2->num << endl;

	return 0;
}
 
End of Question 5


Question 6.

What will the following program output?

class MyInt {
  public: 
    int num;
	MyInt();
	MyInt(int n);
};

MyInt::MyInt() 
{
}

MyInt::MyInt(int n)
{
	num=n;
}

void DoSomething(MyInt **ptrA)
{
	cout << (*ptrA)->num << endl;
	delete (*ptrA);
	*ptrA = new MyInt(103);
	cout << (*ptrA)->num << endl;
}

int main()
{

	MyInt *p1;
	p1 = new MyInt(3);
	
	DoSomething(&p1);

	cout << p1->num << endl;

	return 0;
}
 
End of Question 6


Question 7.

Give the output of the following code:

void DoSomething(char *s)
{
	char *p1,*p2,tmp;

	p1 = s;
	p2 = p1 + strlen(s)-1;
	while (p1<p2) {
		tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
		p1++;
		p2--;
	}
}

int main()
{
	char s[7] = "foobar";
	DoSomething(s);
	cout << s << endl;

	return 0;
}
 
End of Question 7


Question 8.
What is wrong with the following code fragment?

double *dArray = new double[100];
// Code here to use array
delete dArray;

 
End of Question 8


Question 9.

Give the output of the following program:

class MyInt {
private:
    int *p_num;
public:
	MyInt();
	MyInt(int n);
	int GetNum();
	void SetNum(int n);
};

// Constructors
MyInt::MyInt() 
{
	p_num = new int;
	*p_num = -1;
}

MyInt::MyInt(int n)
{
	p_num = new int;
	*p_num = n;
}

int MyInt::GetNum()
{
	return *p_num;
}

void MyInt::SetNum(int n)
{
	*p_num = n;
}

int main()
{
	MyInt i1, i2(10);

	cout << i1.GetNum() << " " 
		<< i2.GetNum() << endl;

	i1 = i2;
	i2.SetNum(55);

	cout << i1.GetNum() << " " 
		<< i2.GetNum() << endl;
	
	return 0;
}
 
End of Question 9


Question 10.

What is the output of the following code?

class MyInt {
private:
    int *p_num;
public:
	MyInt();
	MyInt(int n);
	int GetNum();
	void SetNum(int n);
	MyInt& operator=(const MyInt &rtSide);
};

// Constructors
MyInt::MyInt() 
{
	p_num = new int;
	*p_num = -1;
}

MyInt::MyInt(int n)
{
	p_num = new int;
	*p_num = n;
}

MyInt& MyInt::operator=(const MyInt &rtSide)
{
	*(this->p_num) = *(rtSide.p_num);
	return *this;
}

int MyInt::GetNum()
{
	return *p_num;
}

void MyInt::SetNum(int n)
{
	*p_num = n;
}

int main()
{
	MyInt i1, i2(10);

	cout << i1.GetNum() << " " 
		<< i2.GetNum() << endl;

	i1 = i2;
	i2.SetNum(55);

	cout << i1.GetNum() << " " 
		<< i2.GetNum() << endl;
	
	return 0;
}
 
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