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.

  1. Name the four parts of a function.
    1. return type, name, parameter list, function body *
    2. main, include, link, compile
    3. cout, std_lib, class, comment

  2. Which of the following is most likely an error from the linker?
    1. "Missing } after line 61."
    2. "No main() function found." *
    3. "Invalid type: sqrt() requires a double."
    4. "Can't find include file <string>."

  3. What is the difference between a source file and an object file?
    1. The source file is in Python and the object file is in C++.
    2. The source file tells you where to look on the Internet for good material and the object file complains.
    3. The source file is text in your programming language and the object file is binary. *

  4. What does an .h suffix at the end of a file signify in C++?
    1. It is a heavy file that will take up lots of hard disk space.
    2. It is a hard-coded file with lots of constants.
    3. It is a healthy file that it would be good to incorporate into your program.
    4. It is a header file containing various definitions. *

  5. What function must appear in every C++ program?
    1. printf()
    2. cout
    3. main() *
    4. cin

  6. What is the purpose of a program using return 1; in main()?
    1. It signals to the OS that the program failed. *
    2. It signals to the OS that the program succeeded.
    3. It tells the user that this program did only 1 thing of interest.

  7. What terminates input into an integer?
    1. whitespace
    2. anything but a digit *
    3. a green screen
    4. Ctrl-T

  8. What is \n called?
    1. netline
    2. newline *
    3. nerdline
    4. nexus

  9. What is an object?
    1. The definition of what the possible values are for some variable.
    2. The bits in memory at some address.
    3. A container that holds a value of a certain type. *
    4. A pointer to some memory.

  10. What is a literal?
    1. An error message to the user.
    2. An error that literally crashes the program.
    3. A "plain" value such as 54 or "Goodbye". *

  11. What is the difference between = and ==?
    1. The first assigns a value, the second tests for equality. *
    2. The first tests for equality, the second assigns a value.
    3. They are the same: you can use the interchangeably.

  12. Which of the following is a legal variable name in C++?
    1. isnum?
    2. is_this_ok
    3. number .:
    4. 123streetA

  13. We use a declaration of a function when we...
    1. we want to write the function body, without giving the return type or the types of the parameters.
    2. just want to state its return type and the types of its parameters, but not write the function body. *
    3. just want to declare that the function exists.

  14. Which of the following is a legal variable name that, nevertheless, you should not use?
    1. sWitch
    2. for_
    3. Double
    4. all of the above *

  15. Which of the following is a safe type conversion?
    1. all of these are safe
    2. double to int
    3. char to int *
    4. int to bool

  16. If we convert an int value of 4000000 to an char, the problem is that...
    1. the conversion will lose narrow the value down to less that 256. *
    2. it will produce a run-time error.
    3. the conversion will lose the fractional part of the number.

  17. Which of these operators can be used on integers but not floating point numbers?
    1. /
    2. +
    3. % *
    4. -
    5. *

  18. Which of the following operators requires an lvalue?
    1. ||
    2. +
    3. /
    4. *= *

  19. Which of the following operations can be used on an int but not a string?
    1. <<
    2. - *
    3. +=

  20. The middle part of the header of a for loop...
    1. is done each time around the loop.
    2. initializes the loop variable.
    3. determines when the loop terminates. *

  21. We should prefer a switch statement to if and else statements when...
    1. we only have two options to consider.
    2. there are many cases to consider. *
    3. the conditions for the branches are string comparisons.

  22. 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';
                        

    1. 7
    2. 8 *
    3. 9
    4. 1

  23. If a function is declared as double f(char c); that means...
    1. it doubles the value of its argument x and then converts it to a char.
    2. it returns a double and accepts a char as an argument. *
    3. the name of the function is double and it applies the operator f to a char.

  24. The four major types of errors are...
    1. header errors, variable errors, function errors and class errors
    2. compile-time, link-time, run-time, and logical *
    3. object errors, inheritance errors, template errors and polymorphism errors

  25. A linker error would occur when...
    1. A function used in your source code can't be found in any linked file or library. *
    2. A variable should be linked by assignment to another variable but it is not.
    3. A link between one class and another is missing.

  26. An example of a run-time error is when...
    1. you send the wrong type of argument to a function.
    2. your program tries to access another program's memory. *
    3. you meant to substract tax from income but you added it instead.
    4. you forget a brace at the end of a block.

  27. An example of a logic error is when...
    1. you send the wrong type of argument to a function.
    2. you meant to add tax to the sales price but you subtracted it instead. *
    3. your program tries to access non-existent memory.
    4. you forget the semi-colon at the end of a line of code.

  28. One reason why throwing an exception is better than returning an error value is...
    1. the exception cannot be ignored. *
    2. throwing an exception makes the code that produced the error also handle it.
    3. throwing exceptions is a more modern style.

  29. An example of something student programs are not expected to handle is:
    1. bad input
    2. the OS running out of resources *
    3. divide-by-zero errors

  30. Most large programs...
    1. will always contain some bugs. *
    2. can be fully de-bugged in a couple of days.
    3. will likely be bug-free from the start.

  31. Which of the following does not cause a syntax error to be reported by the C++ compiler?
    1. Bad indentation *
    2. Missing ; at the end of a statement
    3. Missing */ in a comment
    4. Mismatched {}

  32. What will happen when an exception is not caught in a program?
    1. the program will terminate *
    2. the program will write to a log file
    3. that block of code will not execute

  33. List some ways to break a program into smaller, more manageable parts?
    1. classes and functions *
    2. classes and control structures
    3. if and while statements
    4. variables and functions

  34. What are the three main phases of software development?
    1. analysis, design and implementation *
    2. classes, functions, and variables
    3. hacking, coding, and testing

  35. If we input (3+7)/(8-2) to the calculator, then (3+7) is a
    1. expression
    2. term
    3. primary *
    4. number

  36. We typically break an interpreter into two phases called
    1. expressing and optimizing
    2. tokenization and compiling
    3. tokenization and parsing *
    4. parsing and expressing

  37. A primary purpose of classes is to
    1. make our program more classy
    2. organize our code better *
    3. make our program more object-oriented

  38. Classes include
    1. header files and libraries
    2. methods and header files
    3. variables and methods *

  39. What is a "use case"?
    1. It helps in checking the usability of the program at different situations
    2. It is a term that describes how a user can make a program re-usable
    3. It is a term that describe how a user uses a system to accomplish a particular goal *

  40. What is a token?
    1. a variable
    2. a number of characters we consider a "unit," such as a number or variable name*
    3. a constant
    4. smallest element of a program

  41. Why do we want to rely on libraries of code?
    1. It helps in improving your coding skills
    2. It saves time, and uses pre-tested code *
    3. We should not rely on them and should instead write our own code

  42. Which is not a type of access modifier in C++?
    1. private
    2. public
    3. personal *

  43. Why did we introduce a const name like 'number' into our program?
    1. to make the program more maintainable
    2. to make it easier to change the char for a number
    3. to make the program more readable
    4. all of the above are correct *

  44. When should you start to test your program?
    1. only when done coding
    2. testing isn't really necessary
    3. almost as soon as you've started coding *

  45. When should you retest?
    1. Only after major changes.
    2. After every change, no matter how small. *
    3. One round of testing is enough.

  46. What is wrong with commenting x = a + b; with // add a and b and put value in x?
    1. It does not describe the code correctly.
    2. Variable names should never occur in comments.
    3. It will confuse the compiler.
    4. It only says what the code already clearly says. *

  47. What is the purpose of commenting in a program?
    1. It is necessary for the compilation of the code
    2. It is something not to be bothered about
    3. To make the program more readable and easy to understand *
    4. None of the above

  48. What do we call signal_error in the code below?

    const int signal_error = 7;
                    

    1. an integral signal
    2. a constant struggle
    3. a shambolic constant
    4. a symbolic constant *

  49. What is the difference between a function declaration and a function definition?
    1. the declaration includes the function body, while the definition just gives the function's return and parameter types
    2. the declaration just gives the function's return and parameter types, while the definition includes the function body *

  50. What are header files used for?
    1. To head off disaster.
    2. To put a nice header at the top of your code if you print it.
    3. To provide declarations needed for multiple source code files. *

  51. How does indentation help in a C++ program?
    1. it helps the compiler do code optimizations
    2. it makes the structure of the code clearer to a human reader *
    3. it is necessary to compile the code correctly, just as in Python

  52. If we need to alter the original of a variable we are passing to a function, we should...
    1. pass by constant reference
    2. pass by reference *
    3. pass by value

  53. A function declaration consists of...
    1. function name, return type and parameter list *
    2. function body and parameter list
    3. function name and parameter list

  54. Passing by reference means...
    1. parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself) *
    2. the called function's parameter will be a copy of the callers' passed argument

  55. Passing by value means …
    1. parameter will be the same as the callers' passed argument (not the value, but the identity - the variable itself)
    2. the called function's parameter will be a copy of the callers' passed argument *

  56. 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;
        }
    }
                        

    1. var.name == name:
      You can't test string equality like that!
    2. There is no for loop like that in C++.
    3. var.value = val; won't reset the value in the variable table.

  57. What is the return type here: int myMethod(int count, double value) { return 4; }
    1. MyMethod
    2. int *
    3. 4
    4. double
    5. count

  58. Identify the correct statement
    1. A namespace is used to mark the beginning of the program
    2. A namespace is used to separate a class from objects
    3. A namespace is used to group classes, objects and functions *
    4. None of the above

  59. What is the scope of the variable declared in a user-defined function?
    1. the whole program
    2. anywhere called from the main function
    3. only inside the {} block in which it is declared *
    4. none of the above

  60. If your function does not return any value, which of the following keywords should be used as a return type?
    1. double
    2. int
    3. void *
    4. float
    5. unsigned short