









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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
Material Type: Exam; Class: Electrical Engr Computations; Subject: Electrical And Computer Engr; University: University of Tennessee - Knoxville; Term: Unknown 1989;
Typology: Exams
1 / 17
This page cannot be seen from the preview
Don't miss anything!










CS 102 / ECE 206 โ Section 04 โ Spring โ
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
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
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
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
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