Exam question Flashcards
Name at least 5 variables in C++
Bool, double, float, int, short int, long int, long long int, char
What is int overflow?
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)
Why using namespace std might not be a good idea?
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.
Which header do you have to include for the access to std::cout?
include <iostream></iostream>
What would be the simplest “legal” program in C++?
int main() {}
What is the difference between struct and class?
They are C++ keywords used for creating classes. The difference is that class has default private members, while struct has pubblic members by default.
What does break statement do in a loop?
It exits the loop. (Usually it is used when we want to stop the loop given a certain condition)
What does continue statement do in a loop?
It allows to skip an interation of the loop and jumps to the following one
How do you define a static array of type int and size 7?
int arr[7];
(Assume that we are naming the array “arr”)
What can you use to read interactive user input?
#include <iostream>
std::cin>>x;
What is a reference?
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
What is the difference between passing variables to a function by reference and by value?
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.
Why do we want to pass variables to function by reference rather than by pointer?
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.
If the function accepts pointers (int function(int* a)
), how do you pass a variable defined as int x to it?
function (&x)
Why do we want to use const modifier whenever possible?
To prevent bugs, as an error arises when you try to change a constant variable.
Also it makes the code faster.
If you have a pointer named p, how do you access the value it points to? What is the name of the process?
To access the value pointed by a pointer we need to dereference it using a * before the name of the pointer (i.e., *p)
What value is store in the pointer variable itself?
Pointers store the memory address of the variable they point to
What can you say about a function that has signature void function()?
Void functions do not return any value after they execution. Basically they perform a task without returning any value.
What is an auto keyword?
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.
What is function overloading?
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.
What is a recursive function?
Recursive functions are functions thatcall themselves. They are slower than iterations, but in some cases they may help to solve the problem easily
What is the role of a constructor in a class?
Constructors are special member functions with the same name of the class used (to allocate memory and) initialize class member variables
How can we change private member variables of a class?
We can use class member functions or friend function. Both of them have access to private class members
How do you create an object file with g++?
To create an object write g++ -c file.cpp. This will produce the file.o object file
Why can’t you just have everything in one file?
Writing everything in a single file, will make the main unreadable soon. This implies that bug detection and correction becomes more difficult.
What is the purpose of header guards?
Header guards are used to avoid multiple inclusions of the same header files in a program
What does -IFOLDER_NAME mean when passed to g++?
It specify the folder in which g++ has to look for the header files
Do tabs matter in Makefile?
Yes and they cannot be replaced by spaces
What does $@ mean in a makefile?
It evaluates to the name of the target in the current rule.
What does $^ mean in a makefile?
It is a macro evaluated to all the names of the dependencies, separated by a space, of the current rule.
How to use make with a makefile that is not named Makefile?
We can either use -f MymakefileName or –f=MymakefileName
What does it mean if a function is a friend of a class?
It means that it can have access to non-public member (private and protected) of the class it is friend with