Download COP3330 Final Exam NEWEST 2025 COMPLETE QUESTIONS AND CORRECT DETAILED ANSWERS (VERIFIED and more Exams Nursing in PDF only on Docsity!
COP3330 Final Exam NEWEST 2025 COMPLETE
QUESTIONS AND CORRECT DETAILED ANSWERS
(VERIFIED ANSWERS) |ALREADY GRADED A+||BRAND
NEW VERSION!!
Which of the following statements is true regarding classes and objects? - CORRECT ANSWER- Member data of a class is in scope inside its member function definitions. For a class called Monster, which of the following is a conversion constructor, enabling automatic type conversions from type Human to type Monster? - CORRECT ANSWER- Monster(Human x); Which of the following would result in a fatal run-time error? - CORRECT ANSWER- Doing the division (x/y), where x and y are integers and y is 0. In a class, the typical purpose of a destructor member function is... - CORRECT ANSWER- To do any final clean-up tasks on an object before it is deallocated. Which of the following statements about operator overloading is true. - CORRECT ANSWER- Operator overloading cannot be used to create new operator symbols. Which of the following describes an implementation of the composition ('has-a') relationship? - CORRECT ANSWER- An object of type Rabbit is declared as member data of class Forest. Which of the following is not a valid operator overload prototype? (Phrase is a class) - CORRECT ANSWER- bool operator!< (const Phrase& a, const Phrase& b);
Which of the following is a function that will only allow an L-value to be passed as a parameter? (Assume that Icon is a class) - CORRECT ANSWER- int Compute(Icon & i) const; Which of the following is the most appropriate declaration of an accessor function for a class called Grade? - CORRECT ANSWER- double GetValue() const; Which of the following is a correct way to declare a member function of a class so that it will be guaranteed to not change the member data of the calling object? - CORRECT ANSWER- int Convert(float& x) const; If a binary operator is overloaded and written as a member function of a class, the function takes one parameter - CORRECT ANSWER- True A friend of a class has access only to the public section of that class. - CORRECT ANSWER- False The command "g++ -o employee.cpp" invokes the compile stage but not the linking stage of the compilation process. - CORRECT ANSWER- False An L-value is an expression that represents a modifiable storage location. - CORRECT ANSWER- True If Penguin is a class and p1 and p2 are objects of type Penguin, in which of the following statements or situations is the class' copy constructor NOT invoked? - CORRECT ANSWER- When p1 is passed into a function by const reference. Given the declaration: double* dptr; Which of the following can be legally assigned into dptr? - CORRECT ANSWER- The variable numbers, declared as follows: double numbers[10];
Which of the following functions must always exist for a class? (i.e. if you do not create one, the compiler will create one automatically) - CORRECT ANSWER- operator= Which of the following is a correct prototype for the assignment operator overload member function in a class called Book? - CORRECT ANSWER- Book& operator=(const Book &); A function in a derived class that has the same prototype as a function in its base class is said to _____ the base class function - CORRECT ANSWER- override Given a pointer declared as voter* precinct; (where voter is a class). How does one dynamically allocate an array of 500 voter objects? - CORRECT ANSWER- precinct = new voter[500]; Which of the following statements correctly de-allocates the dynamic array created in the last question? - CORRECT ANSWER- delete [] precinct; From inside a member function, the keyword "this" acts as... - CORRECT ANSWER- A pointer to a calling object Consider the declaration: virtual double Sing(int x) = 0; Assume this declaration is in a class called Tenor. The "= 0" on this declaration means? - CORRECT ANSWER- The Sing function will not be defined for the Tenor class. All arrays of type char are considered C-style string. - CORRECT ANSWER- False
The shallow copy is sufficient for an object as long as the object does not have any object or arrays as member data - CORRECT ANSWER- False The keyword virtual is used to delay the binding of function call to definition so that it's done at run time, instead of compile time. - CORRECT ANSWER- True If class Z is derived from class D, a pointer of type (D *) can point to an object of type Z - CORRECT ANSWER- True A protected member function of class X is accessible from class X and from inside any class from which X is derived - CORRECT ANSWER- False A bit is the smallest unit of storage in a computer - CORRECT ANSWER- True If ptr is a pointer then *ptr refers to the target of that pointer - CORRECT ANSWER- True cstring library functions, like strcpy, will protect the caller from overflowing array boundaries on character arrays - CORRECT ANSWER- False Function template - CORRECT ANSWER- template < class T > // template< typename T > T functionName( ) { } typedef - CORRECT ANSWER- -allows the creation of a simple user-defined type
- usually as an alias name for another type
- dequeue: removes an item from the queue (i.e. from the front of the line) vector - CORRECT ANSWER- -A data structure that stores items of the same type, and is based on storage in an array By encapsulating an array into a class (a vector class), we can: - CORRECT ANSWER- -use dynamic allocation to allow the internal array to be flexible in size
- handle boundary issues of the array (error checking for out-of-bounds indices) vector advantages/disadvantages - CORRECT ANSWER- -Advantages: Random access - i.e. quick locating of data if the index is known
- Disadvantages: Inserts and Deletes are typically slow, since they may require shifting many elements to consecutive array slots bit - CORRECT ANSWER- smallest unit of storage in a computer byte - CORRECT ANSWER- -consists of 8 bits
- smallest unit of directly addressible storage char - CORRECT ANSWER- -smallest built-in data type
- 1 byte & - CORRECT ANSWER- -bitwise AND
- Bits in the result set to 1 if corresponding operand bits are both 1, and set to 0 otherwise | - CORRECT ANSWER- -bitwise OR
- Bits in the result set to 1 if at least one of the corresponding bits in the two operands is 1. 0 otherwise ^ - CORRECT ANSWER- -bitwise exclusive OR
- Bits in the result set to 1 if exactly one of the corresponding bits in the two operands is 1. Result bit set to 0 if both corresponding bits in operands are the same << - CORRECT ANSWER- -left shift
- Shifts the bits of the first operand to the left, by the number of bits specified in the second operand. Right fill with 0 bits >> - CORRECT ANSWER- -right shift
- Shifts the bits of the first operand to the right, by the number of bits specified in the second operand
- left fill usually based on sign fill with 0's for positive numbers, 1's for negatives ~ - CORRECT ANSWER- -complement
- Flips the bits in the operand. Similar to negation. (All 1's become 0's, and all 0's become 1's) linked list - CORRECT ANSWER- -A collection of data items linked together with pointers, lined up "in a row"
- Typically a list of data of the same type, like an array, but storage is arranged differently What is a linked list made up of? - CORRECT ANSWER- -collection of "nodes", which are created from a self-referential class (or struct)
- Recursion does involve extra overhead (function calls, activation records on the program call stack)
- iterative one usually is slightly more efficient sorting algorithms - CORRECT ANSWER- -Quicksort
- Merge sort fastest sort algorithms to choose from (recursive versions are easier to write) object - CORRECT ANSWER- -an encapsulation of data along with functions that act upon that data
- single instance of a class object consists of: - CORRECT ANSWER- -name
- member data member functions object name - CORRECT ANSWER- the variable name we give it object member data - CORRECT ANSWER- the data that describes the object object member functions - CORRECT ANSWER- behavior aspects of the object (functions related to the object itself) class - CORRECT ANSWER- -blueprint for objects
- user-defined type that describes what a certain type of object will look like
class description - CORRECT ANSWER- -consists of a declaration and a definition
- these pieces are split into separate files DDU design - CORRECT ANSWER- declare, define, use declare - CORRECT ANSWER- -implementation details aren't needed here
- gives an interface variable declaration gives the type function declaration tells how to use it class declaration shows what an object will look like and what its available functions are define - CORRECT ANSWER- -usually consists of the implementation details
- doesn't need to be seen by the user of the interface function definition is the code that makes a function work class definition consists definitions of its members use - CORRECT ANSWER- - use of an item, through its interface
- ex. user of a function is a programmer, who makes calls to the function interface - CORRECT ANSWER- -what the user sees
- strive to create a clear interface for the user protection levels in a class - CORRECT ANSWER- -public or private
you can define it to do anything you want automatically called when you declare an object how to recognize a constructor - CORRECT ANSWER- -It has the same name as the class
- It has no return type scope resolution operator - CORRECT ANSWER- -double colon ::
- used for specifying which class a member belongs to when we define it separately from its declaration inside the class
- className::memberName To call a member function of an existing object: - CORRECT ANSWER- -use dot operator
- objectName.memberName memberName can be a member function call or a member data item to utilize constructor with parameters - CORRECT ANSWER- pass arguments in when the object is declared default constructor - CORRECT ANSWER- -refer to a constructor with no parameters
- used when no parameters are passed A class module normally consists of: - CORRECT ANSWER- -A header file
- An implementation file use the same base filename for corresponding header and implementation files
header file - CORRECT ANSWER- -contains the declaration of the class (without implementation details)
- filename.h implementation file - CORRECT ANSWER- -contains implementations of the class members
- filename.cpp compilation stages - CORRECT ANSWER- -compile stage
- linking stage compile stage - CORRECT ANSWER- -Syntax checked for correctness
- Variables and function calls checked to insure that correct declarations were made and that they match
- Translation into object code object code - CORRECT ANSWER- translation of your code file -- it is not an executable program, at this point linking stage - CORRECT ANSWER- -Links the object code into an executable program
- May involve one or more object code files when function calls are matched up with their definitions makes sure only one definition for every function that is called end result is usually an executable program reasons of compiling and linking done separately: - CORRECT ANSWER- -Changes to a file require only that file to be re-compiled (rather than everything), along with the re-linking
- neither public nor private to grant friend status - CORRECT ANSWER- declaration of the "friend" is made inside the class definition block, with the keyword friend in front of it when to make a friend function - CORRECT ANSWER- When a function works on two objects, it's often convenient to pass both as parameters and make it a friend member function - CORRECT ANSWER- -one of the objects must be the calling object
- The other object is passed as a parameter parameters are pass-by-value calling object could be changed by the function conversion constructor - CORRECT ANSWER- -constructor with one parameter
- automatic type conversions from the parameter type to the class type explicit - CORRECT ANSWER- -used when constructor with a single parameter should not be used for type conversions
- use the keyword explicit on the constructor declaration const everywhere it is applied in code: - CORRECT ANSWER- -Expresses an intent on the part of the programmer (compiler enforces)
- Expresses the intent more clearly to a user Affects how certain items can be used
how const affects how certain items can be used: - CORRECT ANSWER- -Sometimes this is the difference between using L-values vs. R-values in calls to functions
- Sometimes this affects what other items can be used (whether they are also const or not) pass by value - CORRECT ANSWER- -copy is made of the object
- Any R-value can be sent on the call pass by reference (without const) - CORRECT ANSWER- -no copy is made
- only an L-value can be sent on the call const reference - CORRECT ANSWER- -no copy is made
- object cannot be changed through the reference R-values can be sent in const member functions - CORRECT ANSWER- -at the end of a member function prototype
- the function may not change the calling object itself can only be done to member functions of a class member function will not change the member data of that object const must go on the declaration and on the definition constructors - CORRECT ANSWER- -their job is to initialize the object (i.e. set the member data), -not const functions mutators - CORRECT ANSWER- -usually Set functions
- Their job is to change member data
when is a destructor called? - CORRECT ANSWER- right before an object is deallocated by the system (usually when it goes out of scope) destructors job - CORRECT ANSWER- do any clean-up tasks (usually involving memory allocation) that are needed, before an object is deallocated operator overloading - CORRECT ANSWER- -the creation of new versions of these operators for use with user-defined types
- done for the purpose of using familiar operator notation on programmer-defined types (classes) operator overloading rules - CORRECT ANSWER- -cannot change its precedence
- cannot change its associativity
- cannot change its "arity" (i.e. number of operands) Operator meaning on the built-in types cannot be changed not possible to create new operators
binary operator - CORRECT ANSWER- two operands binary operator as a stand-alone function - CORRECT ANSWER- both operands would be sent as parameters, so it would be a function with two parameters binary operator as a member function - CORRECT ANSWER- -first operand would be the calling object
- other would be sent as a parameter (i.e. a function with one parameter)
unary operator - CORRECT ANSWER- one operand unary operator as a stand-alone function - CORRECT ANSWER- operand is sent as a parameter unary operator as a member function - CORRECT ANSWER- one calling object, no parameters operator overload format - CORRECT ANSWER- returnType operatorOperatorSymbol (parameterList); overloading the arithmetic operators - CORRECT ANSWER- can be overloaded either as stand- alone functions or as member functions Overloading as a friend function - CORRECT ANSWER- -advantage is being able to use the more common infix notation
- n3 = n1 + n2; Overloading an operator as a member function - CORRECT ANSWER- -use the more familiar notation, which still works, and no dot-operator required
- n3 = n1 + n2; (n1 is the calling object, n2 is the argument) Overloading comparison operators - CORRECT ANSWER- -can be overloaded either as stand- alone functions or member functions Overloading the insertion << and extraction >> operators - CORRECT ANSWER- -always defined as outside functions (usually friend functions)