CS311 Midterm

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What C++ keyword is used when throwing an exception?

"Try"

Give an example of a member function that a C++ compiler may automatically write for you.

//Destructor ~Dog();

Given a description of a function, write reasonable pre- and post-conditions for the function.

//Pre: sec cannot be less than zero Int timeOfDay() { Int sec=0; } //Post:

Explain direct vs. indirect recursion.

1. When a method invokes itself 2. When a method invokes another method which ultimately invokes the original

Explain forward, bidirectional, and random-access iterators. How do they differ?

1. only go forward 2. can go either forward or backward 3. can get any element in an array

Class IntArray allocates by doing "new value_type[size]". It could also have done "new int[size]", but your instructor says the former is better. Why?

???Better control over the type???

Your instructor says that talking about "the constructor" (as opposed to "a constructor") usually indicates that there is a misunderstanding. What is this misunderstanding?

A constructor could be a copy or assignment constructor

What is a default constructor?

A constructor with no parameters

What do we mean by a generic container?

A container that can hold items of a client-specified type

What is an operation contract? How is it like a real-world contract?

A function carries out a contract -- if the preconditions are met, then the function is required to make the post conditions true upon its normal termination, if not, it has no responsibilities

Callback function

A function that is triggered by another function when something is true.

What does it mean for a module to be cohesive? Is this a good thing or a bad thing?

A module with all parts have closely related functionality - they belong together Good thing

What is a Linked List?

A sequence of items (nodes) that have sequential, forward access. Better than arrays because insertion and removal is faster but finding a position is slower (because it's sequential access)

function object

A value created by a function definition. The name of the function is a variable that refers to a function object.

What is a template in C++?

A way to write code without specifying the types it deals with (used for generic programming)

What do std::distance and std::advance do? Explain how using them can be a better choice than doing pointer-arithmetic-style operations on iterators using operators.

ADD MORE std::distance(iter1, iter2) is like iter2-iter1 Std::advance(iter, n) is like iter+=n Both are fast for random-access iterators

What does it mean to be recursive?

An algorithmm that calls itself

RAII is misleading, but standard, terminology. What does it really mean? (I am not asking what it stands for.)

An object owns a resource and the destructor will release it when finished (because it is responsible) Resource Acquisition is Initialization

Name and briefly explain each of the four C++ parameter-passing methods we discussed.

By value - simple types, pointers, iterators By reference - virtual functions (modifying x will modify y) By reference-to-const - larger objects, things we're not sure of By Rvalue reference (introduced in C++11) - Constructors that construct new objects from existing objects of the same type

In this class, what do we mean by a client of a module?

Code that uses a component (function, class, package, etc)

What do we mean by abstraction?

Considering a software component in terms of how and why it is used, separate from its implementation

Why is it not good practice to signal an error by throwing a string?

Does not allow for catching an exception by type.

What does DRY stand for? What does it mean?

Don't repeat yourself

Explain C++ equality vs. Equivalence.

Equality: a == b Equivalence: !(a < b) && !(b < a)

Which C++ expressions are simultaneously both Lvalues and Rvalues? Which C++ expressions are neither Lvalues nor Rvalues?

Every C++ expression must either be an R value or L value

Suppose we want a C++ class to have no copy operations. How can we eliminate these operations?

Explicitly delete the copy operations and define (or delete) the other 3 in the Big Five

True or false? When a type conversion is applied to a variable, it may alter the value of the variable.

False-- a type conversion creates a new value, it doesn't modify the original

Briefly explain how the Binary Search algorithm works.

Finds a given key in a sorted list Key = thing to search for (and associated data)

How do we specify a range of data using two iterators? (The iterators each point to something. What somethings do they each point to? Be specific.)

First item and then iterator pointing to the item right after the last element in specified container

In C++, we can implement an overloaded operator using either a global function or a member function. How does this choice affect the parameters the function takes?

Global operator takes at least two parameters (operands) whereas in the member function, the first operand is the object (*this)

Give an example of a rule, as the term is used in "On Following Rules".

Goto is considered harmful Use small functions Loose coupling good, tight coupling bad Use meaningful variable and function names Comment what the code leaves out, not what the code already says

What are circular references? How would they affect they way we make use of ownership?

If A is released, then R1 .. R6 are not released. There is a resource leak.

What is a first-class type?

If new values can be created at run-time ("Tossed around with the ease of something like int")

State the Rule of Five

If you define one of the Big Five you must define all of them (or delete all of them)

Some people are in the habit of writing a whole bunch of code, saying, "I'm done," and then trying to compile it for the first time. Your instructor replies, "No, you're not done; you just started." Explain.

Inevitably there will be errors if compiling for the first time and it will be difficult (well, nearly impossible) to determine what the issues are when the code isn't tested as it is being written.

A Linked List holds essentially the same kind of data as an array. Why, then, would we ever want to use a Linked List? We could simply use (smart) arrays for everything, right?

Insertion and removal is faster

An iterator refers to an item in a container—or it acts as if it does. Explain.

It can be a wrapper around data

Why is Sequential Search sometimes preferable to Binary Search?

It doesn't have to be sorted, which is required for binary search. Also, a binary search has to be random-access (for efficiency)

What happens if a recursive function never reaches its base case?

It will continue on forever (or until computer crashes)

How do we mark the end of a Linked List?

Last node has a null pointer after the last data item

What is an Lvalue? What is an Rvalue?

Lvalue -- expression that persists beyond current expression //has a location in memory Rvalue -- about to go away //complex expressions, return value of a function

What does it mean to release a resource? Give an example of a specific resource, and indicate exactly what it means to release this resource.

Means to clean it up and give up control A file is something that needs to be released once the owner is done with the resource so another process can use it. Also, it prevents memory leaks.

When we talk about a move constructor or a move assignment operator, what do we mean by "move"?

Move is pass by Rvalue reference

An iterator references an item in a container. Does the iterator own that item?

NO

Give an example of a rule that you might correctly break, according to the standards in "On Following Rules", and explain circumstances in which you would break it.

Not commenting code that we wrote that no one else will ever see

What does the destructor of a pointer do?

Nothing!

Under normal circumstances, which parameter-passing method is used to pass a vector<int>?

Pass by reference to const

Under normal circumstances, which parameter-passing method is used to pass an int?

Pass by value

Under normal circumstances, which parameter-passing method is used to pass an iterator?

Pass by value

What is a special circumstance in which we would use pass by value to pass something of a type that we would not normally pass by value?

Pass by value if you copy the parameter anyway

What kinds of troubles does RAII help to prevent?

Prevents memory leaks

Your instructor says that if we search for an item in a list using operator<, then we should also determine whether we found it using operator<. Why?

Recursive and base cases work consistently Some types don't have == operator If it does have == operator, it is not always defined in a way consistent with operator<

What is an iterator

Refers to an item in a container STL containers have iterator member types

According to "On Following Rules", what rules should we never break?

Rules you don't understand

Briefly explain how the Sequential Search algorithm works.

Searches through a container sequentially, from beginning to end. When desired key is found it stops and answers YES If it never finds the key before the end of the list, it answers NO

What is an expression?

Something that has value 34 // Expression of type int abc + 34 // Expression of type int 42.7 // Expression of type double cout << x // Expression of type std::ostream vector<int> vv; vv // Expression of type std::vector<int> vv[2] // Expression of type int

What is a class invariant?

Statements about data members that indicate what it means for an object to be valid or usable

What is an invariant?

Statements about data members that indicate what it means for an object to be valid or usable

What does it mean when two modules are tightly coupled? Is this a good thing or a bad thing?

Strong dependence, changing one thing breaks another Bad thing

When we talk about a copy constructor or a copy assignment operator, what do we mean by "copy"?

Takes a reference to const and makes a copy

What is an assignment operator?

The = operator

Your instructor refers to certain functions as "invisible". What does he mean?

The member functions that are written for you by the compiler that you don't see--only the output (ex: Dog class)

What do we mean by the arity of an operator?

The number operands it has (ex: a*b has one operator, so arity 1)

What happens if a C++ exception is thrown, but it is never caught?

The program will terminate

What two pieces of data does a Linked List node hold?

The single data item and a pointer to the next node

Give an example of a C++ Standard Library class or class template that uses RAII.

The smart-pointer template std::shared ptr<T>

In the context of Binary Search, what is a pivot?

The value in the middle of a sorted list that is used to compare against the given key value and determine if the list should be searched before or after the item

An empty Linked List has no nodes. How do we represent an empty Linked List.

The value of the head is null

Some people dislike exceptions, because using them means that programmers are required to do lots of work. Let us take it as given that (1) exceptions do make lots of work for programmers, and (2) we do not want programmers to be overworked. Nonetheless, your instructor claims that this is a bad reason to dislike exceptions. Why does he say that?

Throwing and catching exceptions can take more work up front be save lots of time and spare the programmer from lots of problems in the long run Writing software is work and catching exceptions is one way to make sure your software actually works

Explain the use of the C++ explicit keyword.

Used when not doing an implicit type conversion It avoids typecasting to the wrong type when not doing an implicit conversion

What does overloading mean?

Using the same name for two things

What can we do with random-access data, that we cannot do with sequential-access data?

We can deal with the data in any order, skipping from one item to another item without going through each sequentially, one-by-one

Your instructor says that the first goal, when writing code, should be to get it to compile. Why is this? What important advantage do we lose when we work on code for an extended time without being able to compile it?

We lose the advantage to test the code bit by bit, allowing us to isolate a given compiler error if we've only written a small amount of code since the last successful compile

What C++ syntax do we use to catch every exception, and then re-throw the same exception when we have done what we need to? Be precise & detailed.

We use "catch( ... )" to catch all exceptions Then to re-throw, we use "throw" inside the catch block to rethrow the same exception These two used together makes sure clean-up is done

Explain pre-conditions and post-conditions. What do we use these for?

What must be true for the function to execute properly A description of the function's effect using statements about values

When do we return by reference / reference-to-const?

When the parameter is large and a copy would be use too much space and take too much time

We generally want to implement an operator using a member function, when we have no good reason not it. Give two good reasons not to, which may apply to some operators.

When there are implicit type conversions Allow for operators whose first operand has a type that is a class you didn't write **Using stream insertion are always global

According to "On Following Rules", when can we break a rule?

When we fully understand it and know what will happen AND have a good reason to break it

What do we mean by ownership?

an object has the responsibility to release the resource when finished

What is an exception?

an object thrown to signal an error condition.

Classify expressions in some C++ code as Lvalue or Rvalue. Example. Consider the following C++ code. vector<int> data(10); const int m = 3; data[0] = m; data[m] = 37+data[0]; Classify each of the following: data data[0] m data[m] 37 37+data[0]

data //Lvalue data[0] //Lvalue m //Lvalue -- variable data[m] //Lvalue 37 // Rvalue --integer 37+data[0] //Rvalue --complex expression

Give an example of some random-access data.

deque

T or F: In C++, constructors should never throw

false

According to "On Following Rules", how should we deal with a rule we do not understand?

follow them

Give an example of some sequential-access data.

linked list, double linked list, reading the cout line

Given a list of size n to search in, at most how many list items does Binary Search need to examine (very roughly)?

n/2

Explain the differences between a copy constructor and a move constructor: how they take their parameters, and what they (probably) do.

passes by reference-to-const parameter is passed by Rvalue reference

A class invariant is a precondition of every public member function, except ...

the constructors

A class invariant is a post-condition of every public member function, except ...

the destructor

T or F: In C++, destructors should never throw.

true Destructors are a "finishing-up" operation Fact 1: An automatic object's dctor is called when it goes out of scope, even if this is due to an exception Fact 2: If an exception is thrown and one of the destructors called before it is caught also throws, then the program terminates

List the the Big Five

~Dog(); //Destructor Dog(const Dog & other); //Copy constructor Dog & operator=(const Dog & rhs) //Copy assignment Dog(Dog && other); //Move constructor Dog & operator = (Dog && rhs); //Move assignment


Set pelajaran terkait

Purpose and Organization of the United Nations

View Set

Prep U for Brunner and Suddarth's Textbook of Medical Surgical Nursing, 13th Edition Chapter 64: Assessment and Management of Patients With Hearing and Balance Disorders

View Set

Control of Gene Expression in Prokaryotes

View Set

History of the Church Chapter 13

View Set

Ethos, Logos and Pathos Assessment

View Set