1. Arrays Flashcards
What is an array?
An array is a data structure that can store a fixed-size sequence of elements of the same type. It is a collection of items stored at contiguous memory locations.
True or False: Arrays can store elements of different data types.
False. Arrays are designed to hold elements of the same data type.
What is the time complexity for accessing an element in an array by index?
The time complexity for accessing an element in an array by index is O(1), which means it takes constant time.
Fill in the blank: The first index of an array in most programming languages is ____.
0
What is the difference between a one-dimensional array and a multi-dimensional array?
A one-dimensional array is a linear list of elements, while a multi-dimensional array contains multiple arrays, allowing for the representation of data in more than one dimension, such as matrices or grids.
Which of the following is a valid way to declare an array in Python? A) int[] arr; B) arr = []; C) array arr[];
B) arr = []; is a valid way to declare an array (list) in Python.
Explain what an associative array is.
An associative array, also known as a map or dictionary, is a collection of key-value pairs where each key is unique and is used to access its corresponding value. Unlike traditional arrays, associative arrays do not use numerical indices.
What is array slicing and how is it useful?
Array slicing is a technique used to extract a portion of an array by specifying a range of indices. It is useful for accessing subarrays without needing to create a new array manually.
True or False: The size of an array can be changed after it has been created in languages like C and C++.
False. In C and C++, the size of a statically allocated array cannot be changed after its creation.
What is the purpose of the ‘length’ property in an array?
The ‘length’ property returns the number of elements in an array, which is useful for iterating through the array or checking if it is empty.
Describe how to iterate through an array in JavaScript.
In JavaScript, you can iterate through an array using a for loop, a forEach method, or a for…of loop. Each method allows you to access each element of the array and perform operations on it.
What is a jagged array?
A jagged array is an array of arrays where the inner arrays can have different lengths. This allows for more flexible memory usage compared to a rectangular array, where all inner arrays must be the same length.
What is the default value of uninitialized elements in an array of integers in Java?
The default value of uninitialized elements in an array of integers in Java is 0.
Explain the concept of array bounds checking.
Array bounds checking is a mechanism used in programming languages to ensure that any access to an array does not exceed its defined limits, which helps prevent runtime errors and potential security vulnerabilities.
What is a dynamic array?
A dynamic array is an array that can resize itself when elements are added or removed. Unlike static arrays, dynamic arrays allocate memory on the heap, allowing for flexibility in size.
How do you find the maximum value in an array?
To find the maximum value in an array, you can iterate through the array, comparing each element to a variable that stores the maximum value found so far, updating it as necessary.
What is the difference between shallow copy and deep copy in relation to arrays?
A shallow copy of an array creates a new array object but does not create copies of the nested objects; it only copies references. A deep copy creates a new array and recursively copies all objects, ensuring that changes to the copied array do not affect the original.
Describe the process of merging two sorted arrays.
Merging two sorted arrays involves comparing the elements of both arrays and placing the smaller element into a new array. This process continues until all elements from both arrays have been added to the new array, resulting in a single sorted array.
What is the purpose of the ‘push’ method in JavaScript arrays?
The ‘push’ method in JavaScript arrays adds one or more elements to the end of an array and returns the new length of the array.
True or False: In C++, you can create an array of pointers.
True. In C++, you can create an array of pointers, allowing you to store addresses of other variables or arrays.
What is array rotation?
Array rotation is the process of shifting the elements of an array to the left or right by a specified number of positions, with the elements that fall off one end being added back to the other end.
How can you remove duplicates from an array in Python?
You can remove duplicates from an array in Python by converting it to a set, which inherently does not allow duplicate values, and then converting it back to a list if necessary.
What are the advantages of using arrays over linked lists?
Arrays provide faster access to elements via indexing (O(1)), more efficient memory usage due to contiguous memory allocation, and better cache performance compared to linked lists, which require traversal and additional memory for pointers.
Explain the concept of a multidimensional array and provide an example.
A multidimensional array is an array that contains more than one dimension, such as a two-dimensional array that can be visualized as a matrix with rows and columns. For example, a 2D array can be declared in C++ as int matrix[3][4], representing 3 rows and 4 columns.