Practice Final Exam with Answers - Electrical Engineering Computations | ECE 206, Exams of Electrical and Electronics Engineering

Material Type: Exam; Class: Electrical Engr Computations; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-aju
koofers-user-aju ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 102 / ECE 206 โ€” Section 04 โ€” Spring โ€˜09
Practice Final Exam
Name: KEY
Instructions:
1. Spread out! There should be at least one 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.
Note! I suggest that you try working the practice problems before looking at
this key!
-page 1 of 17-
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Practice Final Exam with Answers - Electrical Engineering Computations | ECE 206 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

CS 102 / ECE 206 โ€” Section 04 โ€” Spring โ€˜

Practice Final Exam

Name: KEY

Instructions:

  1. Spread out! There should be at least one 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.

Note! I suggest that you try working the practice problems before looking at

this key!

Who can see member variables and functions that are declared: (a) private Private members are visible within the class in which they are declared, but nowhere else. (b) protected Protected members are visible within the class in which they are declared and in its subclasses (derived classes). (c) public Public members are visible to everywhere.

2. (4 points) What is the effect of the following statements? int x = 1/2; assert (x > 0); It will generate an error message because the assertion "x > 0" is not true. 3. (4 points) You have the following code fragment. What is printed with the indicated input values? cin >> x; switch(x) { case 20: x *= 4; break; case 40: x--; x--; break; case 60: x = -5; case 80: if(x != 0) x = 100; break; default: x++; } cout << x << endl; a) input: 20 _ 80 ______ b) input: 60 _ 100 _____

// prototypes for constructors Circle(); Circle(double r); private: double radius; // radius of circle in feet }; #include #include "Circle.h" using namespace std; int main() { Circle circle_1; // invokes the default constructor Circle circle_2 (5.123); // invokes constructor with an arg. // code for main() would be here } // a. *** Implement default constructor that sets radius to 0 Ans: Circle() { radius = 0; } / b. *** Implement constructor that sets radius to the argument if the argument is >= to 0, and sets it to 0 otherwise./** Ans: Circle(double r) { if(r >= 0) radius = r; else radius = 0; }

Explain what is wrong with the following code segment? int* X = new int; *X = 1; delete X; *X = 0; It assigns to a memory location that has been deleted (freed for reuse).

7. (4 points) Complete function read_it() to read values into integer vector v. Stop when either the vector is full or you reach eof. You may assume valid data. Ans: vector read_it(vector & v) { int i; for(i = 0; i < v.size() && !cin.eof(); i++) cin >> v[i]; return v; } 8. (4 points) Rewrite this recursive function using iteration. int factorial(int n) { if (n == 0) return 1; return n * factorial(n-1); } Ans: int factorial(int n) { int fact = 1; while(n != 0) { fact *= n; n--; } return fact; }

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(5, 0) << endl; times called: 1 output: 5 b. cout << recur(5, -1) << endl; times called: 2 output: 4 c. cout << recur(5, 1) << endl; times called: 2 output: 6

What is the output from the following code segment? int x = 628; cout << x / 10 % 10 << x % 10 << x / 100 << endl; Ans: 286

12. (4 points) What is printed from the code segment below? enum Color_Type {green, red, blue, black, clear}; Color_Type pen, pencil; pen = clear; // invisible ink pencil = red; if(pen <= black) cout << "stupid spy" << endl; else cout << "spot on" << endl; if(pencil >= 2) cout << "good choice" << endl; else cout << "not so much" << endl; Ans: spot on not so much

Complete the following function, which sorts the vector V in ascending order. Your sorting algorithm does not have to be efficient. Make sure to declare any variables that you need. Hint : Write an outer loop, with i = 0 to V.size(), and on each iteration use findLowest to find the index of the smallest number from i to the end of the vector. Swap them so that at the end of each iteration the first i elements of V are sorted. Ans: int findLowest (const vector& V, int start) { //start looking at index start int lowest = start; // smallest found so far for (int i = start+1; i < V.size(); i++) if (V[i] < V[lowest]) lowest = i; return lowest; } void sort (vector& V) { for (int i=0; i < V.size(); i++) { int lowest = findLowest (V, i); //lowest of remainder if (lowest != i) { // swap V[i] and V[lowest] double temp = V[i]; V[i] = V[lowest]; V[lowest] = temp; } // now V[i] is the lowest from i to end } }

What does the word virtual mean in front of a function declaration? It means that that function can be redefined in subclasses.

16. (4 points) What (not how) does the following code do? Hint: Trace with input 123. int x, y; for(y = 0, cin >> x; // expr x != 0; // expr y = x % 10 + y * 10, x /= 10); // expr3, null body cout << endl << y << endl; It reverses the digits of x_._ 17. (4 points) Given the following structure definitions, write an output command to print the rank of the 27th^ card in deck. struct Card { int suit, rank; }; struct Deck { vector cards (52); }; Deck deck; Ans: cout << deck.cards[26].rank << endl; // endl is optional

Define a subclass of employee (see question # 19 ) called manager that has an additional public member variable, subordinates, which a vector of pointers to employee records. Ans: class manager : public employee { public: vector<employee*> subordinates; }

21. (4 points) Why are private member variables and member functions important for program maintenance? Private member variables and member functions are hidden from users of the class. Therefore, they can be changed during program maintenance without affecting code that uses the class. This allows modules (classes) to be maintained independently. 22. (4 points) Complete the following definition of a member function add(), which adds two complex numbers. That is, X.add(Y) should return the sum of the complex numbers X and Y. (Recall that the real part of the sum is the sum of the real parts, and the imaginary part of the sum is the sum of the imaginary parts.) class complex { public: double real, imag; complex add (complex Y) { complex sum; //Ans: sum.real = real + Y.real; sum.imag = imag + Y.imag; //end of ans. return sum; } // end of add };

Write a definition to overload the "+" operator to add two complex numbers and return their sum. (You may assume that you have the add function that you defined in question # 22. Treat the operator as a standalone function.) Ans: complex operator + (complex X, complex Y) { return X.add(Y); end;

24. (4 points) What do you use the "::" operator for? It qualifies a name by indicating the class to which it belongs. 25. (4 points) Define an enumeration type Month for (abbreviated) names of the months (JAN, FEB, etc.), but define it in such a way that JAN has the numerical value 1. Ans: enum Month {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

Carefully study the nested loops. What is displayed? a. x = y = 0; for(i = 200; i > 100; i--) { x++; for(j = 30; j < 30; j++) y++; } printf("Result: %d %d %d %d\n", i, j, x, y); Ans: Result: 100 30 100 0 b. x = y = j = 0; // reset variables to 0 for(i = 30; i < 30; i++) { x++; for(j = 200; j > 100; j--) y++; } printf("Result: %d %d %d %d\n", i, j, x, y); Ans: Result: 30 0 0 0

29. (4 points) What does inheritance refer to in the context of object-oriented programming? It refers to the fact that a subclass (derived class) can automatically acquire member functions and member variables from the base class from which it is derived.

What is displayed by the code segment below if the input is 1234? Be careful! int i, power, n, x; i = x = 0; power = 1; cin >> n; // assume a non-negative value for n while (n > 0) { x += n % 10 * power; power *= 10; n /= 10; } cout << x << endl; Ans: 1234

Note: The actual Final Exam will be a little shorter than this practice exam.

A further note: Make sure you understand your mistakes on Exams I and II and what the

correct answers should be!