1/49
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the correct way to access the first element of an array arr in C++?
arr[0]
3 multiple choice options
What is the output of the following code?
#include
using namespace std;
int main() {
int arr[4] = {1, 2, 3, 4};
for (int i=0; i<4; i++) {
cout << arr[i] << " ";
}
}
1 2 3 4
To declare an array in C++, you define the variable type with
[]
Array indexes in C++ start at 1
False
1 multiple choice option
What is the size of an array int arr[10]; assuming int is 4 bytes?
40 bytes
3 multiple choice options
The array uses the index to traverse each of its element
True
1 multiple choice option
What will happen if an array is accessed out of its bound?
Undefined behavior
3 multiple choice options
Given int arr[5] = {10, 20, 30, 40, 50};, arr[2] is
30
What is the output of this code?
int arr[] = {1, 2, 3, 4};
cout << sizeof(arr) / sizeof(arr[0]);
4
Which of the following initializes all elements of an array to 0?
c) Both a and b
3 multiple choice options
Find the error in the following code: int arr[3];
arr[3] = 10;
Index 3 is out of bounds. Valid indexes are 0 to 2.
What does the * operator do in a pointer declaration?
d) Both b and c
3 multiple choice options
Which statement about pointers is correct?
int *p; declares a pointer to an integer
3 multiple choice options
What is the output of the following code?
int x = 10;
int *p = &x;
cout << *p;
10
nullptr in C++ represents a
null pointer
A pointer must always point to a valid memory location
False
1 multiple choice option
What is the correct way to declare a vector in C++?
vector
3 multiple choice options
Which function is used to add an element to a vector?
push_back()
3 multiple choice options
Which function removes the last element of a vector?
pop_back()
3 multiple choice options
The vector header file in C++ is
#include
Vectors automatically resize when new elements are added
True
1 multiple choice option
Find and fix the error in the following code:
vector nums;
nums.push(10);
Use push_back(10) instead of push(10)
What is the output of the following code?
vector v = {1, 2, 3};
v.pop_back();
cout << v.size();
2
What will happen when this code executes?
int arr[3] = {1, 2, 3};
cout << arr[5];
Undefined behavior
3 multiple choice options
What is the correct way to pass an array to a function?
all of the above
3 multiple choice options
What does this code print?
int arr[] = {4, 8, 12, 16};
cout << *(arr + 2);
12
3 multiple choice options
How can you find the number of elements in an array arr?
sizeof(arr) / sizeof(arr[0])
3 multiple choice options
What does this loop output, assuming int arr[] = {10, 20, 30, 40, 50};?
for (int i = 0; i < 5; i++)
cout << *(arr + i) << " ";
10 20 30 40 5
A pointer to an integer is declared as
int *p;
Uninitialized pointers automatically point to nullptr
False
1 multiple choice option
What is the output of this code?
int a = 10, b = 20;
int p = &a; p = b;
cout << a;
20
What is the result of ptr + 1 when ptr is an int*?
Adds 4 bytes to ptr
3 multiple choice options
Using delete on a null pointer is unsafe and causes a runtime error
False
1 multiple choice option
To allocate memory dynamically for 20 integers, you write
int *arr = new int[20];
Fix the error in the following code:
int arr = new int[5];
Change to int *arr = new int[5];
Which is the correct way to deallocate memory allocated by new[]?
delete[] arr;
3 multiple choice options
What is the output of this code? vector v = {1, 2, 3};
v.pop_back();
cout << v.size();
2
Which of the following is true about Pass by Reference in C++?
It allows changes to the original variable inside the function.
3 multiple choice options
What is the main difference between Pass by Value and Pass by Reference in C++?
In Pass by Value, the function receives a copy of the argument
3 multiple choice options
In a function using Pass by Reference, changes made to the parameter inside the function will affect the ____________ variable.
original
In C++, the operator _________ is used to pass a parameter by reference in a function declaration.
&
Pass by Reference allows a function to modify the original variable in the caller's scope
True
1 multiple choice option
When a parameter is passed by value, the function works directly with the original argument.
False
1 multiple choice option
What will the following program output?
void modifyValue(int &x) {
x = 7;
} i
nt main() {
int a = 3;
modifyValue(a);
cout << a;
return 0;
}
7
What will the following program output?
void modifyValue(int x) {
x = 10;
}
int main() {
int a = 5;
modifyValue(a);
cout << a;
return 0;
}
5
What's wrong with this code?
vector v = {1, 2, 3};
v.pop_front();
vector does not support pop_front(). Use v.erase(v.begin());
Which function returns the first element of a vector?
c) Both a and b
3 multiple choice options
What does v.insert(v.begin() + 1, 100); do?
Inserts 100 at index 1
3 multiple choice options
v.at(10) on a vector of size 5 throws a runtime error
True
1 multiple choice option
To remove all elements from a vector v, use
v.clear();