Name:_____________________

 

SID (Last 4):_____________________

 

Login: _____________________

 

 

 

 

 

CS 10 - Spring 2005

Midterm

Friday, April 29

 

 

 

 

 

 

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.

 

 

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.

 

 

 

 

 

 

 

 

TOTAL POINTS = 100
Bubble in the answers to questions 1 – 17 using your personalized answer sheet.

 

Mark  a  for TRUE and mark  b  for FALSE. (2 pts each)

 

  1. An operating system like Windows or Linux is software. 

TRUE

  1. A constant MUST be initialized when it is declared.

TRUE

  1. RAM is a primary storage device that loses all data when the power is turned off.

TRUE

  1. A program will not compile if there is a syntax error.

TRUE

  1. It is a good idea to write an algorithm in syntactically correct C++ code so that you don’t waste time translating the algorithm to C++ when the algorithm is completed.

FALSE

  1. If you want to declare variables of type double, you must include the iostream library.

FALSE

  1. C++ is case-sensitive.

TRUE

 

Multiple Choice.  (2 pts each) 

 

  1. Which of the steps in the following algorithm should be rewritten or clarified due to ambiguity?

 

    1. Ask the user to type 2 integers separated by a space and then hit enter
    2. Declare some variables
    3. Input each number input from standard input into its own int variable
    4. Calculate the average by adding the 2 variables together and dividing by 2.0
    5. Output the calculated average to standard output including any fractional value if any
    6. There are no ambiguous steps in this algorithm.

 

  1. Which of the following statements based on the previous algorithm has a syntax error?

 

a.       cout << “Enter 2 integers separated by a space and then hit Enter\n”;

b.       int num1;

c.       int num2;

d.       cin >> num1 >> num2;

e.       cout << “Average: “ << (num1 + num2 / 2.0);

f.        There are no syntax errors.

 

  1. Which of the statements in Question 9 based on an unambiguous version of the algorithm in Question 8 has a logic error?

 

a.       a

b.       b

c.       c

d.       d

e.       e

f.        There are no logic errors.

 


  1. Which of the following is NOT an expression given that n and m are int variables and p is a Point variable?

 

a.       n != m

b.       (n * m / 2 < 10) && (n % 2 != 0)

c.       n * 2

d.       n

e.       p.get_x()

f.        p.move(n,m)

 

  1. What will be output by the following statements?

 

double x = 10.333;

cout << fixed << setprecision(1) << x << “  “;

x = x * 2;

cout << setprecision(3) << x;

 

a.       10.3 20.6

b.       10.3 20.600

c.       10.3 20.666

d.       10.333 20.666

e.       10.3 20.7

f.        none of the above

 

  1. What is the value of the following expression if a has the value 4 and b has the 5?

 

a + b % 3

 

a.       0

b.       6

c.       true

d.       false

e.       5

f.        none of the above

 

 

 

 

 


Use the following 6 items to answer the next 4 questions. Choose the BEST choice of these 6 possibilities. It is possible that one of these answers may be used by more than one question.

 

    1. A member function
    2. Pseudocode
    3. An object
    4. An object variable
    5. A class
    6. A library

 

 

  1. What is a programmer-defined data type called?

e.

 

  1. Which of the above is applied to an object using the dot notation.

a.

 

  1. The following is an example of __________c.___________.

 

Circle(Point(5,3), 2.5)

 

 

  1. The following is an example of _________d.____________.

 

Time now;

 

 


What is the exact output of the following 3 code fragments? (6 pts each)

 

  1. int temp = 50;

if (temp < 70)

{

                cout << “Too Cold\n”;

               

}

else if (temp > 90)

{

                cout << “Too Hot\n”;

}

else

{

                cout << “Right On\n”;

}

 

Too Cold

 

  1. int temp = 50;

if (temp < 70)

{

                cout << “Too Cold\n”;

               

}

if (temp > 90)

{

                cout << “Too Hot\n”;

}

else

{

                cout << “Right On\n”;

}

 

Too Cold

Right On

 

  1. int y = 8;

if (y < 10)

{

                cout << y;

                y = y * 2;

                cout << setw(y) << y << “ “;

}

cout << y;

 

 

8               16 16

 

 


  1. (18 pts) Write CODE, not a program, to output the smaller of 2 string values stored in the variables str1 and str2. These variables have already been declared and initialized. If the string variables hold the same number of characters, then output both strings values, each on a separate line.

 

 

 

if (str1.length() < str2.length())

{

                cout << str1;

}

else if (str2.length() < str1.length())

{

                cout << str2;

}

else

{

                cout << str1 << endl << str2;

}

 

 

 


  1. (30 pts) Write a PROGRAM that asks the user to enter 2 positive integer values. If either of the values are negative or 0, output “Invalid Input” and nothing else. If they are both positive, output “Both even” if they are both even numbers, “Both odd” if they are both odd numbers, or “One even and one odd” if one is even and the other is odd.

 

 

 

 

#include <iostream>

 

 using namespace std;

 

int main()

{

                int num1, num2;

 

                cout << “Enter 2 positive integers separated by a space: “;

                cin >> num1 >> num2;

 

                if (num1 <= 0 || num2 <= 0)

                {

                                cout << “Invalid Input\n”;

                }

                else if (num1 % 2 == 0 && num2 % 2 == 0)

                {

                                cout << “Both even\n”;

                }

                else if (num1 % 2 == 1 && num2 % 2 == 1)

                {

                                cout << “Both odd\n”;

                }

                else

                {

                                cout << “One even and one odd\n”;

                }

 

                return 0;

}