CS150 Midterm Study
"If the patron's is older than 12 or younger than 66, charge them 8.75 for the ticket. Charge 5.75 for everyone else." What prints? int age = 12; if (age > 12 || age < 66) cout << "$ 8.75" << endl; else cout << "$ 5.75"; A. $ 8.75 B. $ 5.75 C. both D. Syntax error
A. $ 8.75
double bottles; double bottleVolume = bottles * 2; cout << bottleVolume << endl; What prints out here (assuming all includes, etc.) A. 0 B. 1 C. 2 D. Unpredictable result; logic error E. Does not compile; syntax error
A. 0
What prints? (Assume all includes, etc.) int val = 1; while (val++ < 5) cout << val << " "; A. 2 3 4 5 B. 1 2 3 4 C. 1 2 3 4 5 D. Syntax error E. Endless Loop
A. 2 3 4 5
#include <iostream> using std::cout; int main() { cout << "Hello CS 150\n"; } A. Compiles, runs, and prints "Hello CS 150" on a line by itself B. Compiles, runs, prints "Hello CS 150" w/o newline C. Compiles, but crashes when it is run D. Does not compile
A. Compiles, runs, and prints "Hello CS 150" on a line by itself
What prints? (Assume all includes, etc.) string grade = "C"; if (grade == "A" || "A+" || "A-") cout << "Got an A\n"; else if (grade == "B" || "B+" || "B-") cout << "Got a B\n"; else if (grade == "C" || "C+" || "C-") cout << "Got a C\n"; A. Got an A B. Got a B C. Got a C D. Syntax error
A. Got an A
What prints? (assume all includes, namespace OK) int a = 3; if (a = 4) cout << "a is 4; weird!\n"; else cout << "a is 3\n"; A. a is 4; weird! B. a is 3 C. Syntax error D. Runtime crash
A. a is 4; weird!
What prints? (Assume all includes, etc.) int val = 1; while (val < 5) cout << val++ << " "; A. 2 3 4 5 B. 1 2 3 4 C. 1 2 3 4 5 D. Syntax error E. Endless Loop
B. 1 2 3 4
Which int values for x print "hi"? switch (x - 3) { case 7: break; case 6: case 4: cout << "hi"; } A. None. Compilation error. B. 9 or 7 C. 6 or 4 D. 4 only E. 7 only
B. 9 or 7
extern int cost; int size = 42; cost = 9.99; cout << "size=" << size << ", cost=" << cost << endl; What happens (assuming in main, all includes, 1 file) A. size=42, cost=9 B. Compiler Error: assign double value to int variable C. Compiler Error: undeclared variable D. Linker error: object not found (undefined)
B. Compiler Error: assign double value to int variable
#include <iostream> int main() { cout << "Hello CS 150"; } A. Compiles, runs, and prints "Hello CS 150" on a line by itself B. Compiles, runs, prints "Hello CS 150" w/o newline C. Compiles, but crashes when it is run D. Does not compile
B. Compiles, runs, prints "Hello CS 150" w/o newline
#include <iostream> int main() { std::cout << "Hello CS 150"; } A. Compiles, runs, and prints "Hello CS 150" on a line by itself B. Compiles, runs, prints "Hello CS 150" w/o newline C. Compiles, but crashes when it is run D. Does not compile
B. Compiles, runs, prints "Hello CS 150" w/o newline
What prints here? cout << "Hi" || cout << "Bye" << endl; A. Nothing. Compilation error. B. Hi C. Bye D. HiBye E. Something else
B. Hi
int x = 4; do { x -= 5; x++; } while (x >= 0); What is the value of x after this loop completes? A. Compiler error. B. Infinite loop, x never becomes >= 0. C. -1 D. 4 E. 0
B. Infinite loop, x never becomes >= 0.
"Give a 25% discount to children under 13 and to those who are 65 and older". What prints? int age = 12; if (age < 13 && age > 65) cout << "25% discount" << endl; else cout << "No discount" << endl; A. 25% discount B. No discount C. both D. Syntax error
B. No discount
Assume x is an int with the value 4. What prints? if (x <= 2) if (x == 4) { cout << "one"; } else cout << "two"; A. Nothing. Compilation error. B. Nothing. Runs but prints nothing. C. one D. two E. onetwo
B. Nothing. Runs but prints nothing.
What prints? (assume all includes, namespace OK) i nt a = 3; if (a == 3); { a = 4; } cout << "a is " << a << "\n"; A. a is 3 B. a is 4 C. Syntax error D. Runtime crash
B. a is 4
int i = 5; while(i) cout << i--; What prints here? A. Compiler error. i not a Boolean expression. B. Infinite loop C. 54321 D. 543210 E. 43210
C. 54321
What prints? (Assume all includes, etc.) int val = 1; while (val++ < 5); cout << val << " "; A. 2 3 4 5 B. 5 C. 6 D. Syntax error E. Endless Loop
C. 6
What prints here? !(cout << "Hi") && cout << "Bye" << endl; A. Nothing. Compilation error. B. Nothing (but compiles and runs) C. Hi D. Bye E. HiBye
C. Hi
What prints? (assume all includes, namespace OK) int a = 3; if (a == 3) a = 4; else; a = 5; cout << "a is " << a << "\n"; A. a is 3 B. a is 4 C. a is 5 D. Syntax error
C. a is 5
string s = "happy"; int i = -1, len = s.length(); while (i < len - 1) cout << s[++i]; What is printed? A. Compiler error. B. nothing C. happy D. appy E. happ
C. happy
int x = 20, y = 10; x = (x - y) * 2; y = x--; What values are in x and y after running this code? A. x - > 9, y - > 19 B. x - > 20, y - > 19 C. x - > 19, y - > 20 D. x - > 20, y - > 20
C. x - > 19, y - > 20
int i = 5; while(i--) cout << i; What prints here? A. Compiler error. i-- not a Boolean expression. B. Infinite loop C. 54321 D. 43210 E. 4321
D. 43210
Given this prototype and variable definition, which of these function calls are legal (that is, they will compile)? void f(int& n); int a = 7; A. f(2); B. f(&a); C. f(a); D. All of these E. None of these
D. All of these
#include <iostream> using namespace std; int main() { cout << sqrt(64) << endl; } A. Compiles, runs and prints 8 on all platforms B. Compiles, runs and prints 8.0 on all platforms C. Compiles, but crashes when run D. Compiles on some platforms but is incorrect E. Incorrect; does not compile on any C++ platform
D. Compiles on some platforms but is incorrect
#include <iostream> using std::cout; int main() { cout << "Hello CS 150" << endl; } A. Compiles, runs, and prints "Hello CS 150" on a line by itself B. Compiles, runs, prints "Hello CS 150" w/o newline C. Compiles, but crashes when it is run D. Does not compile
D. Does not compile
double bottles = 7; int num = 2.3; char ch = 7.5L; What happens here? A. Compiler errors on all three lines (type check) B. Compiler errors on lines 2 and 3 (type check) C. Compiler error on line 3 (invalid literal) D. Possible compiler warning if requested E. No compiler errors or warnings
D. Possible compiler warning if requested.
What prints? (assume all includes, namespace OK) int a = 3; if (a == 3) a = 4; a = 5; else a = 6; cout << "a is " << a << "\n"; A. a is 4 B. a is 5 C. a is 6 D. Syntax error
D. Syntax error
int size = 42; cost = 9.99; cout << "size=" << size << ", cost=" << cost << endl; What prints out here (assuming all includes, etc.) A. size=42, cost=9.99 B. Logic error; uninitialized variable C. Syntax error; assign double value to int variable D. Syntax error: undeclared variable
D. Syntax error: undeclared variable
Assume x is an int with the value 4. What prints? if (x <= 2) { if (x == 4) cout << "one"; } else cout << "two"; A. Nothing. Compilation error. B. Nothing. Runs but prints nothing. C. one D. two E. onetwo
D. two
int i = 5; while(--i) cout << i; What prints here? A. Compiler error. --i not a Boolean expression. B. Infinite loop C. 54321 D. 43210 E. 4321
E. 4321
What prints? (Assume all includes, etc.) int val = 1; while (val < 5); cout << val++ << " "; A. 2 B. 1 C. 5 D. 1 2 3 4 E. Endless Loop
E. Endless Loop
What prints here? cout << "Hi" && cout << "Bye" << endl; A. Nothing. Compilation error. B. Nothing (but compiles and runs) C. Hi D. Bye E. HiBye
E. HiBye
What prints? (assume all includes, namespace OK) int a = 7; bool b = a - 2; cout << "b is " << b << endl; A. Syntax error! Wrong type for b. B. b is true C. b is false D. b is 5 E. b is 1
E. b is 1