For the following 3 questions, let A = {1, 3, 5, 7} and B = {2, 3, 5, 7, 11}.
Q1. What is A B?
a. {1 , 2, 3, 5 , 7, 11} b. {3, 5, 7} c. {1, 2, 3, 3, 5, 5, 7, 7, 11} d. {2, 11}
[1 mark] Feedback. (a) is correct. (b) is A B. (c) you forgot sets cant have duplicate elements. (d) is B A.
Q2. What is B - A ?
a. {1 , 2, 3, 5 , 7, 11} b. {3, 5, 7} c. {1} d. {2, 11}
[1 mark] Feedback. (d) is correct. (c) is A B, you did it the wrong way round. (a) is A B. (b) is A
Q3. What is A B?
a. {1 , 2, 3, 5 , 7, 11} b. {3, 5, 7} c. {1} d. {3, 3, 5, 5, 7, 7} [1 mark] Feedback. (b) is correct. (a) is A B. (d) you forgot sets cant have duplicate elements. (c) is A B.
In the following 4 questions, let f(x) = 2x + 3 and g(x) = 3x 1.
Q4. What is the function f + g?
a. x - 2 b. 5x + 2 c. 6x + 1 d. 6x 2 + 7x - 3 [1 mark] Feedback. (b) is correct.
f + g = 2x + 3 + 3x -1 = 2x + 3x + 3 -1 = 5x +2
(a) Perhaps you got the signs mixed up? 2x -3x -3 +1 = -x 2 (c) is f g. (d) is f * g
Michaelmas In-Class Test Questions, Answers and Feedback SCC120 201!1"
December 12, 2013 120 Michaelmas In-Class Test . Questions, Answers and Feedback 2
Q5. What is the function f * g?
a. 13x 3 b. 5x 2 3x + 6x 4 c. 5x + 2 d. 6x 2 + 7x - 3 [2 marks] Feedback. (d) is correct. f * g = (2x + 3)(3x 1) = 2x(3x 1) + 3(3x 1) = 6x 2 2x + 9x 3 = 6x 2 + 7x 3
(a) you forgot that x * x = x squared. 6x 2x + 9x -3 = 13x 3. (c) is f + g. (b) 2x(3x 1) + 3(3x 1) != 5x 2 3x + 6x 4. You added instead of multiplying out the brackets.
Q6. What is the function f g?
a. 6x + 1 b. x - 2 c. 6x 2 + 7x - 3 d. 6x + 8 [2 marks] Feedback. f g = f(g(x)). (a) is correct.
f(3x 1) = 2(3x 1) + 3 = 6x 2 + 3 = 6x + 1
(d) You did it the wrong way round. g(f(x)) = 3(2x + 3) 1 = 6x + 9 1 = 6x + 8 (b) is a wrong answer to Q4. (c) is f * g
Q7. Find the inverse function of f(x). f -1 (x) =
a. (x-3) / 5 b. x - 5 c. (x+3)/2 d. (x-3) / 2
[3 marks] Feedback. (d) is correct f(x) = 2x + 3 y = 2x + 3 2x + 3 = y 2x + 3 3 = y 3 2x = y 3 x = (y-3) / 2
So f -1 (x) = (x-3)/2
(c) 2x + 3 = y 2x = y + 3 (mistake) x = (y+3)/2 so f -1 (x) = (x+3)/2 (b) 2x = y - 3 x = y 3 2 (youve subtracted rather than divided by 2) x = y 5 (a) is just weird.
December 12, 2013 120 Michaelmas In-Class Test . Questions, Answers and Feedback 3
Q8. Define the following function recursively. f(x) = 2x +3.
a. f(x) = f(x-1) + 2 b. f(x) = f(x-1) - 2 c. f(x) = f(x-1) + 4 d. f(x) = f(x-1) - 4 [3 marks] Feedback. (a) is correct
2x + 3 = 2x + 1 + B 2x + 1 + B = 2x + 3 2x 2x + 1 -1 + B = 2x -2x +3 1 B = 2
f(x) = f(x-1) + 2
(b) , (c), (d) : mixed up positive/negative signs
Q9. Convert the following English statement into a proposition. I can save lives if I donate blood where A = I can save lives and B = I donate blood
a. A B b. A B c. A B d. B A [1 mark] Feedback. (d) is correct. You have to rewrite the English statement as If I donate blood then I can save lives. So If B then A. B-> A
If you got (a), you didnt spot the need to reverse the order. If you got (b) or (c), you didnt recognize the statement was an implication. (b) is an operator on functions.
For the following 2 questions, here are some equivalences. Implication : (P Q) ( ~ P Q) Associativity : (P Q) R P (Q R) Commutivity : (P Q) (Q P) Complement : (P True) True
Q10. To convert ~B (B C) into (~B B) C, which equivalence do you apply?
a. complement b. associativity c. commutivity d. implication [1 mark] Feedback. (b) is correct its the only equivalence where brackets are being moved.
Q11. To convert B (B C) into ~B (B C), which equivalence do you apply? a. associativity b. commutivity c. complement d. implication [2 marks] Feedback. (d) is correct its the only equivalence that contains a (implication) sign.
December 12, 2013 120 Michaelmas In-Class Test . Questions, Answers and Feedback 4
which of the following declarations in C best implements it?
a. char beverage[] = coffee;
d. char beverage[6];
c. int beverage[] = {0,1,2,3,4,5}; b. struct beverage{ int domain; char codomain; beverage *next; }; [2 marks] Feedback. beverage is a function where the domain is the range of integers 0..5 and the codomain is the set of characters {c,e,f,o}. The string (a) maps elements in the domain (indices) into elements in the codomain (characters) the right way to spell our coffee.
Q13. What would result from the following C code?
char beverage[] = tea; strcpy(beverage, coffee); // copy the string coffee to the //string called beverage
a. beverage now contains the string coffee b. beverage now contains the string teacoffee c. coffee now contains the string beverage d. an error occurs because beverage is not big enough to hold the number of bytes required for coffee [2 marks] Feedback. The first instruction makes beverage a string of length 4 (dont forget the /0 character at the end). There is therefore not enough space to accommodate the string coffee so (d) is correct.
Q14. What would result from the following revised C code?
char beverage[6] = tea; strcpy(beverage, coffee); // copy the string coffee to the //string called beverage
a. beverage now contains the string coffee b. beverage now contains the string teacoffee c. coffee now contains the string beverage d. an error occurs because beverage is not big enough to hold the number of bytes required for coffee [2 marks]
Feedback. The first instruction makes beverage a string of length 6 but there is still not enough space to accommodate the string coffee because it needs 7 characters including the null character at the end. So (d) is correct.
Q15. Given the declaration of the string beverage below:
December 12, 2013 120 Michaelmas In-Class Test . Questions, Answers and Feedback 5
char beverage[6] = tea;
If the runtime system allocates the character t in beverage the address 64 (decimal) what is the address of the character a assuming each byte in memory has its own address?
a. 254 b. 2 c. 66 d. 128 [2 marks]
Feedback. Remember the array mapping function n+(s*i) where n is the base address, s is the sixe of each element and i is the index. n = 64, s = 1 and i = 2; so the address of a is 64 + (1*2) = 66. Thus (c) is correct.
Q16. A speed camera takes digital images of the number plates of vehicles detected speeding. An image is stored locally in a record containing three fields: the image itself (a bit pattern), the time at which the image was taken and the speed of the vehicle. At a fixed time at the end of every day, the collection of records is uploaded to a database in the local police authority. For the software that executes in the speed camera, what is the main disadvantage of storing the records in an array compared with storing them in a chain?
a. scanning an array to upload the data will be less efficient than scanning a chain b. it will be faster to search a chain for a particular car c. the size of the array must anticipate the maximum number of speeding cars in any day d. arrays can only hold primitive types [2 marks] Feedback. (c) is correct. None of the others are true.
Q17. A function to add a new cell to a chain of ordered values must work for which of the following conditions if it is to be error-free? Tick all that apply.
a. the new cell is added to the end of the chain b. the new cell is added to the front of the chain c. the new cell is added somewhere in the middle of the chain d. the new cell is added to an empty chain [4 marks] Feedback. All 4 cases need to be handled.
Q18. To aid scanning a data structure, it is common to use special sentinel values to mark when to stop. For example, in C, the character /0 marks the end of a string. What serves as the sentinel at the end of a singly-linked chain?
a. the character /0 b. a stop sign c. the integer -1 d. a null pointer [2 marks] Feedback. (d) is correct, the others are all wrong.
Q19. Consider the C function used. The record myStruct includes a field called next which is a pointer to another instance of myStruct.
December 12, 2013 120 Michaelmas In-Class Test . Questions, Answers and Feedback 6
int used(myStruct *front) { myStruct *current; int cellSize = sizeof(myStruct); int numB = 0;
current = front; { while (current != null) { numB = numB + cellSize; current = current -> next; }; return numB; }
used does what?
a. it returns the number of cells in the chain for which the parameter is a pointer to the first cell b. it returns the number of bytes of memory used by the chain for which the parameter is a pointer to the first cell c. it returns the number of elements stored in a myStruct array d. it does nothing because it generates a runtime error when the end of the chain is encountered [2 marks] Feedback. (b) is correct. It uses sizeof to return the number of bytes used by an instance of myStruct and multiplies that by the number of cells in the chain. (c) is wrong because it isnt an array and (d) never occurs because we stop when we detect the null pointer.