Q.1 What is the index of the first element in an array in most programming languages?
0
1
-1
Depends on the language
Explanation - In most programming languages like C, C++, and Java, array indexing starts from 0.
Correct answer is: 0
Q.2 Which of the following statements correctly declares an integer array of size 10 in C?
int arr[10];
int arr;
array int[10];
int arr()
Explanation - In C, arrays are declared using the syntax: data_type array_name[size];
Correct answer is: int arr[10];
Q.3 Which operation is used to find the length of a string in C?
length()
size()
strlen()
count()
Explanation - The function strlen() from <string.h> library returns the length of a string in C.
Correct answer is: strlen()
Q.4 In an array of size n, the last element can be accessed at index:
n
n-1
0
n+1
Explanation - Array indexing starts at 0, so the last element is at index n-1.
Correct answer is: n-1
Q.5 What is the output of the following code? char str[] = "Hello"; printf("%c", str[1]);
H
e
l
o
Explanation - Arrays in C are zero-indexed, so str[1] corresponds to the second character, 'e'.
Correct answer is: e
Q.6 Which of the following is TRUE about arrays?
Array size can be changed at runtime
All elements must be of the same data type
Array elements can be of different types
Array index starts from 1 in all languages
Explanation - Arrays store elements of the same data type; dynamic resizing requires other data structures like lists.
Correct answer is: All elements must be of the same data type
Q.7 Which of the following is NOT a valid way to initialize a string in C?
char str[] = "Hello";
char str[6] = "Hello";
char str[] = {'H','e','l','l','o','\0'};
char str = "Hello";
Explanation - Strings are arrays of characters; assigning a string directly to a single char variable is invalid.
Correct answer is: char str = "Hello";
Q.8 Which of the following correctly accesses the third element of array arr in Java?
arr[2]
arr[3]
arr.get(3)
arr.get(2)
Explanation - Java arrays are zero-indexed, so the third element is at index 2.
Correct answer is: arr[2]
Q.9 Which of the following functions can concatenate two strings in C?
stradd()
strcat()
concat()
append()
Explanation - The function strcat() from <string.h> is used to concatenate two strings in C.
Correct answer is: strcat()
Q.10 What is the time complexity to access an element at a specific index in an array?
O(1)
O(n)
O(log n)
O(n^2)
Explanation - Array indexing allows direct access to any element in constant time.
Correct answer is: O(1)
Q.11 Which of the following is a valid way to declare a 2D array in C?
int arr[3][4];
int arr[3,4];
int arr(3,4);
array int arr[3][4];
Explanation - 2D arrays in C are declared as int array_name[rows][columns];
Correct answer is: int arr[3][4];
Q.12 What does the following code print? int arr[] = {1,2,3,4}; printf("%d", arr[3]);
1
2
3
4
Explanation - arr[3] accesses the fourth element of the array, which is 4.
Correct answer is: 4
Q.13 Which of the following is TRUE about strings in Python?
Strings are mutable
Strings are immutable
Strings cannot be indexed
Strings are arrays of integers
Explanation - In Python, strings cannot be changed after creation; they are immutable.
Correct answer is: Strings are immutable
Q.14 Which function is used to copy one string to another in C?
strcopy()
strcpy()
copystr()
strclone()
Explanation - The function strcpy() from <string.h> is used to copy strings in C.
Correct answer is: strcpy()
Q.15 What will be the output of the following C code? char str[6] = "Hello"; str[0] = 'h'; printf("%s", str);
Hello
hello
HHello
Error
Explanation - Strings in C are mutable character arrays, so changing str[0] modifies the first character.
Correct answer is: hello
Q.16 Which of the following is an example of a jagged array?
int arr[3][3];
int arr[2][];
Array of arrays with different lengths
int arr[4];
Explanation - A jagged array is an array of arrays where each sub-array can have a different length.
Correct answer is: Array of arrays with different lengths
Q.17 Which method returns the number of characters in a Java String?
length()
size()
count()
strlen()
Explanation - In Java, the length() method returns the number of characters in a string.
Correct answer is: length()
Q.18 Which of the following represents a valid character array in C?
char c[] = {'a','b','c'};
char c = {'a','b','c'};
char c[3] = "abc";
char c = 'abc';
Explanation - Character arrays can be initialized with curly braces containing individual characters.
Correct answer is: char c[] = {'a','b','c'};
Q.19 Which of the following is NOT an operation on strings?
Concatenation
Traversal
Multiplication
Copying
Explanation - Strings support operations like concatenation, traversal, and copying, but not multiplication.
Correct answer is: Multiplication
Q.20 What is the result of the following Python code? str = "abc" + "def" print(str)
abcdef
abc def
abc+def
Error
Explanation - The + operator concatenates strings in Python without adding spaces.
Correct answer is: abcdef
Q.21 What will be the output of this C code? int arr[5] = {1,2,3}; printf("%d", arr[4]);
0
Garbage value
3
Error
Explanation - In C, missing initialization values are set to 0 for global arrays or may contain garbage for local arrays; here assume global array.
Correct answer is: 0
Q.22 Which of the following correctly declares a string in C++?
string str = "Hello";
char str = "Hello";
str str = "Hello";
char[] str = "Hello";
Explanation - C++ provides a string class for easier handling of strings than character arrays.
Correct answer is: string str = "Hello";
Q.23 Which is the correct syntax to access the last character of a string in Python str variable?
str[-1]
str[0]
str[length]
str[last]
Explanation - Python allows negative indexing; -1 refers to the last character of the string.
Correct answer is: str[-1]
Q.24 Which of the following is true about multi-dimensional arrays?
They can only have 2 dimensions
They can be jagged
They are always contiguous in memory
They cannot store integers
Explanation - Multi-dimensional arrays can be jagged (sub-arrays of different lengths) especially in languages like Java.
Correct answer is: They can be jagged
