Name:_____________________

 

SID (Last 4):_____________________

 

Login: _____________________

 

 

 

 

CS 10 - Fall 2004

Final

 

 

Be sure to read each problem carefully and follow the directions.  Points may be taken off if you do not follow the directions.  For example, if the problem asks you to write code or a statement, do not write an entire program.  Please feel free to ask if you have any questions.  Illegible answers will not be graded.

 

 

You will not be graded on commenting.  You do not need to write any comments.  If, however, you do something strange that you think may be hard for the grader to figure out what you are doing, you may want to comment that part.  Partial credit will be given.  You will also not be graded on error checking unless specifically asked to do so.

 

 

Please work wisely.  Do not spend too much time on any one problem.

 

 

You will be required to show your student ID when you hand in your exam.

 

 

Problem 1 - 50

65

 

Problem 51

5

 

Problem 52

15

 

Problem 53

15

 


TOTAL


100

 

 

 

 

TOTAL POINTS = 100
Mark  a  for TRUE and mark  b  for FALSE. (1.3 pts each)

 

  1. The body of a for loop will always be executed at least once.

FALSE

  1. Primary storage is volatile, will be lost when the power is turned off.

TRUE

  1. The word cout is a reserved word.

FALSE

  1. The identifier X-ray is a valid identifier.

FALSE

  1. If the user is asked to enter an integer and 2 integers are entered, the fail bit will be set when the input is read.

FALSE

  1. If the user is asked to enter an integer and a double is entered, the fail bit will be set when the input is read.

FALSE

  1. If the user is asked to enter an integer and a string is entered, the fail bit will be set when the input is read.

TRUE

  1. A single invocation (call) of a function may return multiple values by including multiple return statements as long as all values returned are the same type.

FALSE

  1. A vector cannot be the return type of a function.

FALSE

  1. A function cannot be defined within another function.

TRUE

  1. If you try to change the value of a parameter passed-by-value within a function, you will get a syntax error.

FALSE

  1. A void function cannot have a return statement.

FALSE

  1. Any function can be invoked (called) within another function.

TRUE

 

Multiple Choice.  (1.3 pts each) 

 

  1. Storing an integer into a double is an example of a
    1. syntax error
    2. semantic error
    3. logic error
    4. no error

 

 

  1. The ____________ manages the resources, such as time and memory, of the computer.

a.        ALU

b.       Input Unit

c.        Memory Unit

d.       Operating System

e.        Secondary Storage Unit

 

 

  1. C++ is an example of

a.        High-level language

b.       Assembly language

c.        Machine language

 

  1. The following code has what kind of error?

Point (10, 0);

move(-2.5, -3);

 

    1. syntax
    2. logic
    3. no error

 

  1. The following code has what kind of error?

Point p1(14, 22);

Point p2 = p1;

Point p3(p1.get_x(), p2.get_y());

 

    1. syntax
    2. logic
    3. no error

 

  1. The following code has what kind of error?

Point dot(rand() % 21 – 10, 0);

 

    1. syntax
    2. logic
    3. no error

 

  1. The following code has what kind of error?

const int A = 5;

A = 10;

 

    1. syntax
    2. logic
    3. no error

 

  1. The following code has what kind of error?

int b;

cout << b;

 

    1. syntax
    2. logic
    3. no error

 

  1. The following code has what kind of error?

int a, b;

cout << “Enter 2 numbers:”;

cin >> a, b;

 

    1. syntax
    2. logic
    3. no error

 

  1. The following code has what kind of error?

string s = Hello;

s = s + s;

 

    1. syntax
    2. logic
    3. no error
  1. Given the following variables, what is the value of the given condition?

int i = 3;

int j = 13;

int k = 30;

 

(i != j) && (((i < 5) || (j < 10)) && (k >= 30))

 

a.        true

b.       false

c.        invalid condition

 

  1. Given the following variables, what is the value of the given condition?

int a = 1;

int b = 0;

bool test1 = true;

bool test2 = false;

 

!test1 || !(a > b && test2)

 

a.        true

b.       false

c.        invalid condition

 

  1. Which of the following expressions is equivalent to the expression?

!(a == b || c <= d)

 

    1. a != b || c >= d
    2. a != b && c >= d
    3. a != b || c > d
    4. a != b && c > d

 

 

  1. What is the output of the following code?

int x = 1;

int a = 3;

 

if (x < 0)

     a++;

else if (x == 1)

     a++;

else if (x > 0)

     a++;

cout << a;

 

a.        3

b.       4

c.        5

d.       6

e.        None of the above

 

 

 

 

 

 

 

  1. What is the output of the following code?

int x = 1;

int a = 3;

 

if (x < 0)

     a++;

if (x == 1)

     a++;

if (x > 0)

     a++;

cout << a;

 

a.        3

b.       4

c.        5

d.       6

e.        None of the above

 

  1. How would the following equation be translated into C++ code?

 

 

 

 


    1. 2/3*a-1+b-1/4
    2. (2/3)*(a-1)+(b-1/4)
    3. (2/3*(a-1))+(b-1)/4
    4. None of the above

 

  1. What is the result of the following expression, given that a is a double with the value of 4.2.

a % 2

 

    1. 0
    2. 0.2
    3. 2.1
    4. 4.2
    5. None of the above

 

  1. What is the result of the following expression, given that all variables are integers and a is 2, b is 4, c is 5, and d is 1.

a + b * c – d

 

    1. 21
    2. 24
    3. 29
    4. 39
    5. None of the above

 

  1. What is the result of the following expression, given that a is an integer with value of 13, b is an integer with value 3, and c is a double with value 2.1.

a % b * c

 

    1. 2.1
    2. 3.1
    3. 6.33
    4. 8.4
    5. None of the above
  1. What does the following code output?

int x = 0;

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

{

   x = x + i;

   cout << x << “ “;

}

 

    1. 0  2 4 6 8
    2. 2 4 6 8 10 12
    3. 0 2 6 12 20
    4. 2 4 6
    5. 0 2 4 6

 

 

  1. What does the following code output?

int x = 1;

while ( x < 5 )

{

   x++;

}

cout << x;

 

    1. 1 2 3 4 5
    2. 2 3 4 5 6
    3. 4
    4. 5
    5. 6

 

  1. What does the following code output?

double x = 10.0;

int count = 0;

do

{

   x = x / 2.0;

   count++;

} while (x > 2.0);

cout << count;

 

    1. 1.25
    2. 2.5
    3. 3
    4. 2
    5. 1

 

 

 

 

 

 

 

 

 

 

 

 

  1. Given the function f declared below and the int variables w, x, y, and z that have been declared and initialized, which of the following invocations (calls) of f is legal?

 

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

 

    1. f(x, y, z);
    2. w = f(x, y, z);
    3. f(10, 20, 30);
    4. f(x, 20, 30);
    5. A and D
    6. All of the above

 

 

Use the following code for the next 2 questions. Read both questions before tracing the code.

 

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

{

      int x = 0;

     

      for (int j = 0; j < 3; j++)

      {

             if ( (i * j) % 2 == 0 )

            {

                   x = x + (i * j);

                   cout << x;

            }  

      }

}

 

  1. What is the output?

 

    1. 0 0 0 0 2 0 2 6 12
    2. 0 0 0 0 2 0 2 6
    3. 0 0 0 0 2 2 4 8 14
    4. 22