Programming exam Flashcards

1
Q

Declaration/ header (.h)

A

In the header file, a developer provides the declaration of a class’ interface. In essence, this gives the “shape” of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Definition/Implementation (.cpp)

A

In the definition file, a developer provides the code that is run each time a function member is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Use/ Driver (.cpp)

A

In a driver file, an external program references the header file using a #include statement and uses the class members (via objects or instances of that class) to solve a problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Class Declaration

A

A class declaration takes this general form
class <class>{
private:
//a series of private data and function member declarations
public:
//a series of public data and function member declaration</class>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Class Definition

A

The definition of a class provides definitions for functions that are declared in the class declaration. This linking is performed using the scope resolution operator “::” between the class name and the member name. An example definition would look like this:

<return> <class>::<function> (<parameter>) {

//code goes here

}
</parameter></function></class></return>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using a Class

A

In a driver program, one would first create instances of a class by declaring a variable of the appropriate type:

Employee e;

After the variable is declared, the programmer now has access to all of the public data and function members using the class member access operator, also known as the “dot” operator:

e.Inactivate()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Compiling a Class and Driver

A

While one could use a single command approach to compilation, it is better to use a multi-step approach that separates the header file from compiled source code. An example compilation of a class and driver with the three files outlined above would be:

Step 1: g++ -c <class>.cpp
Step 2: g++ -c <driver>.cpp
Step 3: g++ <class>.o <driver>.o -o <desired>.exe
In English:</desired></driver></class></driver></class>

Step 1: compile the class source code into consumable object code
Step 2: compile the driver class into object code
Step 3: link the class object code with the driver object code to create an executable file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Private class members

A

can only be accessed within the definition of a class. That is, any attempt to reference a private data or function member from a function or code location that is not associated with the same class using the scope resolution operator (::) is a syntax error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Public class members

A

can be accessed from anywhere an object of the appropriate type is available. The most common example would be a driver program using public function members from an object of a class declared as a local variable within that driver program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Constructors

A

Constructors are special function members that are used to create objects or instances of a class. These functions contain behavior that is run when a driver program creates a variable with the type of your class.
Initialization: Initialize the object’s attributes when it is created.
Same Name as Class: It has the exact same name as the class.
No Return Type: Constructors do not have a return type, not even void.
Automatically Called: It gets called automatically when an object is created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Accessors

A

are public function members used to access the private value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Mutators

A

are public function members used to change the private value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Getters

A

are accessors that only provide access to a private data member.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Setters

A

are mutators that set a private data member to a value specified as a passed in parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

A(n) ___________ function has access to both public and private data members of a class but is NOT a member function:

A

friend

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Classes encapsulate two types of members: function members that define business logic and ______ members that define the state of a class.

A

data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

_______ is a keyword used to ensure that conversion constructors are NOT used implicitly.

A

explicit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

The _______ operator is used to link a member function to a class.

A

::

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

The _____ design methodology is used when creating a C++ program or class.

A

DDU

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

When creating an operator override, the keyword _______ is used to name the implementation method:

A

operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

The name of a destructor must begin with the special character:

A

~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

A(n) ________ is used to set constant data members of a class:

A

Initialization list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

++ is a(n) _______ operator.

A

Unary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

The keyword “friend” is used in a functions’s:

A

Declaration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Conversion constructors can accept multiple paramters (T/F)

A

False (can only have one parameter, its the type its converting from)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Destructors accept only one parameter (T/F)

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

A class can have only one implementation of an override for a given operator (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

A driver program cannot access private data or function members. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Constant member functions can call other non-constant functions. (T/F)

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

A class can have an arbitrary number of constructors. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

The -c flag is used to create object code from a source cpp file with g++. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Classes are instances of objects. (T/F)

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

A driver program can create an arbitrary number of objects from a class. (T/F)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

A setter should always be marked const. (T/F)

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

A ________ member can be accessed outside of the class implementation but a ______ member cannot.

A

public, private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Getters are a special type of ______ that provide access to _____ state.

A

accessor, private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

A _________ is a special function member that specifies defaulting logic for a class’ data members.

A

Constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Which of the following is TRUE about constructors?

A

They are a function with the same name as the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

The header file is responsible for declaring the ________ for a class

A

interface

40
Q

What are the two steps in the compilation process for C++?

A

Compile step and linking step

41
Q

What is the correct g++ flag to perform only the compile step of compilation?

A

-c

42
Q

Which of the following file extensions is used to declare a programming interface in C++?

A

.h

43
Q

A programming ___________ provides a public menu of behavior and state that can be used by a consuming piece of code

A

interface

44
Q

What is the C++ operator that is used to “connect” a function definition to a class declaration( i.e, the scope resolution operator)?

A

::

45
Q

A _________ class is another name for a main class that consumes a piece of code.

A

driver

46
Q

To create a friend function, the “friend” keyword is used in the definition of the function. (T/F)

A

False

47
Q

True or False: The scope resolution operator is NOT needed when defining a friend function.

A

True

48
Q

True or False: All conversion constructors are parameterized constructors but not all parameterized constructors are conversion constructors.

A

True

49
Q

True or False: Only one class can be declared within a header file.

A

False

50
Q

True or False: Friend functions must always be public.

A

False

51
Q

True or False: Const member functions can only call other const functions.

A

True

52
Q

How many times would the word “const” appear in the declaration of a constant function that returns a constant value and takes in two constant parameters by value?

A

4

53
Q

True or False: In C++, you can create a non-constant object that contains constant data members and constant function members.

A

True

54
Q

What is the naming convention for a destructor in C++?

A

A destructor should always be named with a tilde (~) followed by the class name. For example: ~ClassName().

55
Q

Where should destructors be declared and can they have parameters?

A

Destructors should always be declared in the public section of the class and they do not take any parameters.

56
Q

How many destructors can a class have and do they need to be explicitly called?

A

A class can only have a single destructor. Destructors are called implicitly when an object instance falls out of scope, so they do not need to be explicitly called.

57
Q

When is it necessary to define a custom destructor for a class?

A

It’s necessary to define a custom destructor if the class contains an unmanaged resource like a database connection, a file handle, or contains a pointer to memory allocated within the class definition. Otherwise, the default destructor is often sufficient

58
Q

What is the primary purpose of a destructor?

A

The primary purpose of a destructor is to release resources and perform cleanup when an object is destroyed, which occurs when it goes out of scope, such as when a function returns or a conditional code block containing the declaration of the object ends.

59
Q

True/False: All objects are given a default destructor in C++

A

True. All objects are provided a default destructor in C++, which in many cases is all that is needed, especially when the class doesn’t manage any external resources or allocate dynamic memory.

60
Q

What are some scenarios in which a destructor is called?

A

A destructor is called implicitly when an object falls out of scope, such as when a function returns, a conditional code block containing the declaration of the object ends, or when the program execution leaves the scope in which the object is defined.

61
Q

What are the three main components of operators in C++?

A

Operators in C++ have three main components: a return type, a name, and a list of parameters.

62
Q

Can you change the precedence, associativity, or the number of parameters of an operator in C++?

A

No, you cannot change the precedence, associativity, or the number of parameters of an operator in C++ during operator overloading

63
Q

What is “arity” in the context of operators, and what are its types?

A

“Arity” refers to the number of parameters that an operator accepts. Unary operators accept one parameter, binary operators accept two, and (although rare and not directly supported in C++) tertiary operators would theoretically accept three.

64
Q

How must operators be named when being overloaded in C++?

A

When overloading operators in C++, they must be named using the keyword “operator” followed by the operator symbol being overloaded. For example, overloading the plus operator would be named “operator+”.

65
Q

Can you create new operators or change the behavior of operators on primitive or built-in types in C++?

A

No, you cannot create new operators or change the behavior of operators on primitive or built-in types in C++.

66
Q

Can operator overloads be implemented as both member and friend functions, and is there a general rule on which to use?

A

Yes, operator overloads can be implemented as either member or friend functions. There’s no hard and fast rule, but member function operator overloads are typically used unless the left operand needs to be converted, which requires a friend function.

67
Q

What is encapsulation in the context of classes in C++?

A

Encapsulation is a core coding practice where different data types (like int, string, etc.) are bundled together into a user-defined type (e.g., a class named “Employee”). This allows for easier storage and retrieval of data related to specific entities and also enables associating behavior (functions) with data (data members).

68
Q

What are the data members in a class and what do they represent?

A

Data members in a class, often referred to simply as “members” in C++, are variables declared within a class. They represent the state of an object, holding values that define properties or attributes of objects created from that class.

69
Q

What are function members in a class and how do they relate to encapsulation?

A

Function members, or member functions, in a class encapsulate behavior that is directly related to objects of the class. They perform operations on the object’s data members or interact in some way with the object’s state, ensuring that behavior relevant to the data is kept together with the data itsel

70
Q

If classes are considered blueprints, what are objects in C++?

A

Objects in C++ are instances of classes. If a class provides the blueprint (defining properties and behaviors), an object is an individual instantiation of that class, with its own specific state defined by the values of the data members. For example, if Employee is a class, empl1 could be an object of type Employee.

71
Q

What is the distinction between private and public class members in C++?

A

Private class members can only be accessed within the class’s definition, whereas public class members can be accessed from any location where an object of the class type is available, including from functions in a driver program.

72
Q

In general, how should data members and function members be designated in terms of access modifiers, and why?

A

Data members should mostly be private to prevent unintended states by external code and to maintain encapsulation.
Function members should be public if they offer functionality meant to be accessed externally.
Function members should be private if they are meant for internal use within the class, and not to be used by external or driver code.

73
Q

Can one instance of a class access private members of another instance of the same class in C++?

A

Yes, one instance of a class can access private members of another instance of the same class because they share the same definition. However, this can be considered unsafe practice in many cases, especially when internal data validation standards need to be met through public accessors.

74
Q

Why might it be considered unsafe or unwise to directly access private members of another instance of the same class?

A

Direct access to private members might bypass internal data validation which is often implemented through public accessors, potentially allowing the object to enter an invalid state. Using public accessors, even within the class definition, ensures that all interactions with the data adhere to the validation and manipulation rules defined by the class.

75
Q

What is the role of a constructor in C++?

A

Constructors are special function members in C++ used to create objects or instances of a class, initializing their state, and executing any behavior that needs to occur when an object is created.

76
Q

Are constructors typically public or private, and why?

A

Constructors are typically declared public because they need to be accessible to driver programs or other code outside the class, enabling them to create objects of the class type.

77
Q

Can you have multiple constructors and how are they typically used?

A

Yes, a class can have multiple constructors, including the default constructor and parameterized constructors that accept arguments to initialize the object in specific ways. Parameterized constructors allow objects to be initialized with specific data at the time of creation.

78
Q

What is a conversion constructor and how is it used in C++?

A

A conversion constructor is a constructor that takes a single parameter and allows implicit conversion from the type of its parameter to the class type.Example:
Employee(int code);
allows
Employee e = 10101; // implicit call to the conversion constructor
It converts the int value 10101 into an Employee object, using that value to initialize the object.

79
Q

What is the explicit keyword in the context of constructors and why might it be used?

A

The explicit keyword prevents implicit conversions and requires that the constructor be called explicitly. If explicit is used in the declaration like:

explicit Employee(int code);

then implicit calls like Employee e = 10101; would result in a syntax error at compile time, and only explicit calls like Employee e(10101); would be allowed.

80
Q

How are objects instantiated using constructors?

A

Objects can be instantiated by calling the constructor explicitly, like:

Employee e1, e2, e3;
// Default constructor
Employee e(10101);
// Parameterized constructor

or, if no explicit keyword is used with a single-parameter constructor, via implicit conversion, like Employee e = 10101;.

81
Q

What is the primary purpose of accessors and mutators in a class?

A

Accessors are public function members used to access private data members, while mutators are used to modify them. They are utilized to implement encapsulation and data protection in a class, ensuring that data is accessed and modified in a controlled and safe manner.

82
Q

Define getters and setters, and how do they specialize accessors and mutators?

A

Getters are a specialized type of accessor that solely provide access to a private data member.
Setters are a type of mutator that specifically set a private data member to a provided value.
Thus, all getters are accessors and all setters are mutators, but not vice versa due to the specialized use.

83
Q

What is the typical naming convention for getters and setters?

A

The generally accepted naming convention for getters and setters is Get<PrivateMemberName> and Set<PrivateMemberName>, respectively. However, these names are related to their behavior, not strictly the function name, and alternative naming can be used.</PrivateMemberName></PrivateMemberName>

84
Q

Why use getters and setters instead of directly accessing class members?

A

Getters and setters are utilized to enforce validation on private data members. While a public member can be set to any value, setters enable the implementation of logic that validates or modifies input before altering a private member. Similarly, getters can provide a validated or adjusted value upon read, ensuring controlled access to the internal state of an object and maintaining encapsulation.

85
Q

What is a side effect in the context of object-oriented programming, and why is it significant in the context of getters and setters?

A

A side effect occurs when a function member modifies the state of an object in a manner not anticipated by the caller. It is generally considered undesirable in good class design. Getters and setters should ideally avoid side effects, focusing on singular responsibilities: getters should only retrieve values, and setters should only set values, both potentially with validation or transformation logic.

86
Q

How can setters and getters ensure that the data members of an object maintain valid states?

A

Setters can incorporate validation logic to check and possibly modify input values before they are assigned to private data members, ensuring the object does not enter an invalid state. Getters can validate or adjust the stored data before providing it, ensuring that the value retrieved is valid or meets certain criteria, even if the internal state doesn’t.

87
Q

Are mutators and setters the same?

A

No. While setters are a type of mutator, they are distinct in that setters specifically assign a provided value to a private data member. In contrast, mutators might modify private data members in varied ways, not limited to assigning a specific value. All setters are mutators, but not all mutators are setters.

88
Q

How can getters be implemented to “clean” data upon a read?

A

Getters can implement logic that validates, transforms, or adjusts the private data before it is returned. This ensures that even if the internal state of the object is invalid or non-optimal, the value provided by the getter adheres to defined valid or safe parameters.

89
Q

What is the distinction between private and public members in a class declaration?

A

Private members are only accessible within the class definition, while public members are accessible from outside the class definition, such as in a driver program.

90
Q

How do you correctly call a member function of a class using an object of that class?

A

You need to instantiate an object of the class and then use the object to call the member function. For example:
Fraction(1,2).Add(Fraction(1,2));
Here, Add() is called on a Fraction object and another Fraction object is passed as a parameter.

91
Q

What is a friend function in C++ class definition and how does it access class members?

A

A friend function is not a member of the class but is allowed to access its private and protected members. It is declared within the class using the “friend” keyword but is not considered a class member, so it doesn’t adhere to the public/private access controls.

92
Q

Describe the metaphor of private possessions, public possessions, and friends in relation to class members and friend functions.

A

Private possessions (private members) are only available to you (the class). Public possessions (public members) are accessible to anyone you invite (any function or class). Friends (friend functions) can access your private possessions because of trust and closeness (they can access private and protected members of the class).

93
Q

Why doesn’t the friend function use the scope resolution operator in its definition?

A

Friend functions do not use the scope resolution operator because they are not members of the class. They are defined without being scoped to the class, even though they are declared inside the class with the “friend” keyword

94
Q

In the context of C++ coding, provide an example of defining a member function and a friend function to illustrate the difference in syntax.

A

Member function:
Fraction Fraction::Add(Fraction f) {}
Friend function:
Fraction Add(Fraction f1, Fraction f2) {}
The member function uses the scope resolution operator :: because it is a part of the class, whereas the friend function does not.

95
Q

How is dot syntax utilized in member functions versus friend functions when accessing data members?

A

In a member function, the “current” instance accesses members directly or using this->, while parameters use dot syntax. In friend functions, dot syntax is used to access members of all objects passed as parameters since there is no “current” instance.

96
Q

How do you declare a friend function within a class and what is its access level to the class members?

A

A friend function is declared within a class using the “friend” keyword and is placed outside of the public/private blocks since it is not truly a member and thus is not bound by the private/public access specifiers. It can access both private and protected members of the class.

97
Q

How are parameters accessed within a member function versus a friend function when dealing with class instances?

A

In a member function, the “current” instance doesn’t require a specific object or the . operator to access its members. For the parameter objects, you would use dot syntax (e.g., f.numerator). In a friend function, since it is not bound to an instance of the class, all parameter objects would utilize dot syntax to access their members (e.g., f1.numerator, f2.numerator).