Vous êtes sur la page 1sur 3

2014 ab

2014 Exam Solutions


Question 0
Ans: A
Question 1
Ans: C
Question 2
int aTemp = a;
int bTemp = b;
int cTemp = c;
a = bTemp;
b = cTemp;
c = aTemp;
Question 3
The value of z will be 6
Question 4
Ans: C
Question 5
The value of result will be 2
Question 6
Ans: B
Question 7
Notes: count becomes num + 1
loop runs num times
num | 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 11
NB: '%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. 3e70
byte * 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 d
ecimal 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. 0
Question 12
Answer: If you get down to 366 days and the year is a leap year, you will get in
to the while loop, but since you only have 366 days left you will not get into t
he subsequent loop and will still have 366 days left, so will get stuck in an en

dless loop.
This will occur, for example, if days is 1872.
Question 13
Turtle (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 5
call (2,1)
call (2,0)
call (1,0)
call (1,2)
call (1,1)
call (1,0)
The value returned is 5.
Question 14
Question 15
Question 16
Question 17

Vous aimerez peut-être aussi