CS253 Quizzes

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the first line of a bash script in Linux?

#!/bin/bash #! /bin/bash #!/usr/bin/bash #! /usr/bin/bash

What type are these literals? (a) "alpha" (b) "beta"s (c) 'x' (d) "y"

(a) array of const char (b)C++ string object (c)char (d)array of const char

Match the C++ I/O stream object to its description. (a)-cout (b)-cerr (c)-clog (d)-cin (e)-coff

(a)- standard output, for normal output (b)- standard error, for error messages, unbuffered (c)- like cerr, but fully buffered (d)- standard input (e)- not a C++ I/O stream object

1. The header file <cstring> defines the symbol [ (a) ] ["c_str", "string", "strlen"] in the [ (b) ] ["std", "global"] namespace. 2. The header file <string.h> defines the symbol [ (c) ] ["string", "c_str", "strlen"] in the [ (d) ] ["std", "global"] namespace. 3. The header file <string> defines the symbol string in the [ (e) ] ["global", "std"] namespace.

(a)- strlen (b)- std (c)- strlen (d)- global (e)- std

Consider this code: #include <string>using namespace std; double d; int foo() { string z = "Slartibartfast"; return z.length(); } (a)- d is in (b)- foo is in (c)- string is in (d)- z is in (e)- int is in

(a)- the global namespace (b)- the global namespace (c)- the std namespace (d)- no namespace at all (e)- no namespace at all

Consider this function declaration: void foo(int i, double d, int a[9], string s, vector<int> v); How is each parameter passed to the function? (a)- i (b)- d (c)- a (d)- s (e)- v

(a)- value (b)- value (c)- reference (d)- value (e)- value

Match the code with its description. (a)-cout << 1/0; (b)-cout << sizeof(int); (c)-int a, b; if (&a<&b) cout << 42; (d)-int i=42; i >>= 3; cout << i;

(a)-undefined behavior (b)-implementation defined behavior (c)-unspecified behavior (d)-great code

Consider: vector<double> v = {1.1, 2.2, 3.3, 4.4}; I want to access the third element of v that contains 3.3. How should I do that? -v[2] -v.at(2) -v[3.3] -v.2

- that will work - that will work -that won't work -that won't work

In this code, how many times will foo() be called? for (int i=0; i<5; i++) { static auto v = foo(); cout << v << '\n'; }

1

What will this code display? int foo() { static int z = 100; return ++z; } int ma‍in() { int a = fοo(); a = foo(); a = foo(); cοut << a << '\n'; } 100 It cannot be determined. 103 102 0 101

103

What will this code display? cout << ((0xF & 0b11) (8);

11

What will this code display? cout << dec << 0x12 << '\n'; -It will not compile -12 -18 -10 0x12 -0x12

18

void foo (int a, int b, int c = 10, int d=20); How many arguments can i give to this function without a syntax error?

2, 3 or 4

What will this code produce? string s = "abc"; s += 'd'; s.push_back('e'); cοut << s.length(); 8 It cannot be determined. 5 It won't compile; string has no .length() method

5

What is possible output from this code? vector < int > v; //put some data into v cout << v.size() << ' ' << v.max_size() << ' ' << v.capacity();

5 4611686018427387903 8

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); } 32 256 128 64 96

64

The [[noreturn ]] attribute means:

A function will never return.

What version of C++ will this class focus on?

C++17

In a non-const method of class Foo, what type is the special symbol this? Foo * Foo & Foo() void Foo

Foo *

Which is the best declaration for a copy constructor for class German? German(German &other) { *this = other; } German(German) German(const German *) German *new(German &) German(const German &)

German(const German &)

Consider a small Makefile, where foo.cc & foo.h define a class, and main.cc uses that class. prog: main.o foo.o $(CXX) main.o foo.o -o prog main.o: main.cc foo.h $(CXX) -c main.cc foo.o: foo.cc foo.h $(CXX) -c foo.cc Why does the Makefile say that main.o depends on foo.h? Every object file must depend on one .cc file and one .h file. That is an error—there is no reason for that dependency. All object files must depend on all header files. If main() instantiates a Foo object, then it needs to at least know the size of a Foo object.

If main() instantiates a Foo object, then it needs to at least know the size of a Foo object.

Consider this code: int a[10]; cout << a[20]; What will it do? -segmentation violation -It will display an unpredictable value. -It will display zero. -It cannot be determined. -It will throw an error.

It cannot be determined.

What will this code produce, when executed? class Fοo { public: Foo(int n) : valu‍e(n*n) { } Foo() { Fοo(10); } int value; }; int main() { Fοo f; cοut << f.value << '\n'; return 0; } 0 It cannot be determined. 100 10*10 10

It cannot be determined.

In a Makefile, when file Bill depends on file Ted, how does make determine when it's time to recreate Bill from Ted? -It keeps track of when you edit or remove the file Ted. -It always recreates Bill from Ted, whether it's strictly necessary or not. -make spawns a modified sub-shell, which reports file-modifying commands back to make. -It checks the modification times of the files.

It checks the modification times of the files.

public: Bob (int n) : hammer(n) {} int hammer = 52; }; int main() { Bob b; cout << b.hammer << '\n'; }

It will not compile - Bob has no default constructor

Any problems with this code? string s = "chimp"; s[2] = 'u'; -'u' should be "u" -String objects must be allocated with new. -It's fine. -You need to use s.at[2] -You can't modify a C++ string

It's fine

Is this code ok? string s = "Hockle & Jeckle"; s[1] = 'e'; cout << s << "\n"; -You can't modify a string. -"\n" must be '\n', since it's a single character. -You can't use [1] to modify a string; must use .at[1] instead. -endl must be used, not '\n' -It's fine.

It's fine

Consider this code: cout << numeric_limits < short > :: max() << '\n'; select the true statement.

Its value cannot be portably determined

What is Heckendorn's rule? -Never emit a constant error message. -Don't read a pointer after using delete on it. -Check values for zero before dividing. -Save errno before using it. -Every error message must contain a file name. -Initialize all variables.

Never emit a constant error message.

Is this code valid? void foo(int n){ constexpr auto c=n; cout << c; }

No; n is not a compile-time constant

Consider the class Polar, which has two double member variables r and theta. Which of these is a correct copy constructor for Polar? Polar(const Polar rhs) : r(rhs.r), theta(rhs.theta) { } Polar(Polar rhs) : r(rhs.r), theta(rhs.theta) { } Polar(Polar &rhs) : r(rhs.r), theta(rhs.theta) { } Polar(const Polar &rhs) : r(rhs.r), theta(rhs.theta) { } Polar(Polar rhs) const : r(rhs.r), theta(rhs.theta) { }

Polar(const Polar &rhs) : r(rhs.r), theta(rhs.theta) { }

What is the text segment? Strings go there. It contains dynamic memory. Read/write variables go there. Program instructions go there.

Program instructions go there.

What will this code do? Assume that an int is 32 bits. int n; // assume that n gets a value. n <<= 40; -Shifts are implemented as rotates, so it's the same as shifting left by 8 (40-32) bits. -This will not compile, because there is no <<= operator. -Since 40≥32, the result is not determined. -This will shift all the bits off the left end, resulting in zero.

Since 40≥32, the result is not determined.

Consider this code: for (int i=1; i<1'000'000'000; i++) { flοat *f = new float[100'000'000]; } What will be the result? Since the variable f is declared inside the loop, its destructor will free the memory. The system will run out of memory. No problem. The allocated memory will be freed by the garbage collector.

The system will run out of memory.

The word "deprecated", in the context of C++ programming means:

This feature is supported today, but that is expected to change.

Consider this code: double *dp = new double[45]; //use the dynamically-allocated storage here delete dp; Note that delete, not delete[], was used. How will the system react?

This is undefined behavior, so no particular result is guaranteed.

Consider this code: int a; cout << a;

This will invoke undefined behavior

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; } [not_used]] [[not_unused]] [[maybe_used]] [[used]] [[unused]] [[maybe_unused]]

[[maybe_unused]]

Consider this code: int a=1; int main() { int b=2; int *c = new int; }

a is in data, b is on the stack, c points to the heap

What will this code display? cout << "alphabet" + 5 << '\n'; -bet -alphabey -alphabet5 -flphabet

bet

I have a source file hw1.cc. I wish to make a backup copy in the file hw1.save. Which command will accomplish this? -copy hw1.cc hw1.save -save hw1 -cp hw1.cc hw1.save -cp hw1.cc >hw1.save -save hw1.cc

cp hw1.cc hw1.save

When make is executed in a directory containing only the following Makefile, which commands are executed? alpha: delta echo gamma beta: delta echo epsilon delta: echo pi lambda: echo tau

echo pi; echo gamma

What is the difference between '\n' and endl

endl flushes the output, '\n' does not

Which of these statements convert the contents of the int variable iv to a pointer to a float? float *fp = reinterpret_cast<float *>(iv); float *fp = reinterpret_cast(float *, iv); float *fp = reinterpret_cast<int>(iv); float *fp = dynamic_cast<float *>(iv);

float *fp = reinterpret_cast<float *>(iv);

What is a valid signature for main?

int main(int argc, char *argv[])

I want to declare a variable v, and a reference r, and have the reference refer to the variable. Which statement accomplishes this?

int v, &r=v;

I want to declare a variable v, and a pointer p, and have the pointer point to the variable. Which statement accomplishes this? int v, p = *v; int v, *p = &v; int v, *p = v; int v, &p = v;

int v, *p = &v;

Consider this definition: auto alpha = 42L; What type is the variable alpha? -auto -int -42L -It has no type. -long

long

Which statement is true? -main is a constructor -main is optional -main is a function main is a method.

main is a function

I want to find out how many bits are in a int, not counting the sign. For most modern computers, I expect the value 31. Which of these will produce that value? numeric_limits<int>:‍:digits numeric_limits<int>:‍:digits10() It's not needed—the number of bits in an int is guaranteed by the standard to be 31. numeric_limits<int>:‍:max()

numeric_limits<int>:‍:digits

You want to be able to display an instance of class Foo, as shown here: Foo f; cout << f; What would be the correct declaration for a function to do that?

ostream &operator << (ostream &, const Foo &);

Consider this class: class Foo { public: int id; int ego; private; int superego; }; what access does ego have?

public

What's the difference between struct and class? struct is only for C programs, class is only for C++ programs struct can't have methods, class can have methods Only struct can have public data members; in a class data must be private struct defaults to public, class defaults to private

struct defaults to public, class defaults to private

Consider this statement: if (auto now = time(nullptr)) What is the scope of the variable now? -the if statement -the rest of the function -now has global scope -the entire function

the if statement

After #include <iostream>, what namespace contains the symbol cout?

the namespace called "std"

Which method is a good candidate for [[nodiscard]]? vector::clear() string::reserve() ostream ostream::operator<<(int) vector::empty() string::resize()

vector::empty()


संबंधित स्टडी सेट्स

Chapter 31: Care of Patients with Dysrhythmias

View Set

Anatomy I: Test 6 - Elbow Flexors

View Set

Chapter 11 Conductors, Connections, and Protection

View Set

Bacteriological Indicators of Water Pollution:

View Set