Page 7 of 8

CS 240 Computer Science II Quiz #1 September 2, 2009

Name:______Score: ______/110 pts

1.  Assuming that all variables have been previously defined and that the statements are executed in the given order, show the output produced by the following statements if the input value is 9: (5 pts)

cout < endl < “Good job”;

cin > num;

sum = num + 5;

cout < endl < “num = “ < num < “ sum=” < sum;

num = 7;

sum = num + 5;

cout < endl < “num = “ < num < “ sum=” < sum;

2. A. Circle all invalid statements/comments in the following C++ program. (5 pts)

Line # Statements/Comments

1 // This is a sample program

2 // It reads integer values, performs some computations

3 and prints the results.

4 #include <iostream

using namespace std;

5 int main( )

6 {

7 int num1;

8 int num2;

9 int result1;

10 int result2;

11 cin > num1;

12 result1 = num1 + 5; /* compute the sum of the value read and 5 */

13 result2 = num2 + 5;

14 cout < endl < “result1 =”

15 < result1;

16 num1 = num1 + 10;

17 num2 + 7 = num1;

18 cout < num1;

19 < result1;

20 cout < 15;

21 cin > num2 + 3;

22 result3 = num1 + 25;

23 return (0);

24 }


3. Rewrite the following statements using only three declaration statements (with or without initial values): (3 pts)

int num1;

char letter;

double dnum1;

int num2;

char grade;

double dnum2;

num1 = 45;

letter = ‘P’;

dnum2 = -12.64;

grade = ‘B’;

4. Show the output after the execution of each of the following sequences of statements: (4 pts)

a) int n = 3 , m = 8, p; b) int j = 3, k = 9 , l;

m = m + n; l = k + j;

p = m + n ; k = l + 2;

cout < p; ______cout < k; ______

5. Show all steps in the evaluation of the following arithmetic expressions: (9 pts)

a. 6 + 4 * 5 b. 14 - 7 / 3 + ( 15 - 20 / 4)

c. 24 - 11 - 5 d. 2.5 + 13 / 4 - 3.5

e. 31 - (3 * ( 23 / 2 - 5) + 4) f. (23 % 5 + 5) + ( 34 / ( 12 - 5) * 2)

6. Show the trace (to the right) of the execution of the following statements: (5 pts)

1) int num1 = 15, num2, num3 = 5;

2) num2 = num1 * num3;

3) num1 = num1 + 5;

4) num3 = num3 * 2;

5) num2 = num1 / num3;

6) num1 = num1 - num2 + num3;

7. Assuming that variables ch1, ch2, ch3, and num are defined as follows: (3 pts)

char ch1 = ‘P’, ch2, ch3;

int num;

Specify the effect(s) of the execution of each of the following statements:

a. ch2 = ch1 +2 ; ______

b. ch3 = ch1 - 4; ______

c. num = ‘Q’ - ‘O’; ______

8. Assuming that the variables are defined and initialized as follows: (7 pts)

int num = 15;

double dnum = 3.20;

Show the output produced by the following statements. Use the underscore ( _ ) character to indicate the cursor position, and the vertical bar ( | ) character to indicate the left edge of the screen.

a) cout endl “Programming is cool\nand I want to be good at it”;

b) cout endl “num \% 4 =\t” (num % 4);

c) cout endl setw(10) < “START” “Hello!”;

d) cout < setprecision (2) < endl < “dnum + 0.27 =” < setw(8) < (dnum + 0.27);


9. Write a cout statement that will produce each of the following output: (7 pts)

a) |ITEM

b) |dnum=234.50 (Note: 234.5 is the value of variable dnum)

c) |John’s tape

10. Assuming that variables num1, num2, letter, and dnum are defined as follows: (5 pts)

char letter;

int num1, num2;

double dnum;

And that the user has typed the following input line: Input |452.25E+5G456.85¿

a) Show the contents of the memory locations that correspond to these variables after the execution of the following cin statements:

cin num1 dnum letter;

cin num2;

num1 ______num2 ______dnum ______letter ______

b) What is the status of the input stream after the execution of the above cin statements?

Status of the input stream: ______

11. Which of the following statements are incorrect? Also describe the error(s). (4 pts)

a) num + 5 = num2; ______

b) cout < num; ______

c) cout < (num + 5) < “Jo”; ______

d) cin > num1 > num2; ______

h) cin > num - 2; ______

i) cout < (num +2) , ‘A’; ______

j) cin > num1, num2; ______

k) int num1; ______


12. Specify the order of execution of the statements of the following program. (2 pts) ______

#include <iostream.h>

void newtest()

{

1 int num1; // to hold the first integer value

2 int num2; // to hold the second integer value

3 int sum // to hold their sum

4 cin > num1;

5 num2 = 25;

6 sum = num1 + num2;

7 cout endl “sum = ” sum;

}

int main()

{

8 int number1; // the first number

9 int number2; // the second number

10 number1 = 14;

11 number2 = number1 + 50;

12 newtest(); // call function newtest()

13 cout endl “number1 = ” number1;

14 cout “endl < “number2 = ” < umber2;

15 return (0);

}

13. What is the output of the following program? (2 pts) ______

#include <iostream.h>

int newtester2( )

{

return (7);

}

int main( )

{

int result;

result = newtester2( ) + 9;

cout < endl < “result =” < result;

return ( 0 );

}

14. Provide the output of the following program: ______(2 pts)

#include <iostream>

#include <stdlib>

using namespace std;

int main()

{

int num; /* to hold an integer value */

int result; /* to hold the result */

num = -10;

result = abs( num ) + 15;

cout < endl < “The result is: “ < result;

return (0);

}

15.

Write C++ language statements to do the following: (5 pts)

a) read two integer values into the variable num1 and num2.

b) compute the expression num1 – num2 and print the result.

c) Compute the expression num15 + num24 and print the result.
16. Suppose that the contents of the include file test.h are as follows: (4 pts)

/*------test.h ------*/

double compute( ); // to evaluate the expression

const int FLAGT = 10;

double myabsolute(double); // to compute an absolute value

And that the contents of the file myprog.cpp are as follows:

/*------program file ------*/

#include <test.h>

#define MAXVALUE 50 /* the maximum value */

int main ( )

{

int num1 = 15, result1, result2;

result1 = num1 + MAXVALUE;

num1 = num1 + FLAGT;

result2 = MAXVALUE * 4;

return ( 0 );

}

Show the contents of the file myprog.cpp after it has been processed by the preprocessor.


17. Fill in the blanks with the most appropriate answers. (9 pts)

1. A ______is a system program that translates a high-level language program into machine language.

2. An______receives as input the statements of a program and its input data, executes the statements of that program using its input data, and produces its output.

3. In a high-level language program, a memory location is represented by a ______.

4. The ______of a variable tells to the compiler the type of data that will be stored in the corresponding memory location, so that it knows how many bytes to reserve for that memory location.

5. In a high-level language program, a ______is a note or a remark for a human being reading the program, and is ignored by the compiler.

6. A high-level language program consists of one or more files called ______.

7. A ______is a function that comes with the compiler, and that can be called in any program.

8. The ______is a program that first processes and completes a program source file before it is translated into machine language by the compiler.

9. The ______combines all the object modules of a program and the machine language translations of library functions called in the program in a file called executable file.

18. a. Write the compound assignment that corresponds to each of the following assignment statement: (2 pts)

sum = sum * num; ______result = result / (num – 5);______

b. Write the simple assignment that corresponds to each of the following compound assignment: (2 pts)

num + = 7; ______result * = num – 1; ______

19. Assume that variables i, j, n and m are declared as follows: int i = 9 , j = 5 , n , m;

Show the values of these variables after the execution of each of the following instructions:

(NB: the questions are independent). (6 pts)

n = ++i * j- -; n= ______i=______j = ______

m = i- - + ++ j; m= ______i=______j = ______

20. Show the output of the following statements: (4 pts)

int num1 = 10, num2 = 7, result1, result2;

reult1 = - - num1 + 3;

result2 = num2 - - + 3;

cout < “\nnum1=\t” < num1 < “\tresult1=\t” < result1

< “\nnum2=\t” < num2 < “\tresult2=\t” < result2;