aaaa

download aaaa

If you can't read please download the document

description

aa

Transcript of aaaa

2014 ab2014 Exam SolutionsQuestion 0Ans: AQuestion 1Ans: CQuestion 2 int aTemp = a; int bTemp = b; int cTemp = c; a = bTemp; b = cTemp; c = aTemp;Question 3The value of z will be 6Question 4Ans: CQuestion 5The value of result will be 2Question 6Ans: BQuestion 7Notes: count becomes num + 1 loop runs num timesnum | count | result 0 | 1 | 0 1 | 2 | 1 2 | 3 | 3 3 | 4 | 6 4 | 5 | 10 5 | 6 | 15 Answer: sums numbers between 0 and num (inclusive)Question 8 int oldLeft = values[0]; j = 0; while (j < LENGTH) { values[j] = values[j + 1]; j++; } values[LENGTH - 1] = oldLeft;Question 9 double calcMean (int *array, int length) { long sum = 0; int i = 0; while (i < length) { sum += array[i]; i++; } return (double)sum/length; }Question 10***** ********** ** ** ******First Bug: col was not reset for subsequent rows - should set col to 0 when row is incremented.Second Bug: The print of a new line only occured at the very end - should occur in while loop.Question 11NB: '%x' means print in hexadecimal format'printf ("%x\n",nines);'This will print 999 as hexadecimal i.e. 3e7'printf ("%x\n",nines * 0x10);'This will print 999*16 as hexadecimal i.e. 3e70byte * ptr = (byte *) &nines;This converts nines into a 'byte'That is, a char.'printf ("%d\n",ptr[0]);'This will print the first byte (i.e. the last part of 999 as hexadecimal) as a decimal number i.e. 231'printf ("%d\n",ptr[1]);'The next byte i.e. 3'printf ("%d\n",ptr[2]);'The next byte i.e. 0'printf ("%d\n",ptr[3]);'The last byte i.e. 0Question 12Answer: If you get down to 366 days and the year is a leap year, you will get into the while loop, but since you only have 366 days left you will not get into the subsequent loop and will still have 366 days left, so will get stuck in an endless loop.This will occur, for example, if days is 1872.Question 13Turtle (2,1)|gamera | achilles|| 2 | 1 || Turtle (2,0)| |gamera | achilles|| | 2 | 0 || | 1 | 1 || | Turtle (1,0)| | |gamera | achilles|| | | 1 | 0 || | | 0 | 1 || | returns 2| | 0 | 2 || returns 3| 1 | 3 || Turtle (1,2)| |gamera | achilles|| | 1 | 2 || | Turtle (1,1)| | |gamera | achilles|| | | 1 | 1 || | | Turtle (1,0)| | | |gamera | achilles|| | | | 1 | 0 || | | | 0 | 1 || | | returns 2| | | 0 | 2 || | returns 3| | 0 | 3 || returns 4| 0 | 4 |returns 5call (2,1)call (2,0)call (1,0)call (1,2)call (1,1)call (1,0)The value returned is 5.Question 14Question 15Question 16Question 17