General Questions
Vector
Pointers
reference/ Time recording
100

What's the difference between an Array and a Vector? 

Vectors are Dynamic, while Arrays are Static meaning Vectors can be changed as needed. 

100

How can we declare a Vector of type int? 

vector <int> variable;

100

How can I declare a pointer called ptr

int* ptr;
100

Is this valid : 

int fish=5;

int& ref = fish;

Yes it is. 

200

Are Arrays passed by a value or by a pointer? 

Arrays are passed by a pointer. 

Ex:  

arr[]={1,2,3,4,5};

int* ptr = arr;

200

How can we declare a 2D vector of type string

vector <vector<string>> variable;

200

Is there anything missing from the following code? 

If not what is the output

int x = 5;

int*ptr = x;

*ptr = 7;

cout << ptr <<" and " << x;

The code is missing 

& sign on the line int*ptr = (&)x

After changing this the output should be 

7 and 7

200

What's the output of the following code: 

int fish=5;

int& ref = fish;

cout << ref << " and " << &ref;



5 and memory address of fish

300

True or false? 


Arrays have a faster access time versus Vectors (o(n) constant time)

True 

300

Given:  vector<vector<int>> x;

x={{1,2,3},{4,5,6}}

what's a loop which will output both the rows and columns of this 2D vector?

for(int i =0; i < x.size();i++){

for(int col =0; col < x[i].size();col++){

cout << x[i][col];

}

}

300

What do pointers point to? 

what will the following output:

int fish=5;

int*ptr = &fish;

cout << ptr;


The memory address of fish

300
True or false:


Changing a reference variable changes the original variable's value? 

True, both passing by pointer and reference will change the value if that variable is changed. 

400

True or false:

datatype  variable(parameters){

return datatype 

}

Is this a correct way of creating a function? 


True 

400

What will the following function erase? 

vector <int > x = {1,2,3,4,5};

x.erase(x.begin() +3)

It will erase the 3rd value in the vector, 

So, this function will erase the value (3).


400

What's the output of the following code:

int arr[] = { 1,2,3,4,5 };

    int* ptr = arr;

    for (int i = 0; i < 5; i++) {

        cout << ptr << " value:" << *ptr << endl;

        ptr++;

    }

It will output the 

Memory address, followed by the value at each index

400

What is the header 

#include <time.h> 

used for? 

For counting the elapsed time during our program

500

What will happen to the variable x:

int x=6;  

int y = 10;

stuff(x);

void stuff(int &y){

y = 20;

}

The value of x will be changed to 20. 

500

Given:  vector <int> x;

How can I add 3 to this vector, as well as remove the last element in the vector?

ADD:   x.push_back(3);

Remove:  x.pop_back();

500

Will the following code compile? 

int arr[]={1,2,3,4,5};

int* ptr = &arr;

cout << ptr << endl;

No, the following code will not compile because an array is passed by pointers and not reference, In order for the code to compile we would need to remove the &

500

Which part of the code would we enter our testing code for testing?

clock_t begin, end;

    double elapsed_seconds;

    begin = clock();

    for (unsigned int i = 0; i < 100000000; i++)

    {

    }

    end = clock();

    elapsed_seconds = (double(end) - double(begin)) CLOCKS_PER_SEC;

    cout << elapsed_seconds << endl;


 

Inside the for loop