Here are 30 interview questions along with their answers on Chapter 5: Arrays and Strings:
Chapter 5: Arrays and Strings
1. What is an array in C?
- Answer: An array in C is a collection of elements of the same type, stored in contiguous memory locations. The elements can be accessed using an index.
2. How do you declare an array in C?
- Answer: You declare an array in C by specifying the data type, followed by the array name, and the number of elements:
c int arr[10];
3. What is the difference between a one-dimensional and a two-dimensional array?
- Answer:
- A one-dimensional array is a linear list of elements.
- A two-dimensional array is an array of arrays, where each element is itself an array.
c int arr[5]; // One-dimensional int arr[5][5]; // Two-dimensional
4. How do you access elements of an array?
- Answer: You can access array elements using the index, starting from 0. For example:
c int arr[5] = {1, 2, 3, 4, 5}; printf("%d", arr[2]); // Output: 3
5. What are multi-dimensional arrays?
- Answer: Multi-dimensional arrays are arrays that have more than one index. The most common is the two-dimensional array (like a matrix), but arrays can have more dimensions (e.g., 3D arrays).
6. Can arrays in C store elements of different data types?
- Answer: No, arrays in C can only store elements of the same data type. To store different data types, structures or unions should be used.
7. What is a pointer to an array in C?
- Answer: A pointer to an array is a pointer that holds the address of the first element of an array.
c int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; // ptr points to the first element of arr
8. What is the syntax for declaring and initializing an array in C?
- Answer:
c int arr[5] = {1, 2, 3, 4, 5}; // Declaration and initialization
9. How do you initialize a one-dimensional array?
- Answer: You can initialize a one-dimensional array at the time of declaration:
c int arr[] = {1, 2, 3, 4, 5};
10. How do you pass an array to a function?
- Answer: In C, arrays are passed to functions by reference, meaning the function receives the address of the first element of the array.
c void display(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } }
11. What is a string in C?
- Answer: A string in C is a sequence of characters stored in an array, terminated by a null character (
'\0'
).
12. How do you declare a string in C?
- Answer: A string is declared as an array of characters:
c char str[20] = "Hello";
13. How do you find the length of a string in C?
- Answer: You can find the length of a string using the
strlen()
function, which returns the number of characters excluding the null terminator.
c int len = strlen(str);
14. What is the use of the strcpy()
function in C?
- Answer: The
strcpy()
function is used to copy one string to another:
c strcpy(dest, src);
15. What is the difference between strcpy()
and strncpy()
?
- Answer:
strcpy()
copies a string until it reaches the null terminator.
strncpy()
copies at most the specified number of characters, ensuring the destination string is null-terminated.
16. What is the strcmp()
function used for?
- Answer: The
strcmp()
function compares two strings lexicographically and returns:
0
if they are equal,
- A negative value if the first string is lexicographically smaller,
- A positive value if the first string is larger.
17. How do you concatenate two strings in C?
- Answer: The
strcat()
function is used to concatenate two strings.
c strcat(str1, str2);
18. What is the difference between scanf()
and gets()
for reading strings?
- Answer:
scanf()
reads input and stops at the first whitespace (space, tab, or newline).
gets()
reads an entire line of input, including spaces, but is dangerous as it doesn’t check for buffer overflow.
19. What is the size of a string in C?
- Answer: The size of a string in C is determined by the number of characters in the string, including the null terminator
'\0'
.
20. What is the null character \0
in a string?
- Answer: The null character
\0
marks the end of a string in C. It indicates that the string has no more characters.
21. How do you reverse a string in C?
- Answer: You can reverse a string by swapping its characters from the beginning and the end until the middle:
c void reverse(char str[]) { int start = 0, end = strlen(str) - 1; while (start < end) { char temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } }
22. What is the use of the memset()
function?
- Answer: The
memset()
function sets a specified number of bytes in a block of memory to a given value. It’s commonly used to initialize arrays.
c memset(arr, 0, sizeof(arr));
23. What are the limitations of arrays in C?
- Answer: Arrays in C have a fixed size and cannot be resized once allocated. Also, they do not have bounds checking, so accessing out-of-bounds elements can lead to undefined behavior.
24. What is a dynamic array in C?
- Answer: A dynamic array is an array whose size can be determined at runtime using functions like
malloc()
or calloc()
for memory allocation.
25. What is the difference between malloc()
and calloc()
?
- Answer:
malloc()
allocates a block of memory without initializing it.
calloc()
allocates memory and initializes all bytes to zero.
26. How can you access individual characters in a string?
- Answer: You can access individual characters in a string using array indexing:
c char str[] = "Hello"; printf("%c", str[1]); // Output: e
27. What is the strstr()
function?
- Answer: The
strstr()
function searches for the first occurrence of a substring in a string and returns a pointer to it.
c char *substr = strstr(str, "ell");
28. Can you pass a string to a function in C?
- Answer: Yes, strings are passed to functions by reference, meaning the function receives the address of the first character of the string.
29. What is a string literal in C?
- Answer: A string literal is a sequence of characters enclosed in double quotes. It is automatically null-terminated:
c char str[] = "Hello";
30. What is the difference between a string array and a character array in C?
- Answer:
- A string array is an array of characters terminated by
'\0'
(a string).
- A character array is a general array of characters that may or may not form a valid string.
These questions and answers cover the fundamentals of arrays and strings in C, including how to work with them, common functions, and their limitations.