Prepare for your exams

Study with the several resources on Docsity

Earn points to download

Earn points by helping other students or get them with a premium plan

Guidelines and tips

Prepare for your exams

Study with the several resources on Docsity

Earn points to download

Earn points by helping other students or get them with a premium plan

Community

Ask the community

Ask the community for help and clear up your study doubts

University Rankings

Discover the best universities in your country according to Docsity users

Free resources

Our save-the-student-ebooks!

Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors

Coding questions along with their answers. The questions cover various topics such as data types, control structures, vectors, enumerations, and complex numbers. The answers provide the expected output for each question and explain the logic behind the code. The document also includes questions related to recursion, constructors, and inheritance.

Typology: Exams

Pre 2010

1 / 13

Download Coding Questions and Answers and more Exams Electrical and Electronics Engineering in PDF only on Docsity! CS 102 / ECE 206 — Section 04 — Spring ‘09 Final Exam Name: KEY Instructions: 1. Spread out! There should be at least one empty seat between you and any of your neighbors. 2. Write clearly; if we cannot read your answer, it will be counted as incorrect. 3. Be complete and concise. Circle your answers if there might be any question as to what you want considered for grading. 4. Coding questions require only a code segment unless otherwise stated. 5. Coding questions will be graded on efficiency and style, as well as correctness. 6. NO CALCULATORS, NO CELL PHONES, NO HEADPHONES! Having a cell phone during the exam will result in a grade of zero for the exam. Please keep all electronics turned off and put away during the exam. 7. You must omit two questions; indicate them here: I am omitting questions _______ and _______. -page 1 of 13- 1. (4 points) What is displayed by the code segments below? a. int a = 33, b = 15, c = 40; if(a < b || a < c) { if(b <= c && a != b) a += b; else if(a == c) a += c; } else a = b + c; cout << a << endl; Ans: 48 b. a = 33, b = 15, c = 40; if(a == b || b < c) { if(a > c) a++; } else a--; cout << a << endl; Ans: 33 2. (4 points) What is the effect of the following statements? int x, a = 1, b = 2; x = a - b; a = a - x; b = a + x; assert (b == 0); It will generate an error message because the assertion "b == 0" is not true. -page 2 of 13- 9. (4 points) You have the following code fragment. What is printed with the indicated input values? cin >> x; switch(x) { case 40: x %= 6; case 60: x--; x--; break; case 80: x += 100; default: if(x) x = -x; } cout << x << endl; a) input: 40_________ Ans: 2 b) input: 80_________ Ans: -180 10. (4 points) Complete function read_vec(N) to read values into integer vector of size N and returns it. Stop when either the vector is full or you reach eof. You may assume valid data. vector<int> read_vec (const int N) { //Ans: vector<int> v(N); for(int i = 0; i < N && !cin.eof(); i++) cin >> v[i]; //end of ans. return v; } -page 5 of 13- 11. (4 points) Study the recursive function below. For each of the function calls listed, state what gets printed and tell how many times recur() is invoked, counting the original call. int recur(int x, int y) { if(y == 0) return x; else if(y <= 0) return recur(x + 1, y + 1); else return recur(x - 1, y – 1); } a. cout << recur(3, 0) << endl; times called: 1 output: 3 c. cout << recur(3, 2) << endl; times called: 3 output: 1 b. cout << recur(3, -1) << endl; times called: 2 output: 4 -page 6 of 13- 12. (4 points) Complete the following definition of a member function sub(), which subtracts two complex numbers. That is, X.sub(Y) should return the complex number obtained by subtracting the complex number Y from the complex number X. (Recall that the real part of the difference is the difference of the real parts, and the imaginary part of the difference is the difference of the imaginary parts.) class complex { public: double real, imag; complex sub (complex Y) { complex dif; //Ans: dif.real = real - Y.real; dif.imag = imag – Y.imag; //end of ans. return dif; } // end of sub }; 13. (4 points) Write a definition to overload the "<<" operator to print out a complex number (as defined in question #12) . If X is the real part and Y is the imaginary part, it should print it in the form "X + Yi" if Y is nonnegative, and as "X - Yi" if Y is negative (that is, if X = 2 and Y = –1 it should print "2 - 1i". Treat the operator as a standalone function. Ans: ostream& operator << (ostream& os, complex Z) { if (Z.imag > 0) os << Z.real << " + " << Z.imag << "i"; else os << Z.real << " - " << -Z.imag << "i"; end; // It's OK to do endl on the outputs -page 7 of 13- 20. (4 points) Given the following definitions, write code to give Joe a 20% raise and his supervisor a 10% raise: class employee { public: string name; double salary; employee* supervisor; }; employee* Joe; /* omitted code to initialize the employee records, including Joe's */ // put your code here: Ans: Joe->salary *= 1.2; Joe->supervisor->salary *= 1.1; 21. (4 points) Define a subclass of employee (see question #20) called manager that has an additional public member variable, subordinates, which is a vector of pointers to employee records. Ans: class manager : public employee { public: vector<employee*> subordinates; } 22. (4 points) State the Information Hiding Principle. The implementer should have all the information needed to implement a module correctly, and nothing more. The user of a module should have all the information needed to use a module correctly, and nothing more. (To get complete credit, you need to mention both the implementer and the user (or client) and "nothing more" [the hiding part].) -page 10 of 13- 23. (4 points) Complete the function alike(), which returns true if two integer vectors are alike, and false otherwise. The vectors are considered different if they have different lengths. Declare any variables that you need. bool alike(const vector<int>& A, const vector<int>& B) { //Ans: bool same = true; // assume vectors are alike if (A.size() != B.size()) same = false; for(int i = 0; i < A.size() && same; i++) if(A[i] != B[i]) same = false; return same; //end ans. } 24. (4 points) Define briefly: (a) Precondition A precondition is a condition that is expected to be true at the beginning of a function or other code segment. If (due to an error) the precondition is not true, then the function or code segment may not function correctly. (b) Postcondition A postcondition is a condition that is supposed to be true at the end of a function or other code segment. Typically, the function or code segment is supposed to make that condition true, but it might not be made true if the function's or segment's preconditions were not satisfied. (c) Invariant An invariant is something that is supposed to be true all the time (except for short segments of code when variables are being updated). It might be a relation that holds among the member variables of a class, or a proposition that is true every time through a loop. -page 11 of 13- 25. (4 points) Fill in the new array values after the code segment is executed. Leave no blank array elements. const int N = 8; int i, A[N] = {0, 11, 22, 33, 44, 55, 66, 77}; for (i = 0; i < N-1; i++) // notice: N-1 A[i] = A[i+1]; _________________________________________ | | | | | | | | | original A: | 00 | 11 | 22 | 33 | 44 | 55 | 66 | 77 | |---------------------------------------| 0 1 2 3 4 5 6 7 _________________________________________ | | | | | | | | | Ans: new A: | 11 | 22 | 33 | 44 | 55 | 66 | 77 | 77 | |---------------------------------------| 0 1 2 3 4 5 6 7 26. (4 points) Carefully study the nested loops. What is displayed? (Question continues on following page.) a. x = y = 0; for(i = 3000; i > 1000; i--) { x++; for(j = 50; j < 50; j++) y++; } printf("Result: %d %d %d %d\n", i, j, x, y); Ans: Result: 1000 50 2000 0 -page 12 of 13-