Exam 1 practice (other sources)

​1. To display an error, you should

a. cerr <<

b. cout >>

c. cin <<

d. cerr >>

e. cout <<

2. Is this code fragment correct for printing out the value of area?

int area (int length, int width) {
    /* ... */
}
....
area(2, 2);
cout << "Area is " << area << "\n";

Answer: False, because the function would return an integer, but in line 5 it wasn't assigned

3. A string s stores

a. a sequence of chars

b. duplicate values

c. its size

d. a and b

e. a and c

4. Type safety

a. is impossible in practice, so there is no point in trying

b. refers to making sure only legal values are stored in a variable

c. is ignored by the C++ compiler

d. can never be violated

e. none of the above

5. Every type specifies

a. whether values are upper or lower case

b. which compiler to use

c. the font and point size

d. legal values and operations

e. none of the above

6. The compiler

a. outputs a source code file

b. outputs an object code file

c. outputs a header file

d. outputs an executable file

e. none of the above

7. The variable

a. has a name which is its value

b. has a type and a value

c. has a value but not a type

d. has a name but not a value

e. has a value but not a name

8. Is this correct for printing out each element in the vector v and then returning?

for (int i = 0; 0 < v.size(); ++i) {
    cout << "enter value for v[" << i << "]:";
    cin >> v[i];
}
return 0;

Answer: False. (printing out) --> (importing)

9. cout >> x >> y;

a. reads as many character as it can, consistent with the type of x, then the type of y

b. prints x followed by y

c. produces a compile-time error

d. produces a runtime error

e. none of the above

10. x += 1;

a. increments x

b. is the same as x = x + 1;

c. is the same as x++;

d. b and c

e. all of the above

11. += is overloaded because

a. it is defined differently for string and numbers

b. it is formed from the two characters + and =

c. it is defined the same for strings and numbers

d. a and b

e. b and c

12. && (two ampersand characters)

a. indicate a typing mistake

b. indicate an if statement

c. indicate a logical operation (AND)

d. indicate a relational operation

e. indicate a comparison

13. Variable should generally be initialized

a. after each use

b. when they are defined

c. before they are used

d. a and b

e. b and c

14. To add x to the end of vector w you may use

a. w[w.size() + 1] = x;

b. w.push_back(x);

c. x.push_back(w);

d. any of the above

e. none of the above

15.

int count = 0;
while (count < 100) {
    cout << count << '\n';
}

a. terminateds after writing the value of count 100 times

b. terminates after writing the value of count 99 times

c. has an error with cout

d. does not terminate

e. none of the above

16. If ch = '(' what does this code segment return?

switch (ch) {
    case '(' : case ';' : case ';' :
        return Token(ch);
    case '.' : case '0' : case '1' : case '2' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9': {
        /* ... */
        return Token ('8', val);
    }
    default: 
        error("Bad token");
}   

a. Token(ch)

b. Token('8', val)

c. Token(ch), Token('8', val)

d. Token(ch), Token('8', val), error("Bad token")

e. none of the above

17. In C++ void get();

a. says that this void returns a function

b. says that get is a function

c. says that this function returns nothing

d. a and b

e. b and c

18. The value of (false || x)

a. is always true

b. is always false

c. is true if x is true, and false if x is false

d. is false if x is true, and true if x is false

e. none of the above

19. The value of (x && true)

a. is always true

b. is always false

c. is true if x is true, and false if x is false

d. is false is x is true, and false if x is false

e. none of the above

20. The C++ switch statemnet is logically equivalent to

a. while statements

b. when statements

c. if...else if statements

d. on...off statemnets

e. none of the above

21. What is the output of this statement:

for (int i = 2; i <= 3; ++i)
    cout << i << ", ";

a. nothing

b. 3,

c. 2, 3

d. 1, 2, 3,

e. 2, 3,

22. \n means

a. a string containing two characters

b. divide by n

c. a single newline character

d. a single tab character

e. none of the above

23. Given the function declared with

int calculate()

which of the following statements is a valid way to end the function?

a. break

b. continue

c. return;

d. return 0;

e. none of the above

24. if v was previously declared as a vector, and we have the statement

x = v[v.size() - 1];

the value of x is

a. the last element in v

b. the second-to-last element in v

c. undefined, because the statement is an error

d. 0

e. the index of the last element in v

25. vector<char> v(3);

a. means v is a vector containing the value 3

b. means each char in v is initialized to '3'

c. means vector is a char accessed using v[3]

d. means v is a vector of 3 chars

e. none of teh above

26. Syntax errors

a. are detected by the run-time library

b. are detected by the compiler

c. are detected by the linker

d. may not be detected until run time

e. none of the above

27. Misspelling sqrt like this:

double sqrrt(double);
double y = sqrrt(2.5);

would most likely result in

a. a linker error

b. a compile-time error (no defined function)

c. an exception

d. all of the above

e. none of the above

28. Leaving out a function definition will most likely result in

a. a compile-time error

b. a linker error

c. a runtime error

d. a or b

e. a or c

29. To inspect a block of code for run time exceptions, use

a. inspect{...} throw{...}

b. try{...} catch{...}

c. return -1;

d. error("run time exception")

e. all of the above

30. After executing this code:

int dc = 20;
double df = 9 / 5 * dc + 32;

the value of df will be

a. 68.0

b. 52.0

c. 32.0

d. 0

e. none of the above

31. if ch = '2', what is count at the end of the loop?

int count = 0;
switch(ch) {
    case'1': count++;
    case'2': count++;
    case'3': count++;
    default: count = 0;
}

a. 0

b. 1

c. 2

d. 3

e. none of the above

32. What is the output type of 1 / 0.5?

a. integer

b. double

c. dividing by zero error message

d. string

e. none of the above

33. What does this do: if (a <= 0) error("area() post-condition");

i. prints"area() post-condition"

ii. prints system error message concatenated with "area()post-condition"

iii. returns -1

iv. terminates the program

Answer choices: ii and iv

34. We want to print the number i from 1 to 100 inclusive, where the printout starts with 1 and ends with 100. Which is the correct for loop?

a. for (int i = 0; i <= 100; ++i)

b. for (int i = 1; i < 100; ++i)

c. for (int i = 0; i < 100; ++i)

d. for (int i = 1; i <= 100; ++i)

e. none of the above

35. Is this loop

for (b = 0; blue < 255; ++b)
    cout << "B value is [" << b << "]\n";

functionally the same as

for (b = 0; blue < 255; ++b) {
    cout << "B value is [" << b << "]\n";
}

Answer: true//只有一行所以可以没有大括号可以锁紧缩进

36. The purpose of debugging is

a. to remove errors in the specification

b. to eliminate compile-time errors

c. to test the code for correct operation

d. to find and correct logic errors

e. to make the source code more readable

37. A token is

a. a built-in C++ class

b. one digit of a number

c. composed of an operator and its operands

d. a meaningful value, not divisible into smaller tokens

e. all of the above

38. A grammar contains

a. rules to generate syntactically correct expressions

b. rules to determine syntactically valid expressions

c. rules to specify the precedence of operations

d. a, b, and c

e. a and b, but not c

39. A constructor

a. has the same name as its class or struct

b. must have at least one argument

c. always allocates memory

d. all of the above

e. none of the above

40. Functions must be

a. declared before they are called

b. defined before they are declared

c. defined before they are called

d. called before they are defined

e. none of the above

41. Real programs

a. are written from beginning to end in one sitting

b. evolve under pressure of requirements and constraints

c. grow using calls to standard library functions

d. are always large and complicated

e. all of the above

42. Symbolic constants

a. are not allowed in C++

b. are recommended only for strings

c. are recommended for special values other than 0 and 1

d. all of the above

e. none of the above

43. Testing tries to

a. handle program errors systematically

b. trace a program's execution after cleaning up the code

c. break a program by getting it to misbehave

d. a and b

e. a and c

44. Release 1.0 of a program

a. contains all needed feature refinements

b. contains last minute changes to the specification

c. contains fixes to key bugs found during testing

d. a and b

e. b and c

45. Comments in the source code

a. should correctly reflect what the compiler does

b. should be adequate for a reader (not necessarily the original programmer)

c. should be verbose so as to prevent program complexity

d. should be removed after testing

e. none of the above

46. An algorithm

a. is a C++ program

b. is a equence of steps to solve a problem

c. tells what to do in a programming language

d. a and c

e. b and c

47. New exception within a catch(exception& e) handler

a. cause execution of the catch(...) handler following this catch handler

b. are not possible in C++

c. are difficult to deal with

d. a and c

e. b and c

48. "const" means

a. "cannot be changed by =, ++, or --"

b. "cannot be changed after initialization"

c. "constructor"

d. "concatenater"

e. none of the above

49. C++ literal string are delimited using

Answer: matching quotes("...")

50. End of file

a. is signalled by Ctrl-Z on UNIX or Linux

b. is signalled by Ctrl-D on Windows

c. is signalled by Ctrl-D on UNIX or Linux

d. a and b

e. b and c

51. Is this correct for reading in each element into the vector v, and then returning?

vector<int>v(10);
for(int i = 0; 0<v.size(); ++i) {
    cout << "enter value for v["<< i << "]: ";
    cin >> v[i];
}
return 0;

Answer: Yes

52.

cin >> x >> y;

a. Produces a compile-time error

b. Produces a runtime error

c. Ignores whitespace and reads as many characters as it can consistent with type x, then ignores whitespace and reads as many characters as it can consistent with type y

d. prints x followed by y

e. none of the above

53.

++x;

a. Increments x

b. is the same as x =+1; in it's effect

c. is slower than x++;

d. a and b

e. all of the above

54. The best way to initialize a variable is

a. to use ()

b. to use {}

c. to use =

d. to use ==

e. to use >>

55.

int count = 0;
while (count <= 100) {
    cout << ++count << "\n";
}

a. Prints the numbers 0 through 100

b. Prints the numbers 0 through 101

c. Prints the numbers 1 through 100

d. Prints the numbers 1 through 101

e. none of the above

56. In C++ void get();

a. is not legal

b. is both a definition and a declaration

c. is a definition but not a declaration

d. is a declaration but not a definition

e. none of the above

57. cout << 0xb; will print...

a) 0xb

b) xb

c) 1010

d) 11

e) none of the above

58. Logic errors...

a. are detected by the preprocessor

b. are detected by the compiler

c. are detected by the linker

d. may not be detected until runtime

e. none of the above

59. What is the difference between class and struct?

a) A class starts out public, while a struct starts out private

b) A struct starts out public, while a class starts out private

c) A class has member functions, while a struct does not

d) A struct can be defined by a user, but a class must be part of the C++ language

e) none of the above

60. Which arguments can a function change?

a) non-const arguments

b) const arguments, if it is not a const member function

c) reference arguments which are not const

d) a and b

e) a and c

61. A::b means...

a) the name b in the namespace A

b) the name A in the namespace b

c) initialize A to the value b

d) initialize b to the value A

e) none of the above

62. Which is the correct process?

source code -> C++ compiler ->object code ->linker -> executable file

Last updated

Was this helpful?