0
0
C++programming~5 mins

Common array operations in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the syntax to declare an array of 5 integers in C++?
You declare it like this: int arr[5]; This creates an array named arr that can hold 5 integers.
Click to reveal answer
beginner
How do you access the third element of an array named arr?
You use the index 2 because counting starts at 0. So, arr[2] gives you the third element.
Click to reveal answer
intermediate
How can you find the size of a statically declared array arr in C++?
Use sizeof(arr) / sizeof(arr[0]). This divides the total bytes of the array by the bytes of one element to get the number of elements.
Click to reveal answer
intermediate
What is the difference between arr[i] and *(arr + i)?
Both access the element at index i. arr[i] is array indexing syntax, while *(arr + i) uses pointer arithmetic. They are equivalent.
Click to reveal answer
intermediate
How do you copy elements from one array to another in C++?
You can use a loop to copy each element one by one, like:<br>for(int i = 0; i < size; i++) { dest[i] = src[i]; }<br>Or use std::copy from the <algorithm> header.
Click to reveal answer
What is the index of the first element in a C++ array?
A0
B1
C-1
DDepends on the array size
Which expression correctly gives the number of elements in a statically declared array arr?
Asizeof(arr)
Bsizeof(arr) / sizeof(arr[0])
Csizeof(arr) * sizeof(arr[0])
Dsizeof(arr[0]) / sizeof(arr)
What does arr[3] mean in C++?
AThe fourth element of the array
BThe third element of the array
CThe element at index 2
DAn invalid syntax
Which of the following copies elements from one array to another correctly?
Acopy(dest, src);
Bdest = src;
Cdest.copy(src);
Dfor(int i=0; i&lt;size; i++) dest[i] = src[i];
What is the result of *(arr + 2)?
AThe second element of the array
BThe first element of the array
CThe third element of the array
DA pointer to the array
Explain how to declare, access, and find the size of a static array in C++.
Think about how memory size relates to element count.
You got /3 concepts.
    Describe two ways to copy elements from one array to another in C++.
    Remember arrays cannot be assigned directly.
    You got /2 concepts.