Exam question Flashcards

1
Q

Name at least 5 variables in C++

A

Bool, double, float, int, short int, long int, long long int, char

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

What is int overflow?

A

Int overflow occurs when we try to store into an int variable a value that is larger than the maximum value that an int variable can take. (Usually it’s an arithmetic error resulting from operations between integers)

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

Why using namespace std might not be a good idea?

A

It is bad practice as it might lead to collisions.

It imports all the identifiers belonging to std namespace in the global namesapce. This can cause ambiguity or conflicts between functions having the same name but belonging to different namespaces (or user-defined ones) which may result in “undefined behaviour”. Specifiyng to which namespace an identifier belongs using the scope operator (::) is really advisable for writing clean, unambiguous and robust error-free code.

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

Which header do you have to include for the access to std::cout?

A

include <iostream></iostream>

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

What would be the simplest “legal” program in C++?

A

int main() {}

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

What is the difference between struct and class?

A

They are C++ keywords used for creating classes. The difference is that class has default private members, while struct has pubblic members by default.

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

What does break statement do in a loop?

A

It exits the loop. (Usually it is used when we want to stop the loop given a certain condition)

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

What does continue statement do in a loop?

A

It allows to skip an interation of the loop and jumps to the following one

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

How do you define a static array of type int and size 7?

A

int arr[7];
(Assume that we are naming the array “arr”)

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

What can you use to read interactive user input?

A

#include <iostream>
std::cin>>x;

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

What is a reference?

A

A reference is an alias for a variable. It means that when a reference is initialized to a variable, we can either refer to the reference or to the original variable interchangably as they will have the same memory location

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

What is the difference between passing variables to a function by reference and by value?

A

When passing a variable to a function by value, a copy of that variable (with a different memory location) is made inside of the function and so changes will be apported to the copy and not to the original variable. Instead, in passing by reference, the original variable is passed to the function (no copy is made) and so changes will be reflected on the variable itself.

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

Why do we want to pass variables to function by reference rather than by pointer?

A

To avoid confusion with arrays (which are already pointers). In C++ it’s better to use reference to pass single variables and pointers for arrays.

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

If the function accepts pointers (int function(int* a)), how do you pass a variable defined as int x to it?

A

function (&x)

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

Why do we want to use const modifier whenever possible?

A

To prevent bugs, as an error arises when you try to change a constant variable.
Also it makes the code faster.

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

If you have a pointer named p, how do you access the value it points to? What is the name of the process?

A

To access the value pointed by a pointer we need to dereference it using a * before the name of the pointer (i.e., *p)

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

What value is store in the pointer variable itself?

A

Pointers store the memory address of the variable they point to

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

What can you say about a function that has signature void function()?

A

Void functions do not return any value after they execution. Basically they perform a task without returning any value.

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

What is an auto keyword?

A

The auto keyword is used when we do not know the returning type of a function or it is too long. By using the auto keyword we let the compiler chose the appropriate type for us.

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

What is function overloading?

A

Function overloading is a feature of OOP which allows two (or more) functions to have the same name if they have a different number of parameters or same number but different type of parameters.

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

What is a recursive function?

A

Recursive functions are functions thatcall themselves. They are slower than iterations, but in some cases they may help to solve the problem easily

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

What is the role of a constructor in a class?

A

Constructors are special member functions with the same name of the class used (to allocate memory and) initialize class member variables

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

How can we change private member variables of a class?

A

We can use class member functions or friend function. Both of them have access to private class members

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

How do you create an object file with g++?

A

To create an object write g++ -c file.cpp. This will produce the file.o object file

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

Why can’t you just have everything in one file?

A

Writing everything in a single file, will make the main unreadable soon. This implies that bug detection and correction becomes more difficult.

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

What is the purpose of header guards?

A

Header guards are used to avoid multiple inclusions of the same header files in a program

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

What does -IFOLDER_NAME mean when passed to g++?

A

It specify the folder in which g++ has to look for the header files

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

Do tabs matter in Makefile?

A

Yes and they cannot be replaced by spaces

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

What does $@ mean in a makefile?

A

It evaluates to the name of the target in the current rule.

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

What does $^ mean in a makefile?

A

It is a macro evaluated to all the names of the dependencies, separated by a space, of the current rule.

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

How to use make with a makefile that is not named Makefile?

A

We can either use -f MymakefileName or –f=MymakefileName

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

What does it mean if a function is a friend of a class?

A

It means that it can have access to non-public member (private and protected) of the class it is friend with

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

Why should non-class memeber operators be friends of classes?

A

So that they can access non-public members of the class (private and protected)

34
Q

Which header do I need to include to work with files?

A

include <fstream></fstream>

35
Q

How do you open a file in append mode?

A

filevar.open(“test.txt”, std::ios_base::app);

36
Q

What’s the main advantage of using template?

A

Templates allow to have the same function/class working for different data types without
the need to rewrite it for each type, but just by passing the wanted type as parameter of
the template

37
Q

Why do people usually put definition of templated functions in .hpp file?

A

Because templates get expanded to the given type(s) at compilation time (i.e., they are
instantiated at compilation time) and so the compiler must know the definition as well

Because templates need to be instantiated every time you use them, so it is faster to define them just one time in the header with their function/class.

38
Q

Except on type, what else can we template on?

A

We can template class/function on ordinary values instead of data types, but this holds
only for int, short int, long int and long long int variables

39
Q

What is template specialization?

A

It’s a feature of templates for which we can have different functionality based on type

40
Q

What is a variadic template?

A

Variadic templates allow to have a variable number of parameters. To tell the compiler
that the number of parameters can change we must specify the ellipsis (…) in the
argument

41
Q

Why using push_back for std::vector is a bad idea?

A

The push_back command appends data at the end of a vector. When the allocated memory is not enough, it will copy everything to a new location and it will take too much time in case of large amounts of data,
furthermore the memory allocated in the new location might still not be enough

42
Q

How do you pass data from std::vector to a C-style function that need a pointer?

A

You can use &myvec[0] or myvec.data()

43
Q

Why shouldn’t you use a “vector of vectors”?

A

Because vectors will not be allocated in contiguous memory regions (as we would like to
happen for matrices for example)

44
Q

How do you allocate a dynamic array in C++?

A

int* p =new int[N]; It will be dynamically allocated in the heap memory

45
Q

What’s the difference between delete and delete[]?

A

The are both used to free memory on a heap, but delete is for object only and
delete[] for arrays

46
Q

When do you need to overload assignment operator for your class?

A

Whenever dynamic memory is allocated in the class and a destructor is needed, you also need to overload the assignment operator. With the default assignment operator a shallow copy is created, which will result in an error as the destructor will be called twice on the same memory region. Assignment operator is used to make a deep copy of an object of the same class.

47
Q

When do you need to create a copy constructor for your class?

A

Whenever dynamic memory is allocated in the class and a destructor is needed, you also need to create the copy constructor. Copy constructor is used to initialize an object as a deep copy of an other object of the same class.

48
Q

When do you need to create a move constructor for your class?

A

When we want to move objects instead of copying them. In case our class has pointers, we should write our own move constructor to avoid possible problems

49
Q

What should you do if your class allocates resources, but you are sure you will
never need a copy constructor?

A

We can make it delete so that we will get a clear error message if we try to use it
Class<T>::Class ( const Class<T>& p ) = delete

50
Q

What are protected class members?

A

Protected class member are class variables that can be accessed by friends of the
class and derived classes (i.e., classes that inherit from that class)

51
Q

What are virtual functions?

A

A virtual function is a member function which is defined within a base class with the keyword virtual and (cab be) re-defined (overridden) in a derived class.

52
Q

What is an abstract class?

A

An abstract class is a class which contains (at least) a pure virtual function (declared
with =0) and from which an object cannot be created.

53
Q

Explain dynamic polymorphism.

A

Dynamic polymorphism in C++, also known as runtime polymorphism, is a mechanism in which a call to a virtual function is dynamically bound to the correct implementation at runtime based on the type of the object. When an object of a derived class is referred to as a pointer (or reference) of the base class type, the correct implementation of the
virtual function is determined at runtime based on the actual type of the object, not the type of the reference.

54
Q

Why destructor should be made virtual?

A

Destructors in base classes should be made virtual to ensure that the correct derived class’s destructor is called when a derived object is deleted through a pointer to the base class.

55
Q

What is {}()?

A

This a lambda function. Lambda function are anonymous functions (without a name)
that can be assigned to a variable or passed as an argument to another function

56
Q

What’s the general structure of a lambda function?

A

[captured parameters](formal parameters)->return_type{body of the function}(actual parameters)

actual parameters are the parameters values we want to use to evaluate the function.

57
Q

What does “mutable” keyword do in a lambda function?

A

The mutable keyword in a lambda function allows the lambda to modify inside its body the variables that are captured by value.
By default variables are immutable.

58
Q

How do you create markdown cell in Jupyter?

A

To create a markdown cell, press esc, then m

59
Q

How do you delete a cell in Jupyter?

A

To delete a cell, press esc, then dd

60
Q

How do you add a cell above the current one in Jupyter?

A

To add a cell above the current one, press esc, then a

61
Q

How do you add a cell below the current one in Jupyter?

A

To add a cell below the current one, press esc, then b

62
Q

What advantage does conda environment provide over a system-wide installation
of python?

A

Allows to have different environment with different version of the same library. We can also have different version of python.
We do this to avoid conflicts.

63
Q

What does it mean that variables are dynamically typed in python?

A

It means that the type of the variable does not need to be specified as it is checked
at running time and it is allowed to change over the lifetime of the variable. (C++ is
statically typed, variable type must be specified and it is checked at compilation
time, so without running the code)

64
Q

What’s the difference between python and C/C++ integers?

A

In python, integers are implemented as classes.
Unlike C/C++, Python has no limits on the size of the integers.

65
Q

Explain the difference between lists, sets and tuples in python.

A

Lists are mutable (can change), resizable, can store objects of different types
Set are mutable collections of unordered
unique items (no indexing is available for sets and no duplicates occur).
Tuples are immutable, you cannot change them once created (but they are faster than lists because of that)

66
Q

What does negative index mean when accessing list elements?

A

Negative index means that we are accessing the elements of a list from the end.

67
Q

How do you print the first N elements of a list?

A

The following command print the first N elements of a list called l: print(l[:N])

68
Q

How do you print every Nth element of a list?

A

We print the Nth elements of a list l using the following command: print(l[::N])

69
Q

How do you print the reverse list?

A

To print the reverse of a list l, we can do print(l[::-1])

70
Q

Give an example of a list comprehension.

A

[x**2 for x in range(10)]

71
Q

Give an example of cell and line magic commands in Jupyter.

A

%%timeit is the magic to time a cell
%timeit is the corresponding magic to time a line

72
Q

Write a “hello world” function in python

A

def hello():
print(“Hello world!”) #Note that indentation matters

73
Q

What are keyword function arguments in python and what is their advantage?

A

Keyword is an argument passed to a function which is preceded by a keyword and an equal sign: function (keyword=argument).
The advantage is that we don’t need to remember the order of the arguments and we can set a default value.

74
Q

Why shouldn’t you do from numpy import *?

A

We should’t do from numpy import * because it is very easy to have a clash of names. It is better to import the library that we need or import numpy as np to avoid the conflict of names and improve code readability.

75
Q

What is the advantage of using numpy arrays over lists? What are the dangers?

A

np arrays are faster and occupy less memory than python lists, as they are written in C. For this reason they have to be homogeneous and memory overflow is possible.

76
Q

What’s the most widely used package for plotting in python?

A

Matplotlib

77
Q

What’s the purpose of __str__ method in a python class?

A

Specifies how to convert [type cast] the class into a string.

78
Q

What package can you use in python for symbolic calculations?

A

SymPy

79
Q

What are decorators? Write an example

A

Decorators are functions used to modify other functions: they allow to add new functionality to an already defined function without changing its structure.

lo pseudocodice non lo rendera

def change_sign(func):
def wrapper(args, **kwargs):
return -func(
args, **kwargs)
return wrapper

@change_sign
def my_f(x):
return 7*x

80
Q

Name two ways of calling C++ code from Python.

A

CTypes
PyBind11

81
Q

What is pandas? Name at least 5 functions from that package.

A

Pandas is a Python package specifically tailored for data analysis and machine learning purposes.
Series(list) : creates a one dimensional array, i.e. a column vector
DataFrame(dataset) : creates two dimensional arrays, i.e. tables
read_csv(“filename.csv”) : import a csv file in dataframe format
dropna(df) : delete observations with NA values from a pandas df
mean(df) : returns column averages of a pandas df
concat()
cut()