Past Exam Question Preparation Flashcards

1
Q

What is overloading in C++?

A

Overloading in C++ is the ability to create multiple functions with the same name but different parameters. This allows functions to behave differently based on the input arguments.

void print(int i);
void print(double d);

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

Example of overloading

A

void print(double d);
void print(const std::string& s);

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

Provide an example illustrating the use of namespaces.

A

namespace First {
int value() { return 5; }
}

namespace Second {
int value() { return 10; }
}

int val = First::value(); // val is 5

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

What is scope in C++ and what is the relevance of namespaces to scope?

A

Scope determines the visibility and lifetime of a variable or function. Namespaces are used to define a scope for identifiers to avoid name collisions, allowing the same name to be used in different contexts.

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

What does the stream manipulator endl do?

A

endl is used to insert a newline character into the output stream and flush the stream.

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

How are reference variables and pointers declared in C++? Give an example of each.

A

A reference variable is declared with an & symbol, a pointer with a *. Examples:

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

Give an example using endl.

A

std::cout &laquo_space;“Hello” &laquo_space;std::endl; // Outputs “Hello” followed by a newline, and flushes the output stream.

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

What role do assertions play in defensive programming?

A

Assertions are used to ensure that certain conditions hold true in a program. If an assertion fails, the program is terminated. This helps catch bugs by validating assumptions during development.

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

Provide an example of using an assertion.

A

include <cassert></cassert>

int divide(int num, int denom) {
assert(denom != 0); // Program terminates if denom is 0
return num / denom;
}

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

If a collection of type X contains X and Y objects, how are X and Y related?

A

X and Y are related by inheritance. Y is likely a subclass of X, meaning Y inherits from X. This allows a collection of type X to store objects of type Y.

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

How do you declare an instance of a class Example with a member function void function(); and use it?

A

First, declare the instance of Example, then call the function() on it.
Example myInstance;
myInstance.function();

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

How are the roles of constructors and destructors related to the lifetime of an object?

A

Constructors initialize an object when it is created, and destructors clean up before the object is destroyed. They manage resource allocation and deallocation, ensuring proper object lifecycle management.

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

Provide an example to illustrate inheritance used in a collection.

A

class X {};
class Y : public X {};

std::vector<X*> collection;
collection.push_back(new X()); // X object
collection.push_back(new Y()); // Y object, which is a type of X

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

Explain how we manage command line arguments to a program we have written.

A

Command line arguments are managed in the main function, which can accept two parameters: int argc (argument count) and char* argv[] (argument vector), representing the number of arguments and the arguments themselves.

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

What does it mean for a class to be abstract, and how do we make a class abstract?

A

A class is abstract if it has at least one pure virtual function, which means it cannot be instantiated. We make a class abstract by declaring a pure virtual function using the syntax virtual ReturnType FunctionName() = 0;.

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

Why might using getline(cin, input, ‘:’); to process data line by line be problematic?

A

Using getline with a delimiter like ‘:’ can be problematic if the delimiter appears within the data itself, leading to incorrect partitioning of the input.

12
Q

What is the role of a makefile?

A

A makefile automates the build process for software. It specifies how to compile and link the program, often defining build targets and dependencies.

12
Q

Describe two limitations of the C library rand function.

A
  1. The rand() function often has a limited range and may not produce truly random numbers (predictable sequence).
  2. Without srand(), rand() will produce the same sequence of numbers on each run.
12
Q

What is the purpose of using the protected access specifier?

A

The protected access specifier allows a class member to be accessible within its own class, friend classes, and subclasses, but not by other means.

13
Q

What is a header guard and why is it used?

A

A header guard prevents multiple inclusions of the same header file, which can lead to errors like redefinition. It’s usually implemented with #ifndef, #define, and #endif preprocessor directives.

14
Q

Provide an example code illustrating the use of a header guard.

A

ifndef MY_HEADER_H

#define MY_HEADER_H
// Header file contents
#endif // MY_HEADER_H

14
Q

How are namespaces related to scope?

A

Namespaces define a scope that allows for the organization of code and prevention of name conflicts

15
Q

What does the stream manipulator endl do?

A

endl inserts a newline character into the output stream and flushes the stream.

16
Q

How are reference variables declared in C++?

A

Reference variables are declared using the & symbol and must be initialized when declared.

17
Q

How are pointers declared in C++?

A

Pointers are declared using the * symbol and can be initialized to point to a specific memory address.

18
Q

What role do assertions play in defensive programming?

A

Assertions are used to validate assumptions made by the program and to catch bugs during development.

19
Q

How are X and Y related in a collection of type X?

A

In a collection type X that contains X and Y objects, Y must be a type that is derived from (inherits from) X or can be converted to type X.

20
Q

Explain polymorphism in a collection of type X containing Y objects.

A

Polymorphism allows objects of Y, a derived class of X, to be treated as objects of type X.

21
Q

How to create a reference

A

int a = 5;
int &b = a;

22
Q

What is a way to remember what a reference is

A

Think of b as an “alias” for a now.

23
Q

Explain with an example how function overloading works in C++. Create a function named print that is overloaded to handle both int and float types.

A

void print(int i) {
cout &laquo_space;“Integer: “ &laquo_space;i &laquo_space;endl;
}
void print(float f) {
cout &laquo_space;“Float: “ &laquo_space;f &laquo_space;endl;
}

24
Q

What is the byte size of Test test?

class Test {

static int i;
int j, t;

};

int Test::i;

int main(){

Test test;
cout << "Compute the size" << endl;
cout << sizeof(test) << endl;
return 0; }
A

It is 4 as static members dont count

25
Q

How to reverse a STRING

A

string reverseString(const string &str) {
string reversed = str;
int n = str.length();
for (int i = 0; i < n / 2; i++) {
swap(reversed[i], reversed[n - i - 1]);
}
return reversed;
}

26
Q

Function to calculate factorial (using recursion)

A

int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// DONT FORGET BASE CASE