Xyz

Page 1

Sheet # 1 Student Name :

Sec:

Q1: MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) In a C++ program, two slash marks ( // ) indicate: A) the end of the program B) the end of a statement C) the beginning of a block of code D) the beginning of a comment E) None of these 2) The ________ is/are used to display information on the computer's screen. A) backslash B) opening and closing quotation marks C) cout object D) opening and closing braces E) None of these 3) ________ represent storage locations in the computer's memory. A) Integers B) Literals C) Variables D) Comments E) None of these 4) Of the following, which is a valid C++ identifier? A) myExtraLongVariableName B) ___department C) _employee_number D) June1997 E) All of the above are valid identifiers. 5) The numeric data types in C++ can be broken into two general categories: A) real and unreal B) numbers and characters C) singles and doubles D) integer and floating point E) None of these 6) Which escape sequence causes the cursor to move to the beginning of the current line? A) \n B) \t C) \r D) \a 7) What will the following code display? int number = 7; cout << "The number is " << "number" << endl;

A) The number is number C) The number is 7

B) The number is7 D) The number is 0

1

E) \b


8) What will the following code display? int x = 0, y = 1, z = 2; cout << x << y << z << endl;

A) 0 1 2

B) 012

C) xyz

D) 0 1 2

9) What will the following code display? cout << "Four\n" << "score\n"; cout << "and" << "\nseven"; cout << "\nyears" << " ago" << endl;

A) Four score and seven years ago

B) Four score and seven years ago D) Four score and seven years ago

C) Four score and seven years ago

10) What will the value of x be after the following statements execute? int x; x = 18 / 4;

A) 4.5

B) 0

C) 4

11) Which statement is equivalent to the following? number += 1;

A) number = 1; C) number = number + 1;

B) number + 1; D) None of these

2

D) unknown


12) After execution of the following code, what will be the value of input_value if the value 0 is entered at the keyboard at run time? cin >> input_value; if (input_value > 5) input_value = input_value + 5; else if (input_value > 2) input_value = input_value + 10; else input_value = input_value + 15;

A) 10

B) 15

C) 25

D) 5

E) 0

13) Assume that a program has the following string object definition: string name; Which of the following statements correctly assigns a string literal to the string object? A) name = (Jane); B) name = “Jane”; C) name = Jane; D) name = ‘Jane’;

14) Relational operators allow you to ________ numbers. A) average B) add C) compare D) multiply E) None of these 15) Which line in the following program will cause a compiler error? 1 2 3 4 5 6 7 8 9 10

#include <iostream> using namespace std; int main( ) { const int MY_VAL = 77; MY_VAL = 99; cout << MY_VAL << endl; return 0; }

A) 6

B) 8

C) 7

3

D) 9


16) Which line in the following program will cause a compiler error? 1 2 3 4 5 6 7 8 9 10

#include <iostream> using namespace std; int main( ) { const int MY_VAL; MY_VAL = 77; cout << MY_VAL << endl; return 0; }

A) 6

B) 7

C) 9

D) 8

C) x = x * x;

D) None of these

17) Which statement is equivalent to the following? x = x * 2;

A) x *= 2;

B) x * 2;

18) What will the value of x be after the following statements execute? int x = 0; int y = 5; int z = 4; x = y + z * 2;

A) 18

B) 13

C) 0

D) unknown

19) What will the value of result be after the following statement executes? result = 6 - 3 * 2 + 7 - 10 / 2 ;

A) 1.5

B) 2

C) 6

20) When using the sqrt function you must include this header file. A) iostream B) cstdlib C) cstring

D) 8

D) cmath

E) iomanip

21) Assume that x is an int variable. What value is assigned to x after the following assignment statement is executed? x = -3 + 4 % 6 / 5;

A) 1 B) 2 C) -3 D) 0 E) None of these

4


22) The function, pow(x, 5.0), requires this header file. A) cmath B) cstring C) iomanip

D) cstdlib

E) iostream

23) What is the value stored at x, given the statements: int x; x = 3 / static_cast<int>(4.5 + 6.4); A) .275229 B) .3 C) 0 D) 3.3 E) None of these

24) When this operator is used with string operands it concatenates them, or joins them together. A) * B) & C) % D) + E) None of these 25) The ________ operator always follows the cin object, and the ________ operator follows the cout object. A) conditional, binary B) binary, unary C) >>, << D) <<, >> E) None of these 26) The ________ causes a program to wait until information is typed at the keyboard and the Enter key is pressed. A) cout object B) cin object C) output stream D) preprocessor E) None of these 27) What will the value of x be after the following statements execute? int x; x = 18 % 4;

A) 2

B) 4

C) 0.45

5

D) unknown


28) What will the following code display? cout << "Monday"; cout << "Tuesday"; cout << "Wednesday";

A) Monday Tuesday Wednesday

B) "Monday" "Tuesday" "Wednesday" D) Monday Tuesday Wednesday

C) MondayTuesdayWednesday

29) Look at the following program and answer the question that follows it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

// This program displays my gross wages. // I worked 40 hours and I make $20.00 per hour. # include <iostream> using namespace std; int main( ) { int hours; double payRate, grossPay;

}

hours = 40; payRate = 20.0; grossPay = hours * payRate; cout << "My gross pay is $" << grossPay << endl; return 0;

Which line(s) in this program cause output to be displayed on the screen? A) 13 B) 8 and 9 C) 15 D) 13 and 14

E) 14

30) Which one of the following would be an illegal variable name? A) itemsorderedforthemonth B) dayOfWeek C) 3dGraph D) June1997 E) _employee_num 31) This control sequence is used to skip over to the next horizontal tab stop. A) \h B) \' C) \a D) \n 32) A variable whose value can be either true or false is of this data type. A) T/F B) float C) bool D) binary E) None of these

6

E) \t


33) Every complete C++ program must have a __________. A) symbolic constant B) preprocessor directive C) function named main D) comment E) cout statement 34) How would you consolidate the following declaration statements into one statement? int x = 7; int y = 16; int z = 28;

A) int x = 7, y = 16, z = 28; B) int x = 7; y = 16; z = 28; C) int x, y, z = 7, 16, 28 D) int x = 7 y = 16 z = 28; E) None of these will work. 35) What will be the output of the following code segment after the user enters 0 at the keyboard? int x = -1; cout << "Enter a 0 or a 1 from the keyboard: "; cin >> x; if (x) cout << "true" << endl; else cout << "false" << endl;

A) Nothing will be displayed. C) false

B) x D) true

36) What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y;

A) 10 B) The string "x >= y" C) 7 D) 0 E) 1

7


37) If you place a semicolon after the statement if (x < y)

A) the compiler will interpret the semicolon as a null statement B) the if statement will always evaluate to false C) the code will not compile D) All of the above E) None of these 38) What will following segment of code output? int x = 5; if (x = 2) cout << "This is true!" << endl; else cout << "This is false!" << endl; cout << "This is all folks!" << endl;

A) This is true! This is all folks! B) This is true! C) This is false! D) This is true! This is false! E) None of these 39) What will the following segment of code output? You can assume the user enters a grade of 90 from the keyboard. cout << "Enter a test score: "; cin >> test_score; if (test_score < 60); cout << "You failed the test!" << endl; if (test_score > 60) cout << "You passed the test!" << endl; else cout << "You need to study for the next test!";

A) You passed the test! B) You failed the test! You passed the test! C) You failed the test! You did poorly on the test! D) You failed the test! E) None of these

8


40) When an if statement is placed within the conditionally-executed code of another if statement, this is known as: A) nesting B) validation C) complexity D) overloading E) None of these 41) What is the output of the following segment of code if 4 is input by the user when asked to enter a number? int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 4: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout << total << endl;

A) 13 B) 3 C) 28 D) 0 E) None of these 42) What will the following segment of code output? score = 40; if (score > 95) cout << "Congratulations!\n"; cout << "That's a high score!\n"; cout << "This is a test question!" << endl;

A) This is a test question! B) That's a high score! This is a test question! C) Congratulations! That's a high score! D) Congratulations! That's a high score! This is a test question! E) None of these

9


43) This operator represents the logical AND. A) || B) && C) @ D) ++ E) None of these 44) This operator is known as the logical OR operator. A) // B) || C) -D) # E) None of these 45) This operator performs a logical NOT operation. A) ! B) -C) <> D) >< E) None of these 46) What will the following program display? # include <iostream> using namespace std; int main( ) { int a = 0, b = 2, x = 4, y = 0; cout << (a == b) << " "; cout << (a != b) << " "; cout << (b <=x) << " "; cout << (y > a) << endl; return 0; }

A) 0 0 1 0 B) 1 1 0 1 C) 0 1 1 0 D) 1 0 0 1 E) None of these 47) What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2;

A) 0

B) 10

C) 2

10

D) 12


48) This loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop. A) for B) while C) do-while D) infinite E) None of these 49) This is a pre-test loop that is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. A) while B) infinite C) for D) do-while E) None of these 50) This statement causes a loop to terminate early. A) stop B) null C) terminate D) break E) None of these 51) If you want a user to enter exactly 20 values, which loop would be the best to use? A) do-while B) while C) for D) infinite E) None of these 52) This statement may be used to stop a loop's current iteration and begin the next one. A) return B) break C) continue D) terminate E) None of these

11


53) What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; }

A) 0 1 2 3 4 5 B) 0 1 2 3 4 C) 01 2 3 4 D) The loop will display numbers starting at 0, for infinity. 54) What will the following code display? int number = 6; number++; cout << number << endl;

A) 7

B) 6

C) 5

D) 0

C) 0

D) 5

C) 0

D) 7

55) What will the following code display? int number = 6; ++number; cout << number << endl;

A) 6

B) 7

56) What will the following code display? int number = 6; cout << number++ << endl;

A) 5

B) 6

12


57) What will the following code display? int number = 6; cout << ++number << endl;

A) 6

B) 0

C) 7

D) 5

C) 7

D) 6

C) 6

D) 0 1 2

58) What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl;

A) 0

B) 5

59) What will the following code display? int x = 0; for (int count = 0; count < 3; count++) x += count; cout << x << endl;

A) 3

B) 0

60) How many times will the following loop display "Hello"? for (int i = 0; i < 20; i++) cout << "Hello!" << endl;

A) 21 C) 19

B) 20 D) an infinite number of times

61) How many times will the following loop display "Hello"? for (int i = 1; i < 20; i++) cout << "Hello!" << endl;

A) 21 C) 19

B) 20 D) an infinite number of times

62) How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl;

A) 19 C) 20

B) 21 D) an infinite number of times

13


63) How many times will the following loop display "Hello"? for (int i = 20; i > 0; i--) cout << "Hello!" << endl;

A) 19 C) 21

B) 20 D) an infinite number of times

64) What will the following code display? int number = 6 int x = 0; x = --number; cout << x << endl;

A) 0

B) 6

C) 7

65) What is the output of the following code segment? n = 1; for ( ; n <= 5; ) cout << n << ' '; n++;

A) 1 B) 1 C) 2 D) 1 E) 2

2 2 3 1 3

3 3 4 1 4

4 4 5 5 ... and on forever 5 6

66) Look at the following statement. while (x++ < 10) Which operator is used first? A) ++ B) < C) Neither. The expression is invalid.

67) This means to increase a value by one. A) modulus B) decrement C) increment D) parse E) None of these

14

D) 5


68) This statement may be used to stop a loop's current iteration and begin the next one. A) break B) continue C) re-iterate D) terminate E) None of these 69) You may define a ________ in the initialization expression of a for loop. A) new data type B) constant C) variable D) function E) None of these 70) In a for statement, this expression is executed only once. A) initialization B) null C) test D) validation E) None of these 71) A for statement contains three expressions: initialization, test, and: A) reversal B) validation C) null D) update E) None of these 72) The statements in the body of a while loop may never be executed, whereas the statements in the body of a do-while loop will be executed: A) never B) at least twice C) at least once D) as many times as the user wishes E) None of these 73) What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++;

A) 2 B) 2 C) 1 D) 1 E) 1

3 3 2 1 2

4 5 6 4 5 3 4 5 1... and on forever 3 4

15


74) These are operators that add and subtract one from their operands. A) conditional and relational B) plus and minus C) ++ and -D) binary and unary E) None of these 75) What is the output of the following code? int w = 98; int x = 99; int y = 0; int z = 1; if (x >= 99) { if (x < 99) cout << y << endl; else cout << z << endl; } else { if (x == 99) cout << x << endl; else cout << w << endl; }

A) 99

B) 98

C) 1

D) 0

76) Given that x = 2, y = 1, and z = 0, what will the following cout statement display? cout << "answer = " << (x || !y && z) << endl;

A) answer = 2

B) answer = 0

C) answer = 1

77) In C++ the = operator indicates: A) negation B) equality C) assignment D) subtraction E) None of these 78) This operator is used in C++ to represent equality. A) >< B) !! C) = D) == E) None of these

16

D) None of these


79) Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression. A) exit B) break C) scope D) switch E) None of these 80) This operator takes an operand and reverses its truth or falsehood. A) ! B) relational C) arithmetic D) || E) None of these 81) Assuming x is 5, y is 6, and z is 8, which of the following is false? 1. 2. 3. 4. 5. 6. 7.

x == 5; 7 <= (x + 2); z < = 4; (1 + x) != y; z >= 8; x >= 0; x <= (y * 2)

A) Only 5 is false. B) 3 and 4 are false. C) 3, 4, 6, 7 are false. D) All are false. E) None of these 82) When a relational expression is false, it has the value ________. A) zero B) one C) zero, one, or minus one D) less than zero E) None of these 83) Which character signifies the beginning of an escape sequence? A) / B) # C) \ 84) This is used to mark the end of a complete C++ programming statement. A) void B) semicolon C) pound sign D) data type E) None of these

17

D) //

E) {


85) What is the output of the following statement? cout << 4 * (15 / (1 + 3)) << endl;

A) 63 B) 72 C) 12 D) 15 E) None of these 86) Which data type typically requires only one byte of storage? A) short B) double C) float

D) char

E) int

87) What is the modulus operator? A) & B) +

D) %

E) *

C) ||

88) A character literal is enclosed in ________ quotation marks, whereas a string literal is enclosed in ________ quotation marks. A) triple, double B) open, closed C) single, double D) double, single E) None of these

18


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.