final_practice_no_ans

final_practice_no_ans - CS31 Introduction to Computer...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
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.1.2 Part b Rewrite reverse() using a do-while loop: 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
1.1.3 Part c Rewrite reverse() using pointer arithmetic (i.e. the new function must not have any square brackets). 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
Background image of page 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 undeFned 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: 2.2 Part b The incomplete program below allocates and de-allocates several variables dynamically. ±or 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 // TODO 13 for ( int i = 0; i < DIM; i++) 14 { 15 // TODO 16 for ( int j = 0; j < DIM; j++) 17 doub[i][j] = 5; 18 } 19 20 // 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 // TODO 33 34 // De-allocate doub 35 for ( int i = 0; i < DIM; i++) 36 // TODO 37 // TODO 38 39 // De-allocate doub2 40 for ( int i = 0; i < DIM; i++) 41 // TODO 42 // TODO 43 } 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
3 Mystery Code 3.1 Part (a) Consider the code below: 1 class Clazz 2{ 3 int x; 4 }; 5 6 struct Strukt9 7{ 8 int x; 9 }; 10 11 int main() 12 { 13 Clazz c; 14 Strukt9 s; 15 c.x = 5; 16 s.x = 5; 17 cout << c.x << ’ ’ << s.x << endl; 18 } What is wrong with the above code? (Choose a single answer) 1. Nothing, it prints ”5 5”
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 8

final_practice_no_ans - CS31 Introduction to Computer...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online