{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

final_practice_ans

# final_practice_ans - CS31 Introduction to Computer Science...

This preview shows pages 1–4. Sign up to view the full content.

CS31: Introduction to Computer Science I Final Exam Practice May 30, 2011 TA: Paul Wais ( [email protected] ) 1 Short Questions 1.1 Loop Review Consider a reverse() function for C-strings: 1 void reverse( char word[]) 2 { 3 int len = strlen(word); 4 for ( int i = 0; i < len / 2; i++) 5 { 6 char temp = word[i]; 7 word[i] = word[len - 1 - i]; 8 word[len - 1 - i] = temp; 9 } 10 } 1.1.1 Part a Rewrite reverse() using a while loop: 1 void reverse( char word[]) 2 { 3 int len = strlen(word); 4 int i = 0; 5 while (i < len / 2) 6 { 7 char temp = word[i]; 8 word[i] = word[len - 1 - i]; 9 word[len - 1 - i] = temp; 10 i++; 11 } 12 } 1.1.2 Part b Rewrite reverse() using a do-while loop: 1 void reverse( char word[]) 2 { 3 int len = strlen(word); 4 int i = 0; 5 if (len > 0) 6 { 7 do 8 { 9 char temp = word[i]; 10 word[i] = word[len - 1 - i]; 11 word[len - 1 - i] = temp; 12 i++; 13 } while (i < len / 2); 14 } 15 } 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1.1.3 Part c Rewrite reverse() using pointer arithmetic (i.e. the new function must not have any square brackets). 1 void reverse( char word[]) 2 { 3 int len = strlen(word); 4 for ( int i = 0; i < len / 2; i++) 5 { 6 char temp = * (word + i); 7 * (word + i) = * (word + len - 1 - i); 8 * (word + len - 1 - i) = temp; 9 } 10 } 1.2 Buggy Code What is wrong with the code below? 1 class C 2 { 3 public : 4 C(); 5 private : 6 int x; 7 int y; 8 }; 9 10 C::C() 11 : x = 0, y = 1 12 { } (Choose one answer) 1. Nothing is wrong, the above code will compile. 2. The initializer list for the C constructor is wrong, it should be (write a new one here): 3. There should be braces surrounding the public and private sections of C 4. C is an invalid class name. Answer: (2) The initializer list for the C constructor is wrong, it should be: 1 C::C() 2 : x(0), y(1) 3 { } 2
2 Pointer and Dynamic Allocation Party 2.1 Part a Consider the following code snippet: 1 int arr[] = {5, 6, 7, 8, 9}; 2 int * dawg = arr + 5; Which of the following statements could we execute immediately before the statement cout << * dawg; to avoid undefined behavior? (Choose zero or more options.) 1. dawg += 0; 2. dawg--; 3. dawg++; 4. dawg += -3; 5. dawg = (dawg - * arr) + 1; 6. dawg -= * (arr + 1); Answer: Options 2, 4, and 5 work. 2.2 Part b The incomplete program below allocates and de-allocates several variables dynamically. For each line containing a // TODO comment, write a line of code to make the program work without memory leaks. 1 int main() 2 { 3 const int DIM = 3; 4 int arr[3] = {1, 2, 3}; 5 int * single; 6 int ** doub; 7 int ** doub2; 8 9 single = new int ; 10 * single = 5; 11 12 doub = new int * [DIM]; // TODO 13 for ( int i = 0; i < DIM; i++) 14 { 15 doub[i] = new int [DIM]; // TODO 16 for ( int j = 0; j < DIM; j++) 17 doub[i][j] = 5; 18 } 19 20 doub2 = new int * [DIM]; // TODO 21 for ( int i = 0; i < DIM; i++) 22 { 23 doub2[i] = new int ; 24 * (doub2[i]) = 5; 25 } 26 27 cout << * single << endl; // Prints 5 28 cout << doub[1][2] << endl; // Prints 5 29 cout << * (doub2[2]) << endl; // Prints 5 30 31 // De-allocate single 32 delete single; // TODO 33 34 // De-allocate doub 35 for ( int i = 0; i < DIM; i++) 36 delete [] doub[i]; // TODO 37 delete [] doub; // TODO 38 39 // De-allocate doub2 40 for ( int i = 0; i < DIM; i++) 41 delete doub2[i]; // TODO 42 delete [] doub2;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}