CS 253: Midterm 1 1
Consider the run-time efficiency of the different methods of passing parameters in these function signatures: void alpha(vector<int> a); void bravo(vector<int> &b); void charlie(const vector<int> c); Which one of the following statements is true, for a typical compiler? A. calling bravo is fastest B. calling alpha is fastest C. calling charlie is slowest D. calling alpha is slowest E. calling bravo is slowest F. calling charlie is fastest
A. calling bravo is fastest
Consider this class: class Foo { public: int id; int ego; private: int superego; }; What access does ego have? A. public B. private C. protected D. It has no access. E. That will not compile—an access specifier is required.
A. public
The [[noreturn]] attribute means: A. A function will never return. B. A function always throws an error. C. A function contains an infinite loop. D. A function doesn't contain a return statement. E. A function sometimes throws an error.
A. A function will never return.
I want to declare a variable v, and a pointer p, and have the pointer point to the variable. Which statement accomplishes this? A. int v, p = *v; B. int v, *p = &v; C. int v, *p = v; D. int v, &p = v;
B. int v, *p = &v;
What will this code display? int foo() { static int z = 100; return ++z; } int main() { int a = fοo(); a = foo(); a = foo(); cοut << a << '\n'; } A. 0 B. 100 C. 103 D. 102 E. It cannot be determined. F. 101
C. 103
What will this code display? Assume that a char is one byte. class Foo { public: char red[32]; private: char blue[32]; static char green[32]; }; int main() { Foo bar; cout << sizeof(bar); } A. 128 B. 96 C. 64 D. 256 E. 32
C. 64
What is the text segment? A. It contains dynamic memory. B. Strings go there. C. Read/write variables go there. D. Program instructions go there.
D. Program instructions go there.
Which statement is true? A. const is compile-time constancy, constexpr is run-time constancy. B. const is for methods, constexpr for values. C. const is for the data segment, constexpr is for the stack or heap. D. const says that you can't change it, constexpr says that it doesn't change. E. const is only for C, constexpr is only for C++.
D. const says that you can't change it, constexpr says that it doesn't change.
I want to define a variable prez that contains the name of the first U.S. president. How would I do that? A. string prez = new string("George Washington"); B. String prez = "George Washington"; C. String prez = new String("George Washington"); D. string prez = "George Washington";
D. string prez = "George Washington";
Consider this code: int a[10]; cout << a[20]; A. It will throw an error. B. It will display an unpredictable value C. segmentation violation D. It will display zero. E. It cannot be determined.
E. It cannot be determined.
Consider the class Polar, which has two double member variables r and theta. Which of these is a correct copy constructor for Polar? A. Polar(const Polar rhs) : r(rhs.r), theta(rhs.theta) { } B. Polar(Polar rhs) const : r(rhs.r), theta(rhs.theta) { } C. Polar(Polar &rhs) : r(rhs.r), theta(rhs.theta) { } D. Polar(Polar rhs) : r(rhs.r), theta(rhs.theta) { } E. Polar(const Polar &rhs) : r(rhs.r), theta(rhs.theta) { }
E. Polar(const Polar &rhs) : r(rhs.r), theta(rhs.theta) { }
What's the literal type? 'a'
char
What's the literal type? "b"
const char array
Match the C++ I/O stream object to its description. coff
not a C++ I/O stream object
What's the literal type? 42ULL
unsigned long long
Consider this code: #include <string> using namespace std; double d; int foo() { string z = "Slartibartfast"; return z.length(); } A. the global namespace B. the std namespace C. no namespace at all 1. d is in 2. foo is in 3. string is in 4. z is in 5. int is in
1. d is in the global namespace 2. foo is in the global namespace 3. string is in the std namespace 4. z is in no namespace at all 5. int is in no namespace at all
A good compiler might complain that zorkmid never gets used in the following code. How would I convince the compiler to stop complaining about that? void dungeon() { int zorkmid = 42; } A. [[maybe_unused]] B. [[not_used]] C. [[unused]] D. [[maybe_used]] E. [[not_unused]] F. [[used]]
A. [[maybe_unused]]
Any problem with this code? vector<double> v; v[0] = 34; A. Can't write to v[0] when v.size()==0 B. It's fine. C. Can't write an int to a vector of double. D. You must use the .append() method for that.
A. Can't write to v[0] when v.size()==0
Any problems with this code? string s = "chimp"; s[2] = 'u'; A. It's fine. B. 'u' should be "u" C. String objects must be allocated with new. D. You need to use s.at[2] E. You can't modify a C++ string.
A. It's fine.
Consider this statement: if (auto now = time(nullptr)) What is the scope of the variable now? A. The if statement. B. The entire function. C. Now has global scope. D. The rest of the function.
A. The if statement
Consider the code: int a; cout << a; A. This will invoke undefined behavior. B. This code will display nothing. C. This will invoke unspecified behavior. D. This code will not compile. E. This will invoke implementation-defined behavior. F. This code will display 0.
A. This will invoke undefined behavior.
Which of these statements convert the contents of the int variable iv to a pointer to a float? A. float *fp = reinterpret_cast<float *>(iv); B. float *fp = reinterpret_cast<int>(iv); C. float *fp = dynamic_cast<float *>(iv); D. float *fp = reinterpret_cast(float *, iv);
A. float *fp = reinterpret_cast<float *>(iv);
Select the answer that contains the three literals which all have the same value. A. 0x12 012 0b12 B. 0B100011 0x23 043 C. 0 0000 42-42 00000000000 D. 10 010 0x10 0b10
B. 0B100011 0x23 043
In a non-const method of class Foo, what type is the special symbol this? A. Foo() B. Foo * C. Foo D. void E. Foo &
B. Foo *
What wil this code display? cout << "alphabet" + 5 << '\n'; A. alphabet5 B. bet C. flphabet D. alphabey
B. bet
Is this code ok? string s = "Hockle & Jeckle"; s[1] = 'e'; cout << s << "\n"; A. "\n" must be '\n', since it's a single character. B. It's fine. C. You can't modify a string. D. endl must be used, not '\n' E. You can't use [1] to modify a string; must use .at[1] instead.
B. It's fine.
Which is the best declaration for a copy constructor for class German? A. German(const German *) B. German *new(German &) C. German(const German &) D. German(German &other) { *this = other; } E. German(German)
C. German(const German &)
In a Makefile, when file Bill depends on file Ted, how does make determine when it's time to recreate Bill from Ted? A. It keeps track of when you edit or remove the file Ted. B. It always recreates Bill from Ted, whether it's strictly necessary or not. C. It checks the modification times of the files. D. make spawns a modified sub-shell, which reports file-modifying commands back to make.
C. It checks the modification times of the files.
What is Heckendorn's rule? A. Initialize all variables. B. Don't read a pointer after using delete on it. C. Never emit a constant error message. E. Check values for zero before dividing. F. Save errno before using it. G. Every error message must contain a file name.
C. Never emit a constant error message.
Consider this constructor: Foo(int a, double d) { alpha=a; delta=d; } Which answer is equivalent code, but using a member initialization list? A. Foo(int a, double d) : alpha,delta(a,d) { } B. No change needed—it already uses a member initialization list. C. Foo(int alpha=a, double beta=d) { } D. Foo(int a, double d) : alpha(a), delta(d) { } E. Foo(int a, double d) alpha=a; delta=d; { }
D. Foo(int a, double d) : alpha(a), delta(d) { }
Consider this code: cοut << numeric_limits<short>::max() << '\n'; Select the true statement. A. It is guaranteed to produce 65535. B. It is guaranteed to produce 32767. C. It will not compile. D. Its value cannot be portably determined.
D. Its value cannot be portably determined.
Consider this code: dοuble *dp = new dοuble[45]; // use the dynamically-allocated storage here delete dp; Note that delete, not delete[], was used. How will the system react? A. It is guaranteed to be a syntax error. B. This is undefined behavior, so a compiler error message is guaranteed. C. It is guaranteed to be a runtime error. D. This is not an error, and the system will correctly accept delete instead of delete[]. E. This is undefined behavior, so no particular result is guaranteed.
E. This is undefined behavior, so no particular result is guaranteed.
When make is executed in a directory containg only the following Makefile, which commands are executed? alpha: delta echo gamma beta: delta echo epsilon delta: echo pi lambda: echo tau A. echo tau; echo gamma B. echo tau; echo epsilon C. echo pi; echo epsilon D. echo epsilon; echo gamma E. echo pi; echo gamma
E. echo pi; echo gamma
I have a directory called backup, which contains several files. I want to get rid of the directory and all of the files. How do I do that? A. unmkdir backup B. rm backup/* C. rm * D. rmdir backup E. rm -r backup
E. rm -r backup
What will this code display? auto b = 10; b++; cout << b << '\n'; A. It cannot be determined. B. 12 C. 10 D. 1 E. It will throw an error. F. 11
F. 11
What is possible output from this code? A. vectοr<int> v; // put some data into v cοut << v.size() << ' ' << v.max_size() << ' ' << v.capacity(); A. 8 5 4611686018427387903 B. 8 4611686018427387903 5 C. 5 8 4611686018427387903 D. 4611686018427387903 5 8 E. 4611686018427387903 8 5 F. 5 4611686018427387903 8
F. 5 4611686018427387903 8
In a bash script, I want to write the three letters "ABC", followed by a newline, to standard output. How do I accomplish this?
echo "ABC"
What's the literal type? 3.4F
float
What's the literal type? 42
int
Match the C++ I/O stream object to its description. clog
like cerr, but unbuffered
What's the literal type? 42L
long
What's the literal type? 3.4L
long double
What's the literal type? "c"s
std::string
Match the C++ I/O stream object to its description. cerr
standard error, for error messages
Match the C++ I/O stream object to its description. cin
standard input
Match the C++ I/O stream object to its description. cout
standard output, for normal output