CS 1044 Fall 2002 Test 2 Answers Q A Reason -------------------------------------------------------------------------------- 1 4 The loop body will be entered with j having the values 5, 10, and 20. 2 7 The value of j is doubled within the loop body before being printed, so the last value printed will be 40. 3 4 The first parameter is used for communication into and out of the function, so it needs to be passed by reference. The second parameter is only used to communicate a value out of the function, but that also requires being passed by reference. 4 4 In an array, all elements must be of the same type, and elements are accessed by specifying a position (via an index value), and there is no support for assigning an array to an array in C++. With structs, the members can be of any types needed, elements are accessed by using their names, and assignment of struct to struct is supported as long as they are of the same type. 5 7 Tracing the execution of the loops: Sum | 0 5 12 21 24 29 36 37 40 45 ------------------------------------------- i | 5 3 1 ------------------------------------------- k | 0 2 4 0 2 4 0 2 4 For questions 6 through 8, just trace the execution of the function call. The only issues are that the first parameter is passed by value, the second parameter is passed by reference, and there is a global variable named Tmp as well as one local to the function Foo(). The reference to Tmp inside Foo() binds to the variable Tmp declared there, and so the global variable is not modified. 6 3 7 6 8 5 9 1 2 is invalid because MyStudent is of type Student and that type does not have a field named First. 3 is invalid because StudentName isn't the name of a declared variable. 10 8 Because the first parameter of Fix() is passed by value, the actual parameter can be a constant, a variable, a literal constant or an expression. But, the second parameter to Fix() is passed by reference, and so the actual parameter must be a variable. That eliminates responses 1, 3 and 4. 11 4 MAXSIZE is used an the dimension of an array, so it must be an integer constant. 12 4 myArray[] is of dimension 41, so the cells would have indices 0 through 40. 13 3 There is a typo here; the first two answers should refer to Punctuate instead of Calculate. Fortunately, that doesn't matter because neither of those could be right anyway. If the given code compiles, there must be a declaration of COMMA that is in scope when the first statement in the function body is reached; that is only possible if COMMA is declared globally, since it isn't declared within the function (before the relevant statement), and it isn't a parameter. 14 8 1 is invalid because Part is being used as a variable name when it is really a type name. 15 7 The function's formal parameter is of type Name. In 1, the actual parameter is an array of Name variables, not a Name. In 3, the actual parameter is non-existent cell of an array. 16 1 The function's formal parameter is an array of Name variables. In 2, the actual parameter is a single array cell, which is of type Name. In 3, the actual parameter is a variable of type Name. In 4, the actual parameter is simply syntactically invalid. 17 2 The parameter is an array of type int, and the contents of the array need to be modified by the function, so it needs to be passed by reference. Since arrays are passed by reference by default, the correct response is "int". 18 3 The function works by swapping corresponding elements of the array; that is, first with last, second with next-to-last, etc. Since Right is used as the index of the later element, it needs to be initialized to the index of the last element. 19 1 The loop must terminate when Left and Right converge; there are actually two cases. If the array has an even number of elements, we must stop when Left > Right. If the array has an odd number of elements, we should stop (without doing a swap) when Left == Right. Choice 2 will also work, but will do an unnecessary swap if the array holds an odd number of values. 20 4 Lines 5 through 7 are performing a swap. Since statement 6 overwrites the value at index Right, line 5 must save that value. 21 4 Statement 7 must complete the swap by placing the value that was at index Right at index Left. 22 2 To make the algorithm work, Left and Right have to move toward the middle of the array. Since Left is starting at 0, it needs to move up by increasing. 23 3 Plank is the name of a variable of type Board; variables of type Board have a field of type Size named Dimensions; variables of type Size have a field named smoothFaces. 1 refers to an undeclared variable smoothFaces. 2 uses Wood as a variable name when it's never been declared. 24 2 1 uses Size as a variable name when it's a type name. 3 uses Width as if it were a field name for the type Board; it's not. 25 3 1 uses Size as a variable (field) name. 2 repeats that mistake.