Midterm with Answer Key
Correct answers are followed by a *.
In some cases, the text below may differ slightly from the
text on the actual test, due to the effort to correct any
typos or ambiguities.
-
Name the four parts of a function.
-
return type, name, parameter list, function body *
-
main, include, link, compile
-
cout, std_lib, class, comment
-
Which of the following is most likely an error from the linker?
-
"Missing } after line 61."
-
"No main() function found." *
-
"Invalid type: sqrt() requires a double."
-
"Can't find include file <string>."
-
What is the difference between a source file and an object file?
-
The source file is in Python and the object file is in C++.
-
The source file tells you where to look on the Internet for good material and the object file complains.
-
The source file is text in your programming language and the object file is binary. *
-
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. *
-
What function must appear in every C++ program?
-
printf()
-
cout
-
main() *
-
cin
-
What is the purpose of a program using
return
1;
in main()
?
-
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 only 1 thing
of interest.
-
What terminates input into an integer?
-
whitespace
-
anything but a digit *
-
a green screen
-
Ctrl-T
-
What is \n called?
-
netline
-
newline *
-
nerdline
-
nexus
-
What is an object?
-
The definition of what the possible values are for some variable.
-
The bits in memory at some address.
-
A container that holds a value of a certain type. *
-
A pointer to some memory.
-
What is a literal?
-
An error message to the user.
-
An error that literally crashes the program.
-
A "plain" value such as 54 or "Goodbye". *
-
What is the difference between = and ==?
-
The first assigns a value, the second tests for equality. *
-
The first tests for equality, the second assigns a value.
-
They are the same: you can use the interchangeably.
-
Which of the following is a legal variable name in C++?
-
isnum?
-
is_this_ok
-
number .:
-
123streetA
-
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.
-
Which of the following is a legal variable name that,
nevertheless, you should not use?
-
sWitch
-
for_
-
Double
-
all of the above *
-
Which of the following is a safe type conversion?
-
all of these are safe
-
double to int
-
char to int *
-
int to bool
-
If we convert an int value of 4000000 to an char,
the problem is that...
-
the conversion will lose narrow the value down to less
that 256. *
-
it will produce a run-time error.
-
the conversion will lose the fractional part of the
number.
-
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 header of a for loop...
-
is done each time around the loop.
-
initializes the loop variable.
-
determines when the loop terminates. *
-
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.
-
What will the following code print?
Vector<int> v;
v.push_back(7);
v.push_back(8);
v.push_back(9);
v.push_back(1);
cout << v[1] << '\n';
-
7
-
8 *
-
9
-
1
-
If a function is declared as double f(char c); that means...
-
it doubles the value of its argument x and then
converts it to a char.
-
it returns a double and accepts a char as an argument. *
-
the name of the function is double and it applies the
operator f to a char.
-
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 function used in your source code can't be found in any linked file or library. *
-
A variable should be linked by assignment to another variable but it is not.
-
A link between one class and another is missing.
-
An example of a run-time error is when...
-
you send the wrong type of argument to a function.
-
your program tries to access another program's memory. *
-
you meant to substract tax from income but you
added it instead.
-
you forget a brace at the end of a block.
-
An example of a logic error is when...
-
you send the wrong type of argument to a function.
-
you meant to add tax to the sales price but you
subtracted it instead. *
-
your program tries to access non-existent memory.
-
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...
-
the exception cannot be ignored. *
-
throwing an exception makes the code that produced the
error also handle it.
-
throwing exceptions is a more modern style.
-
An example of something student programs are not
expected to handle is:
-
bad input
-
the OS running out of resources *
-
divide-by-zero errors
-
Most large programs...
-
will always contain some bugs. *
-
can be fully de-bugged in a couple of days.
-
will likely be bug-free from the start.
-
Which of the following does not cause a syntax error to be reported by the C++ compiler?
-
Bad indentation *
-
Missing ; at the end of a statement
-
Missing */ in a comment
-
Mismatched {}
-
What will happen when an exception is not caught in a program?
-
the program will terminate *
-
the program will write to a log file
-
that block of code will not execute
-
List some ways to break a program into smaller, more manageable
parts?
-
classes and functions *
-
classes and control structures
-
if and while statements
-
variables and functions
-
What are the three main phases of software development?
-
analysis, design and implementation *
-
classes, functions, and variables
-
hacking, coding, and testing
-
If we input (3+7)/(8-2) to the calculator, then (3+7) is a
-
expression
-
term
-
primary *
-
number
-
We typically break an interpreter into two phases called
-
expressing and optimizing
-
tokenization and compiling
-
tokenization and parsing *
-
parsing and expressing
-
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 *
-
What is a "use case"?
-
It helps in checking the usability of the program at different situations
-
It is a term that describes how a user can make a program re-usable
-
It is a term that describe how a user uses a system to accomplish a particular goal *
-
What is a token?
-
a variable
-
a number of characters we consider a "unit," such as a
number or variable name*
-
a constant
-
smallest element of a program
-
Why do we want to rely on libraries of code?
-
It helps in improving your coding skills
-
It saves time, and uses pre-tested code *
-
We should not rely on them and should instead write our own code
-
Which is not a type of access modifier in C++?
-
private
-
public
-
personal *
-
Why did we introduce a
const
name like 'number' into our program?
-
to make the program more maintainable
-
to make it easier to change the char for a number
-
to make the program more readable
-
all of the above are correct *
-
When should you start to test your program?
-
only when done coding
-
testing isn't really necessary
-
almost as soon as you've started coding *
-
When should you retest?
-
Only after major changes.
-
After every change, no matter how small. *
-
One round of testing is enough.
-
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 do we call
signal_error
in the code below?
const int signal_error = 7;
-
an integral signal
-
a constant struggle
-
a shambolic constant
-
a symbolic constant *
-
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. *
-
How does indentation help in a C++ program?
-
it helps the compiler do code optimizations
-
it makes the structure of the code clearer to a human
reader *
-
it is necessary to compile the code correctly, just as
in Python
-
If we need to alter the original of a variable we are passing
to a function, we should...
-
pass by constant reference
-
pass by reference *
-
pass by value
-
A function declaration consists of...
-
function name, return type and parameter list *
-
function body and parameter list
-
function name and parameter list
-
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 *
-
Let's say we need to fill in the value of a variable
in a function called
set_value()
. What will be the
problem with the following loop?
#include "vars.h" // assume this declares class Variable
void set_value(string nm, double val)
{
for(Variable var : var_table) { // assume var_table is in this file
if(var.name == nm)
var.value = val;
}
}
-
var.name == name
:
You can't test string
equality
like that!
-
There is no
for
loop like that
in C++.
-
var.value = val;
won't
reset the value in the variable table.
-
What is the return type here:
int myMethod(int count, double value) { return 4; }
-
MyMethod
-
int *
-
4
-
double
-
count
-
Identify the correct statement
-
A namespace is used to mark the beginning of the program
-
A namespace is used to separate a class from objects
-
A namespace is used to group classes, objects and
functions *
-
None of the above
-
What is the scope of the variable declared in a user-defined
function?
-
the whole program
-
anywhere called from the main function
-
only inside the {} block in which it is declared *
-
none of the above
-
If your function does not return any value, which of the
following keywords should be used as a return type?
-
double
-
int
-
void *
-
float
-
unsigned short