CPSC 1020 Exam 1 Questions
What does the following C++ expression evaluate to? 6 - 6 / 3 + 3 A- 7 B- none of these C- 5 D- 0 E- 3
A- 7
What value will be assigned to the variable number by the following statement? int number = 7.8; A- 7 B- none of these C- it's unpredictable. That's the problem. D- 7.8 E- 8
A- 7
A flag is a variable, usually of data type ________, that signals whether or not some condition exists. A- bool B- logical C- string D- int E- char
A- bool
The ________ statement acts like a chain of if statements. Each performs its test, one after the other, until one of them is found to be true or until the construct is exited without any test ever evaluating to true. A- if/else if B- if/else C- if/then D- if/endif E- if/not if
A- if/else if
When a program lets the user know that an invalid menu choice has been made, this is an example of A- input validation B- menu reselection C- being user friendly D- invalidation E- output validation
A- input validation
To use stream manipulators, you should include the ________ header file. A- iomanip B- format C- stream D- fstream E- iostream
A- iomanip
The following statement number = rand() % 5 + 10; assigns number a value in the range of A- none of these B- 0-15 C- 10-15 D- 5-10 E- 5-15
A- none of these
#include <iostream> is an example of a(n) A- preprocessor directive B- comment C- stream directive D- I/O statement E- compiler option
A- preprocessor directive
In any program that uses the cin object, you must #include A- the iostream header file B- named constants C- none of these D- >> and << operators E- the fstream header file
A- the iostream header file
In C++, a value can be raised to a power by using A- the pow function B- the exp operator C- the square function D- the power operator E- the ^ operator
A- the pow function
The ________ operator is used in C++ to test for equality. A- && B- == C- || D- = E- <>
B- ==
The following 4 lines of C++ code, use strings. string firstName; //Define a string object char lastName[7]; //Define a C-string firstName = "Abraham"; //Assign a value to the string object lastName = Lincoln"; //Assign a value to the C-string A- all 4 lines of code are correct B- the string object is assigned a value correctly, but the C-string is not C- The C-string is assigned a value correctly, but the string object is not D- The string object is defined incorrectly because no size is given for it E- The 2 string definitions are correct, but the 2 assignment statements are wrong
B- The string object is assigned a value correctly, but the C-string is not
Which of the following expressions will evaluate to 2.5? A- static_cast<double>(5/2) B- both static_cast<doubles>(5)/2 and 5/static_cast<double>(2), but not static_cast<double>(5/2) C- static_cast<double>(5)/2 D- 5/static_cast<double>(2) E- all three of these
B- both static_cast<double>(5)/2 and 5/static_cast<double>(2), but not static_cast<double>(5/2)
Which of the following will cause the next output to begin on a new line? A- cout << "endl"; B- cout << endl; C- cout << endl; and cout << "/n"; but not cout << "endl"; D- all of these E- cout << "/n"
B- cout << endl;
Program code that can be evaluated to a value is called a(n) A- result B- expression C- operation D- evaluator E- line
B- expression
________ are data items whose values cannot change while the program is running. A- variables B- literals C- fixed data D- integers E- none of these
B- literals
A sentinel is a special value that A- is used for data validation B- marks the end of a list of values C- is all of these D- must be a negative number E- must be boolean
B- marks the end of a list of values
________ can be used to override the rules of operator precedence. A- operands B- parentheses C- nothing D- some operators E- associativity
B- parentheses
The while loop has two important parts: a condition that is tested and a statement or block of statements that is A- repeated until the condition becomes true B- repeated as long as the condition is true C- always skipped D- always done at least once, then repeated if condition is true E- done once if the condition is true
B- repeated as long as the condition is true
C++ automatically places ________ at the end of a string literal. A- quotation marks B- the null terminator C- a newline escape sequence D- a semicolon E- a blank
B- the null terminator
You may define a(n) ________ in the initialization expression of a for loop. A- new data type B- variable C- constant D- new keyboard E- operator
B- variable
The ________ directive causes the contents of another file to be inserted into a program. A- #library B- #getfile C- #include D- #insert E- none of these
C- #include
In a C++ program, two slash marks ( // ) indicate the beginning of A- a program B- a variable definition C- a comment D- a block of code E- none of these
C- a comment
Hand tracing a program is A- a program run-time testing technique B- none of these C- a program debugging technique D- a program design technique E- creating a drawing of what a program's output screen will look like
C- a program debugging technique
To use an output file in a C++ program you must A- do all of these B- make sure the file already exists C- do create a file stream object that will "point to" (i.e. reference) the file and open the file, but not make sure the file already exists D- create a file stream object that will "point to" (i.e. reference) the file E- open the file
C- do create a file stream object that will "point to" (i.e. reference) the file and open the file, but not make sure the file already exists
In a for statement, the ________ expression is executed only once. A- test B- validate C- initialization D- update E- repeat
C- initialization
Which of the following statements will assign number a value from 90 — 100. A- number = rand() % 10 + 90; B- number = rand() % 90 + 10; C- number = rand() % 11 + 90; D- number = rand() % 90 + 11; E- none of these
C- number = rand() % 11 + 90;
The cin object must be followed by A- a single stream insertion (<<) operator B- one or more stream insertion operators (<<) C- one or more stream extraction (>>) operators D- no operators E- a single stream extraction (>>) operator
C- one or more stream extraction (>>) operators
When converting some algebraic expressions to C++, you may need to insert ________ and ________ that do not appear in the algebraic expression. A- operands, parentheses B- operators, operands C- operators, parentheses D- values, exponents E- operators, calculations
C- operators, parentheses
The while loop is a(n) ________ loop, whereas the do-while loop is a(n) ________ loop. A- infinite, finite B- post test, pretest C- pretest, post test D- simple, complex E- finite, infinite
C- pretest, post test
The expression x < y is called a(n) ________ expression. A- comparison B- arithmetic C- relational D- logical E- binary
C- relational
The ________ stream manipulator can be used to establish a field width for the value immediately following it. A- width B- setField C- setw D- iomanip E- cin
C- setw
When the final value of an expression is assigned to a variable, it will be converted to A- the data type of the expression B- none of these C- the data type of the variable D- the smallest possible data type E- the largest possible data type
C- the data type of the variable
The default section of a switch statement performs a similar task as the ________ portion of an if/else if statement. A- body B- break C- trailing else D- conditional test E- else if
C- trailing else
The ________ operator is known as the logical OR operator. A- ! B- // C- || D- & E- &&
C- ||
Which of the following is/are valid C++ identifiers? A- June-2010 B- 2010June C- Both June_2010 and 2010June D- June_2010 E- June.2010
D- June_2010
A variable that keeps a running total of data values is called a(n) A- sum B- loop control variable C- counter D- accumulator E- total
D- accumulator
What will the following statement do if x equals 17 and answer = 20? answer = x > 100 ? 0 : 1; A- assign 1 to x B- assign 0 to answer C- assign 17 to answer D- assign 1 to answer E- assign 0 to x
D- assign 1 to answer
The ________ object causes data to be input from the keyboard. A- cout B- standard input C- dataIn D- cin E- keyboard buffer
D- cin
The statement cout << setw(4) << num4 << " "; A- outputs the value of num4 rounded to 4 decimal places B- outputs the first 4 digits of the number stores in num4 C- outputs the value stored in num4 four times D- does none of these E- output "setw(4)" before the value in the variable num4
D- does none of these
The ________ statement executes one statement, or block of statements, if a condition is true and skips it, doing nothing, if the condition is false. A- if/endif B- if/else if C- if/else D- if E- switch
D- if
Operator associativity is either left to right or A- nothing else; it is always left to right B- top to bottom C- inside to outside D- right to left E- front to back
D- right to left
A C++ character literal is enclosed in ________ quotation marks, whereas a string literal is enclosed in ________ quotation marks. A- triple, double B- open, closed C- double, single D- single, double E- no, some
D- single, double
________ causes a program to wait until information is typed at the keyboard and the Enter key is pressed. A- nothing B- the cout object C- a preprocessor D- the cin object E- an input device
D- the cin object
A for statement contains three expressions: initialization, test, and A- increment B- repeat C- quit D- update E- validate
D- update
For data validation, it is best to use a(n) A- switch statement B- nested loop C- for loop D- while loop E- if statement
D- while loop
If s1 and s2 are string objects, s1 == s2 is true when A- s1 = "cat" and s2 = "cat" B- none of these because string objects cannot be compared with regional operators C- s1 = "dog" and s2 = "DOG" D- s1 = "lion" and s2 = "lioness" E- none of these because in each case one or more characters in the strings have different ASCII codes
E- none of these because in each case one or more characters in the strings have different ASCII codes
When an arithmetic expression contains two or more different operators, such as * and +, the order in which the operations is done is determined by A- left to right order B- the compiler C- operator associativity D- the programmer E- operator precedence
E- operator precedence
What will the following expression evaluate to? ! (6 > 7 || 3 == 4) A- 6 B- 0 C- -1 D- false E- true
E- true
True/False: A switch statement branches to a particular block of code depending on the value of a numeric (i.e. integer or floating-point) variable or constant.
False
True/False: A variable of the char data type can hold a set of characters like "January".
False
True/False: A while loop is somewhat limited, because the counter can only count up, not down.
False
True/False: A while loop may have a semicolon after the test expression and before the body of the loop, but it is not required.
False
True/False: If number is an int variable, both of the following statements will print out its value: cout << number; cout << "number";
False
True/False: If the sub-expression on the left side of an && operator is true, the expression on the right side will not be checked.
False
True/False: If the value of dollars is 5.0, the following statement will output 5.00 to the monitor: cout << fixed << showpoint << setprecision(4) << dollars << endl;
False
True/False: In C++, the 5 basic arithmetic operators are addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (^).
False
True/False: Relational operators connect two or more relational expressions into one, or reverse the logic of an expression.
False
True/False: The cin object lets the user enter a string that contains embedded blanks.
False
True/False: The following C++ statement will assign 1.5 to the result variable. int result = 3.0 / 2.0;
False
True/False: The following C++ test checks if the variable child is in the range 3 - 12. if (child >= 3 && <= 12)
False
True/False: The following pair of C++ statements is legal. const double taxRate; taxRate = .05;
False
True/False: The following pair of C++ statements will cause 3.5 to be output. double number = 7 / 2; cout << numbers;
False
True/False: The following statement sets the value of total to -3. total -= 3;
False
True/False: The following statements will not print anything. x = 5; if (x < 5) cout << "Hello "; cout << "world \n";
False
True/False: The following two C++ statements perform the same operation. wages = regPay + overTime; regPay + overTime = wages;
False
True/False: The only difference between C-strings and string objects is how they are declared and internally stored. They are used exactly the same way.
False
True/False: The scope of a variable is the program it is defined in.
False
True/False: To decrement a number means to increase its value.
False
True/False: When a C++ expression is evaluated, binary operations are performed before unary operations.
False
True/False: When a loop is nested inside another loop, the outer loop goes through all its iterations for each iteration of the inner loop.
False
True/False: When an operator has two operands of different data types, C++ always converts them both to double before performing the operation.
False
True/False: An expression in a C++ if statement that evaluates to 5, -5, or for that matter anything other than 0, is considered true.
True
True/False: An initialization expression may be omitted from the for loop if no initialization is required.
True
True/False: Assuming moreData is a Boolean variable, the following two tests are logically equivalent. if (moreData == true) if (moreData)
True
True/False: C++ is a case-sensitive language.
True
True/False: One reason for using functions is to break programs into a set of manageable units, or modules.
True
True/False: Relational expressions and logical expressions are both Boolean, which means they evaluate to true or false.
True
True/False: The block of code in the body of a while statement can contain an unlimited number of statements, provided they are enclosed in a set of braces.
True
True/False: The cin object can be used to input more than one value in a single statement.
True
True/False: The following statement sets sum1, sum2, and sum3 all to zero. sum1 = sum2 = sum3 = 0;
True
True/False: The following two expressions evaluate to the same thing: c + a * b c + (a * b)
True
True/False: The following two statements will assign the same value to result. result = a + b * c; result = b * c + a;
True
True/False: When an operator's operands are of different data types, such as int and double, C++ automatically converts one of them so that they are the same data type.
True
________ are C++ operators that change their operands by one. A- binary and unary B- arithmetic and relational C- conditional and relational D- + and - E- ++ and --
E- ++ and --
The expression 5 % 2 evaluates to A- 2.5 B- 10 C- 2 D- 5.2 E- 1
E- 1
The expression 5 / 2 evaluates to A- 1 B- 2.5 C- 10 D- 5.2 E- 2
E- 2
What will the following code print? num = 8; cout << --num << " "; cout << num++ << " "; cout << num; A- 7 8 8 B- none of these C- 8 7 8 D- 8 7 7 E- 7 7 8
E- 7 7 8
The ________ operator always follows the cin object, and the ________ operator follows the cout object. A- <<, >> B- >>, >> C- input, endl D- getChar, printChar E- >>, <<
E- >>, <<
Which of the following statements about named constants is/are true? A- All 3 of these statements are true B- The identifier name of a named constant can only contain capital letters and underscores C- A named constant must be initialized with a value at the same time it is declared D- The initial value of a named constant, cannot be changed during the program execution E- A named constant must be initialized with a value at the time it is declared and the initial value of a named constant, cannot be changed during the program execution are true, but the identifier name of a named constant can only contain capital letters and underscores is not.
E- A named constant must be initialized with a value at the time it is declared and the initial value of a named constant, cannot be changed during the program execution are true, but the identifier name of a named constant can only contain capital letters and underscores is not.
The ++ operator A- both adds one to the value of its operand and can operate in prefix or postfix mode, but not is a unary operator B- adds one to the value of its operand C- is a unary operator D- can operate in prefix or postfix mode E- all of these
E- all of these
Which of the following definitions will allow the variable total to hold floating-point values? A- float total; and double total; but not auto total = 0.0; B- float total; C- auto total - 0.0; D- double total; E- all of these
E- all of these
The statements in the body of a do-while loop are executed A- until the test condition becomes true B- forever until the user hits the break key C- exactly once D- only if the test condition is initially true E- at least once
E- at least once
Which of the following statements doubles the value stored in answer? A- all three of these B- answer += 2; C- answer = answer * 2; D- answer *= 2; E- both answer *= 2; and answer = answer * 2; but not answer += 2;
E- both answer *= 2; and answer = answer * 2; but not answer += 2;
To use the sqrt() function, or other mathematical library functions, you must #include the ________ header file in your program. A- iomanip B- algebra C- iostream D- mathlib E- cmath
E- cmath
The ________ is used to display information on the computer's screen. A- print object B- cin object C- output object D- < symbol E- cout object
E- cout object
You must have a(n) ________ for every variable you include in a program. A- output statement B- purpose C- comment D- numeric value E- definition
E- definition
To use files in a C++ program you must include the ________ header file. A- both iostream and fstream B- file C- both file and iostream D- iostream E- fstream
E- fstream
Which of the following will allow an entire line of text to be read into a string object, even if it contains embedded blanks? A- cin >> B- cin.ignore() C- both getline() and cin >>, but not cin.get() or cin.ignore() D- cin.get() E- getline()
E- getline()
The bool data type A- can be used to store a single character B- does none of these C- is used to represent numbers in E notation D- is used to store extra-large numbers E- has only two values: true and false
E- has only two values: true and false