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.
Which of the following is a valid way to declare an array of integers containing five elements?
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.
If an array named 'colors' contains 4 elements, what is the index of its last element?
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.
Why are strings referred to as immutable in many programming languages?
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.
How would you access the third element in an array named 'scores'?
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.
Given a string named 'message' with the value 'Hello', what would the length of this string be?
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.
What is the result of concatenating the strings 'cat' and '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.
Which expression retrieves the number of elements in an array called '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.
What will be the result if you reverse the string 'desk'?
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.
If an array named 'items' contains ['pen', 'book', 'eraser'], which index holds the element 'book'?
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.
What is the value of the length property for an empty string?
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.
What is the typical initial value of an integer array in most programming languages if not explicitly set?
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.
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.
Are strings 'Tree' and 'tree' considered equal in a case-sensitive comparison?
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.
To add a number to the end of a dynamic array named 'list', which operation would you typically use?
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.
If an array is declared to store integers, can it hold a string value?
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.
What happens if you try to assign a new value to an index in a mutable array?
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.