EAS230 Fall 2011

profilexoon
practice_exam.doc

EAS230 Fall 2011 Exam 1 50 minutes Each question is worth 3 points

Name : _________________________________ Person Number : ________________

Answer Sheet: Place all of your answers here. Please return the entire exam. If you use pencil, you forfeit your right for any re-grades.

1. ______________________________

2. ______________________________

3. ______________________________

4. ______________________________

5. ______________________________

6. ______________________________

7. ______________________________

8. ______________________________

9. ______________________________

10. ______________________________

11. ______________________________

12. ______________________________

13. ______________________________

14. ______________________________

15. ______________________________

16. ______________________________

17. ______________________________

18. ______________________________

19. ______________________________

20. ______________________________

21. ______________________________

22. ______________________________

23. ______________________________

24. ______________________________

25. ______________________________

26. ______________________________

27. ______________________________

28. ______________________________

29. ______________________________

30. ______________________________

This is a closed book, closed notes exam. There are no trick questions. The obvious answer is usually correct.

Answer questions (1) to (10) based on the C++ program given below. Note that the lines have been numbered for easy reference so the numbers are not part of the code.

1 #include <iostream> 2 #include <cstdlib> 3 #include <ctime> 4 #include <cmath> 5 using namespace std; 6 int main() { 7 int coinFlips = 0; //no. of coin flips; 8 int randomNumber = 0; // result of rand num generator 9 int numberOfHeads = 0, numberOfTails = 0; 10 int signOfTrouble = 0; 11 double percentage = 0.0; 12 13 srand( (int)time(0) ); 14 15 // flip 10000 coins 16 for (coinFlips = 1; coinFlips <= 10000; coinFlips++) 17 { 18 // generate a random number 0-9 19 randomNumber = rand()%10; 20 // if it's 5 or greater, count as a head 21 if (randomNumber >= 5) 22 { 23 numberOfHeads++; 24 } 25 // 4 or less, count as a tail 26 else 27 { 28 numberOfTails++; 29 } 30 cout<<randomNumber<<" "<<numberOfHeads<<" "<<numberOfTails<<endl; 31 } // end for 32 33 // this refelcts the number of coin tosses that stray from 50/50 34 signOfTrouble = abs(numberOfHeads - numberOfTails)/2; 35 cout << signOfTrouble << endl; 36 37 // as a percentage of total coin tosses... 38 percentage = ((double)signOfTrouble / (double)coinFlips) * 100.00; 39 cout << percentage << endl; 40 41 // if greater than 10%, trouble ahead 42 if (percentage > 10.0 ) 43 { 44 cout << "Go to the basement" << endl; 45 } 46 else 47 { 48 cout << "Everything is cool, go to school" << endl; 49 } 50 } // end program

Give specific line numbers for the occurrence of the following program features in the above code. There may be more than one answer for each, but only one line number is required.

1. The line that includes the library that allows us to call math functions

2. Closing bracket of main

3. Declaration and initialization of a floating point number variable

4. The FIRST statement that prints something to the screen

5. Gets a random number

6. Casts ints to doubles

7. The statement that cause this program to flip 10000 coins

8. A comment noting the end of a for loop

9. Allows us to use cout, cin, and endl without typing std::

10. Seeds the random number generator with number of seconds since 1/1/70

---------------------------------------------------------------

For questions 11 thru 33, select the BEST answer

and write your answers on the answer sheet.

11. Which of the following is an int:

a. 14

b. 14.0

c. “Fourteen”

12. A variable declared as bool, can hold …

a. decimal numbers

b. whole numbers

c. human readable text

d. true or false values

13. A variable declared as string, can hold …

a. decimal numbers

b. whole numbers

c. human readable text

d. true or false values

14. Functions are...

a. symbols meant to hold values

b. stand-alone C++ code that can be “called”

c. any bracketed statement

15. Which of the following symbols is the assignment operator ?

a. = =

b. =

c. !=

d. ++

16. Which of the following symbols is the “comparison/equal to” operator ?

a. = =

b. =

c. !=

d. ++

17. Which of the following symbols is the “less than or equal to” operator ?

a. = =

b. =

c. <=

d. ++

18. All C++ commands end in a semicolon

a. True

b. False

19. The following C++ code …

int addNumbers( int a, int b)

a. is declaring a function named addNumbers

b. is declaring a variable named addNumbers

c. is declaring a program named addNumbers

d. is declaring a file named addNumbers

20. Which gets input from the user?

a. cout

b. cin

c. get

d. user

21. Which of the following is NOT a loop command?

a. do … while

b. while

c. loop

d. for

22. Which C++ command creates an array

of 15 chars?

a. char z[15];

b. char 1…15;

c. char [15];

d. char a,b,c,d,e,f,g,h,I,j,k,l,m,n,o;

23. The following “for” declaration

for ( x=0; x < 5; x = x+1 )

creates which values of x during execution ?

a. 0,1,2,3,4

b. 0,2,4

c. 0,2,4,5

d. 2,4

24. Which of the following is a C++ keyword

(i.e. reserved word, never used as a variable or

function name)?

a. myName

b. x

c. bool

d. test

25. The AND logical operator is

a. ||

b. &&

c. +

d. .AND.

26. Which of the following statements is false?

a. Comments are used to document the program and make it more readable.

b. The compiler interprets the contents of comments.

c. Double slashes (//) are used to turn any line or part of a line into a single-line comment

d. Multi-line comments can be identified by /* and */

27. Functions which do not return any value should be declared

a. true

b. false

c. void

d. null

28. One way to get a program to loop forever:

a. loop( ALL )

b. while (true)

c. do … while( 0 )

d. for (x > 0 )

29. If x, y, are declared as ints, and z is a String, which is a

LEGAL operation

a. z = x + y;

b. z = x * y;

c. z = (x * true) + (y * false);

d. x = y + 14;

30. If y is 16, what will the following line print to the screen?

cout << “Y = ” << y << endl;

a. 16

b. 32

c. Y = 16

d. “Y = 16

For the following, the code that you write must be perfect and compilable.

31. On the answer sheet, write an if statement to test if the boolean variable quitFlag is false.

32. On the answer sheet, write a while statement that loops if A is greater than 10 or B is less than 0.

33. On the answer sheet, declare a character variable myVar and initialize it to the letter Q.