Enhance your understanding of arrays and string manipulation with this focused quiz designed to assess knowledge of basic array operations, common string methods, element access, searching, and modification techniques. Ideal for beginners seeking to strengthen their foundation in handling arrays and strings for programming and problem-solving tasks.
Given an array numbers = [10, 20, 30, 40], which value is accessed by numbers[2]?
Explanation: The index for arrays is typically zero-based, so numbers[2] refers to the third element, which is 30. The option 10 is the first element (index 0), 20 is the second (index 1), and 40 is the fourth (index 3). Choosing 20 or 40 confuses element positions.
What is the length of the array colors = ['red', 'green', 'blue', 'yellow']?
Explanation: This array contains four elements, so its length property would return 4. Option 3 undercounts by one, 5 overestimates, and 0 is incorrect as the array is not empty.
What is the result of joining the strings 'sun' and 'flower' using the '+' operator?
Explanation: Joining two strings with the plus operator combines them directly into 'sunflower' without spaces. The option 'sun flower' inserts an extra space, 'flower sun' reverses the order, and '+sunflower' incorrectly adds a plus sign.
If fruits = ['apple', 'banana', 'cherry'] and you set fruits[1] = 'pear', what is the updated second element?
Explanation: Setting fruits[1]' updates the second element, replacing 'banana' with 'pear'. The distractor 'apple' is still the first element, 'banana' was replaced, and 'cherry' remains third.
What does the length property return for the string message = 'hello world'?
Explanation: The length property returns the number of characters, including spaces, so 'hello world' has 11. Option 10 is one less, perhaps forgetting the space; 12 is one too many, and 0 is incorrect since the string is not empty.
If tools = ['hammer', 'wrench', 'pliers'], at what index is 'wrench' found?
Explanation: 'wrench' is at index 1 because arrays are zero-based. Option 0 points to 'hammer', 2 to 'pliers', and 3 is out of bounds for this array.
What is the result of changing 'code' to all uppercase letters?
Explanation: Converting to uppercase transforms 'code' to 'CODE'. 'Code' is only capitalized, 'code' remains unchanged, and 'cOdE' mixes cases, which is not a full uppercase transformation.
For animals = ['cat', 'dog', 'mouse', 'bird'], what does animals.slice(1, 3) return?
Explanation: animals.slice(1, 3) returns elements from index 1 up to, but not including, 3: so ['dog', 'mouse']. 'cat', 'dog' starts at 0, 'mouse', 'bird' starts too late, and 'cat', 'mouse' skips an element.
Does the string 'elephant' contain the substring 'ant'?
Explanation: 'ant' appears at the end of 'elephant', so the answer is 'Yes'. 'No' suggests it doesn't appear, 'Only at the beginning' is incorrect, and 'Only at the end' is misleading since a substring can occur anywhere, but here it is indeed at the end.
Given numbers = [1, 2, 3], what is the array after adding 4 at the end?
Explanation: Adding 4 at the end gives [1, 2, 3, 4]. [4, 1, 2, 3] adds at the beginning, [1, 2, 4] omits 3, and [1, 3, 4] skips 2. Therefore, only [1, 2, 3, 4] is correct.