Multiple Choice Practice Flashcards

1
Q

How would you compile an object file? An executable? How do you link object files into an executable?

A

Compiling an object file:
g++ -c p1.cc
Compiling an executable:
g++ -o p1 p1.cc
Linking object files into an executable:
g++ foo foo.o foo2.o

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

How would you code an input parameter? How would you code an output parameter? How would you code a
return value?

A

void foo(int a, int& b, Date** d){
return e;
}
a: Input parameter. Pass-by-value
b, d: Output parameters. Pass-by-reference/pointer
e: Return value

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

What types of constructors are there and what are their function signatures?

A

Default Constructor:
Date(int = 1901, int = 1, int = 1);
Date();
Zero argument constructor/constructor with all default arguments
Copy Constructor:
Date(Date&);
Date(const Date&);
Takes a reference to an object of the same class
Conversion Constructor:
DateTime(Date&);
Takes a reference to an object of a different data type

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

What are namespaces and how are they used?

A

Region that provides a name scope. Used to prevent name collisions.
namespace companies{
class Apple{
}
}
namespace fruits{
class Apple{
}
}
Scope resolution operator:
companies::Apple();
fruits::Apple();
Using keyword:
using namespace companies:
Apple();

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

How do you dynamically allocate an object? How would you statically allocate an object? What is the
difference?

A

Dynamically allocating an object:
int* x = new int;
Date* date = new Date;
Memory is stored on the heap. Must be deleted explicitly:
delete x;
delete Date;
Statically allocating an object:
int x;
Date date;
Memory is reserved where the variable was declared. This memory is automatically deleted when the containing object is delete or the stack frame is popped off.

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

When storing classes, what are the 4 types of arrays and how would they be implemented?

A

Statically allocated array of objects:
Date dates[size];
Statically allocated array of pointers:
Date* dates[size];
Dynamically allocated array of objects:
Date* dates = new Date[size];
Dynamically allocated array of pointers:
Date** dates = new Date*[size];

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

What are the object categories and what are their purposes?

A

Entity objects:
- Model real world objects or information tracked by the program
Control objects:
- Objects in charge of program flow, manages how classes interact
Boundary (View) objects:
- Manage the interaction of the application with foreign entities
Collection objects;
- Storage of multiple entities of the same type

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

What type of information is shown in a UML diagram? What type of information is not shown?

A

Shown in UML diagram:
- Classes:
-> Name
-> Data members
-> Member functions
- Inheritance and relationship type
Not Shown:
- Classes:
-> Getters and setters
-> constructor and destructors
- Collection classes
- Friendship associations

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

What is encapsulation? What is the principle of least privilege? What are their purposes?

A

Encapsulation is the fundamental concept in OOP that implies:
- Grouping of related data and functionality to form a class
- Restricting direct access to some of the class’ components
Principle of least privilege:
- The philosophy of giving software modules the minimal capabilities needed to accomplish their tasks
- Making member functions private unless required by other parts of the program, and providing getters and setters for the attributes only when necessary

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

What is member initializer syntax and where should it be used?

A

Member initializer syntax:
- Must be used to initialize constant data members
-> May be used to initialize non-constant data members
- Allows us to pass parameters to member variable constructors
-> The only way to pass value to statically allocated member variable objects
Test::test(string name, int number): name(name), number(number){}

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

In what ways can you use the const keyword, and what is the purpose of each? What is the effect in each
case?

A

const is used for protection
- const objects
-> The data members of the object cannot change
-> Only constant member functions can be called on a constant object
- const member functions
-> Function will not modify the object in it
-> Can only call other constant member functions
- const data members:
-> Can never be modified
- must be initialized before the body of the constructor using member initialization syntax

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

What is Friendship and what does it do? How would you use it?

A

Friendship gives complete access to class members of that object
- not transitive, not symmetric
-> each friendship must be explicitly granted
- Friend function (of a class)
-> Global function given complete access to the class
- Friend class (of a class)
-> Another class that is given complete access to the class
class Address{
friend class Student;
};

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

How do you declare and initialize a static member variable? How do you declare and call a static function?

A

Static class member:
- One copy of a static member exist
- Can be accessed
-> Using class name and binary scope resolution operator
-> From any object of that class
- Property of the class as a whole
Static data member:
- Must be initialized at file scope
-> In source file by convention
*do NOT include the static keyword when initializing a static variable in the source file!
- Value shared by all instances
Static member function:
- Can only access static members
- Can still take objects of this type as arguments
- Specified as static in the class definition
-> Not in the source file

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