Object-Oriented Programming, Summer 2018 Final

Name: _______________________________

NetID: ______________________________

  1. In C++, _____ implement generic programming, while _____ implement object-oriented programming.
    1. templates, classes *
    2. include files, .cpp files
    3. classes, templates
    4. .cpp files, include files
  2. Name the four parts of a function.
    1. pre-process, include, link, compile
    2. cout, cin, vector, template
    3. return type, name, parameter list, function body *
  3. The goal of generic programming is
    1. to make programs as "plain vanilla" as possible.
    2. to allow programmers to write an algorithm once that can then handle all appropriate types. *
    3. deal with the generic sort of problems typical users complain about.
  4. What does the pre-processor do for your program?
    1. Turns your C++ code into an object file.
    2. Pulls in all of your include files. *
    3. Joins separate object files and libraries into a single executable.
    4. Ensures there is a main() function in your program.
  5. The C++ compiler... ?
    1. Turns Python code into C++ code.
    2. Pulls together different object files into an executable.
    3. Turns source files into object files . *
  6. If we write char ch[16] then sizeof(ch) on a 64-bit machine will be:
    1. 8
    2. 16 *
    3. 32
    4. 100
  7. If we write char ch[8]; char* p = ch; then sizeof(p) on a 64-bit machine will be:
    1. 8 *
    2. 16
    3. 32
    4. 100
  8. If we have a pointer in 64-bit Windows, its sizeof() most likely is... ?
    1. the size of whatever it points to
    2. 8 *
    3. 4
  9. 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. *
  10. The role of main() in a C++ program is... ?
    1. To indicate to the compiler what part of the program is most important.
    2. To tell the linker where to start execution. *
    3. To tell the pre-processor what macros to expand.
    4. All of the above.
  11. In our first program, what is the purpose of return 0;?
    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 nothing of interest.
  12. What does the character '\n' do in an output string?
    1. terminates all output
    2. drops the output down to the next line *
    3. adds a nerdline
    4. becomes a nexus for further output
  13. Generic programming in C++ uses
    1. classes
    2. structures
    3. templates *
  14. Programming using class hierarchies is called
    1. procedural programming
    2. generic programming
    3. object-oriented programming *
  15. Resolving what type's method will be called at run-time is characteristic of
    1. functional programming
    2. generic programming
    3. object-oriented programming *
  16. In C++, a region of memory that holds a value of a certain type is called a(n) ... ?
    1. object *
    2. list
    3. heap
    4. pointer
  17. What is a string literal?
    1. Any hard-coded value
    2. An error message to the user.
    3. Taking the value of a variable too literally.
    4. A value such as "hello" or "good luck".
  18. What is the difference between 'a' and "a" in C++?
    1. The first is a character literal, the second is a string literal.
    2. They are the same: you can use the interchangeably.
    3. The first is a string literal, the second is a character literal.
  19. Which of the following is a legal variable name in C++?
    1. isdigit()
    2. is_this_ok *
    3. number!
    4. 3street4
  20. 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.
  21. A member function of a class can ..
    1. Access subclass members
    2. Access all the members of the class *
    3. Access only the private members of the class
    4. Access only public members of the class
  22. Which of the following is not necessary for constructors?
    1. It must contain a definition body *
    2. Its name must be same as that of class
    3. It must not have any return type
  23. Which of the following operators cannot be overloaded?
    1. . (Dot operator)
    2. ?: (Ternary Operator)
    3. :: (Scope resolution operator)
    4. All of the above *
  24. For a small set of constant values for something like "boroughs of New York City," a good construct to use would be a(n)
    1. conditional type
    2. compound type
    3. template
    4. enumeration *
  25. 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
    1. only 1, 3 and 4
    2. only 1, 2 and 3
    3. only 1,2 and 4
    4. all *
  26. An istream fundamentally...
    1. hides all user input from the programmer
    2. hides the details of input devices from the programmer *
    3. hides what data types are being input from the programmer
  27. A file is fundamentally...
    1. an icon inside a folder
    2. a list of records
    3. a sequence of bytes stored on some media *
  28. Which of the following is a legal variable name that, nevertheless, you should not use?
    1. CaSe *
    2. i
    3. rval
    4. all of the above answers
  29. Which of the following is a safe type conversion?
    1. double to int
    2. int to char
    3. int to bool
    4. none of these are safe *
  30. If we are trying to represent pi as a double... ?
    1. our representation will only approximate the actual value *
    2. it will produce a run-time error.
    3. that value is out of range, since the digits continue forever
    4. we can represent it exactly
  31. Which of these operators can be used on integers but not floating point numbers?
    1. /
    2. +
    3. % *
    4. -
    5. *=
  32. Which of the following operators requires an lvalue?
    1. []
    2. ->
    3. /
    4. %= *
  33. Which of the following operations can be used on an int but not a string?
    1. +
    2. % *
    3. +=
  34. The middle part of the for loop statement...
    1. initializes the loop variable.
    2. determines when the loop terminates. *
    3. is done each time around the loop.
  35. 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.
  36. When writing an assignment operator overload, why do we return *this?
    1. Because in C++, assignment returns a value. *
    2. Because we want to draw attention to the object that was assigned to.
    3. Because we need to tell the compiler what object we are assigning to.
  37. Why don't we add new memory to our vector's storage on every call to push_back()?
    1. Because the system won't let us make that many calls to malloc.
    2. Because it costs a lot of CPU cycles to do that. *
    3. Because we are lazy, and don't want to write that much code.
  38. If we have a vector named v, then v[2] will access the ___th element of that vector?
    1. 4
    2. 3 *
    3. 2
    4. can't say
  39. If a function is declared as double f(char* c); that means...
    1. it doubles the value of its argument c and then converts it to a char.
    2. the name of the function is double and it applies the operator f to a character.
    3. it returns a double and accepts a pointer to a character as an argument. *
    4. it returns a double and accepts a character as an argument.
  40. 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
  41. A linker error would occur when...
    1. A linked list is specified incorrectly.
    2. A function with the same signature is found to be defined twice. *
    3. A link between one class and another is missing.
  42. An example of a run-time error is when...
    1. you use a template to call a function not in the template.
    2. you left off the semi-colon at the end of a line of code.
    3. you meant to add tax to the sales price but you subtracted it instead.
    4. your program deallocates memory twice. *
  43. An example of a logic error is when...
    1. you use a type for a template that does not support the semantics of the template. *
    2. you send the wrong type of argument to a function.
    3. your program accesses memory that is outside its allotted area.
    4. you forget the semi-colon at the end of a line of code.
  44. One reason why throwing an exception is better than returning an error value is...
    1. throwing an exception is more up-to-date.
    2. an exception can be caught at various places in the program. *
    3. throwing an exception makes the code that produced the error also handle it.
    4. throwing exceptions is a more modern style.
  45. Most large programs...
    1. can be fully de-bugged in a couple of days.
    2. will likely be bug-free from the start.
    3. are read more often than re-written. *
  46. Which of the following does not cause a syntax error to be reported by the C++ compiler?
    1. Missing ; at the end of a statement
    2. Missing */ in a comment
    3. Mismatched []
    4. Using a pointer to access unallocated memory *
  47. If we want a safer version of a pointer we can use... ?
    1. de-referencing a pointer
    2. a reference *
    3. a pointer to a pointer
    4. all of the above
  48. Which of the following is not a syntax error?
    1. std::cout << "Hello world! ';
    2. std::cout << 'Hello world! ';
    3. std::cout << "Hello world! "; *
  49. Run-time errors are ..
    1. the errors which are traced by the compiler during compilation, due to wrong grammar for the language used in the program
    2. the errors encountered during execution of the program, due to unexpected input or output *
    3. the errors encountered when the program does not give the desired output
  50. A downside of range checking the indexes used on a vector is that
    1. it will take some time and slow the code *
    2. sometimes an index will be out of range and produce an error
    3. it stifles programmer creativity
    4. none of the above
  51. What are the three main phases of software development?
    1. analysis, design and implementation *
    2. classes, functions, and variables
    3. hacking, coding, and testing
  52. 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
  53. In the switch() in the expression function, why is the default to "put back" the token?
    1. That token will probably be a + or - and can be dealt with by the next call to expression.
    2. That token will be meaningless, and we want to throw it away.
    3. If we hit the default case, we have read one token too many. *
  54. 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
  55. Classes include
    1. header files and libraries
    2. methods and header files
    3. variables and methods *
  56. If we have a constructor that takes a single argument of another type, like:
    Complex::Complex(double d);
    that can be used for:
    1. enumerating a new type
    2. creating a template
    3. implicit type conversions between the types *
  57. What is a token?
    1. a variable
    2. Smallest element of a program meaningful to the compiler *
    3. a constant
    4. Smallest element of a program
  58. Why did we introduce a const name like 'number' into our program?
    1. to make the program more maintainable *
    2. it saves storage since the const takes up less space than the char
    3. the compiler demands this or it will produce an error
    4. all of the above
  59. When we want to verify a class we wrote works before using it in a larger program, we write a... ?
    1. sub-class
    2. template
    3. test harness *
  60. 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. *
  61. 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
  62. What is the use of symbolic constant?
    1. Nothing much useful in it
    2. The code is less error prone, easier to change and understand *
    3. Symbolic constants help the code to run faster
    4. None of the above
  63. 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 *
  64. 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. *
  65. We should use "pass by constant reference" when... ?
    1. the object being passed is too large to pass by value, but we don't want to change its value inside our function *
    2. it makes the structure of the code clearer to a human reader
    3. we want to refer to the object constantly, in many lines of our code
  66. We should use "pass by reference" when... ?
    1. the type we are passing is very small
    2. we only want to refer to an argument to our function, not change it
    3. we need to change the value of an argument to our function *
  67. We use a move assignment when we
    1. do not wish to copy all storage from the object assigned from and the object assigned to *
    2. we wish to move values from one object to another
    3. the values being copied are highly mobile
    4. all of the above
  68. If we write
    char* cptr = &c[10];
    we get:
    1. a pointer to the 11th element of the array c *
    2. a pointer to the 10th element of the array c
    3. a pointer to an array of 10 elements
    4. a pointer to an array of 11 elements
  69. 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
  70. 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 *
  71. Function definition consists of …
    1. function name and body
    2. function body and return type
    3. function name, parameters, return value type, and body *
  72. What is the return type here: int myMethod(int count, double value) {    return 4; }
    1. MyMethod
    2. int *
    3. 4
    4. double
    5. count
  73. A local variable is one that is... ?
    1. declared to be part of a class
    2. declared in a header file
    3. declared in a function *
  74. What is the scope of a variable declared in a user-defined function?
    1. the whole program
    2. the main function
    3. always and only the entire function
    4. none of the above *
  75. A copy constructor is use... ?
    1. to copy the code of another constructor
    2. to convert between basic C++ types
    3. to construct an object of type A given another object of type A *
    4. to construct an object of type A given an object of type B
  76. What are the two parts of a class?
    1. variants and constants
    2. interface and implementation *
    3. local and global
    4. interference and instantiation
  77. Which of the following is a class invariant for Date?
    1. month should be defined as an int
    2. printed dates should never take up more than one column in a spreadsheet
    3. the year cannot be a leap year
    4. we must always have a day less than 31 *
  78. When should the body of a function (its definition) be put in the class definition?
    1. when we want the compiler to inline the function *
    2. when the function returns a built-in type
    3. when the function is over 10 lines long
    4. when the function returns a user-defined type
  79. What would be a reasonable way to overload the * operator?
    1. to multiply two matrices *
    2. to sort a list of strings
    3. to access a website and add all of its pages to your site
  80. What does adding const to a member function do?
    1. promises that the function will not change the object *
    2. says the function is a constructor
    3. asserts that the function will constantly return the same value for the same input
  81. A class can hold the following
    1. data
    2. functions
    3. both data & functions *
    4. none of the mentioned
  82. How do we specify a method of a class when we are defining it externally?
    1. ClassName~method()
    2. ClassName::method() *
    3. ClassName->method() *
    4. none of the above
  83. Which of the following is an access specifier?
    1. protected
    2. private
    3. public
    4. all of the above *
  84. Why is input usually harder than output?
    1. istream is a trickier class than ostream
    2. input devices require more assembly language code than output devices
    3. The user might input almost anything! *
  85. What are the four steps for reading a file?
    1. delete the file, read from stdin, write the results there, and close stdin
    2. know its name, open it, read in the contents, and close it *
    3. generate the file, write out its contents, close it, and verify a good result
  86. Unformatted input functions are handled by
    1. instream
    2. bufstream
    3. ostream
    4. istream *
  87. A class that defines cout, cerr and clog objects and stream insertion operator is
    1. istream
    2. fstream
    3. ostream *
    4. kstream
  88. Which of the following is not a basic floating-point output manipulator?
    1. setprecision
    2. explicit *
    3. scientific
    4. fixed
  89. What does isalpha(c) return?
    1. true if is c a letter; false otherwise *
    2. the character equivalent of some ASCII code
    3. is c printable; false otherwise
    4. the ASCII code for a character
  90. Your class needs a destructor if...
    1. it contains a vector
    2. it acquires resources while in use *
    3. it is a type of stack
    4. it has a virtual member function
  91. A pointer that is immutable and automatically de-referenced is a
    1. a pointer to a pointer
    2. a reference *
    3. an array
  92. x = d[-3]; would be valid C++ when d is
    1. a vector
    2. a double
    3. an array *
  93. A good use of shallow copying would be when
    1. we have a very large object to copy *
    2. we just need to scoop out the top few values from an object
    3. we don't really care if all the field values are the same
  94. A good use of deep copying would be when
    1. we need to make sure the values in one object change when values in the other do
    2. we need to make sure each objects uses separate memory *
    3. we need to save memory
  95. We want a default constructor when
    1. we can establish meaningful invariants using default values *
    2. we really don't know what should initialize a class
    3. we want to eliminate the faults (defaults) from our classes
  96. We need a destructor when our class
    1. has many members
    2. has default values
    3. has no copy constructor
    4. acquires resources *
  97. If we write char* student = "Daniel"; that will create a character array of ___ bytes:
    1. 6
    2. depends on the compiler
    3. 8
    4. 7 *
  98. Among common pointer problems are:
    1. access off the end of an array
    2. access through the null pointer
    3. access to a deallocated object
    4. all of the above *
  99. In C++, we can give our own classes array-style access by:
    1. overloading the [] operator *
    2. all answers are correct
    3. writing get() and set() functions
    4. using pointers to pointers
  100. Generic programming means
    1. programming according to generic standards.
    2. writing code that works with a variety of types presented as arguments. *
    3. 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:

Please reason about the construction of reserve() and resize() to argue that resize() will (or won't!) handle these four possibilities correctly.