Arrays and Strings Manipulation Essentials Quiz Quiz

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.

  1. Accessing Array Elements

    Given an array numbers = [10, 20, 30, 40], which value is accessed by numbers[2]?

    1. 20
    2. 10
    3. 30
    4. 40

    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.

  2. Basic Array Length

    What is the length of the array colors = ['red', 'green', 'blue', 'yellow']?

    1. 5
    2. 3
    3. 4
    4. 0

    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.

  3. String Concatenation

    What is the result of joining the strings 'sun' and 'flower' using the '+' operator?

    1. flower sun
    2. sun flower
    3. sunflower
    4. +sunflower

    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.

  4. Changing Array Elements

    If fruits = ['apple', 'banana', 'cherry'] and you set fruits[1] = 'pear', what is the updated second element?

    1. banana
    2. pear
    3. apple
    4. cherry

    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.

  5. String Length Property

    What does the length property return for the string message = 'hello world'?

    1. 12
    2. 10
    3. 11
    4. 0

    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.

  6. Finding an Element in an Array

    If tools = ['hammer', 'wrench', 'pliers'], at what index is 'wrench' found?

    1. 1
    2. 0
    3. 3
    4. 2

    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.

  7. Converting a String to Uppercase

    What is the result of changing 'code' to all uppercase letters?

    1. Code
    2. cOdE
    3. CODE
    4. code

    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.

  8. Slicing Arrays

    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.

  9. Checking for a Substring

    Does the string 'elephant' contain the substring 'ant'?

    1. No
    2. Yes
    3. Only at the end
    4. Only at the beginning

    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.

  10. Adding Elements to Arrays

    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.