Sample Exam 1 Solutions

Question 1

1

The following code segment will NOT produce an error:

int input = 123;
double output = (double) input;
	

This is because when you typecast the int input into a double, your output will simply be 123.00000000. When you typecast a double into an int, though, eg by double input = 123.3042; int output = (int) input; your output becomes 123, and the decimal part gets truncated.

2

This is false, since the scope of a is inside the for loop, you are allowed to change a.

3

The wording on this one is unclear. I'd say this is false, since ofstream, ifstream and fstream are the file streams themselves. The functions such as open() or close() allow us to manipulate these file streams. And ofstream, fstream are the datatypes that allow us to manipulate files, so in either interpretation this should be false.

4

int ****p; is a valid, albeit useless declaration. Trust me, no one uses pointers to pointers to pointers to pointers to an integer.

Question 2

The output of the code given will be 20 100 20 19 10 18. Just keep in mind that in test() and test2() the global copies of g are used, and in main() the local copy of g is used. Although it's not good practice to give variables such misleading names.

Question 3

I'm pretty sure there is a typo here. We need to know the address of q and not p, so I'll assume that we know that instead.
The address of q is: 0x7ffc67f6e6f0 and the address of i is: 0x7ffc63874ed4.

#include <iostream>
using namespace std;

int main () 
{
	int * p,q;
	int i=1;
	p=&i;	 // p = address of i = 0x7ffc63874ed4
	cout << p << '\n'; 
	cout << (*p)++ << '\n';  	// first *p is outputted, then *p is incremented, so this line outputs 1, and increments i = *p to 2.
	cout << *(p++) << '\n';	// first *p is outputted, then p is incremented, so this line makes p = 0x7ffc63874ed4 + 4 = 0x7ffc63874ed8
	cout << p << '\n';
	q=++i;	// First i = 3, then q = i.
	p=&q;	// p = &q means p is now a new pointer pointing to q. The address of q was 0x7ffc67f6e6f0, so now p is this value.
	cout << i+q << '\n';	// q was 3, i was 3, so q+i = 6.
	cout << *p+&q <<'\n';	// *p = q = 3, and &q = 0x7ffc67f6e6f0. Now you should know that (&q + 3) is similar to adding 3*4 = 12 to the hexadecimal address, so you will get 0x7ffc67f6e6fc.
	return 0;
}
	

Read through the comments while reading the code, and the output should be clear. We need to note that for pointers p, p+3 is the same thing as doing p++ three times.

0x7ffc63874ed4
1
2
0x7ffc63874ed8
6
0x7ffc67f6e6fc

Question 4

The function you need to define is void TwoMax(int* Max, int* SecondMax, int*ArrIntegers);, and from this definition it is clear that you are asked to call this function by reference. Therefore a sample function call would be something like TwoMax(&Max,&SecondMax,ArrIntegers). You pass the third entry by value, since it's just an array, and supposedly ArrIntegers is an array of integers of length 20.

Now, for the mechanics of the TwoMax() function. Recall that in Quiz 1 we have already found the minimum of an array. We can tweak things around a little bit to find the max of the array instead. The simplest way of writing this TwoMax() function that I can think of, is by first finding the max of the array via a for loop, then changing that value to INT_MIN, and then running the loop again.

Challenge: Can you do this without actually changing the values of the array?

#include<iostream>
#include<climits> 		// For INT_MIN
using namespace std;

void TwoMax(int* Max, int*SecondMax, int*ArrIntegers)
{
	*Max = ArrIntegers[0];
	int index_of_max=0;		// Let us store the position where the max is attained in this variable.
	
	for(int i=1; i<20; i++)	 // We know that the array has 20 entries!!
	{
		if(*Max < ArrIntegers[i])		 // If Max is not the actual max, then update it. Also update the index/position.
		{
			*Max = ArrIntegers[i];
			index_of_max = i;
		}
	}
	ArrIntegers[index_of_max] = INT_MIN;		// Set the max value to the smallest integer possible, so that in the second pass you get the second max.

	*SecondMax = ArrIntegers[0];		// We don't need to keep track of the index this time!
	for(int i=1; i<20; i++)
	{
		if(*SecondMax < ArrIntegers[i])
		{
			*SecondMax = ArrIntegers[i];
		}
	}

	ArrIntegers[index_of_max] = *Max;		// Fix ArrIntegers[index_of_max] back to its original value. This is optional.
}

int main()		// The so-called "Driver" function for TwoMax()
{
	int Max, SecondMax;
	int ArrIntegers[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
	cout << "The ArrIntegers[] are: \n";
	for(int i=0; i<20; i++)
	{
		cout << ArrIntegers[i] << " ";
	}
	TwoMax(&Max, &SecondMax, ArrIntegers);
	cout << "\nThe Max is: " << Max << ", and the SecondMax is: " << SecondMax << endl;
	
	return 0;
}	
	

Question 5

We haven't seen linked lists yet, according to you guys (I myself don't know what's been covered and what hasn't). Working under that assumption, this question is out of scope for this midterm. :)