






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Student ID: ___________________ Lab Section: _______________
This test is closed book, closed notes. Points for each question are shown inside [ ] brackets at the beginning of each question. You should assume that, for all quoted program segments, the appropriate header files are included and the standard namespace is used.
Remember to write out and sign the pledge at the end of the test.
Circle all of the following which would make sense as the function prototype.
void double(int &value);
int twice(int value);
void int by_two(int &value);
int value();
void double_val(int &value);
int test( int x) { if (x > 3) return 1; else return 0; }
int main() { int y = 6; while (test(y + 1) == 1) { cout << y << " "; y = y - 1; } cout << endl; return 0; }
What is the output of the following program fragment?
int curse( int n) { if (n <= 0) { return 1; } else { return n * curse(n-2); } }
cout << curse(5) << endl;
void func();
int i = 2; int j = 3; int k = 4;
int m = 5; int main() { cout << i + j << " " << k << " " << m << endl; func(); cout << i + j << " " << k << " " << m << endl; return 0; }
void func() { int m = i + j; j = i + m; k = m * m; }
void one( int &x) { x = x + 1; }
void two( int x) { x = x + 2; }
int main() { int x = 3; one(x); cout << x << " "; two(x); cout << x << endl; }
show the output produced when the code is executed. If the code segment will not compile, briefly
state the reason for this.
Here is the definition of a class of rectangular objects - the class Rect (don’t confuse this class with class RectangleShape from the textbook):
class Rect { public : Rect(); Rect( int j, int k); int get_l(); int get_w(); int area(); private : int length; int width;
};
// now the definitions of the member functions
Rect::Rect() { length = -234; width = 0; }
Rect::Rect( int j, int k) { length = j; width = k; }
int Rect::get_l() { return length; }
int Rect::get_w() { return width; }
int Rect::area() { return (length * width); }
The definitions in the following header file, named rational.h,are in effect for the remaining questions #ifndef RATIONAL_H #define RATIONAL_H class Rational { public : // member functions and operator Rational(); Rational( const int numer, const int denom); protected : int GetNumerator() const ; int GetDenominator() const ; void SetNumerator( const int numer); void SetDenominator( const int denom); private : int Numerator; int Denominator; }; Rational operator +( const Rational &r, const Rational &s); Rational operator *( const Rational &r, const Rational &s); bool operator !=( const Rational &r, const Rational &s); ostream& operator <<(ostream &sout, const Rational &r); istream& operator >>(istream &sin, Rational &r); #endif
(a) The functions will not modify their actual parameters. (b) The functions will return a constant value. (c) The functions will not modify the invoking object. (d) None of the above.
(a) The functions will not modify their actual parameters. (b) The functions will return a constant value. (c) The functions will not modify the invoking object. (d) None of the above.
(Note that 3/6 is equivalent to 1/2)
(a) the parameter is a reference (&) parameter. Why do we pass non-fundamental objects by reference instead of by value?
Your program will be graded based on its correctness and style. Do not include comments in your program.
remember to write out and sign the pledge at the bottom of the next page.
Write your code here: #include