A+ Work (NO WORK SHOWN ONLY CORRECT ANSWERS)





CS2A Final Exam

 

For problems 1 and 2 consider the following program:

#include <iostream>

using namespace std;

void test(int &a, int &b, int c);

int main()

{

    int a = 9, b = 8, c = 7;

    test(c, a, b);

    cout << a << " " << b << " " << c << endl;

    

}

 

void test(int &a, int &b, int c) 

{

    cout << a << " " << b << " " << c << endl;

    a = 91;   b = 92;    c = a + b;

}

1. What is the first line of output when this program  is run?

 a. 9 8 7

 b. 7 9 8

 c. 8 9 7

 d. 7 8 9

 e. None of the above.

2. What is the second line of output when this program is run?

 a. 91 92 183 

 b. 91 92 7 

 c. 92 8 92

 d. 92 183 91

 e. None of the above.

 

For problems 3 and 4 consider the following program:

#include <iostream>

using namespace std;

void test(int &a, int &b, int c);

int main()

{

    int a = 9, b = 8, c = 7;

    test(c, c, c);

    cout << a << " " << b << " " << c << endl;

    

}

void test(int &a, int &b, int c)

{

    a = 91;   b = 92;    c = a + b;

    cout << a << " " << b << " " << c << endl;

}

3. What is the first line of output when this program is run?

 a. 91 92 183

 b. 91 92 184

 c. 91 91 182

 d. 92 92 184

 e. None of the above.

4. What is the second line of output when this program is run?

 a. 9 8 92

 b. 9 8 91

 c. 9 92 7

 d. 9 92 183 

 e. None of the above. 

 

5. What is the output of the following program? (In the choices given below, "?" is used to indicate that the output is unknown.)

#include <iostream>

using namespace std;

void test();

int d;

int main()

{

    d = 6;

    cout << d << " ";

    test();

    cout << d << " ";

}

void test()

{

    cout << d << " ";

    d = 97;

    cout << d << " ";

}

 a. 6 ? 97 6

 b. 6 97 97 6

 c. 6 6 97 97

 d. 6 6 97 6

 e. None of the above.

6. Given the function prototype

 

    void Fix(int&, double); 

which of the following is an appropriate function call? (someInt is of type int, and somedouble is of type double.)

 a. Fix(24, 6.85);

 b. somedouble = 0.3 * Fix(someInt, 6.85);

 c. Fix(someInt + 5, somedouble);

 d. a and c above

 e. None of the above.

 

7. Given the function definition

void Twist(int  a, int& b)

{

    int c;

    c = a + 2;

    a = a * 3;

    b = c + a;

}

What is the output of the following code fragment that invokes Twist? (All variables are of type int.)

    r = 1;

    s = 2;

    t = 3;

    Twist(t, s);

    cout << r << ' ' << s << ' ' << t << endl;

 a. 1 14 3

 b. 1 10 3

 c. 5 14 3

 d. 1 14 9

 e. None of the above.

8. Consider the function definition

    void Demo(int intVal, double& doubleVal)

    {

        intVal = intVal * 2;

        doubleVal = double(intVal) + 3.5;

    } 

 

Suppose that the caller has variables myInt and mydouble whose values are 20 and 4.8, respectively. What are the values of myInt and mydouble after return from the following function call?

    Demo(myInt, mydouble);

 a. myInt = 20 and mydouble = 43.5

 b. myInt = 40 and mydouble = 4.8

 c. myInt = 20 and mydouble = 4.8

 d. myInt = 40 and mydouble = 43.5

 e. None of the above.

 

9. Consider the function definition

    void Demo(int&  intVal, double doubleVal)

    {

        intVal = intVal * 2;

        doubleVal = double(intVal) + 3.5;

    }

Suppose that the caller has variables myInt and mydouble whose values are 20 and 4.8, respectively. What are the values of myInt and mydouble after return from the following function call?

    Demo(myInt, mydouble);

 a. myInt = 20 and mydouble = 43.5

 b. myInt = 40 and mydouble = 4.8

 c. myInt = 20 and mydouble = 4.8

 d. myInt = 40 and mydouble = 43.5

 e. None of the above.

10. Consider the function definition

    void DoThis(int& alpha, int  beta)

    {

        int temp;

        alpha = alpha + 100;

        temp = beta;

        beta = 999;

    }

Suppose that the caller has integer variables gamma and delta whose values are 10 and 20, respectively. What are the values of gamma and delta

after return from the following function call? 

    DoThis(gamma, delta);

 a. gamma = 10 and delta = 20

 b. gamma = 110 and delta = 20

 c. gamma = 10 and delta = 999

 d. gamma = 110 and delta = 999

 e. None of the above.

 

11. What is the output of the following program?

    #include <iostream>

    using namespace std;

    void Try(int&, int);

    int x;

    int y;

    int z;

    int main()

    {

        x = 1;

        y = 2;

        z = 3;

        Try(y, x);

        cout << x << ' ' << y << ' ' << z << endl;

    }

    void Try(int& a, int  b)

    {

        int x;

        x = a + 2;

        a = a * 3;

        b = x + a;

    }

 a. 10 6 3

 b. 10 2 3

 c. 1 2 3

 d. 1 6 3

 e. None of the above.

12. Given the function definition

 

    int Mystery(double someVal) 

    {

        if (someVal > 2.0)

            return 3 * int(someVal);

        else

            return 0;

    }

what is the value of the expression Mystery(4.2) ?

 a. 12

 b. 12.0

 c. 0

 d. 0.0

 e. Nothing--the function call is invalid. 

13. Given the function definition

    int Trans(int alpha, int beta)

    {

        if (alpha > beta)

            return alpha + 10;

        else

            return 2 * beta;

    }

what is printed by the following code?

    cout << Trans(5, Trans(9, 4)) << endl;

 a. 15

 b. 38

 c. 16

 d. 19

 e. 8



14. Given the function definition

    bool IsZip(double somedouble)

    {

        return (somedouble == 0.0);

    }

what is the value of the expression IsZip(2.4)?

 a. 0.0

 b. true 

 c. 2.4 

 d. false

 e. "somedouble == 0.0"

 

15. Given the function prototype

    bool IsGreater(int, int);

which of the following statements use valid (i.e. syntactically correct) calls to the IsGreater function? (The data types of the variables are suggested by their names.)

 a.  

someBoolean = IsGreater(someInt, 8);

 

 b. 

if (IsGreater(5, someInt)) 

        intCounter++;

 c. 

    while (IsGreater(inputInt, 23))

        cin >> inputInt;

 d. b and c above

 e. a, b, and c above

16. What happens if a value-returning function with the prototype double Average(int, int, int);

is called by using the following statement? (alpha and beta are int variables.)

    Average(alpha, 34, beta);

 a. The compiler issues a syntax error message.

 b. The function is executed, and the function value is discarded.

 c. The function is executed, and the function value is assigned to alpha.

 d. The function is not executed, and the program halts with a run time error

message. 

 e. None of the above. 

17. The function heading

    double TenToThePower(int n)

is for a function that returns 10.0 raised to any integer power. Which of the

following statements stores into somedouble the value 10.0 raised to the

power someInt?

 a. TenToThePower(somedouble, someInt);

 b. TenToThePower(someInt);

 c. TenToThePower(someInt) = somedouble;

 d. somedouble = TenToThePower(someInt);

 e. someInt = TenToThePower(somedouble); 

18. The function heading

    double TenToThePower(int n)

is for a function that returns 10.0 raised to any integer power. Which of the

following statements represents an appropriate use of the TenToThePower

function?

 a.  

somedouble = 4.96 * TenToThePower(8) + 2.5;

 

 b.  

TenToThePower(6);

 c. 

    if (TenToThePower(someInt) > somedouble)

        beta = 3;

 d. a and b above

 e. a and c above



19. Given the function prototype

    int Top(int, int);

which of the following statements contain valid calls to the Top function?

 a. someInt = 4 + Top(oneInt, anotherInt);

 b. cin >> Top(oneInt, anotherInt); 

 c. cout << Top(5, Top(3, 4)); 

 d. a and c above

 e. a, b, and c above

 

20. This question demonstrates the hazards of side effects. Given the

function definition

    int Power(int& base, int& exp)

    {

        int product = 1;

        while (exp >= 1)

        {

            product = product * base;

            exp--;

        }

        return product;

    }

what is the output of the following code? (All variables are of type int.)

  n = 2;

  pow = 3;

  result = Power(n, pow);

  cout << n << " to the power " << pow << " is " << result;

 a. 2 to the power 3 is 8

 b. 2 to the power 0 is 8

 c. 0 to the power 0 is 0

 d. 2 to the power 3 is 1

 e. None of the above.

 

 

21. Given the class declaration

 

    class MyClass {

        public:

            ...

            void Func(); 

        private: 

            int n;

    };

 

what notation does the body of Func use to assign the value 3 to the private data member n?

 a. n = 3;

 b. MyClass.n = 3; 

 c. int n = 3; 

 d. someObject.n = 3;

 e. It can't be done--n is private.

 

22. Given the class declaration

 

     class X {

        public:

            void F( int );

        private:

            int n;

    };

 

which line(s) of the following client code cause(s) a compile-time error?

 

    X alpha;        // Line 1

    alpha.F();      // Line 2

    alpha.n = 42;   // Line 3

 

 a. line 1

 b. line 2

 c. line 3

 d. lines 1 and 2

 e. lines 2 and 3 

 

23. Consider the class declaration

 

     class MyClass {

        public:

            ...

            MyClass();

                // Postcondition:

                //     Private data initialized to zero

            MyClass(int n);

                // Postcondition:

                //     Private data initialized to n

        private:

            int priv;

    };

 

and client code

 

    MyClass gamma(5); 

After gamma is created, what is the value of gamma.priv? 

 

 a. 0

 b. 5

 c. n

 d. Unknown, but the declaration of gamma is valid.

 e. The declaration of gamma is invalid. 

 

For problems 24 through 27, consider the following class declaration:

 

    class rectangle {

        public:

            // member function prototypes have been omitted

        private:

            int length;

            int width;

    };

 

24. Which of the following is the best function header for a member

function named doubleSize() that takes no arguments and returns a

rectangle object whose length and width are twice those of the calling

object. The function must not modify the calling object! 

 a. rectangle rectangle::doubleSize() 

 b. rectangle rectangle::doubleSize() const

 c. void rectangle::doubleSize()

 d. void rectangle::doubleSize() const

 e. None of the above. 

 

25. Which of the following is the correct body of the doubleSize() function

described in problem 4? 

 a. 

    length = length * 2; 

    width = width * 2; 

 b.  

    rectangle temp;

    temp.length = length * 2;

    temp.width = width * 2;

    return temp;

 

 c. 

    rectangle temp;

    length = length * 2;

    width = width * 2;

    return temp;

 

 d. 

    rectangle temp; 

    temp.length = temp.length * 2;

    temp.width * temp.width * 2; 

    return temp; 

 e. None of the above. 

 

26. Which of the following is the best function header for a member

function named hasSmallerAreaThan() that takes a single rectangle object

argument and returns true if the calling object has a smaller area than the

argument, false otherwise. The function must modify neither the argument

nor the calling object! 

 a. bool rectangle::hasSmallerAreaThan(const rectangle& r) const;

 b. bool rectangle::hasSmallerAreaThan(rectangle r) const; 

 c. bool rectangle::hasSmallerAreaThan(rectangle r); 

 d. bool rectangle::hasSmallerAreaThan(const rectangle& r); 

 e. None of the above.

 

 

27. Which of the following is the correct body of the hasSmallerAreaThan()

function described in problem 6? 

 a. if (length * width < r.length * r.width) return true; 

 b. return length < r.length && width < r.width; 

 c. return area1 < area2;

 d. return length * width < r.length * r.width;

 e. None of the above. 

 

28. Suppose that the class declaration of someclass includes the following

function prototype: 

    bool lessthan(someclass s);

 

Which of the following tests in the client code correctly compares two

class objects alpha and beta? 

 a. if (alpha < beta) 

 b. if (alpha.lessthan(beta))

 c. if (lessthan(alpha, beta))

 d. if (lessthan(beta))

 e. if (lessthan(alpha).beta) 

 

29. Given the declarations

 

 int status[10];

 int i;

 

which of the following loops correctly zeros out the status array?

 

 a. 

for (i = 0; i <= 10; i++) 

    status[i] = 0;

 

 b. 

for (i = 0; i < 10; i++) 

    status[i] = 0;

 

 c.  

for (i = 1; i <= 10; i++) 

    status[i] = 0;

 

 d.  

for (i = 1; i < 10; i++)

    status[i] = 0;

 

 e. 

for (i = 1; i <= 11; i++) 

    status[i] = 0;

 

 

30. After execution of the code fragment

 

 int arr[5];

 int i;

 

 for (i = 0; i < 5; i++) { 

     arr[i] = i + 2;

     if (i >= 3)

         arr[i-1] = arr[i] + 3;

 }

 

what is contained in arr[1]?

 

 a. 2 

 b. 3

 c. 7

 d. 8

 e. none of the above 

 

31. After execution of the code fragment

 

 int arr[5];

 int i;

 for (i = 0; i < 5; i++) {

     arr[i] = i + 2;

     if (i >= 3)

         arr[i-1] = arr[i] + 3;

 }

 

what is contained in arr[3]?

 

 a. 5 

 b. 3

 c. 8

 d. 9

 e. none of the above 

 

32. Given a 5000-element, one-dimensional array beta, which of the code 

fragments below could be used to print out the values of beta[0], beta[2],

beta[4], and so forth? (All variables are of type int.) 

 a.  

 for (i = 0; i < 5000; i = i + 2)

     cout << beta[i] << endl;

 

 b. 

 for (i = 0; i < 2500; i++)

     cout << beta[2*i] << endl;

 c. 

 for (i = 0; i < 2500; i++)

     cout << beta[i]*2 << endl;

 

 d. a and b above

 e. a, b, and c above 

 

33. Which of the following cannot be used to input values into a 3-element

int array named alpha? 

 

 

 a. 

 cin >> alpha[0] >> alpha[1] >> alpha[2]; 

 b.  

 cin >> alpha;

 c. 

 for (i = 0; i < 3; i++) 

     cin >> alpha[i];

 d. 

 cin >> alpha[0]; 

 cin >> alpha[1];

 cin >> alpha[2]; 

 e. Any of these could be used. 

 

34. Given the program fragment

 

 char alpha[200];

 char beta[200];

 ...

 Copy(alpha, beta);//Copy all components of beta into alpha

 

which of the following is the best function heading for the Copy function?

 

 a. void Copy(char arr1[], char arr2[] )

 b. void Copy(const char arr1[], char arr2[] )

 c. void Copy(char arr1[], const char arr2[] ) 

 d. void Copy(const char arr1[], const char arr2[] )