DOC PREVIEW
UCLA COMSCI 31 - final_practice_ans

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS31: Introduction to Computer Science IFinal Exam PracticeMay 30, 2011TA: Paul Wais ([email protected])1 Short Questions1.1 Loop ReviewConsider 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 aRewrite 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 bRewrite 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 do8{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 }11.1.3 Part cRewrite 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 CodeWhat is wrong with the code below?1 class C2{3 public:4 C();5 private:6 int x;7 int y;8 };910 C::C()11 : x = 0, y = 112 { }(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 C4. 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{}22 Pointer and Dynamic Allocation Party2.1 Part aConsider 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 bThe incomplete program below allocates and de-allocates several variables dynamically. For each linecontaining 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;89 single = new int;10*single = 5;1112 doub = new int*[DIM]; // TODO13 for (int i = 0; i < DIM; i++)14 {15 doub[i] = new int[DIM]; // TODO16 for (int j = 0; j < DIM; j++)17 doub[i][j] = 5;18 }1920 doub2 = new int*[DIM]; // TODO21 for (int i = 0; i < DIM; i++)22 {23 doub2[i] = new int;24*(doub2[i]) = 5;25 }2627 cout <<*single << endl; // Prints 528 cout << doub[1][2] << endl; // Prints 529 cout <<*(doub2[2]) << endl; // Prints 53031 // De-allocate single32 delete single; // TODO3334 // De-allocate doub35 for (int i = 0; i < DIM; i++)36 delete[] doub[i]; // TODO37 delete[] doub; // TODO3839 // De-allocate doub240 for (int i = 0; i < DIM; i++)41 delete doub2[i]; // TODO42 delete[] doub2; // TODO43 }33 Mystery Code3.1 Part (a)Consider the code below:1 class Clazz2{3 int x;4 };56 struct Strukt97{8 int x;9 };1011 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”2. Compile error because Strukt9 is an invalid struct name3. Compile error because Clazz is missing public and private keywords4. The statement s.x = 5; on line 16 causes a compile error5. The statement s.x = 5; on line 16 causes a runtime error6. The statement c.x = 5; on line 15 causes a compile error7. The statement c.x = 5; on line 15 causes a runtime errorAnswer: The statement c.x = 5; on line 15 causes a compile error43.2 Part (b)What does the code below print?1 void superIncrement(int w, int &x, int*y, int*&z)2{3 w++;4 x++;5 y++;6 z++;7(*y)++;8(*z)++;9}1011 void reverse1(string w)12 {13 for (int i = 0; i < w.size() / 2; i++)14 {15 char temp = w[i];16 w[i] = w[w.size() - 1 - i];17 w[w.size() - 1 - i] = temp;18 }19 }2021 void reverse2(char*w)22 {23 char*b = w;24 while (*b != ’\0’)25 b++;26 b--;27 while (w != b)28 {29 char t=*w;30*w=*b;31*b = t;32 w++;33 b--;34 }35 }3637 int main()38 {39 string s = "I am s";40 char c[] = "I am c!";4142 int*joey = new int[3];43 joey[0] = 5;44 joey[1] = 6;45 joey[2] = 7;4647 int*zero = joey;48 int*one = joey + 1;49 int*two = joey + 2;5051 superIncrement(joey[0], joey[0], joey, joey);52 reverse1(s);53 reverse2(c);5455 cout <<*zero << endl;56 cout <<*one << endl;57 cout <<*two << endl;58 cout <<*joey << endl;59 cout << s << endl;60 cout << c << endl;6162 delete[] zero;63 }Answer:162837485 I am s6 !c ma I54 A Program for Managing UCLA On-Campus HousingUCLA has hired a group of CS Majors to write prototype C++ software for managing on-campus housing.However, a group of Silicon Valley start-up companies hired the students before they graduated, so thesoftware is incomplete. Strangely, the system also tracks how many slices of pizza each resident has eatenwhile a student at UCLA. The data structures the CS majors designed for the system are below:1 struct Resident2{3 string name;4 int pizza_eaten;56 Resident(string name, int pizza_eaten)7 : name(name), pizza_eaten(pizza_eaten)8{}9 };1011 const int NUMFLOORS = 5;12 const int NUMROOMS = 10;13 struct Residence14 {15 string name;1617 // Represent a room as a pointer to the resident who lives there;18 // NULL pointer means empty room.19 Resident*residents[NUMFLOORS][NUMROOMS];2021 Residence()22 {23 // Set all residents to NULL to indicate empty rooms24 for (int f = 0; f < NUMFLOORS; f++)25 for (int r = 0; r < NUMROOMS; r++)26 residents[f][r] = NULL;27 }28 };Here is some test code that demonstrates how they expected to use the above structs:1 int main()2{3 Residence houses[3];45 houses[0].name = "Cedar";6 houses[0].residents[0][0] = new Resident("PizzaMonster92", 100000);7 houses[0].residents[2][3] = new Resident("Bert", 0);8 houses[0].residents[1][0] = new Resident("Phil", 3000);9 // All other rooms in Cedar are empty1011 houses[1].name = "Dogwood";12 houses[1].residents[2][3] = new Resident("Maria", 0);13 houses[1].residents[4][1] = new Resident("Song-Chun", 50);14 // All other rooms in Dogwood are empty1516 // The 0th room on the 0th floor of Dogwood should be empty17 assert(houses[1].residents[0][1] == NULL);1819 houses[1].name = "Weyburn";20 // All rooms in Weyburn are


View Full Document

UCLA COMSCI 31 - final_practice_ans

Download final_practice_ans
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view final_practice_ans and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view final_practice_ans 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?