Final Exam Study Guide

20 of the following questions will be asked on the final exam. Unless otherwise specified, all questions are relative to the C++ programming language.

Language concepts

1. What is a variable?

2. All objects have what in common?

3. Which of the following is mandatory for a function definition?

4. Which expression performs a function call?

5. What is the meaning of the return value of the main function?

6. A function with a return type of void signifies

7. A sequence of operators and their operands, followed by a semicolon is a(n):

8. All assignment and compound assignment expressions return:

9. All non-assignment, arithmetic operators, when used on values of type int or double, return:

10. The return type of a comparison operator is:

11. A unary operator requires how many operands?

12. What variable scope has the longest lifetime?

13. A variable whose initial value cannot be replaced must be declared as:

14. When a value undergoes conversion, what happens?

15. C-style cast expressions and functional cast expressions are examples of:

16. Which of the following describes a conversion where no information can be lost?

17. Variables declared directly within a class are called:

18. Functions declared directly within a class are called:

19. A private member of a class can be accessed from:

20. A public member of a class can be accessed from:

21. The kinds of statements that repeatedly execute code are generally known as:

22. Which of the following is always true about referrences?

23. A pointer with the value 0 is known as:

24. A condition is convertible to:

25. Another term for a compound statement is:

Standard library concepts

26. Which of the following is a valid expression that returns the number of elements in a vector

27. When integer literals are used as arguments, almost all of the common mathematical functions return a value of type:

28. What is the name of the function that raises a number to a power?

29. Which of the following is a valid operator that performs stream insertion?

30. Which of the following is a valid operator that performs stream extraction?

31. Which of the following is a valid expression that erases all elements from a vector named v?

32. Which of the following is a valid expression that returns the first element of a vector named v?

33. Which of the following is a valid expression that returns the last element of a vector named v?

34. What is the name of the function that reads a line from an input stream?

35. The rand function never returns a value...

36. The string type represents:

37. One valid set of arguments to string::erase is:

38. What is the name of the function that can convert an integer into a string?

39. What is the name of the function that can convert a string to an integer?

40. Assuming c is a variable of type char, which of the following is a valid expression that extracts the next character, including whitespace, from cin and assigns it to c?

41. Assuming s is a variable of type string, which of the following is a valid expression that extracts the next word (non-whitespace characters) from cin and assigns it to s?

42. If cin is associated with the keyboard, what key combination signals the end of all further user input without necessarily terminating the program?

43. What does assert do?

44. What is the name of the function that exchanges the values between two variables of the same type?

45. The expression time(0) returns a value that represents the current time in...

Code analyzation:

46. What does the following code print?

int k = 1; cout << k++ << ' '; cout << ++k << endl;

47. What does the following code print?

int k = 5; cout << --k << ' '; cout << k-- << endl;

48. What does the following code print?

int x = 1; int y = 0; { int x = 2; y = x + x; { int y = 4; x = y + 1; } } cout << x << ' ' << y << endl;

49. What does the following code print?

int x = 0; { int x = 1; cout << x << ' '; } cout << x << endl;

50. What does the following code print?

int x = true; double y = int(5.4) + 0.1 * true + false; cout << (x - y) << endl;

51. What does the following code print?

int x = 3.4; double d = false; cout << (x + d) << endl;

52. What does the following code print?

cout << (100 % 5) << endl;

53. What does the following code print?

cout << (31 % 2) << endl;

54. What does the following code print?

class T { public: T() { cout << 'A'; } char f() { return 'B'; } char g(char x) { if (x == 'A') return 'C'; else return 'D'; } }; int main() { T t, u; t.g('B'); u.f(); t.g('A'); }

55. What does the following code print?

class T { public: T() { data = 'X'; cout << 'A'; } void f() const { cout << 'B' << data; } char g(char x) { cout << 'C' << x; return x; } char data; }; int main() { T a; a.f(); a.data = a.g('Z'); a.g('Q'); a.f(); return 0; }

Coding

56. Write C++ code that reads 2 floating-point numbers and displays the sum of those numbers.

57. Write C++ code that reads 1 line and displays the length of that line.

58. Define the body of is_even, this function shall return true if the value of x is an even integer; false shall be returned otherwise.

bool is_even(int x) { }

59. Define the body of sum_of_double, this function shall return the sum of the values found in v or 0 if v is empty.

double sum_of_doubles(const vector<double>& v) { }

60. Define the body of find_double, this function shall return the index of the first occurrence of the value of x found in one of the elements of v or -1 if no such value was found. Do not perform function calls in your code.

int find_double(const vector<double>& v, double x) { }

61. Complete the following program; this program should display a random integer value between a minimum and maximum value (inclusive) specified by the user.

#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { int min, max; srand(time(0)); cout << "Minimum integer: "; cin >> min; cout << "Maximum integer: "; cin >> max; return 0; }

62. Define the body of factorial, this function shall return the factorial of x or 1 if x is less than or equal to 1. Examples: factorial of 0 is 1, factorial of 1 is 1, factorial of 3 is 3 * 2 * 1 = 6, factorial of 4 is 4 * 3 * 2 * 1 = 24, factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.

int factorial(int x) { }

63. Define the body of is_prime, this function shall return true if x is a prime integer and false otherwise.

bool is_prime(int x) { }