Arrays and Strings # MCQs Practice set

Q.1 Which of the following correctly declares an array in C?

int arr[10];
arr int[10];
array arr(10);
int[10] arr;
Explanation - In C, arrays are declared by specifying the type followed by the name and the size in square brackets.
Correct answer is: int arr[10];

Q.2 What is the index of the first element in a typical array?

1
0
-1
Depends on language
Explanation - In most programming languages like C, C++, and Java, array indexing starts from 0.
Correct answer is: 0

Q.3 Which function is used in C to find the length of a string?

strlen()
length()
size()
count()
Explanation - The strlen() function in C returns the number of characters in a string excluding the null terminator.
Correct answer is: strlen()

Q.4 What is the output of strlen("Hello")?

4
5
6
Error
Explanation - The string 'Hello' has 5 characters. The null terminator is not counted by strlen().
Correct answer is: 5

Q.5 In C, how is the end of a string marked?

With a space
With a '\0' character
With a newline
With a semicolon
Explanation - In C, strings are null-terminated. The '\0' character marks the end of the string.
Correct answer is: With a '\0' character

Q.6 Which of the following allows dynamic resizing of arrays?

C arrays
Java arrays
Python lists
C++ static arrays
Explanation - Python lists are dynamic and can grow or shrink in size, unlike static arrays in C, C++, or Java.
Correct answer is: Python lists

Q.7 What is the time complexity to access an element in an array by index?

O(1)
O(log n)
O(n)
O(n^2)
Explanation - Array elements can be accessed directly by index in constant time.
Correct answer is: O(1)

Q.8 Which operator is used to access an element in a string in C++?

.
->
[]
&
Explanation - In C++, strings are arrays of characters and can be accessed using the [] operator.
Correct answer is: []

Q.9 What will be the size of int arr[5] in C if int size is 4 bytes?

5
10
20
25
Explanation - Each int takes 4 bytes. For 5 integers, the size is 5 * 4 = 20 bytes.
Correct answer is: 20

Q.10 Which of the following is immutable in Python?

List
Dictionary
String
Set
Explanation - Strings in Python are immutable, meaning they cannot be modified after creation.
Correct answer is: String

Q.11 In C, what happens if you access an array element out of its bound?

Compilation error
Runtime error
Undefined behavior
0 is returned
Explanation - C does not perform bounds checking. Accessing out-of-bounds may cause undefined behavior.
Correct answer is: Undefined behavior

Q.12 Which string function in C is used to concatenate two strings?

stradd()
strcat()
strappend()
concat()
Explanation - The strcat() function in C appends one string to the end of another.
Correct answer is: strcat()

Q.13 What is the length of the string stored in char str[20] = "Hello"?

5
6
20
19
Explanation - The string 'Hello' has 5 characters. The array size is 20, but strlen() counts characters only up to '\0'.
Correct answer is: 5

Q.14 Which method is used in Java to get the length of a string?

length()
size()
count()
strlen()
Explanation - In Java, the length() method returns the number of characters in a string.
Correct answer is: length()

Q.15 In Python, what does the expression len([1,2,3,4]) return?

3
4
5
Error
Explanation - The list [1,2,3,4] has 4 elements, so len() returns 4.
Correct answer is: 4

Q.16 In C++, how can you find the size of an array?

sizeof(arr)/sizeof(arr[0])
arr.length()
arr.size()
count(arr)
Explanation - In C++, static arrays do not have a built-in size method. The total size divided by element size gives length.
Correct answer is: sizeof(arr)/sizeof(arr[0])

Q.17 Which of these is the default value of elements in a Java array of int?

0
null
garbage value
1
Explanation - In Java, arrays of primitive types are initialized with default values. For int, it is 0.
Correct answer is: 0

Q.18 Which string method in Python is used to convert to uppercase?

upper()
uppercase()
toUpper()
cap()
Explanation - The upper() method in Python returns a new string in uppercase.
Correct answer is: upper()

Q.19 Which of the following languages allows negative indexing in arrays?

C
C++
Java
Python
Explanation - In Python, negative indices are valid and count from the end of the list or string.
Correct answer is: Python

Q.20 Which function is used in C to compare two strings?

strcmp()
strcomp()
compare()
equals()
Explanation - The strcmp() function compares two strings in C and returns 0 if they are equal.
Correct answer is: strcmp()

Q.21 What will be the output of 'abc' + '123' in Python?

abc123
Error
abc 123
123abc
Explanation - In Python, the + operator concatenates strings. 'abc' + '123' results in 'abc123'.
Correct answer is: abc123

Q.22 Which of the following is true about 2D arrays?

They store data in linear memory
They are always jagged
They cannot be created
They are faster than 1D arrays
Explanation - 2D arrays are stored in linear memory in row-major or column-major order depending on language.
Correct answer is: They store data in linear memory

Q.23 In C, what is the type of the string literal "Hello"?

char*
int*
string
char[]
Explanation - String literals in C are of type char[] (array of characters).
Correct answer is: char[]

Q.24 In Java, what happens if you try to access arr[arr.length]?

Returns null
Throws ArrayIndexOutOfBoundsException
Returns 0
Compiles but gives garbage
Explanation - Java checks array bounds at runtime. Accessing index equal to length throws ArrayIndexOutOfBoundsException.
Correct answer is: Throws ArrayIndexOutOfBoundsException

Q.25 Which function is used to copy one string into another in C?

strcpy()
copy()
stringcopy()
assign()
Explanation - The strcpy() function in C copies the contents of one string into another.
Correct answer is: strcpy()