CS260 - Exam 1 - Sample

(revised 14FEB01)


1. Write the output generated by the following program when 5.2 and 6.3 are entered at the prompt.

#include <iostream.h>
int main() {
	float x, y;
	cout <<"Enter two floats: ";
	cin >> x >> y;
	cout << (x * 1 + y) << endl;
	return 0;
}

2. Give examples of two C++ keywords

3. Give examples of two C++ data types

4. Which of the following are valid identifiers (list all that are valid)?

a. a one c. r2d2 &#9; e. mispellt

b. 3cpo d. P_S_U_Berks f. 1_2_3

5. Write the output generated by the following program:

#include <iostream.h>
int main() {
	int i = 4, j = 8, k = 21 / 4;
	j = i - 1;
	k = 12 / 3;
	cout << j << " " << k << endl;
	return 0;
}

6. Write the output generated by the following program:

#include <iostream.h>
int main() {
	float sales = 12.34;
	cout << sales << endl;
	sales = sales + 0.1 * sales;
	cout << sales << endl;
	return 0;
}

7. Which of the following are valid identifiers (list all that are valid)?

a. anti_diss_pro b. lA c. Al d. R2D2/3CPO e. miles/hour

8. Evaluate the following expressions (assume integer arithmetic):

a. 1/2

b. 3*2

c. 15% 4

d. 1+2-3*4/5

9. Assuming int j=5, k=7, evaluate the following expressions.

a. j - k

b. j - k * j + k

c. j * 3 / k

10. Write the output from the following program:

#include <iostream.h>
void main() {
	int j = 3;
	float x = 7.5;
	cout << "j: " << j << endl;
	cout << "x: " << x << endl;
	j = x;
	cout << "j: " << j << endl;
}
11. Assuming the proper include file are available, write the value of each expression.
a. floor(10.5)
b. pow(2,4)
c. sqrt(16.0)
d. fabs(-8787.9)
e. sqrt(sqrt(sqrt(256)))
12. What does float (the one before diff) signify in this function prototype?
float diff(float a, float b);
13. In the function prototype in question 12, how many arguments must be used to call the function diff?
14. Arguments to diff (question 12) should be of which type?
15. For the function prototype in question 12, write code that inserts into cout, a valid call to function diff 
     using any arguments you wish. Assume iostream.h has been included.
16. What return value would be generated by the function call in question 15?
17. Write the output generated by the following program:
#include <iostream.h>
int fun(int, float);
int x = 6;
int main() {
	int x = 4;
	cout << (x + ::x + fun(::x, (float) x) );
	cin >> x;
	return 0;
}
int fun(int x, float y) {
	return x - y + ::x;
}

True/False

18. The physical components of a computer are know as hardware.

19. The () symbols have lower precedence than the / operator..

20. The :: operator is called the Scope Resumption Operator.

21. Float variables may have *, +, <<, and >> operations applied to them.

22. The following represents a cast of a variable: {float} i.

23. The statement if (0 <= i <= 5) will test if i has a value in the range 0 through 5.

24. If no errors are detected in a program, we can safely assume the program is correct.

25. Inserting endl in a cout statement generates a tab on the computer screen.

26. cin is an object associated with program output.

Use the following declaration to answer questions 27 through 29: float x;

27. We cannot determine the name of the object.

28. We cannot determine the operations that may be applied to x.

29. We cannot determine the value of x.

30. One int object can store any size integer.

31. The floating point variable type float allows only for numeric values less than 0.0.

32. One char type variable may store a sequence of characters of any length.

33. The double type is similar to int in that double stores integer numbers with no fractional part.

34. sqrt(4.0) returns 16.0.

35. ceil(1.1) returns 1.0.

36. floor(1.9999) returns 1.0.

37. fabs(-1.0) returns 1.0.

38. cout << sqrt(-1); generates a compile time error.

39. ceil(floor(x)) is an expression that always returns x.

40. pow(4, 0.5) is an expression that generates a runtime error.

41. double f(double x); is a valid function prototype.

42. Like int and float, cin and cout is always available, without an #include directive.

43. % is an operator may be applied to only to float variables.

44. The expression cout >> "this is it"; will generate a compile time error.

45. The floor(i) function is equivalent to 1/i.

Use this function prototype to answer the questions 46 through 51:

double f(double x, double y);

// Parameter Conditions: x and y are both positive or zero.

// Function Purpose: Returns x to the y power.

46. The name of the function is foo.

47. The return type of f is int.

48. f must be called with exactly two arguments.

49. f may be called with character arguments.

50. The function will always fulfill its specification even when x and y are negative.

51. f(2.0, 3.0) returns 9.0

Multiple Choice

52. Which operation(s) can alter the value of a float variable:

a. cout << b. cin >> c. = d. + e. b and c f. none of these

53. Which operation(s) can NOT alter the value of a float variable:

a. cout << b. cin >> c. = d. + e. a and d f. none of these

Use Program ONE_A below to answer the following multiple choice questions

54. How many unique variables exist in the source code of program ONE?

a. 0 b.1 c. 2 d.3 e. 5 f. none of these

55. In Program ONE_A, how many variables have all attributes (name, type and value) satisfied

before the comment // Point B assuming the user inputs: 4 3 5 ?

a. 0 b.1 c. 2 d. All of them e. none of these

56. What does Program ONE_A generate following the comment

// Point B assuming the user inputs: 4 3 5 ?

a. 3 b.5 c.8 d.12 e. none of these

57. What output does Program ONE_A generate following the comment

// Point B assuming the user inputs: 0 4 -1 ?

a. 3 b.5 c.8 d.12 e. none of these

// Program ONE_A
#include <iostream.h>
int main()  {
	float x;
	float y;
	float z = 0.0;
	// Point A
	cout << "Enter three numbers: ";
	cin >> x >> y >> z;
	// Point B
	cout << (x + y + z);
	return 0;
}

58. Which of the following is a VALID identifier

a. Q Z b. [I c. I*8 d. z1 e. $Cost f. none of these

59. Which of the following is an INVALID identifier (choose one)

a. SunRise b. Sun Set c. Time_Spent d. MiLeSpEr e._ERROE_

60. Evaluate the follow expression (assume integer arithmetic): 5 % 2 / 3 * 2 - 1


Use Program TWO to answer the following questions

61. The output of Program TWO is
a. 4 4 A b. 4.5 4.5 A c. 4.5 4 A d. 4 4.5 A e. j / 2 x / 2 ch
62. How many variables are listed in the source code?
a. 0 b. 1 c. 2 d. 3 e. 4
// Program TWO
#include <iostream.h>
int main()  {
	int j = 9;
	char ch = 'A';
	float x = 9.0;
	cout << j / 2 << " " << x / 2 << " " << ch << endl;
	return 0;
}
63. pow(4.0, 2.0) returns
a. 2.0 b. 4.0 c. 8.0 d. 16.0 e. 2.1412
64. ceil(3.6 - 0.5) returns
a. 0.0 b. 2.0 c. 4.0 d. 8.0 e. none of the above
65. floor(2.6 - 0.5) returns
a. 0.0 b. 2.0 c. 4.0 d. 8.0 e. none of the above
66. fabs(-8) returns
a. 0.0 b. 2.0 c. 4.0 d. 8.0 e. none of the above