CISP 31 Midterm
The following program displays #include <iostream> using namespace std; int main() { cout << "A"; cout << "B"; return 0; } Select one: a. B A b. BA c. AB d. A B
AB
What is a expression
An expression is a sequence of operators and their operands, that specifies a computation.
A block is enclosed inside Select one: a. Quotes b. Parentheses c. Braces d. Brackets
Braces
__________ is the brain of a computer. Select one: a. Hardware b. CPU c. Memory d. Disk
CPU
Casting does not what
Casting does not change the variable being cast. For example, d is not changed after casting in the following code: double d = 4.5; int i = static_cast<int>(d); // d is not changed
Why computers use 0 and 1
Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention.
Does the return statement in the following function cause syntax errors? void f() { int max = 0; if (max != 0) cout << max; else return; } Select one: a. No b. Yes
No
Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; } Select one: a. Yes b. No
No
What variable controls a loop
Often the number of times a loop is executed is not predetermined. You may use an input value to signify the end of the loop. Such a value is known as a sentinel value or control variabel loop
A character is stored in Select one: a. four bytes b. three bytes c. two bytes d. one byte
One byte
Logical Operators
!, &&, ||
Which of the following expression will yield 0.5? Select one: a. 1.0 / 2 b. (double) (1 / 2) c. 1 / 2 d. (int) 1 / 2 e. (float)(1 / 2)
1.0 / 2
How many times the following code prints "Welcome to C++"? int count = 0; do { cout << "Welcome to C++"; count++; } while (count < 10); Select one: a. 11 b. 9 c. 8 d. 0 e. 10
10
How many times the following code prints "Welcome to C++"? int count = 0; do { cout << "Welcome to C++"; } while (++count < 10); Select one: a. 0 b. 10 c. 11 d. 9 e. 8
10
How many times the following code prints "Welcome to C++"? int count = 0; while (count < 10) { cout << "Welcome to C++"; count++; } Select one: a. 11 b. 0 c. 10 d. 9 e. 8
10
How many times the following code prints "Welcome to C++"? int count = 0; while (count++ < 10) { cout << "Welcome to C++"; } Select one: a. 0 b. 11 c. 9 d. 10 e. 8
10
Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10). Select one: a. 11 b. 9 c. 10
10
What is the value in count after the following loop is executed? int count = 0; do { cout << "Welcome to C++"; } while (count++ < 9); cout << count; Select one: a. 8 b. 11 c. 0 d. 9 e. 10
10
Suppose void nPrint(char ch, int n) { while (n > 0) { cout << ch; n--; } } What is k after invoking nPrint('a', k)? int k = 2; nPrint('a', k); Select one: a. 0 b. 2 c. 3 d. 1
2
What is Math.floor(3.6)? Select one: a. 3 b. 5.0 c. 4 d. 2
3
What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5); Select one: a. 6 b. 8 c. 7 d. 5
6
Note that the ASCII for character A is 65. The expression 'A' + 1 evaluates to Select one: a. B b. Illegal expression c. A1 d. 66
66
The ASCII of 'a' is 97. What is the ASCII for 'c'? Select one: a. 98 b. 99 c. 97 d. 96
99
The "less than or equal to" comparison operator is __________. Select one: a. << b. != c. < d. <= e. =<
<= <=
Relational Operators
>=, <=, <, >, ==, !=
Function prototype
A function prototype is a function declaration without implementation. The implementation can be given later in the program.
What is Overflowing
When a variable is assigned a value that is too large to be stored, it causes overflow. For example, executing the following statement causes overflow, because the largest value that can be stored in a variable of the short type is 32767. 32768 is too large. short value = 32767 + 1;
What is the printout of the following switch statement? char ch = 'a'; switch (ch) { case 'a': case 'A': cout << ch << endl; break; case 'b': case 'B': cout << ch << endl; break; case 'c': case 'C': cout << ch << endl; break; case 'd': case 'D': cout << ch << endl; } Select one: a. ab b. aa c. a d. abcd
a
Suppose void nPrint(char ch, int n) { while (n > 0) { cout << ch; n--; } } What is the printout of the call nPrint('a', 4)? Select one: a. invalid call b. aaaaa c. aaaa d. aaa
aaaa
Why do computers use zeros and ones? Select one: a. because binary numbers are simplest. b. because digital devices have two stable states and it is natural to use one state for 0 and the other for 1. c. because combinations of zeros and ones can represent any numbers and characters. d. because binary numbers are the bases upon which all other number systems are built.
because digital devices have two stable states and it is natural to use one state for 0 and the other for 1.
To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159 Select one: a. constants b. variables c. classes d. functions
constants
Which of the following statement prints smith\exam1\test.txt? Select one: a. cout << "smith\"exam1\"test.txt"; b. cout << "smith"\exam1"\test.txt"; c. cout << "smith\\exam1\\test.txt"; d. cout << "smith\exam1\test.txt";
cout << "smith\\exam1\\test.txt";
The signature of the main function in C++ is __________. Select one: a. Main(String[] args) b. main(String[] args) c. int main() d. Main(String args[]) e. void main(String[] args)
int main()
Which of the following justifies the output to the left? Select one: a. fixed b. left c. right d. showpoint e. setw(width
left
The following code displays ______________. #include <iostream> using namespace std; void maxValue(int value1, int value2, int max) { if (value1 > value2) max = value1; else max = value2; } int main() { int max = 0; maxValue(1, 2, max); cout << "max is " << max << endl; return 0; } Select one: a. max is 1 b. max is 2 c. max is d. max is 0
max is 0
var--
postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.
var++
postincrement The expression (var++) evaluates to the original value in var and increments var by 1.
--var
predecrement The expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement.
++var
preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the increment.
To return an uppercase letter from char variable ch, use Select one: a. toupper(ch) b. isalpha(ch) c. isdigit(ch) d. tolower(ch) e. islower(ch)
toupper(ch)
To assign a value 1 to variable x, you write Select one: a. x == 1; b. x = 1; c. 1 := x; d. x := 1; e. 1 = x;
x = 1;
To add a value 1 to variable x, you write Select one: a. x = x + 1; b. x := 1; c. x = 1++ x; d. 1 + x = x; e. x ++= 1;
x = x + 1;
What is the output of the following code? #include <iostream> using namespace std; void f(double &p) { p += 2; } int main() { double x = 1; double y = 1; f(x); f(y); cout << "x is " << x; cout << " y is " << y << endl; return 0; } Select one: a. x is 1 y is 1 b. x is 2 y is 2 c. x is 3 y is 3 d. x is 1 y is 2 e. x is 2 y is 1
x is 3 y is 3
Numeric Operators
-, +, /, *, %
The extension name of a C++ source code file is Select one: a. .cpp b. .exe c. .java d. .class e. .obj
.cpp
Suppose x is 1. What is x after x -= 1? Select one: a. -2 b. 1 c. 2 d. 0 e. -1
0
What is the output of the following code? inline void print(int i) { cout << i << endl; } int main() { print(1); return 0; } Select one: a. 1 b. 0 c. nothing d. 2
1
What is the output of the following code? void f() { cout << 1 << endl; } int main() { f(); return 0; } Select one: a. nothing b. 1 c. 0 d. 0 1 e. 1 0
1
Does the function call in the following function cause syntax errors? #include <iostream> #include <math> using namespace std; int main() { pow(2.0, 4); } Select one: a. No b. Yes
No
Analyze the following code: #include <iostream> using namespace std; int xfunction(int n, long t) { cout << "int"; return n; } long xfunction(long n) { cout << "long"; return n; } int main() { cout << xfunction(5); } Select one: a. The program does not compile because the compiler cannot distinguish which xfunction to invoke. b. The program displays int followed by 5. c. The program runs fine but displays nothing. d. The program displays long followed by 5.
The program displays long followed by 5.
-=(assignement operator)
f -= 8.0 f = f - 8.0
What is the output of the following code? bool even = false; cout << (even ? "true" : "false") << endl; Select one: a. nothing b. true false c. true d. false
false
The signature of a function consists of ____________. Select one: a. function name and parameter list b. return type, function name, and parameter list c. function name d. parameter list
function name and parameter list
Programming style is important, because Select one: a. a program may not compile if it has a bad style b. good programming style can make a program run faster c. good programming style makes a program more readable d. good programming style eliminates programming errors
good programming style makes a program more readable
%=(assignement operator)
i %= 8 i = i % 8
*=(assignement operator)
i *= 8 i = i * 8
+= (assignement operator)
i += 8 i = i + 8
/=(assignement operator)
i /= 8 i = i / 8
What will be the output of the following code? #include <iostream> using namespace std; int j = 1; int main() { int i = 2; cout << "i is " << i << " j is " << j << endl; } Select one: a. i is 1 j is 2 b. i is 1 j is 1 c. i is 2 j is 1 d. i is 2 j is 2
i is 2 j is 1
Suppose the input for number is 9. What is the output from running the following program? #include <iostream> using namespace std; int main() { cout << "Enter an integer: "; int number; cin >> number; int i; bool isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } cout << "i is " << i << endl; if (isPrime) cout << number << " is prime" << endl; else cout << number << " is not prime" << endl; return 0; } Select one: a. i is 3 followed by 9 is not prime b. i is 3 followed by 9 is prime c. i is 4 followed by 9 is prime d. i is 4 followed by 9 is not prime
i is 4 followed by 9 is not prime
Which of the following code displays the area of a circle if the radius is positive. Select one: a. if (radius != 0) cout << radius * radius * 3.14159; b. if (radius >= 0) cout << radius * radius * 3.14159; c. if (radius > 0) cout << radius * radius * 3.14159; d. if (radius <= 0) cout << radius * radius * 3.14159;
if (radius > 0) cout << radius * radius * 3.14159;