uoit calculus 2 midterm

26
1. Which of the following statements should you include in a C++ program that performs an input or output operation? a. using namespace std; b. #include <iostream> c. int main() *d. All of the listed options 2. Which of the following expressions represents a legal way of checking whether a value assigned to the number variable falls between 50 and 100? a. if (number >= 100 && number <= 50) *b. if (number >= 50 && number <= 100) c. if (number >= 50 || number <= 100) d. if (number >= 100 || number <= 50) 3. Which of the following statements is valid with respect to the usage of the endl symbol? a. The endl symbol denotes the end of line marker. b. When the end of line marker is sent out to cout, the cursor is moved to the first column in the next screen row. c. It is a good practice to end all lines of output with an end of line marker. *d. All of the listed options.

Upload: nickshan-nahenthiram

Post on 02-Nov-2014

269 views

Category:

Documents


4 download

DESCRIPTION

uoit calculus 2 midterm

TRANSCRIPT

Page 1: uoit calculus 2 midterm

1. Which of the following statements should

you include in a C++ program that performs

an input or output operation?

a. using namespace std; b. #include <iostream> c. int main() *d. All of the listed options

2. Which of the following expressions

represents a legal way of checking whether a

value assigned to the number variable falls

between 50 and 100?

a. if (number >= 100 && number <= 50) *b. if (number >= 50 && number <= 100) c. if (number >= 50 || number <= 100) d. if (number >= 100 || number <= 50)

3. Which of the following statements is

valid with respect to the usage of the endl

symbol?

a. The endl symbol denotes the end of

line marker. b. When the end of line marker is sent

out to cout, the cursor is moved to the

first column in the next

screen row. c. It is a good practice to end all lines

of output with an end of line marker. *d. All of the listed options.

Page 2: uoit calculus 2 midterm

4. Which of the following conditions tests

for the failure of the user to assign an

integer value to the floor variable?

int floor; cout << "Floor: "; cin >> floor;

a. if (fail(floor)) *b. if (cin.fail()) c. if (cin.fail) d. if (floor < 0)

5. What will be the result of running the

following code fragment?

int time = 0; int year = 0;

int rate = 0; int principal = 1000;

int interest = 0;

while (year < 10) { interest = (principal * time * rate) /

100; cout << interest << endl; }

a. The code fragment will display the

interest calculated for nine years. b. The code fragment will not display the

calculated interest and halt abruptly. c. The code fragment will not display any

output because it will not compile. *d. The code fragment will continue to

display the calculated interest because

Page 3: uoit calculus 2 midterm

the loop will never end.

6. Consider the following statements about

computer programs:

I. Computer programs can be written by

someone who has a basic knowledge of

operating a computer.

II. Computer programs can complete complex

tasks quickly.

III. Large and complex computer programs are

generally written by a group of programmers.

IV. Computer programs are composed of

extremely primitive operations.

Which one of the following options is

correct?

a. II and III are correct statements. b. I, II, III, and IV are correct

statements. *c. II, III, and IV are correct

statements. d. I and IV are correct statements.

7. Which of the following is the

mathematical equivalent of the following C++

expression?

h = (4.0 * a * b - pow(b, 2)) / c;

a. h = 4ab – 2b / c b. h = (4ab – 2b) / c

c. h = 4ab – b2 / c

*d. h = (4ab – b2) / c

Page 4: uoit calculus 2 midterm

8. What is the purpose of the following

algorithm?

somenum = 0

Repeat the following steps for 50 times

input variable1

if variable1 > somenum then

somenum = variable1

end of if

end of repeat

print somenum

*a. To find the highest among 50 numbers b. To print out the 50 numbers c. To find the smallest among 50 numbers d. To search for a particular number

among 50 numbers

9. Which of the following symbols is used to

terminate a C++ program statement?

a. Colon *b. Semicolon c. Single quote d. Period

10. Assuming that a user enters 45, 78, and

12 one after another, what is the output of

the following code snippet?

int main() {

int num1; int num2; int num3 = 0;

Page 5: uoit calculus 2 midterm

cout << "Enter a number: "; cin >> num1;

cout << "Enter a number: "; cin >> num2; cout << "Enter a number: ";

cin >> num3; if (!(num1 > num2 && num1 > num3)) {

cout << num1 << endl; } else if (!(num2 > num1 && num2 > num3))

{ cout << num2 << endl; }

else if (!(num3 > num1 && num3 > num2)) { cout << num3 << endl;

} return 0; }

a. 12 *b. 45 c. 78 d. There is no output due to compilation

errors.

11. What is the output of the code snippet

given below?

int i;

int j = 0;

for (i = 0; i < 6; i++) { if (i % 2 == 0)

{ i = i + 2; j++;

}

Page 6: uoit calculus 2 midterm

else {

i++; j = j + 2; }

j++; } cout << "i=" << i << ", j=" << j;

a. i = 7, j = 7 *b. i = 7, j = 8 c. i = 8, j = 7 d. i = 8, j = 8

12. Which of the following statements are

valid with respect to the main function in

C++?

a. The opening and closing parentheses

after the function name main are

optional. b. A semicolon is required after the

declaration "int main()." c. A semicolon is required after the

closing curly braces in the main

function. *d. Every executable C++ program has a

main function.

13. Which of the given cout statements

generates the following output?

///"\\\

Page 7: uoit calculus 2 midterm

a. cout << "///"\\\" << endl; *b. cout << "///\"\\\\\\" << endl; c. cout << "//////""\\\\\\" endl; d. cout << ///"\\\ << endl;

14. Which one of the following is an

assignment statement?

a. int a = 20; *b. a = 20; c. assign a = 20; d. assign 20 to a;

15. Which of the following statements must

you include in a C++ program that performs

an input or output operation?

a. using namespace std; *b. #include <iostream> c. int main(int num, double new_num) d. return 0;

16. Which of the following code snippets

will generate a random number between 0 and

99?

*a. srand (time(NULL));

val = rand() % 100; b. srand (time(NULL));

val = rand() % 100 - 1; c. srand (time(NULL));

Page 8: uoit calculus 2 midterm

val = rand() % 99; d. srand (time(NULL));

val = rand() * 100;

17. Is there any error in the following code

snippet, which is used for calculating the

average age for a group of three students?

#include <iostream>

int main()

{

int age1 = 15;

int age2 = 18;

int age3 = 24;

int average = age1 + age2 + age3 / 3;

cout << "The average is " << average;

return 0;

}

a. Yes, the code snippet has a syntax

error. *b. Yes, the code snippet has a logical

error. c. Yes, the code snippet has a run-time

error. d. No error; the code snippet is

completely accurate.

18. What is the output of the following code

snippet?

int main() {

int num = 100; if (num != 100) { cout << "100" << endl; } else { cout << "Not 100" << endl; }

Page 9: uoit calculus 2 midterm

return 0; }

a. There is no output due to compilation

errors. b. 100 *c. Not 100 d. 100

Not 100

19. Which of the following statements is

correct about a sentinel?

a. A value that creates a bridge between

a data set and unrelated input. *b. A value that indicates the end of an

input sequence. c. A value that is part of the data to be

processed by the program. d. A value that terminates a program.

20. In C++, which special character is used

to invoke a member function?

a. A semicolon b. A colon *c. A dot d. A comma

21. Which operator is used to concatenate

two or more strings?

Page 10: uoit calculus 2 midterm

*a. + b. % c. & d. ^

22. Assuming that a user enters 68 as his

score, what is the output of the following

code snippet?

int main()

{

int score = 0; cout << "Enter your score: "; cin >> score;

if (score < 50) { cout << "F" << endl; } else if (score >= 50 || score < 55) { cout << "D" << endl; }

else if (score >= 55 || score < 65) { cout << "C" << endl; } else if (score >= 65 || score < 75) { cout

<< "B" << endl; } else if (score >= 75 || score < 80) { cout << "B+" << endl; }

else { cout << "A" << endl; } return 0; }

*a. D b. C c. B d. A

23. Consider the following C++ statement:

string name = "Harry";

Page 11: uoit calculus 2 midterm

Which of the following is true?

a. name is a string literal. *b. name is a string variable. c. "Harry" is a string variable. d. The statement contains a syntax error.

24. What is the output of the code snippet

given below?

string s = "aeiou";

int i = 0; do {

cout << s.substr (i, 1); i++; if (i >= 2)

{ i = 5; }

} while (i < 5);

a. a *b. ae c. aeiou d. The code snippet does not display any

output.

25. Which of the following options checks

that country

is neither China nor Denmark?

a. if (country != "China" || country !=

"Denmark") *b. if !(country == "China" || country ==

Page 12: uoit calculus 2 midterm

"Denmark") c. if !(country == "China" && country ==

"Denmark") d. if (country != "China" || country ==

"Denmark")

26. Assuming that the user inputs “twenty”

as the input, what is the output of the

following code snippet?

int num_employees = 0; cout << "Please enter the number of your

employees: "; cin >> num_employees; if (cin.fail())

{ cout << "Invalid Data!" << endl; return 0;

} if (num_employees < 10) {

cout << "Very small business!"; return 0; }

else { cout << "Small business" << endl;

if (num_employees > 100) { cout << "Mid size business" << endl;

}

else {

cout << "Large business" << endl; } return 0;

}

Page 13: uoit calculus 2 midterm

a. Very small business b. Small business c. Mid size business!

Large business *d. Invalid Data!

27. Which of the following activities can be

simulated using a computer?

a. Picking stocks b. Tossing a coin c. Shuffling cards for a card game *d. All of the listed items

28. Which one of the following is a correct

method of declaring and initializing an

integer variable with name value?

*a. int value = 30; b. Int value = 30; c. int value = .30; d. Int value = .30;

29. How many times does the following loop

execute?

double num; double sum = 0;

for (int i = 0; i < 10; i++) { cout << "Please enter a number: ";

cin >> num; if (sum > num) {

Page 14: uoit calculus 2 midterm

sum = num; }

if (i == 3) { i = 8;

} }

a. 0 times *b. 5 times c. 8 times d. 10 times

30. Consider the following code snippet:

cout << "Please enter a number: "; int num1; cin >> num1;

cout << "Please enter another number: "; int num2; cin >> num2;

cout << "The product is " << num1 * num2 << endl;

Suppose the student provides the answer 2 5

[Enter] as input to the first prompt. What

will the program do?

a. Print "The product is 25" *b. Print "The product is 10" c. Produce an error because the input for

the first prompt is not a single integer d. Produce an error because no value was

provided after the second prompt

31. Consider the following code snippet:

int main()

Page 15: uoit calculus 2 midterm

{ int num1 = 0;

int num2 = 0; int num3 = 0; int num4 = 0;

int num5 = 0; cout << "Enter a number: "; cin >> num1;

cout << "Enter a number: "; cin >> num2;

if (num1 < num2)

{ num3 = num1; }

else { num3 = num2;

} if (num1 < num2 + 10) {

num4 = num1; } else if (num1 < num2 + 20)

{ num5 = num1; }

cout << "num1 = " << num1 << " num2 = " << num2 << " num3 = " << num3 << " num4 = " << num4 << " num5 = " << num5 << endl;

return 0; }

Assuming that the user enters 20 and 12 as

the two input values, what is the output of

the code snippet?

a. num1 = 20 num2 = 12 num3 = 20 num4 =

20 num5 = 0 b. num1 = 20 num2 = 12 num3 = 12 num4 = 0

Page 16: uoit calculus 2 midterm

num5 = 20 *c. num1 = 20 num2 = 12 num3 = 12 num4 =

20 num5 = 0 d. num1 = 20 num2 = 12 num3 = 20 num4 = 0

num5 = 20

32. What is the output of the following code

snippet?

int i = 1;

while (i < 10)

{ cout << i << " "; i = i + 2;

if (i == 5) { i = 9;

} }

a. 1 3 5 *b. 1 3 9 c. 1 3 5 7 d. 1 3 5 9

33. What is the result of the following

snippet?

int son_age = 5;

int father_age = son_age * 3 + 5;

a. son_age = 5 and father_age = 15 *b. son_age = 5 and father_age = 20 c. son_age = 15 and father_age = 45 d. son_age = 15 and father_age = 50

Page 17: uoit calculus 2 midterm

34. Which one of the following errors

represents a part of a program that is

incorrect according to the rules of the

programming language?

*a. Syntax errors b. Run-time errors c. Logical errors d. Out-of-memory errors

35. What will be the output of the following

code snippet?

bool token1 = true; while (token1) {

for (int i = 0; i < 10; i++) { cout << "Hello" << endl;

} token1 = false; }

a. No output. b. No output because of compilation

error. *c. “Hello” will be displayed 10 times. d. “Hello” will be displayed infinite

times.

36. Consider a situation where multiple if

statements are combined to evaluate a

Page 18: uoit calculus 2 midterm

complex condition. Which of the following

reserved words is used to define the branch

to be executed when none of the conditions

are true?

a. if b. else if *c. else d. All of the above items

37. What is the outcome of the following

code snippet?

bool val1 = true;

bool val2 = false;

while (val1)

{

if (val1)

{

cout << "What do you C?" << endl;

}

val1 = val2;

}

a. No output will be displayed because of

compilation error. *b. “What do you C?” will be displayed

only once. c. “What do you C?” will be displayed

infinite times. d. No output will be displayed even after

successful compilation of the code

snippet.

Page 19: uoit calculus 2 midterm

38. What is the output of the following code

snippet?

#include <iostream>

using namespace std; int main() {

int value = 3; value++; cout << value << endl;

return 0;

}

a. 2 b. 3 *c. 4 d. No output due to syntax error

39. In C++, which of the following orderings

is used to compare strings?

*a. Lexicographic b. Semantic c. Cyclic d. Syntactic

40. Which of the following conditions tests

for the failure of the user to assign an

integer value to the number variable?

int number;

cout << "Number: "; cin >> number;

a. if (fail(number)) *b. if (cin.fail())

Page 20: uoit calculus 2 midterm

c. if (cin.fail) d. if (number < 0)

41. Which of the following code snippets

displays the output exactly 10 times?

a. int i = 0;

while (i < 10);

{

cout << "This is example 1." << endl; i++;

} *b. int i = 0;

while (i < 10) {

cout << "This is example 2." << endl; i++; } c. int i = 0;

while (i < 10) { cout << "This is example 3." << endl;

}; d. int i = 0;

while (i < 10);

{ cout << "This is example 4." << endl; i++;

};

42. Consider the following code snippet:

bool attendance = true; bool failed = false;

Which of the following if statement includes

a condition that evaluates to true?

Page 21: uoit calculus 2 midterm

*a. if (attendance == "true") { ... } b. if (attendance) { ... } c. if (failed) { ... } d. if (attendance == failed) { ... }

43. What is the output of the following code

snippet?

#include <iostream> using namespace std;

int main() { short x;

double d = 366.25; x = d / 100; cout << x << endl;

return 0; }

*a. 3 b. 3.6625 c. 3.7 d. No output because the code snippet

generates compilation errors

44. Assuming that the valid cost should be

between 100 and 200, does the following code

snippet test this condition correctly?

int main()

{ const int MIN_COST = 100; const int MAX_COST = 200;

int cost = 0;

Page 22: uoit calculus 2 midterm

cout << "Please enter the cost: "; cin >> cost;

if (cost < MIN_COST) { cout << "Error: The cost is too low.";

return 0; } else if (cost > MAX_COST)

{ cout << "Error: The cost is too high.";

}

else { cout << "The cost entered is in the

valid cost range."; } return 0;

}

*a. This code snippet ensures that the

cost value is between 100 and 200. b. This code snippet only ensures that

the cost value is greater than 100. c. This code snippet only ensures that

the cost value is less than 200. d. This code snippet ensures that the

cost value is either less than 100 or

greater than 200.

45. What is the output of the following code

snippet?

int i = 1; while (i < 20) {

cout << i << " ";

i = i + 2;

Page 23: uoit calculus 2 midterm

if (i == 15) {

i = 19; } }

a. 1 3 5 7 9 11 13 15 17 19 *b. 1 3 5 7 9 11 13 19 c. 1 3 5 7 9 11 13 15 17 d. 1 3 5 7 9 11 13 17 19

46. What will be the output of the following

code snippet?

bool token1 = true; while (token1)

{ for (int i = 0; i < 5; i++) {

cout << "Hello there" << endl; } token1 = false;

}

a. No output. b. No output because of compilation

error. *c. “Hello there” will be displayed 5

times. d. “Hello there” will be displayed

infinite times.

47. What is the output of the following code

snippet, if the input is 25?

#include <iostream> using namespace std;

Page 24: uoit calculus 2 midterm

int main() {

int i; cout << "Please enter a number: "; cin >> i;

if (i > 25) { i++;

} else

{

i--; } cout << i << endl;

return 0; }

*a. 24 b. 25 c. 26 d. 27

48. What is the output of the code snippet

given below?

int i; int j = 0; for (i = 0; i < 5; i++)

{ if (i % 2 == 0)

{

i = i + 2; j++; }

else { i++;

j = j + 2;

Page 25: uoit calculus 2 midterm

} j++;

} cout << "i=" << i << ", j=" << j;

a. i=7, j=7 b. i=7, j=6 c. i=6, j=7 *d. i=5, j=5

49. Assuming that a user enters 64 as his

golf score, what is the output of the

following code snippet?

int main() { int golf_score = 0;

cout << "Enter your golf score: "; cin >> golf_score; if (golf_score < 60) { cout <<

"Astounding!" << endl; } if (golf_score >= 60 && golf_score < 70) { cout << "Professional!" << endl; }

if (golf_score >= 70 && golf_score < 80) { cout << "Pretty good!" << endl; } if (golf_score >= 80 && golf_score < 90) {

cout << "Not so hot!" << endl; } if (golf_score >= 90) { cout << "Keep your day job!" << endl; }

return 0; }

a. Astounding! *b. Professional! c. Pretty good! d. Keep your day job!

Page 26: uoit calculus 2 midterm

50. Which of the following code snippets

will generate a random number between 0 and

79?

*a. srand (time(NULL));

val = rand() % 80; b. srand (time(NULL));

val = rand() % 80 - 1; c. srand (time(NULL));

val = rand() % 79; d. srand (time(NULL));

val = rand() * 80;