Section A — 1 Mark Questions
1. What is an array? [1 Mark]
Answer: An array is a collection of similar data items stored at contiguous memory locations.
2. How is a one-dimensional array declared in C? [1 Mark]
Answer: data_type array_name[size];
3. What is the index of the first element in a C array? [1 Mark]
Answer: The index of the first element is always 0.
4. Write the syntax to initialize a one-dimensional integer array with 5 elements. [1 Mark]
Answer: int arr[5] = {1, 2, 3, 4, 5};
5. What is a two-dimensional array? [1 Mark]
Answer: A two-dimensional array is an array of arrays, often visualized as a table with rows and columns.
6. Write the syntax for declaring a two-dimensional array. [1 Mark]
Answer: data_type array_name[rows][cols];
7. What is a character array in C used to represent? [1 Mark]
Answer: A character array is typically used to represent strings.
8. What character automatically terminates a string in C? [1 Mark]
Answer: The null character (\0).
9. Write the syntax to declare a character array for holding a string of size 10. [1 Mark]
Answer: char str[11];
10. Which format specifier is used with scanf and printf for reading and writing strings? [1 Mark]
Answer: The %s format specifier.
11. Does scanf require the ampersand (&) operator when reading a string into a character array? [1 Mark]
Answer: No, because the array name itself acts as a pointer to the first element.
12. State one limitation of using scanf with %s for reading strings. [1 Mark]
Answer: It stops reading input when it encounters the first whitespace character.
13. What is the total number of elements in a two-dimensional array declared as int matrix[3][4]? [1 Mark]
Answer: 12 elements (3 rows × 4 columns).
14. How do you access the element at the second row and third column of a 2D array named arr? [1 Mark]
Answer: arr[1][2]
15. What happens if you initialize an array with fewer elements than its declared size? [1 Mark]
Answer: The remaining elements are automatically initialized to zero.
16. Can the size of an array be changed after its declaration? [1 Mark]
Answer: No, arrays have a fixed size determined at compile-time.
17. Write the syntax for printing a string variable named text using printf. [1 Mark]
Answer: printf("%s", text);
18. What is the storage representation of a 2D array in computer memory? [1 Mark]
Answer: It is stored in a contiguous block of memory in row-major order.
19. Why must string buffers in C be one character larger than the maximum expected string length? [1 Mark]
Answer: To accommodate the null terminator (\0).
20. What is array subscripting? [1 Mark]
Answer: It is the mechanism of accessing individual elements of an array using square brackets [].
Section B — 2/3 Mark Questions
1. Explain the concept of contiguous memory allocation in one-dimensional arrays. [2 Marks]
Answer: In a one-dimensional array, all elements are stored in adjacent memory locations. This means if the starting address of an integer array is known, the address of any subsequent element can be calculated directly using its index and the size of the data type.
2. Differentiate between declaration and initialization of an array with an example. [3 Marks]
Answer:
- Declaration: Telling the compiler about the array name, data type, and size without assigning values (e.g., int a[3];).
- Initialization: Assigning initial values to the array at the time of declaration (e.g., int a[3] = {10, 20, 30};).
- Difference: Declaration allocates memory space while initialization populates that allocated space with specific data.
3. Write a short code snippet to declare and initialize a 2D array representing a 2x3 matrix, and explain its layout. [3 Marks]
Answer: Code: int mat[2][3] = {{1, 2, 3}, {4, 5, 6}}; Explanation: This creates a matrix with 2 rows and 3 columns. The first row contains elements 1, 2, 3 and the second row contains 4, 5, 6, stored sequentially in memory.
4. Why is the null character (\0) important in character arrays representing strings? [2 Marks]
Answer: The null character marks the logical end of a string in C. Without it, functions like printf would not know where the string ends, leading to unpredictable output or reading garbage memory values.
5. Explain how scanf reads strings and why it fails with sentences containing spaces. [3 Marks]
Answer: scanf uses the %s specifier to read strings up to the first whitespace, tab, or newline character. When reading a sentence like "Hello World", scanf stops reading at the space, storing only "Hello" in the character array and leaving the rest in the input buffer.
6. Write the code to read a string from the terminal using scanf and print it using printf. [2 Marks]
Answer: char name[50]; printf("Enter name: "); scanf("%s", name); printf("Entered name: %s", name);
7. What is row-major order in two-dimensional arrays? [2 Marks]
Answer: Row-major order means that elements of a 2D array are stored row by row in memory. The entire first row is stored first, followed immediately by the entire second row, and so on.
8. Can you initialize a 2D array without specifying the row count? Give an example. [3 Marks]
Answer: Yes, you can omit the row dimension when initializing a 2D array at the time of declaration. The compiler automatically determines the number of rows based on the initializer list. Example: int arr[][2] = {{1, 2}, {3, 4}, {5, 6}};.
9. Explain what happens during partial initialization of a one-dimensional array. [2 Marks]
Answer: When an array is partially initialized (e.g., int arr[5] = {1, 2};), the explicitly provided values are assigned to the first few elements, and all remaining unassigned elements are automatically set to zero by the compiler.
10. How do character arrays differ from regular integer arrays in terms of input and output operations? [3 Marks]
Answer: While integer arrays require loop-based input/output for each individual element using format specifiers like %d, character arrays configured as strings can be read and written as a single entity using the %s format specifier with scanf and printf.
Section C — 5 Mark Questions
1. Explain the declaration, initialization, and memory representation of one-dimensional arrays in detail. [5 Marks]
Answer:
Introduction
A one-dimensional array is a linear data structure used to store a fixed number of elements of the same data type under a single variable name.
Declaration and Initialization
Declaration specifies the type and size: data_type array_name[size];. Initialization assigns values: int marks[4] = {85, 90, 78, 92};. If the size is omitted during initialization, the compiler allocates enough space for the provided elements.
Memory Representation
Elements are stored contiguously. For instance, if an integer takes 4 bytes, an array int arr[3] at base address 2000 will store arr[0] at 2000, arr[1] at 2004, and arr[2] at 2008.
Advantages
Allows easy traversal using loops and provides direct random access to any element via its index.
2. Discuss two-dimensional arrays comprehensively, covering their declaration, initialization, and memory mapping. [5 Marks]
Answer:
Introduction
Two-dimensional arrays represent tabular data consisting of rows and columns, useful for matrices and grids.
Declaration and Initialization
Declared as data_type name[rows][cols];. Initialized using nested brace notation, such as int grid[2][2] = {{1, 2}, {3, 4}};
Memory Mapping
Although conceptualized as a grid, computers store 2D arrays linearly using row-major order. Row 0 elements are stored first, followed sequentially by Row 1 elements.
Accessing Elements
Two indices are required: one for the row and one for the column, accessed via nested loops (outer loop for rows, inner loop for columns).
Conclusion
2D arrays form the basis for handling multi-dimensional mathematical and tabular computations in C.
3. Explain character arrays and strings in C, including their declaration, initialization, and terminal input/output using scanf and printf. [5 Marks]
Answer:
Introduction
Strings in C are implemented as character arrays terminated by a null character (\0).
Declaration and Initialization
Character arrays can be declared as char str[20]; and initialized either character-by-character or as a string literal like char str[] = "Hello";, which automatically includes the null terminator.
Terminal Input/Output
Strings are read from the terminal using scanf with the %s specifier and printed using printf with %s. Note that scanf does not require the & operator because the array identifier evaluates to the memory address of its first element.
Limitations with scanf
scanf terminates reading at the first whitespace, making it unsuitable for multi-word strings unless restricted or handled appropriately.
Conclusion
Understanding character arrays and string termination is fundamental for text processing in C.
4. Write a complete explanation of how to write a C program to read and print elements of a one-dimensional array using loops, scanf, and printf. [5 Marks]
Answer:
Introduction
Processing arrays requires repetitive operations for storing and retrieving values, typically achieved using for loops combined with standard I/O functions.
Reading Array Elements
To populate an array, a loop runs from index 0 to size-1. Inside the loop, scanf reads individual elements using the address-of operator: scanf("%d", &arr[i]);.
Printing Array Elements
To display the stored elements, another loop iterates through the same index range, outputting each element using printf: printf("%d ", arr[i]);.
Step-by-Step Execution
- Declare an array and a size variable.
- Prompt the user to enter array elements.
- Run a loop to input values into arr[i].
- Run a second loop to output values from arr[i].
Conclusion
This pattern of loop-based traversal is the cornerstone of array manipulation in procedural programming.
5. Compare and contrast one-dimensional and two-dimensional arrays with respect to structure, declaration, memory allocation, and usage. [5 Marks]
Answer:
Introduction
Both 1D and 2D arrays store homogeneous data elements, but they differ in complexity and application structure.
Structural Comparison
| Feature | One-Dimensional Array | Two-Dimensional Array |
|---|---|---|
| Structure | Linear sequence of elements | Tabular grid of rows and columns |
| Declaration | data_type arr[size]; | data_type arr[rows][cols]; |
| Indices Required | One index [i] | Two indices [i][j] |
| Memory Layout | Contiguous 1D block | Contiguous block stored in row-major order |
| Typical Use | Lists, queues, single sequences | Matrices, game boards, tables |
Explanation
While 1D arrays manage linear lists, 2D arrays extend this concept to represent multi-dimensional relationships while still maintaining a flat physical memory structure in hardware.
Conclusion
Choosing between 1D and 2D arrays depends entirely on whether the underlying problem domain is linear or matrix-based.