Object-Oriented Programming, Summer 2018 Final
Name: _______________________________
NetID: ______________________________
-
In C++, _____ implement generic programming, while
_____ implement object-oriented programming.
-
templates, classes *
-
include files, .cpp files
-
classes, templates
-
.cpp files, include files
-
Name the four parts of a function.
-
pre-process, include, link, compile
-
cout, cin, vector, template
-
return type, name, parameter list, function body *
-
The goal of generic programming is
-
to make programs as "plain vanilla" as possible.
-
to allow programmers to write an algorithm once that
can then handle all appropriate types. *
-
deal with the generic sort of problems typical users
complain about.
-
What does the pre-processor do for your program?
-
Turns your C++ code into an object file.
-
Pulls in all of your include files. *
-
Joins separate object files and libraries into a single
executable.
-
Ensures there is a main() function in your program.
-
The C++ compiler... ?
-
Turns Python code into C++ code.
-
Pulls together different object files into an
executable.
-
Turns source files into object files . *
-
If we write
char ch[16]
then
sizeof(ch)
on a 64-bit machine will be:
-
8
-
16 *
-
32
-
100
-
If we write
char ch[8]; char* p = ch;
then
sizeof(p)
on a 64-bit machine will be:
-
8 *
-
16
-
32
-
100
-
If we have a pointer in 64-bit Windows, its sizeof()
most likely is... ?
-
the size of whatever it points to
-
8 *
-
4
-
What does an .h suffix at the end of a file signify in C++?
-
It is a heavy file that will take up lots of hard disk
space.
-
It is a hard-coded file with lots of constants.
-
It is a healthy file that it would be good to
incorporate into your program.
-
It is a header file containing various definitions. *
-
The role of main() in a C++ program is... ?
-
To indicate to the compiler what part of the
program is most important.
-
To tell the linker where to start execution. *
-
To tell the pre-processor what macros to expand.
-
All of the above.
-
In our first program, what is the purpose of
return 0;
?
-
It signals to the OS that the program failed.
-
It signals to the OS that the program succeeded. *
-
It tells the user that this program did nothing of interest.
-
What does the character '\n' do in an output string?
-
terminates all output
-
drops the output down to the next line *
-
adds a nerdline
-
becomes a nexus for further output
-
Generic programming in C++ uses
-
classes
-
structures
-
templates *
-
Programming using class hierarchies is called
-
procedural programming
-
generic programming
-
object-oriented programming *
-
Resolving what type's method will be called at run-time is characteristic of
-
functional programming
-
generic programming
-
object-oriented programming *
-
In C++, a region of memory that holds a value of a certain type is
called a(n) ... ?
-
object *
-
list
-
heap
-
pointer
-
What is a string literal?
-
Any hard-coded value
-
An error message to the user.
-
Taking the value of a variable too literally.
-
A value such as "hello" or "good luck".
-
What is the difference between 'a' and "a" in C++?
-
The first is a character literal, the second is a
string literal.
-
They are the same: you can use the interchangeably.
-
The first is a string literal, the second is a
character literal.
-
Which of the following is a legal variable name in C++?
-
isdigit()
-
is_this_ok *
-
number!
-
3street4
-
We use a declaration of a function when we...
-
we want to write the function body, without giving the
return type or the types of the parameters.
-
just want to state its return type and the types of its
parameters, but not write the function body. *
-
just want to declare that the function exists.
-
A member function of a class can ..
-
Access subclass members
-
Access all the members of the class *
-
Access only the private members of the class
-
Access only public members of the class
-
Which of the following is not necessary for constructors?
-
It must contain a definition body *
-
Its name must be same as that of class
-
It must not have any return type
-
Which of the following operators cannot be overloaded?
-
. (Dot operator)
-
?: (Ternary Operator)
-
:: (Scope resolution operator)
-
All of the above *
-
For a small set of constant values for something like
"boroughs of New York City," a good construct to use would be
a(n)
-
conditional type
-
compound type
-
template
-
enumeration *
-
In C++, const qualifier can be applied to : 1. Member functions
of a class; 2. Function arguments; 3. to a class data member
which is declared as static; 4. Reference variables
-
only 1, 3 and 4
-
only 1, 2 and 3
-
only 1,2 and 4
-
all *
-
An
istream
fundamentally...
-
hides all user input from the programmer
-
hides the details of input devices from the programmer
*
-
hides what data types are being input from the
programmer
-
A file is fundamentally...
-
an icon inside a folder
-
a list of records
-
a sequence of bytes stored on some media *
-
Which of the following is a legal variable name that,
nevertheless, you should not use?
-
CaSe *
-
i
-
rval
-
all of the above answers
-
Which of the following is a safe type conversion?
-
double to int
-
int to char
-
int to bool
-
none of these are safe *
-
If we are trying to represent pi as a double... ?
-
our representation will only approximate the actual
value *
-
it will produce a run-time error.
-
that value is out of range, since the digits continue
forever
-
we can represent it exactly
-
Which of these operators can be used on integers but not
floating point numbers?
-
/
-
+
-
% *
-
-
-
*=
-
Which of the following operators requires an lvalue?
-
[]
-
->
-
/
-
%= *
-
Which of the following operations can be used on an int but not a string?
-
+
-
% *
-
+=
-
The middle part of the for loop statement...
-
initializes the loop variable.
-
determines when the loop terminates. *
-
is done each time around the loop.
-
We should prefer a switch statement to if and else statements when...
-
we only have two options to consider.
-
there are many cases to consider. *
-
the conditions for the branches are string comparisons.
-
When writing an assignment operator overload, why do we return *this?
-
Because in C++, assignment returns a value. *
-
Because we want to draw attention to the object that was assigned to.
-
Because we need to tell the compiler what object we are assigning to.
-
Why don't we add new memory to our vector's storage on every
call to
push_back()
?
-
Because the system won't let us make that many calls to
malloc
.
-
Because it costs a lot of CPU cycles to do that. *
-
Because we are lazy, and don't want to write that much code.
-
If we have a vector named v, then v[2] will access the ___th
element of that vector?
-
4
-
3 *
-
2
-
can't say
-
If a function is declared as double f(char* c); that means...
-
it doubles the value of its argument c and then
converts it to a char.
-
the name of the function is double and it applies the
operator f to a character.
-
it returns a double and accepts a pointer to a
character as an argument. *
-
it returns a double and accepts a character as an argument.
-
The four major types of errors are...
-
header errors, variable errors, function errors and class errors
-
compile-time, link-time, run-time, and logical *
-
object errors, inheritance errors, template errors and polymorphism errors
-
A linker error would occur when...
-
A linked list is specified incorrectly.
-
A function with the same signature is found to be
defined twice. *
-
A link between one class and another is missing.
-
An example of a run-time error is when...
-
you use a template to call a function not in the
template.
-
you left off the semi-colon at the end of a line of
code.
-
you meant to add tax to the sales price but you
subtracted it instead.
-
your program deallocates memory twice. *
-
An example of a logic error is when...
-
you use a type for a template that does not support the
semantics of the template. *
-
you send the wrong type of argument to a function.
-
your program accesses memory that is outside its
allotted area.
-
you forget the semi-colon at the end of a line of code.
-
One reason why throwing an exception is better than returning
an error value is...
-
throwing an exception is more up-to-date.
-
an exception can be caught at various places in the
program. *
-
throwing an exception makes the code that produced the
error also handle it.
-
throwing exceptions is a more modern style.
-
Most large programs...
-
can be fully de-bugged in a couple of days.
-
will likely be bug-free from the start.
-
are read more often than re-written. *
-
Which of the following does not cause a syntax error to be
reported by the C++ compiler?
-
Missing ; at the end of a statement
-
Missing */ in a comment
-
Mismatched []
-
Using a pointer to access unallocated memory *
-
If we want a safer version of a pointer we can use... ?
-
de-referencing a pointer
-
a reference *
-
a pointer to a pointer
-
all of the above
-
Which of the following is not a syntax error?
-
std::cout << "Hello world! ';
-
std::cout << 'Hello world! ';
-
std::cout << "Hello world! "; *
-
Run-time errors are ..
-
the errors which are traced by the compiler during
compilation, due to wrong grammar for the language used
in the program
-
the errors encountered during execution of the program,
due to unexpected input or output *
-
the errors encountered when the program does not give
the desired output
-
A downside of range checking the indexes used on a vector
is that
-
it will take some time and slow the code *
-
sometimes an index will be out of range and produce an
error
-
it stifles programmer creativity
-
none of the above
-
What are the three main phases of software development?
-
analysis, design and implementation *
-
classes, functions, and variables
-
hacking, coding, and testing
-
We typically break an interpreter into two phases called
-
expressing and optimizing
-
tokenization and compiling
-
tokenization and parsing *
-
parsing and expressing
-
In the switch() in the expression function, why is the default
to "put back" the token?
-
That token will probably be a + or - and can be dealt
with by the next call to expression.
-
That token will be meaningless, and we want to throw it
away.
-
If we hit the default case, we have read one token too
many. *
-
A primary purpose of classes is to
-
make our program more classy
-
organize our code better *
-
make our program more object-oriented
-
Classes include
-
header files and libraries
-
methods and header files
-
variables and methods *
-
If we have a constructor that takes a single argument of
another type, like:
Complex::Complex(double d);
that can be used for:
-
enumerating a new type
-
creating a template
-
implicit type conversions between the types *
-
What is a token?
-
a variable
-
Smallest element of a program meaningful to the compiler *
-
a constant
-
Smallest element of a program
-
Why did we introduce a const name like 'number' into our
program?
-
to make the program more maintainable *
-
it saves storage since the const takes up less space
than the char
-
the compiler demands this or it will produce an error
-
all of the above
-
When we want to verify a class we wrote works before using it in
a larger program, we write a... ?
-
sub-class
-
template
-
test harness *
-
What is wrong with commenting x = a + b; with // add a and b
and put value in x
-
It does not describe the code correctly.
-
Variable names should never occur in comments.
-
It will confuse the compiler.
-
It only says what the code already clearly says. *
-
What is the purpose of commenting in a program?
-
It is necessary for the compilation of the code
-
It is something not to be bothered about
-
To make the program more readable and easy to
understand *
-
None of the above
-
What is the use of symbolic constant?
-
Nothing much useful in it
-
The code is less error prone, easier to change and
understand *
-
Symbolic constants help the code to run faster
-
None of the above
-
What is the difference between a function declaration and a
function definition?
-
the declaration includes the function body, while the
definition just gives the function's return and
parameter types
-
the declaration just gives the function's return and
parameter types, while the definition includes the
function body *
-
What are header files used for?
-
To head off disaster.
-
To put a nice header at the top of your code if you
print it.
-
To provide declarations needed for multiple source code
files. *
-
We should use "pass by constant reference" when... ?
-
the object being passed is too large to pass by value,
but we don't want to change its value inside our
function *
-
it makes the structure of the code clearer to a human
reader
-
we want to refer to the object constantly, in many
lines of our code
-
We should use "pass by reference" when... ?
-
the type we are passing is very small
-
we only want to refer to an argument to our function,
not change it
-
we need to change the value of an argument to our
function *
-
We use a move assignment when we
-
do not wish to copy all storage from
the object assigned from and the object assigned to *
-
we wish to move values from one object to another
-
the values being copied are highly mobile
-
all of the above
-
If we write
char* cptr = &c[10];
we get:
-
a pointer to the 11th element of the array c *
-
a pointer to the 10th element of the array c
-
a pointer to an array of 10 elements
-
a pointer to an array of 11 elements
-
Passing by reference means …
-
parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself) *
-
the called function's parameter will be a copy of the callers' passed argument
-
Passing by value means …
-
parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself)
-
the called function's parameter will be a copy of the callers' passed argument *
-
Function definition consists of …
-
function name and body
-
function body and return type
-
function name, parameters, return value type, and body *
-
What is the return type here: int myMethod(int count, double value) { return 4; }
-
MyMethod
-
int *
-
4
-
double
-
count
-
A local variable is one that is... ?
-
declared to be part of a class
-
declared in a header file
-
declared in a function *
-
What is the scope of a variable declared in a user-defined
function?
-
the whole program
-
the main function
-
always and only the entire function
-
none of the above *
-
A copy constructor is use... ?
-
to copy the code of another constructor
-
to convert between basic C++ types
-
to construct an object of type A given
another object of type A *
-
to construct an object of type A given
an object of type B
-
What are the two parts of a class?
-
variants and constants
-
interface and implementation *
-
local and global
-
interference and instantiation
-
Which of the following is a class invariant for
Date
?
-
month should be defined as an
int
-
printed dates should never take up more than one column
in a spreadsheet
-
the year cannot be a leap year
-
we must always have a day less than 31 *
-
When should the body of a function (its definition) be put in
the class definition?
-
when we want the compiler to inline the function *
-
when the function returns a built-in type
-
when the function is over 10 lines long
-
when the function returns a user-defined type
-
What would be a reasonable way to overload the *
operator?
-
to multiply two matrices *
-
to sort a list of strings
-
to access a website and add all of its pages to your
site
-
What does adding
const
to a member function do?
-
promises that the function will not change the object *
-
says the function is a constructor
-
asserts that the function will constantly return the same value for the same input
-
A class can hold the following
-
data
-
functions
-
both data & functions *
-
none of the mentioned
-
How do we specify a method of a class when we are defining it
externally?
-
ClassName~method()
-
ClassName::method() *
-
ClassName->method() *
-
none of the above
-
Which of the following is an access specifier?
-
protected
-
private
-
public
-
all of the above *
-
Why is input usually harder than output?
-
istream
is a trickier class than
ostream
-
input devices require more assembly language code than
output devices
-
The user might input almost anything! *
-
What are the four steps for reading a file?
-
delete the file, read from stdin, write the results
there, and close stdin
-
know its name, open it, read in the contents, and close
it *
-
generate the file, write out its contents, close it,
and verify a good result
-
Unformatted input functions are handled by
-
instream
-
bufstream
-
ostream
-
istream *
-
A class that defines cout, cerr and clog objects and stream insertion operator is
-
istream
-
fstream
-
ostream *
-
kstream
-
Which of the following is not a basic floating-point output
manipulator?
-
setprecision
-
explicit *
-
scientific
-
fixed
-
What does isalpha(c) return?
-
true if is c a letter; false otherwise *
-
the character equivalent of some ASCII code
-
is c printable; false otherwise
-
the ASCII code for a character
-
Your class needs a destructor if...
-
it contains a vector
-
it acquires resources while in use *
-
it is a type of stack
-
it has a virtual member function
-
A pointer that is immutable and automatically de-referenced
is a
-
a pointer to a pointer
-
a reference *
-
an array
-
x = d[-3];
would be valid C++ when d is
-
a vector
-
a double
-
an array *
-
A good use of shallow copying would be when
-
we have a very large object to copy *
-
we just need to scoop out the top few values from an object
-
we don't really care if all the field values are the same
-
A good use of deep copying would be when
-
we need to make sure the values in one object change when values in the other do
-
we need to make sure each objects uses separate memory *
-
we need to save memory
-
We want a default constructor when
-
we can establish meaningful invariants using default values *
-
we really don't know what should initialize a class
-
we want to eliminate the faults (defaults) from our classes
-
We need a destructor when our class
-
has many members
-
has default values
-
has no copy constructor
-
acquires resources *
-
If we write
char* student = "Daniel";
that will
create a character array of ___ bytes:
-
6
-
depends on the compiler
-
8
-
7 *
-
Among common pointer problems are:
-
access off the end of an array
-
access through the null pointer
-
access to a deallocated object
-
all of the above *
-
In C++, we can give our own classes array-style access by:
-
overloading the [] operator *
-
all answers are correct
-
writing
get()
and set()
functions
-
using pointers to pointers
-
Generic programming means
-
programming according to generic standards.
-
writing code that works with a variety of types
presented as arguments. *
-
writing just standard variety code with nothing
outstanding about it.
Problem
8 points
Consider the following code, part of a vector class
where sz holds the number of slots used in the vector's storage, and
space records the vectors current maximum number of items that can be
stored:
void vector::reserve(int newalloc)
{
if(newalloc <= space) return;
double* p = new double[newalloc];
for(int i = 0; i < sz; ++i) p[i] = elem[i]; // copy elements
delete[] elem; // deallocate old space
elem = p;
space = newalloc;
}
void vector::resize(int newsize)
{
reserve(newsize);
for(int i = sz; i < newsize; ++i) elem[i] = 0;
sz = newsize;
}
resize()
needs to handle four possibilities:
-
The new size is larger than the old allocation.
-
The new size is larger than the old size,
but smaller than or equal to the old
allocation.
-
The new size is equal to the old size.
-
The new size is smaller than the old size.
Please reason about the construction of reserve() and resize()
to argue that resize() will (or won't!) handle these four
possibilities correctly.