0
0
C++programming~5 mins

Array traversal in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does array traversal mean in programming?
Array traversal means going through each element of an array one by one to read or process them.
Click to reveal answer
beginner
How do you write a basic for loop to traverse an array in C++?
You use a for loop with an index starting at 0 up to the array size minus one, accessing each element by its index.
Click to reveal answer
beginner
Why is it important to know the size of the array when traversing it?
Knowing the size prevents accessing elements outside the array, which can cause errors or crashes.
Click to reveal answer
beginner
What is the output of this code?<br>
int arr[] = {1, 2, 3};
for(int i = 0; i < 3; i++) {
  std::cout << arr[i] << " ";
}
The output is: 1 2 3 <br>It prints each element of the array separated by spaces.
Click to reveal answer
intermediate
Can you traverse an array backwards? How?
Yes, by starting the loop index at the last element (size - 1) and decreasing it until 0.
Click to reveal answer
What is the starting index for traversing a C++ array?
AArray size
B1
C-1
D0
Which loop is commonly used for array traversal in C++?
Afor loop
Bwhile loop
Cdo-while loop
Dif statement
What happens if you access an array element outside its bounds?
AIt prints zero
BIt automatically resizes the array
CProgram may crash or show garbage values
DNothing happens
How do you know how many times to loop when traversing an array?
AUse the array size
BLoop forever
CUse random number
DLoop once
Which of these is a valid way to traverse an array backwards?
Afor(int i = 1; i <= size; i++)
Bfor(int i = size - 1; i >= 0; i--)
Cwhile(i < size)
Dfor(int i = 0; i < size; i++)
Explain how to traverse an array in C++ using a for loop.
Think about how you count from the first to the last element.
You got /4 concepts.
    Why must you be careful about array size when traversing?
    Imagine walking on a path that ends at a certain point.
    You got /4 concepts.