CS 240 Test 1 Review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Given the code below determine the frequency count and Big O for (int i=0; i < n; i++) { cout << i << endl; p = p + i; }

Frequency count: 4n + 2 Big O: O(n)

Given the code below determine the frequency count and Big O for (int i=0; i < n ; i++) for int j=0; j < n; j++) { cout << i << endl; p = p + i; }

Frequency count: 4n^2+4n+2 Big O: O(n^2)

Suppose we have a class named Spring, and a pointer to an instance of Spring, declared and initialized correctly with a string value constructor as follows: Spring *s = new Spring("flowers"); Which of the following correctly dereferences s and calls function grow() on s?Select all correct answers. (*s)->grow(); (&s).grow() s = &s; s.grow(); s->grow(); (*s).grow()

s->grow(); (*s).grow()

Consider the following C++ member function on class ParamTest. int ParamTest::testMe(int &one, int two, const int &three) { one = 0; two = 0; return 0; } If variables a, x, y, and z are declared as follows: ParamTest a; int x, y, z; Which of the following calls would result in variable z being set to 0? Select all that apply! y = a.testMe(z, x, y); y = a.testMe(x, z, y); z = a.testMe(x, y, z); x = a.testMe(x, y, z);

y = a.testMe(z, x, y); z = a.testMe(x, y, z);

Suppose we implement a C++ class named Day, and include it in a program containing the following statements: Day monday, tuesday, wednesday; Day *today, *tomorrow; today = &monday;tomorrow = new Day; What type is the expression *today? In other words, if the program includes *today somewhere later in the code, after the above four lines have executed, then what type is it?

*today is type Day

In which file would you expect to find each of the following lines? Please assume a C++ program that uses the CS240 approach to separate compilation in multiple files, and that does not use any function inlining. (That is, the implementations of member functions are not in the class definition.) __ Student *student; ____ g++ -c Roster.cpp -o Roster.o ____ #include "Roster.h" ____ class Roster { ____ Roster::Roster() { 1. A C++ header file called Roster.h 2. A C++ source code file called Roster.cpp 3. A makefile called makefile 4. In none of the other listed files

1 3 2 1 2

Suppose an object or variable is declared as an instance of a class that has a destructor. That variable could be either dynamically allocated, local/automatic, or global/static. For each of these possibilities, select the correct description for when the destructor will be called. __ Global/static ____ Local/automatic ____ Dynamically allocated 1. Never 2. When the program ends 3. When the object's function returns 4. When the object is deleted

2 3 4

Match each of the following files to the entity that most directly creates it, in a program that uses a separate compilation build process. The same selection may or may not be used more than once for a file. ("Most directly" means, for example, that if Sally tells Jose to bake a cake, and Jose tells Sanjeev to bake it, and I ask who baked the cake, the answer is Sanjeev, not Sally or Jose.) ____ A makefile called makefile ____ An executable file named say_hello ____ A C++ source code file named Hello.cpp ____ A C++ object code file named Hello.o 1. Created by the linux make program 2. Created by the compiler 3. Created by the programmer 4. Created by the linker

3 4 3 2

Suppose you want to implement a Stack as a linked list using the minimal amount of data members in the linked list object and in each linked list node, but still support the push() and pop() operations with their best possible runtime complexities. Which of the following would you need? (Assume the top of the stack is the head of the list.) A prev pointer in the linked list node A next pointer in the linked list node A head pointer in the linked list A tail pointer in the linked list

A next pointer in the linked list node A head pointer in the linked list

With which of the following List implementations can a binary search operate in tight upper bound O(log N) time? (In the selections, "ordered" means the elements are stored in order, for example from smallest to largest, within the array) An ordered doubly linked list An ordered static array An unordered dynamically allocated array An ordered singly linked list An unordered doubly linked list An ordered dynamically allocated array

An ordered static array An ordered dynamically allocated array

Which of the following is the correct function signature for a copy assignment operator on class Clonable? (Assume that the signature resides within the class definition, so we don't need Clonable::.) Clonable operator(Clonable &other); bool =(const Clonable &one, const Clonable &two); Clonable &operator=(const Clonable &rhs); Clonable(Clonable &other);

Clonable &operator=(const Clonable &rhs);

Suppose a class called Container uses the heap to store items, and it needs to perform a deep copy of the contained data. In which functions would you expect to find the code that makes the deep copy? Container::Container(const Container &other); Container &Container::operator=(const Container &other); Container::Container(); Container::~Container() Container &Container::operator++(); Container::Container(int max_elements);

Container::Container(const Container &other); Container &Container::operator=(const Container &other);

Which of the following is the correct way to declare and create a dynamically allocated array of 100 Insect objects? Insect &bug[100]; Insect bug[100]; int bug[Insect] = new 100; Insect *bug = new Insect[100];

Insect *bug = new Insect[100];

Which of the following best describes what const does in the following function declaration, within class Safe? Safe::callMe(string yes, string &no) const; Prevents callMe() from changing any of the public data members (variables declared within the public section of the Safe class). Keeps callMe() from changing the variable yes. Prevents callMe() from changing any of its local variables (variables declared within the callMe() function itself). Keeps callMe() from changing the variable no. Prevents callMe() from changing any of the private data members (variables declared within the private section of the Safe class).

Prevents callMe() from changing any of the public data members (variables declared within the public section of the Safe class). Prevents callMe() from changing any of the private data members (variables declared within the private section of the Safe class).

Which of the following is the most "dangerous" thing to assign a global variable pointer to, in a C++ program? Consider "dangerous" to mean that it is most likely to cause a segmentation fault elsewhere in the code. The address of an automatic/local variable nullptr The address of a dynamically allocated variable The address of a global variable allocated in the static area

The address of an automatic/local variable

Which of the following are true statements about dynamically allocated arrays in C++? Once allocated, they grow automatically, so it is safe to assign values even beyond the end of the dynamically allocated space. They get deleted one array element at a time in a loop They get destroyed automatically when the function within which they are declared, returns. They are allocated on the heap using the C++ keyword new. Their size can be decided at runtime, for example by reading a value as input from the program's user

They are allocated on the heap using the C++ keyword new. Their size can be decided at runtime, for example by reading a value as input from the program's user

Suppose Algorithim A has a worst case tight upper bound runtime complexity of O(log N). And Algorithm B has a worst cast tight upper bound runtime complexity of O(N). Which of the following is/are true statements? When answering this question, compare A vs. B when run for the same exact input. "Very large" means arbitrarily large; larger than any important number or threshold. When implemented as programs, A will complete before B for all inputs of all sizes A requires fewer basic operations than B on some inputs, for all very large input sizes A requires fewer basic operations than B on all inputs, for some very large input sizes A requires fewer basic operations than B for all inputs of all sizes

When implemented as programs, A will complete before B for all inputs of all sizes A requires fewer basic operations than B on some inputs, for all very large input sizes

Suppose you want to implement a First In First Out (FIFO) Queue as a linked list using the minimal amount of data members in the linked list object and in each linked list node, but still support the enqueue() and remove() operations with their best possible runtime complexities. Which of the following would you need? a prev pointer in the linked list node a head pointer in the FIFO queue object a next pointer in the linked list node a tail pointer in the FIFO queue object

a head pointer in the FIFO queue object a next pointer in the linked list node a tail pointer in the FIFO queue object

Consider a singly linked list List implementation. The list may store duplicate elements, but if it does then the duplicates all must be stored one after another in the list, with no different values in between. Other than that, the elements can be stored in any order. What are the best tight upper bound runtime complexities of the following functions?: int count(v). // given a value v, return the number of instances of v in the list insert(v). // add a new value v to the list count() is O(N2); insert() is O(N) count is O(N); insert is O(1) count() is O(N2); insert() is O(1) count() and insert() are both O(N)

count() and insert() are both O(N)

Suppose Course is a C++ class with data members that store information about a course, and Student is a C++ class with data members that store information about students. All courses are stored in a dynamically allocated array named course, and all students are stored in a dynamically allocated array named student, declared as follows: Course *course; Student *student; For a single course's roster, a private data member of the Course class is declared as follows: string *bnumber; // array of student bnumbers For a single student's schedule, a private data member of the Student class is declared as follows: string *crn; // array of course CRNs If the program is implemented properly, then which of the following would you expect to find in the function named Course::~Course()? delete [] course; none of these delete [] crn; delete [] bnumber; delete [] student;

delete [] bnumber;


Ensembles d'études connexes

H American History ch.22.1-2 quiz

View Set

Georgia life and health missed q #1

View Set

P4O10 and SO2 (Phosphorus Pentoxide + Sulphur Dioxide) Properties

View Set

US history Unit 14 Great Depression

View Set

Cpl's Course- Tactical Tools: Tactical Communication

View Set

Chapter 29: The Child with Endocrine Dysfunction

View Set

theory of chromatographic analysis of drugs

View Set

MGT 370: Chapter 07 Assignment: Designing Adaptive Organizations

View Set