Arrays u0026 Strings Fundamentals Quiz Quiz

Test your knowledge of arrays and strings with 15 easy, fundamental questions covering their properties, operations, and common usage scenarios. Perfect for beginners wanting to reinforce essential concepts and terminology on array and string manipulation.

  1. Identifying Array Declaration

    Which of the following is a valid way to declare an array of integers containing five elements?

    1. int numbers = array[5];
    2. arrayu003Cintu003E numbers = 5;
    3. int(5) numbers;
    4. int[] numbers = new int[5];

    Explanation: The correct answer uses conventional syntax for creating an integer array with space for five elements. The second option incorrectly uses 'array' as if it's a type, which is invalid. The third uses incorrect type declaration notation. The fourth option confuses array creation with function-like syntax, which is not appropriate.

  2. Zero-Based Indexing

    If an array named 'colors' contains 4 elements, what is the index of its last element?

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

    Explanation: Array indices typically start at 0, so the last element is at index 3 when there are 4 elements. Index 4 would be out of bounds, while 2 and 1 are for earlier elements. Remembering that counting starts from 0 helps prevent off-by-one errors.

  3. String Immutability

    Why are strings referred to as immutable in many programming languages?

    1. They require manual memory deallocation.
    2. They can store any type of data.
    3. Their contents cannot be changed after creation.
    4. They can shrink or expand dynamically.

    Explanation: The immutability of strings means once created, their contents remain fixed; if you modify them, a new string is created instead. Dynamic resizing refers to lists or dynamic arrays, not to immutable strings. Strings do not store all data types, and they generally do not need manual deallocation in high-level languages.

  4. Accessing Array Elements

    How would you access the third element in an array named 'scores'?

    1. scores(3)
    2. scores[2]
    3. scores[3]
    4. scores{2}

    Explanation: The correct way is scores[2], as array indices are 0-based, making the third element at index 2. scores(3) is incorrect because parentheses are not used in this context. scores[3] would actually access the fourth element. scores{2} uses braces, which are not standard for array indexing.

  5. Finding String Length

    Given a string named 'message' with the value 'Hello', what would the length of this string be?

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

    Explanation: 'Hello' consists of five characters, so the correct length is 5. Six would be incorrect unless there were an extra character like a space or null terminator. Four undercounts the actual length, and zero is only possible if the string is empty.

  6. Concatenation of Strings

    What is the result of concatenating the strings 'cat' and 'fish'?

    1. catfish
    2. cat_fish
    3. fishcat
    4. cat fish

    Explanation: 'catfish' is the correct answer when combining the two strings without any separator. 'cat fish' inserts a space, which is not specified. 'cat_fish' adds an underscore, and 'fishcat' reverses the order of concatenation.

  7. Array Length Retrieval

    Which expression retrieves the number of elements in an array called 'data'?

    1. data.length
    2. data.length()
    3. data.size
    4. len(data)

    Explanation: Many programming languages use data.length for arrays. data.length() looks similar but is typically a method for collections, not basic arrays. data.size is more commonly used for other structures. len(data) is a function in some languages but not universally used for arrays.

  8. Reversing a String

    What will be the result if you reverse the string 'desk'?

    1. ksed
    2. desks
    3. kdes
    4. sdek

    Explanation: Reversing 'desk' results in 'ksed', which is the exact backward spelling. The other options are either shuffled letters, start with the wrong letter, or append an extra character not found in the original string.

  9. Finding an Element in an Array

    If an array named 'items' contains ['pen', 'book', 'eraser'], which index holds the element 'book'?

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

    Explanation: Arrays start counting at 0, so 'pen' is at 0, 'book' at 1, and 'eraser' at 2. Index 2 points to 'eraser'. Index 0 is the first element, and index 3 does not exist for this array.

  10. Empty String Detection

    What is the value of the length property for an empty string?

    1. null
    2. 0
    3. 1
    4. -1

    Explanation: An empty string has zero characters, so its length is 0. One would imply a character exists, -1 is never used for valid string lengths, and null is not a value for length properties.

  11. Array Initial Values

    What is the typical initial value of an integer array in most programming languages if not explicitly set?

    1. null
    2. 1
    3. 0
    4. undefined

    Explanation: Uninitialized integer arrays usually default to zero in many languages. 'undefined' is more common in loosely typed languages, 'null' is for reference types, and '1' is not a default unless you set it specifically.

  12. Splitting a String

    What will splitting the string 'a,b,c' using the comma character as the separator result in?

    Explanation: Splitting 'a,b,c' on commas divides it into three elements: 'a', 'b', and 'c'. ['abc'] keeps the string together. ['a b c'] adds spaces, and ['a;c'] splits incorrectly by a semicolon.

  13. Case Sensitivity in Strings

    Are strings 'Tree' and 'tree' considered equal in a case-sensitive comparison?

    1. Only if trimmed first
    2. No
    3. Depends on the language
    4. Yes

    Explanation: In case-sensitive comparisons, 'Tree' and 'tree' are not equal due to the difference in the uppercase and lowercase 'T'. 'Yes' would be true only for case-insensitive checks. It does not depend on the language if comparison is case-sensitive. Trimming only removes spaces, not case differences.

  14. Adding Elements to an Array

    To add a number to the end of a dynamic array named 'list', which operation would you typically use?

    1. Delete the number
    2. Append the number
    3. Prepend the number
    4. Slice the list

    Explanation: Appending adds an element to the end of a list or array. Deleting removes elements. Prepending adds to the beginning, not the end. Slicing is used to get a subset of elements, not to add new ones.

  15. Array Element Type Consistency

    If an array is declared to store integers, can it hold a string value?

    1. Yes, if assigned at runtime
    2. Yes, it can hold any type
    3. No, it can only store integers
    4. Only if the array is empty

    Explanation: A statically typed integer array cannot store values of other types, such as strings. While some languages support mixed types in lists, integer arrays do not. Runtime assignment doesn't change the original type. The array being empty does not allow insertion of other types.

  16. Mutating Arrays

    What happens if you try to assign a new value to an index in a mutable array?

    1. A new array is created
    2. The value at that index is updated
    3. An error always occurs
    4. The old value is permanently lost

    Explanation: In mutable arrays, assigning to an index updates the value at that spot. Usually, no error occurs unless the index is invalid. Creating a new array occurs with immutable structures, not mutable ones. The old value is overwritten, but not necessarily lost forever, as it might still exist elsewhere in memory.