2.
You should only use pointers with delete that were previously used with
__________.
Explanation
When using the delete operator in C++, it is important to only use pointers that were previously allocated using the new operator. This is because new and delete are a pair of operators that work together to dynamically allocate and deallocate memory. Using delete with a pointer that was not allocated with new can lead to undefined behavior and potential memory leaks. Therefore, it is necessary to ensure that the pointer being used with delete was indeed allocated using new.
3.
When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to.
Explanation
When the indirection operator (*) is used with a pointer variable, it allows us to access and manipulate the value stored at the memory address pointed to by the pointer. In other words, it dereferences the pointer and gives us access to the actual value being pointed to. Therefore, the statement "When the indirection operator is used with a pointer variable, you are actually working with the value the pointer is pointing to" is true.
4.
A class is very similar to a(n) _________.
Correct Answer
structure
Explanation
A class is very similar to a structure because both are used to define a blueprint for creating objects. They both contain data members and member functions that define the properties and behavior of the objects created from them. Like a structure, a class can have public and private access specifiers to control the visibility of its members. Additionally, both structures and classes can be used to group related data and functions together for better organization and modularity in programming.
5.
The default access specification of class members is _________.
Correct Answer
private
Explanation
The default access specification of class members is private. This means that if no access specifier is explicitly defined for a class member, it will be accessible only within the same class and not from outside the class or its derived classes.
6.
The default access specification of a struct in C++ is _________.
Correct Answer
public
Explanation
The default access specification of a struct in C++ is public. This means that all members of the struct can be accessed by any code within the program without any restrictions.
7.
A(n) _________ constructor is one that requires no arguments.
Correct Answer
default
Explanation
A default constructor is one that requires no arguments. It is a constructor that is automatically generated by the compiler if no other constructors are defined in the class. This default constructor initializes the object with default values or performs default actions. It is used when an object needs to be created without any specific initialization parameters.
8.
A(n) _________ is a member function that is automatically called when an object is destroyed.
Correct Answer
destructor
Explanation
A destructor is a member function that is automatically called when an object is destroyed. It is responsible for cleaning up any resources that were allocated by the object during its lifetime. This includes freeing memory, closing files, releasing locks, or any other necessary cleanup operations. The destructor has the same name as the class, preceded by a tilde (~), and does not take any parameters. When an object goes out of scope or is explicitly deleted, the destructor is called to ensure proper cleanup.
9.
Like constructors, destructors cannot have a(n) _________ type.
Correct Answer
return
Explanation
Destructors, like constructors, do not have a return type because they are automatically called when an object is destroyed or goes out of scope. They do not return any value, so specifying a return type is not necessary.
10.
Classes and structures in C++ are very similar.
Explanation
Classes and structures in C++ are very similar because they both allow the creation of user-defined types. They both can have data members and member functions, and can be used to create objects. The main difference between classes and structures is that by default, members of a class are private, while members of a structure are public. However, this difference can be overridden by using access specifiers. Overall, the similarities between classes and structures make them interchangeable in many cases, allowing programmers to choose the one that best suits their needs.
11.
You can use the new operator to dynamically allocate an instance of a class.
Explanation
The statement is true because the new operator in programming languages like C++ and Java is used to allocate memory for a new object dynamically at runtime. This means that the memory for the object is allocated on the heap instead of the stack, allowing for more flexibility in object creation and destruction. By using the new operator, you can create instances of a class dynamically and access them using a pointer or reference.
12.
Member functions may be overloaded.
Explanation
Overloading in programming refers to the ability to define multiple functions with the same name but different parameters. In the case of member functions, it means that within a class, you can have multiple functions with the same name but different parameters. This allows for more flexibility and convenience when working with objects of that class, as you can choose the appropriate function based on the arguments provided. Therefore, the statement "Member functions may be overloaded" is true.
13.
A constructor whose arguments all have default values is a(n) _________ constructor.
Correct Answer
default
Explanation
A constructor whose arguments all have default values is referred to as a default constructor.
14.
When a program is finished with a chunk of dynamically allocated memory, it should
free it with the __________ operator.
Correct Answer
delete
Explanation
When a program is finished with a chunk of dynamically allocated memory, it should be freed using the "delete" operator. This operator deallocates the memory previously allocated by the "new" operator, ensuring that the memory is released and can be reused by the program or other processes. Failing to properly free dynamically allocated memory can lead to memory leaks and inefficiency in the program's execution.
15.
When used as function parameters, reference variables are much easier to work with than pointers.
Explanation
Reference variables are easier to work with than pointers when used as function parameters because reference variables provide a more intuitive and convenient way to access and manipulate the data being passed to a function. Unlike pointers, reference variables do not require explicit dereferencing and can be used directly as if they were the original variables. This simplifies the syntax and reduces the chances of errors caused by mistakenly dereferencing a null pointer or accessing invalid memory locations. Additionally, reference variables are automatically bound to the original variables they reference, eliminating the need for explicit memory allocation and deallocation.
16.
The new operator dynamically allocates memory.
Explanation
The statement is true because the new operator in C++ is used to dynamically allocate memory during runtime. It allows the program to request memory from the operating system and then use that memory for storing data. This dynamic allocation is useful when the size of the data needed is not known at compile time or when the program needs to allocate memory for objects that have a variable size. By using the new operator, the program can allocate and deallocate memory as needed, improving flexibility and efficiency.
17.
Each byte of memory is assigned a unique address.
Explanation
In computer systems, memory is divided into small units called bytes. Each byte is assigned a unique address, which allows the computer to locate and access specific data stored in memory. This addressing scheme is essential for efficient data retrieval and manipulation by the computer's processor. Therefore, the statement that each byte of memory is assigned a unique address is true.
18.
A(n) _________ is automatically called when an object is created.
Correct Answer
constructor
Explanation
A constructor is automatically called when an object is created. It is a special member function of a class that is responsible for initializing the object's data members. When an object is created, the constructor is invoked automatically, and it sets the initial values for the object's attributes. This ensures that the object is in a valid state when it is first created. Constructors have the same name as the class and do not have a return type.
19.
It is legal to define a pointer to a class object.
Explanation
In programming, it is legal to define a pointer to a class object. Pointers are used to store memory addresses, and they can be used to access and manipulate objects of a class. By defining a pointer to a class object, we can create dynamic instances of the class, allocate memory for them, and access their properties and methods through the pointer. This allows for more flexibility and dynamic behavior in the program.
20.
Constructors may have default arguments.
Explanation
Constructors in programming are special methods that are used to initialize objects of a class. They can have parameters, which are used to pass values to the constructor during object creation. Default arguments are values that are automatically assigned to parameters if no value is provided when calling the constructor. This allows for flexibility and convenience when creating objects, as certain parameters can be optional. Therefore, constructors can indeed have default arguments, making the given answer "True" correct.
21.
_________ programming is centered around functions or procedures.
Correct Answer
procedural
Explanation
Procedural programming is a programming paradigm that focuses on using procedures or functions to structure the program. In procedural programming, the program is divided into smaller, reusable procedures or functions, which are then called in a specific sequence to perform tasks. This approach allows for modular and organized code, making it easier to understand and maintain.
22.
A destructor has the same name as the class but is preceded by a(n) _________ character.
Correct Answer
~
Explanation
A destructor has the same name as the class but is preceded by a tilde (~).
23.
Each byte in memory is assigned a unique __________.
Correct Answer
address
Explanation
In computer memory, each byte is assigned a unique address. This address is used to identify and locate the specific byte in memory. The address allows the computer to access and manipulate data stored in memory by referencing the unique location of each byte.
24.
Constructors cannot have a(n) _________ type.
Correct Answer
return
Explanation
Constructors cannot have a return type because their purpose is to initialize objects and allocate memory for them. When a constructor is called, it automatically creates and returns the object being constructed. Therefore, specifying a return type for a constructor is unnecessary and invalid.
25.
Pointers may be compared using the relational operators.
Explanation
Pointers in programming can be compared using relational operators such as greater than, less than, equal to, etc. This is because pointers hold memory addresses, and these addresses can be compared to determine their relative positions in memory. For example, if one pointer points to a memory location that comes before another pointer, then the first pointer would be considered "less than" the second pointer. Therefore, the statement "Pointers may be compared using the relational operators" is true.
26.
When you add value to a pointer, you are actually adding that number times the size of the data type referenced by the pointer.
Explanation
When you add value to a pointer, you are actually adding that number times the size of the data type referenced by the pointer. This is because pointers in C++ are used to store memory addresses, and when you perform arithmetic operations on pointers, the compiler automatically scales the value by the size of the data type. For example, if you have a pointer to an integer and you add 1 to it, the pointer will actually move forward by the size of an integer (typically 4 bytes on a 32-bit system). Therefore, the statement is true.
27.
_________ are useful for performing initialization or setup routines in a class object.
Correct Answer
constructors
Explanation
Constructors are special methods in a class that are used to initialize the object's state or perform setup routines. They are automatically called when an object of the class is created. Constructors are useful for setting default values for object properties, allocating memory, or performing any other necessary initializations. They ensure that the object is in a valid and usable state before any other methods or operations are performed on it.
28.
If you were writing the external definitions of the Canine class s member functions,
you would save them in a file named _________.
Correct Answer
Canine.cpp
Explanation
The external definitions of the Canine class's member functions would be saved in a file named "Canine.cpp". This file is likely to contain the implementation of the Canine class's member functions, which are defined separately from the class declaration in a header file. By saving the external definitions in a file with the same name as the class, it helps organize the code and makes it easier to locate and maintain the implementation of the class's member functions.
29.
_____________ variables are designed to hold addresses.
Correct Answer
pointer
Explanation
Pointer variables are designed to hold addresses. In programming, a pointer is a variable that stores the memory address of another variable. By using pointers, we can indirectly access and manipulate the data stored in that memory address. Therefore, pointer variables are specifically created to store addresses.
30.
Pointer variables are designed to hold addresses.
Explanation
Pointer variables are designed to hold addresses. This is because pointers are variables that store memory addresses as their values. They are used to point to the memory locations of other variables. By storing addresses, pointer variables allow us to access and manipulate data indirectly, by referring to the memory location where the data is stored. Therefore, the statement is true.
31.
If you were writing the declaration of a class named Canine, what would you name
the file it was stored in? _________
Correct Answer
Canine.h
Explanation
The file for the declaration of the class named Canine would be named "Canine.h". The ".h" extension is commonly used for header files in C++ to indicate that it contains the declarations of classes, functions, and variables. Naming the file "Canine.h" is a standard convention that helps to organize and identify the contents of the file.
32.
A(n) _________ is a member function with the same name as the class.
Correct Answer
constructor
Explanation
A constructor is a member function with the same name as the class. It is used to initialize objects of a class and is automatically called when an object is created. Constructors have no return type and can have parameters. They are used to set initial values for the data members of the class.
33.
The __________ operator can be used to determine a variable's address
Correct Answer
ampersand(&)
ampersand
&
Explanation
The ampersand (&) operator can be used to determine a variable's address. By placing the ampersand symbol before the variable name, the memory address of that variable is obtained. This is useful in situations where the address of a variable needs to be passed as an argument to a function or when working with pointers in programming languages like C or C++.
34.
The __________ operator is used to dynamically allocate memory.
Correct Answer
new
Explanation
The "new" operator is used to dynamically allocate memory. It is used in programming languages like C++ to create objects or variables at runtime, rather than at compile time. The "new" operator allocates memory for the object or variable on the heap, and returns a pointer to the allocated memory. This allows for dynamic memory management and the creation of objects or variables with a size determined at runtime.
35.
_________ programming is centered around objects.
Correct Answer
object oriented
Explanation
Object-oriented programming is a programming paradigm that revolves around the use of objects. In this approach, data and functions are encapsulated within objects, which are instances of classes. The main goal of object-oriented programming is to organize and structure code in a way that promotes modularity, reusability, and flexibility. By focusing on objects, this programming style allows for easier maintenance and modification of code, as well as better collaboration among developers.
36.
When a member function s body is written inside a class declaration, the function is
_________.
Correct Answer
inline
Explanation
When a member function's body is written inside a class declaration, the function is considered as an inline function. This means that the function is defined and implemented within the class itself. Inline functions are typically used for small and simple functions that are called frequently, as they eliminate the overhead of function calls. By defining the function inside the class declaration, the compiler can directly substitute the function code wherever it is called, resulting in faster execution.
37.
Class members are private by default.
Explanation
In object-oriented programming, class members refer to the variables and methods within a class. By default, these members are set to private, which means they can only be accessed within the class itself. This is done to encapsulate the internal workings of the class and prevent direct access from outside. Therefore, the statement "Class members are private by default" is true.
38.
Constructors may not have a return type.
Explanation
Constructors are special methods in a class that are used to initialize objects. They do not have a return type because their main purpose is to create and set the initial state of an object. When an object is created using the constructor, it is automatically returned to the caller. Therefore, there is no need for a return type in constructors.
39.
Destructors cannot take arguments.
Explanation
Destructors in programming languages like C++ are special member functions that are automatically called when an object is destroyed or goes out of scope. They are used to release resources and perform cleanup tasks. Destructors do not take any arguments because they are invoked automatically and do not need any input from the user. Therefore, the statement "Destructors cannot take arguments" is true.
40.
In C++ the _________ is the construct primarily used to create objects.
Correct Answer
class
Explanation
In C++, the "class" is the construct primarily used to create objects. A class is a blueprint or template for creating objects that define their properties and behaviors. It encapsulates data and functions together, allowing objects to be created based on the defined class. By creating objects from a class, we can utilize its properties and methods to perform various operations and achieve desired functionality in our C++ programs.
41.
A classes responsibilities are the things the class is responsible for knowing, and actions the class must perform.
Explanation
This statement is true because in object-oriented programming, a class is responsible for defining the properties (knowledge) and methods (actions) that an object of that class can have. The class encapsulates the data and behavior of the objects it represents, and it is responsible for knowing and performing those actions.
42.
The ____________ operator can be used to work with the variable a pointer points to.
Correct Answer
indirection(*)
indirection
*
Explanation
The indirection operator (*) is used to work with the variable a pointer points to. It is used to access the value stored at the memory location pointed to by the pointer. By dereferencing the pointer with the indirection operator, we can manipulate the value stored at that memory location. So, in this case, the correct answer is indirection(*).
43.
Under older compilers, if the new operator cannot allocate the amount of memory
requested, it returns __________.
Correct Answer
0 or null
0
null
Explanation
The new operator in older compilers returns either 0 or null if it is unable to allocate the requested amount of memory.
44.
A class may only have one destructor.
Explanation
In C++, a class can only have one destructor. The destructor is a special member function that is automatically called when an object of the class goes out of scope or is explicitly deleted. It is responsible for releasing any resources that were allocated by the object during its lifetime. Having multiple destructors in a class would lead to ambiguity and confusion, as the compiler would not know which destructor to call. Therefore, it is a rule in C++ that a class can only have one destructor.
45.
Creating variables while a program is running is called __________.
Correct Answer
Dynamic Memory Allocation
Explanation
Dynamic Memory Allocation refers to the process of allocating memory for variables at runtime, rather than at compile time. This allows the program to create variables as needed during program execution. It is commonly used when the size or number of variables required is not known in advance or may change during program execution. This technique is often used in programming languages like C and C++ to manage memory efficiently and avoid wasting memory resources.
46.
The & symbol is called the indirection operator.
Explanation
The & symbol is not called the indirection operator. The indirection operator in programming languages is typically represented by the * symbol, not the & symbol. The & symbol is commonly used as the address-of operator, which returns the memory address of a variable.
47.
The address operator is not needed to assign the address an array to a pointer.
Explanation
The address operator is not needed to assign the address of an array to a pointer because the name of an array itself represents the address of its first element. Therefore, when assigning the address of an array to a pointer, the address operator (&) is not required.
48.
Constructors cannot take arguments.
Explanation
This statement is false. Constructors can take arguments. In object-oriented programming, a constructor is a special method that is used to initialize objects. It can have parameters that allow the caller to provide initial values for the object's attributes. By passing arguments to the constructor, we can customize the initialization process and create objects with different initial states.
49.
Any mathematical operation, including multiplication and division, may be performed on a pointer.
Explanation
Pointers are used to store memory addresses, not numerical values. Therefore, mathematical operations like multiplication and division cannot be performed directly on pointers. These operations only make sense when performed on numerical values. Hence, the statement is false.