C++ Flashcards

1
Q

True or False?
A constructor has to return a value

A

False
A constructor has no return type
(not even void)

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

True or False?
A constructor must have the same name as the class

A

True

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

True or False?
A constructor can not take arguments

A

False

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

True or False?
A constructor can be overloaded

A

True
A class can have multiple constructors

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

Explain “constructor”
in 3 short statements

A

Constructor:
- Same name as the class
- No return type
- Initializes the member variables

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

True or False?
Abstract classes can only have pure virtual methods

A

False
Abstract classes must have at least one pure virtual method.
But it can have a mix of pure virtual, virtual and non-virtual methods

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

True or False?
You can not create objects from an abstract class

A

True
An abstract class can not be initilialized directly

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

What makes a class abstract?

A

It has at least one pure virtual method

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

Explain encapsulation

A

Encapsulation promotes modularity by allowing the internal details of a scope to be modified without affecting other parts of the program
Ex. Changes to the internal variables are isolated within the scope, and won’t be changed outside and variable created within a scope only exists within that scope.

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

True or false?
A class’ static methods can access private member variables of the class

A

False
Static methods of a class do not have access to the private member variables of the class directly

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

Explain private member

A

Private members are only accessible within the scope of the class itself

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

Explain protected member

A

A protected member is accessible within the class that declares it and by classes derived from it
However, it is not accessible from outside the class hierarchy

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

Explain public member

A

The public access specifier allows members of the class to be accessed from outside the class definition

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

Explain Pure virtual Methods

A

A pure virtual method is a virtual function that has no implementation in the base class and is meant to be overridden by derived classes

It is declared with the = 0 syntax at the end of its declaration.

Ex.
virtual void pureVirtualMethod() = 0;

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

Explain virtual Methods

A

A virtual method is a member function of a class that can be overridden in a derived class

When a method is declared as virtual in a base class, it allows a derived class to provide its own implementation of that method

Ex.
virtual void virtualMethod()
{
std::cout«“Hello”«std::endl;
}

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

Explain static methods

A

Static method:
- A member function of a class that is associated with the class itself rather than an instance of the class
- A static method can be called on the class itself, rather than on an object of the class
- Static methods are declared using the static keyword

Ex.
static void staticMethod()
{
std::cout «“This is a static method.”«std::endl;
}

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

Explain overriding

A

Overriding:
- Allows a derived class to provide a specific implementation for a method that is already defined in its base class
- The overridden method in the derived class must have the same signature (name, return type, and parameters) as the method in the base class

Ex.
void virtualMethod() const override
{
std::cout«“DerivedClass::virtualMethod()”«std::endl;
}

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

Explain function overloading

A

Function overloading allows you to define multiple functions with the same name but different parameter lists.

(The compiler determines which function to call based on the number, types, and order of the arguments passed during the function call.)

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

what concept does the following code demonstrate?

class A
{
void print (int i)
{
std :: cout &laquo_space;“print: “ &laquo_space;i endl;
}
};

class B : class A
{
void print (float f)
{
std :: cout &laquo_space;“print again: “ &laquo_space;f endl;
}
};

a) Overriding
b) Encapsulation
c) Overloading
d) Polymorphism

A

c) Overloading

You can still call print on an object of class B with an int as parameter, in that case the method in the base class will be called.

Ex.
B objB;
objB.print(42); // Calls A::print(int)
objB.print(3.14f); // Calls B::print(float)

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

Explain polymorphism
- Compile-time (Static) Polymorphism
- Runtime (Dynamic) Polymorphism

A

Polymorphism allows objects of different types to be treated as objects of a common base type

Compile-time (Static) Polymorphism:
- Also known as function overloading and operator overloading
- Occurs at compile time
- Multiple functions or operators with the same name but different parameter types or a different number of parameters
- The compiler determines which function or operator to call based on the arguments provided

Runtime (Dynamic) Polymorphism:
- Achieved through virtual functions and inheritance
- Occurs at runtime
- Allows a base class pointer or reference to refer to objects of derived classes

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

Explain composition

A

Composition represents a “has-a” relationship, indicating that an object is composed of other objects

Ex. A car has an engine and 4 wheels

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

Syntax for calling the superclass A method x() from subclass B

A

A::x();

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

Explain Struct

A

A struct is like a class, but the default access control is set to public

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

Fields and methods of a struct is by default:
a) Private
b) Public
c) Protected
d) Secured

A

b) Public

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

Fields and methods of a class is by default:
a) Private
b) Public
c) Protected
d) Secured

A

a) Private

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

int x = 5;
Create a reference variable to x.

A

int &ref = x;

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

What is the memory area called where dynamic allocations are stored?

A

Heap

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

What is the memory area called where static allocations are stored?

A

Stack

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

Syntax to dynamically allocate an objact of class A?
Syntax to deallocate that memory?

A

A *a = new A;
delete a;

30
Q

Syntax to dynamically allocate an array of objects of class A?
Syntax to deallocate that memory?

A

A *arrayA = new A[5];
delete[] arrayA;

31
Q

True or False?
In case of inheritence, when an object of an derived class is created then the base class constructor will be imvoked first followed by the derived class constructor.

A

True

32
Q

True or False?
You can only insert elements at the beginning of linked lists.

A

False

In a linked list, you can insert elements at various positions, not just at the beginning. The ability to insert elements at different positions is one of the advantages of using linked lists.

33
Q

Explain default constructor

A

A default constructor is a constructor that is automatically generated by the compiler if no constructor is provided in a class

34
Q

Explain Member Initializer List

A

The member initializer list is used in the constructor to initialize class members before the constructor body executes.

Ex.
class MyClass {
private:
int x;
public:
MyClass(int value) : x(value) {
// Member initializer list
}
};

35
Q

Explain function pointer

A

A function pointer is a variable that stores the address of a function.

ex.
void Foo() {
//implementation
}
void (*funcPtr)() = &Foo;

General:
functionReturnType (*pointerName)(functionParameters) = &functionName

36
Q

Explain pointer

A

A pointer is a variable that holds the memory address of another variable.

ex
int x = 5;
int *xptr = &x;

37
Q

Explain Linked List

A

A linked list is a data structure consisting of nodes where each node contains data and a reference (link) to the next node in the sequence.

(It provides dynamic memory allocation and efficient insertions/deletions.)

38
Q

Explain Reference Variable

A

A reference variable is an alias for an existing variable.

It provides an alternative name for a variable and is often used in function parameters to pass values by reference.

ex.
int x = 5;
int &ref = x;

39
Q

Why is polymorphism needed in object oriented programming?

A

Polymorphism in object-oriented programming allows for code flexibility by enabling objects of different classes to be treated as objects of a common base class.
It supports more adaptable code, making it easier to manage and extend systems.

40
Q

What does dynamic binding have to do with polymophism?

A

Dynamic binding in polymorphism refers to the ability of the program to determine at runtime which method or function to invoke based on the actual type of the object.

when a base class declreas a method as virtual and a sub class overrides it, the actual method to be called is determined at runtime based on the type of object being referred to (polymorphism)

41
Q

What are pass-by-value and pass-by-reference?

A

Pass-by-value involves passing the actual value of a variable to a function. Pass-by-reference involves passing the memory address (reference) of a variable to a function,
allowing the function to directly modify the original variable.

ex pass-by-value:
int Foo (int value){
//implementation
}
int x = 3;
Foo(x);

ex pass-by-reference:
int Foo (int &value){
//implementation
}
int x = 3;
Foo(x);

42
Q

How are pass-by-value and pass-by-reference used?

A

In pass-by-value, a copy of the variable is passed to a function. Changes within the function don’t affect the original variable.
In pass-by-reference, the memory address of the variable is passed, allowing the function to modify the original variable directly.

43
Q

What are dynamic allocations in C++?

A

Dynamic allocations in C++ involve allocating memory at runtime using operators like new and delete. It allows flexible memory management and is commonly used for creating objects on the heap.

44
Q

How do you dynamically allocate and deallocate memory?

A

Dynamically allocate memory in C++ using new and deallocate it using delete.

Example
int *array = new int[5];
delete[] array;

45
Q

What is a memory leak?

A

A memory leak occurs in programming when a program allocates memory but fails to deallocate or release it, leading to a gradual increase in the program’s memory usage and potential performance issues.

Typically this happens when memory is dynamically alloceted and not deallocated.

46
Q

Can you leak memory without dynamic allocations?
If so, an give example

A

Yes, for example when a program opens a file and forgets to close it.

47
Q

What is the purpose of encapsulation in object oriented programming?

A

The purpose of encapsulation in object-oriented programming is to bundle data and methods that operate on the data into a single unit, known as a class.
Encapsulation promotes information hiding, code organization, and maintenance.

48
Q

Give a code-example of encapsulation
(Do this on paper, there will be an example given to compare with when you klick on this card)

A

class Person {
private:
std::string name;
int age;

public:
// Public methods to access and modify private members
void setName(string newName) {
name = newName;
}

std::string getName() const {
    return name;
}

void setAge(int newAge) {
    if (newAge >= 0) {
        age = newAge;
    } else {
        std::cout << "Invalid age!" << std::endl;
    }
}

int getAge() const {
    return age;
} };
49
Q

True or False?
Non-static methods can use static fields

A

True

Explanation:

Non-static methods can access static fields in a class. Static fields are associated with the class itself rather than with instances of the class. Therefore, both static and non-static methods can access static fields.

50
Q

Which of the following can be used to call a superclass A method x() from a subclass B?
a) A.x()
b) A->x()
c) A::x()
d) this->x()

A

c) A::x()

51
Q

Choose the correct pure virtual method definition from the following.
a) virtual void f() : 0;
b) void virtual f()=0 { }
c) virtual void f() {} = 0;
d) None of the above

A

d) None of the above

It should be:
virtual void f() = 0;

52
Q

What is/are true about abstract classes?
a) They can only have pure virtual methods
b) You cannot create objects from them
c) They can be used in polymorphism
d) A & B
e) B & C
f) A, B & C

A

e) B & C

You cannot create objects from them
They can be used in polymorphism

53
Q

Which of the following statements is/are true about constructor:
a) It must have same name than the class
b) It has no return value
c) There can be only one constructor in a class
d) A and B
e) A, B and C

A

d) A and B

It must have same name than the class
It has no return value

54
Q

Which of these pairs represents composition?
a) Car – Engine
b) Car – Motorcycle
c) Car – Vehicle
d) Car – Road

A

a) Car – Engine

55
Q

Fields and methods of a class are by default:
a) private
b) public
c) protected
d) secured

A

a) private

56
Q

Which of the following shows a correct way to declare a reference variable?
a) int &x = 5;
int ref = x;

b) int x = 5;
int ref = &x;

c) int x = 5;
int &ref = x;

d) int *x = 5;
int &ref = &x;

A

c) int x = 5;
int &ref = x;

57
Q

Which of the following is the correct way to dynamically allocate memory for an int variable?
a) int *i = new int();
b) int i;
c) int &i = new int;
d) int *i = new int;

A

a) int *i = new int();
eller
d) int *i = new int;

58
Q

What is the memory area called where function calls are stored?
a) Dump
b) Heap
c) Stack
d) Pile
e) Queue

A

c) Stack

59
Q

In case of inheritance, when an object of a derived class is created then___________ .
a) only the derived class’ constructor will be invoked
b) only the base class’ constructor will be invoked
c) the derived class’ constructor will be invoked first followed by the base class’ constructor
d) the base class’ constructor will be invoked first followed by the derived class’ constructor

A

d) the base class’ constructor will be invoked first followed by the derived class’ constructor

60
Q

If the base class has a constructor with arguments, then it is ________________ for the derived class to
have a constructor and pass the arguments to the base class constructor.
a) Mandatory
b) Optional
c) Compiler dependent
d) Error

A

a) Mandatory

Example:
class Base {
public:
Base(int v){}
};

class Derived : public Base {
public:
Derived(int derivedV, int baseV) : Base(baseV){}
};

61
Q

Which form of delete is used to deallocate a dynamically allocated array?
a) delete array
b) delete *array
c) delete array[]
d) delete[] array

A

d) delete[] array

62
Q

class Derived : public Base {
// Other code here
};

what does it mean when using public above?

A

Public Members of class Base are still accessible as public in Derived class

63
Q

class Derived : protected Base {
// Other code here
};

what does it mean when using protected above?

A

Public Members of class Base are now accessible as protected in Derived class

64
Q

class Derived : private Base {
// Other code here
};

what does it mean when using private above?

A

Public and protected Members of class Base are now private in Derived class

65
Q

Explain Default constructor

A

When no constructor is created within a class it’s given a default constructor which looks like this:

class Foo {
Foo(){}
};

no parameter and no initialization

66
Q

Explain Initializer list

A

Initialziser listcan refer to a member initializer list (the one for a constructor) or any other initializer list (like the one for an array or vector).
It is essentially a list where the values are initialized.

example iwth member initializer list:
It is a part of the constructor, and a way to initialize values of member variables when a new instance of the class is created.

class MyClass {
private:
int x;
int y;

public:
// Constructor with initializer list
MyClass(int a, int b) : x(a), y(b)
//this will set vaules to x and y as the instance is created
{
// Other constructor code
}
};

67
Q

What are function pointers in C++?

A

Function pointers in C++ are variables that store addresses of functions. They enable dynamic selection and invocation of functions at runtime. Function pointers can be useful for implementing callbacks, creating function tables, and achieving greater flexibility in program design.

68
Q

How can memory leaks be prevented (give a code example)?

A

By always deleting dynamically allocated memory, suggestingly in a deconstructor so it won’t be forgotten.
And by always closing an opened file.

Example:

class Person {
private:
std::string* name;
int* age;

public:
Person(std::string newName, int newAge) :
name(new std::string(newName)),
age(new int(newAge)) {}
~Person() {
delete name;
delete age;
}
};

69
Q

Explain pointer

A

A pointer in programming is a variable that holds the memory address of another variable
Ex.
int x = 10;
int *ptr = &x;

70
Q

Explain reference variable

A

A reference variable in programming is an alias or alternative name for an existing variable.
It allows you to refer to the same memory location as another variable, providing a convenient way to access or modify the data without duplicating it.

Ex.
int x = 10;
int &ref = x;