pointers, arrays, and vectors

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/49

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

50 Terms

1
New cards

What is the correct way to access the first element of an array arr in C++?

arr[0]

3 multiple choice options

2
New cards

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

3
New cards

To declare an array in C++, you define the variable type with

[]

4
New cards

Array indexes in C++ start at 1

False

1 multiple choice option

5
New cards

What is the size of an array int arr[10]; assuming int is 4 bytes?

40 bytes

3 multiple choice options

6
New cards

The array uses the index to traverse each of its element

True

1 multiple choice option

7
New cards

What will happen if an array is accessed out of its bound?

Undefined behavior

3 multiple choice options

8
New cards

Given int arr[5] = {10, 20, 30, 40, 50};, arr[2] is

30

9
New cards

What is the output of this code?

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

cout << sizeof(arr) / sizeof(arr[0]);

4

10
New cards

Which of the following initializes all elements of an array to 0?

c) Both a and b

3 multiple choice options

11
New cards

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.

12
New cards

What does the * operator do in a pointer declaration?

d) Both b and c

3 multiple choice options

13
New cards

Which statement about pointers is correct?

int *p; declares a pointer to an integer

3 multiple choice options

14
New cards

What is the output of the following code?

int x = 10;

int *p = &x;

cout << *p;

10

15
New cards

nullptr in C++ represents a

null pointer

16
New cards

A pointer must always point to a valid memory location

False

1 multiple choice option

17
New cards

What is the correct way to declare a vector in C++?

vector v;

3 multiple choice options

18
New cards

Which function is used to add an element to a vector?

push_back()

3 multiple choice options

19
New cards

Which function removes the last element of a vector?

pop_back()

3 multiple choice options

20
New cards

The vector header file in C++ is

#include

21
New cards

Vectors automatically resize when new elements are added

True

1 multiple choice option

22
New cards

Find and fix the error in the following code:

vector nums;

nums.push(10);

Use push_back(10) instead of push(10)

23
New cards

What is the output of the following code?

vector v = {1, 2, 3};

v.pop_back();

cout << v.size();

2

24
New cards

What will happen when this code executes?

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

cout << arr[5];

Undefined behavior

3 multiple choice options

25
New cards

What is the correct way to pass an array to a function?

all of the above

3 multiple choice options

26
New cards

What does this code print?

int arr[] = {4, 8, 12, 16};

cout << *(arr + 2);

12

3 multiple choice options

27
New cards

How can you find the number of elements in an array arr?

sizeof(arr) / sizeof(arr[0])

3 multiple choice options

28
New cards

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

29
New cards

A pointer to an integer is declared as

int *p;

30
New cards

Uninitialized pointers automatically point to nullptr

False

1 multiple choice option

31
New cards

What is the output of this code?

int a = 10, b = 20;

int p = &a; p = b;

cout << a;

20

32
New cards

What is the result of ptr + 1 when ptr is an int*?

Adds 4 bytes to ptr

3 multiple choice options

33
New cards

Using delete on a null pointer is unsafe and causes a runtime error

False

1 multiple choice option

34
New cards

To allocate memory dynamically for 20 integers, you write

int *arr = new int[20];

35
New cards

Fix the error in the following code:

int arr = new int[5];

Change to int *arr = new int[5];

36
New cards

Which is the correct way to deallocate memory allocated by new[]?

delete[] arr;

3 multiple choice options

37
New cards

What is the output of this code? vector v = {1, 2, 3};

v.pop_back();

cout << v.size();

2

38
New cards

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

39
New cards

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

40
New cards

In a function using Pass by Reference, changes made to the parameter inside the function will affect the ____________ variable.

original

41
New cards

In C++, the operator _________ is used to pass a parameter by reference in a function declaration.

&

42
New cards

Pass by Reference allows a function to modify the original variable in the caller's scope

True

1 multiple choice option

43
New cards

When a parameter is passed by value, the function works directly with the original argument.

False

1 multiple choice option

44
New cards

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

45
New cards

What will the following program output?

void modifyValue(int x) {

x = 10;

}

int main() {

int a = 5;

modifyValue(a);

cout << a;

return 0;

}

5

46
New cards

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());

47
New cards

Which function returns the first element of a vector?

c) Both a and b

3 multiple choice options

48
New cards

What does v.insert(v.begin() + 1, 100); do?

Inserts 100 at index 1

3 multiple choice options

49
New cards

v.at(10) on a vector of size 5 throws a runtime error

True

1 multiple choice option

50
New cards

To remove all elements from a vector v, use

v.clear();