[go: up one dir, main page]

0% found this document useful (0 votes)
10 views42 pages

Arraya

The document provides a quiz on arrays in C programming, covering topics such as declaration, indexing, initialization, and memory management. It includes multiple-choice questions with correct answers and explanations for each. Key concepts discussed include array size, pointer relationships, and the implications of accessing array elements out of bounds.

Uploaded by

selvarani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views42 pages

Arraya

The document provides a quiz on arrays in C programming, covering topics such as declaration, indexing, initialization, and memory management. It includes multiple-choice questions with correct answers and explanations for each. Key concepts discussed include array size, pointer relationships, and the implications of accessing array elements out of bounds.

Uploaded by

selvarani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

arraya

1. What is the correct way to declare an array of 10 integers in C?

A. int arr;
B. int arr[10];
C. array int arr[10];
D. int arr(10);
✅ Correct Answer:

B. int arr[10];

💡 Explanation:

In C, arrays are declared by specifying the type, the array name, and the size in square
brackets. For example, 'int arr[10];' declares an integer array with 10 elements.
2. How are elements in a C array indexed?

A. From 1 to n
B. From -1 to n-1
C. From 0 to n-1
D. From n to 0
✅ Correct Answer:

C. From 0 to n-1

💡 Explanation:

Array indexing in C starts at 0, so the first element is at index 0, the second at 1, and so
on. This means the last element of an array of size n is at index n-1.
3. What will happen if you try to access an array element outside its
declared size?

A. The compiler will give an error


B. The program will automatically resize the array
C. The accessed element will be zero
D. Undefined behavior may occur
✅ Correct Answer:

D. Undefined behavior may occur

💡 Explanation:

Accessing elements outside the array bounds leads to undefined behavior. The program
may crash, produce garbage values, or overwrite other memory.
4. Which of the following correctly initializes an integer array with values 1,
2, and 3?

A. int arr[3] = {1, 2, 3};


B. int arr[3] = (1, 2, 3);
C. int arr = {1, 2, 3};
D. int arr[3] = [1, 2, 3];
✅ Correct Answer:

A. int arr[3] = {1, 2, 3};

💡 Explanation:

In C, you can initialize an array at declaration using curly braces with the values
separated by commas.
5. What is the type of the expression 'arr' when 'arr' is an array name in C?

A. An integer
B. A pointer to the first element
C. An array copy
D. A function pointer
✅ Correct Answer:

B. A pointer to the first element

💡 Explanation:

In most expressions, the name of an array decays to a pointer to its first element, so
'arr' acts like a pointer to the array's element type.
6. How can you find the number of elements in an array 'arr' declared as
'int arr[10];'?

A. sizeof(arr) / sizeof(arr[0])
B. sizeof(arr) * sizeof(arr[0])
C. sizeof(arr[0]) / sizeof(arr)
D. length(arr)
✅ Correct Answer:

A. sizeof(arr) / sizeof(arr[0])

💡 Explanation:

You can use 'sizeof(arr) / sizeof(arr[0])' to get the number of elements because
sizeof(arr) gives total bytes and sizeof(arr[0]) gives size of one element.
7. Which of these statements about arrays in C is true?

A. Arrays can be resized automatically


B. Arrays can hold elements of different types
C. Array size is fixed once declared
D. Array elements are always initialized to zero
✅ Correct Answer:

C. Array size is fixed once declared

💡 Explanation:

Arrays in C have a fixed size determined at compile time for static arrays and cannot be
resized dynamically without pointers and memory management.
8. What is the default value of elements in an uninitialized local array in C?

A. Zero
B. One
C. Null
D. Garbage (undefined) values
✅ Correct Answer:

D. Garbage (undefined) values

💡 Explanation:

Local arrays are not automatically initialized and contain garbage values. Only static or
global arrays are initialized to zero by default.
9. Which of the following correctly accesses the third element of an array
named 'numbers'?

A. numbers[3]
B. numbers[2]
C. numbers(3)
D. numbers{3}
✅ Correct Answer:

B. numbers[2]

💡 Explanation:

Since array indexing starts at 0, the third element is at index 2, so 'numbers[2]' accesses
it.
10. What does the expression '*(arr + i)' mean in C when 'arr' is an array?

A. The address of element i


B. The array size plus i
C. The value of element at index i
D. A syntax error
✅ Correct Answer:

C. The value of element at index i

💡 Explanation:

In C, 'arr[i]' is equivalent to '*(arr + i)', which accesses the element at index i by pointer
arithmetic.
11. Which header file must be included to use the 'sizeof' operator in C?

A. No header required
B. stdio.h
C. stdlib.h
D. string.h
✅ Correct Answer:

A. No header required

💡 Explanation:

'sizeof' is a compile-time operator and does not require any header file to be included.
12. If you declare 'int arr[5] = {1, 2};', what will be the values of the
remaining elements?

A. Garbage values
B. Uninitialized
C. Random values
D. Zero
✅ Correct Answer:

D. Zero

💡 Explanation:

When partially initializing an array, the remaining elements are automatically set to
zero.
13. What is the size of an array declared as 'char arr[20];' on a system
where char is 1 byte?

A. 10 bytes
B. 40 bytes
C. 20 bytes
D. Depends on compiler
✅ Correct Answer:

C. 20 bytes

💡 Explanation:

Each char is 1 byte, so the total size is 20 bytes for 20 elements.


14. Which statement about multi-dimensional arrays in C is correct?

A. They can only have two dimensions


B. They are arrays containing other arrays
C. They must be initialized with zeros
D. They are pointers to pointers
✅ Correct Answer:

B. They are arrays containing other arrays

💡 Explanation:

Multi-dimensional arrays are arrays of arrays. For example, a 2D array is declared as 'int
arr[3][4];'.
15. How do you access the element in the second row and third column of
a 2D array 'arr' declared as 'int arr[3][4];'?

A. arr[1][2]
B. arr[2][3]
C. arr[3][2]
D. arr[2][1]
✅ Correct Answer:

A. arr[1][2]

💡 Explanation:

Indexing starts at 0, so second row is index 1 and third column is index 2, accessed by
arr[1][2].
16. What happens to the memory layout of an array when you pass it to a
function in C?

A. A complete copy of the array is passed to the function


B. A pointer to the first element of the array is passed
C. Only the first element of the array is passed
D. The array size is passed instead of the array itself
✅ Correct Answer:

B. A pointer to the first element of the array is passed

💡 Explanation:

When an array is passed to a function, what is actually passed is a pointer to its first element, not
the entire array copy. This means the function can access and modify the original array elements
directly.
17. Why is it important to specify the size of an array when declaring it in
C?

A. Because it defines the type of elements the array holds


B. Because it tells the compiler how much memory to allocate
C. Because it initializes all elements to zero
D. Because it allows the array to change size dynamically
✅ Correct Answer:

B. Because it tells the compiler how much memory to allocate

💡 Explanation:

In C, the size of the array determines how much memory is allocated for it. Without specifying the
size, the compiler cannot reserve the correct amount of memory. This is essential for indexing and
accessing elements safely. Other options may confuse the compiler or lead to undefined behavior.
18. Explain how arrays and pointers are related in C.
✅ Suggested Answer:

In C, the name of an array acts as a pointer to its first element.


You can use pointer arithmetic to access array elements.
However, arrays and pointers are not the same; arrays have fixed size, pointers do not.
Array elements are stored contiguously in memory, allowing pointers to traverse
them.
19. What is a key difference between an array and a pointer in C?

A. Arrays can be resized during runtime, but pointers cannot


B. Array names are fixed addresses, pointers can be reassigned
C. Pointers always allocate memory automatically, arrays do not
D. Pointers cannot be used to access array elements
✅ Correct Answer:

B. Array names are fixed addresses, pointers can be reassigned

💡 Explanation:

An array name acts as a constant pointer to its first element but cannot be reassigned, whereas a pointer variable can be changed
to point elsewhere.

Arrays allocate a fixed block of memory, while pointers can be reassigned to different memory locations.

This distinction affects how they can be used and manipulated in code.
20. What is a potential risk when using arrays without proper bounds
checking in C?

A. The array elements will automatically resize to fit new data


B. Undefined behavior such as memory corruption or crashes
C. The compiler will generate an error and stop compiling
D. The array will ignore invalid indexes and return zero
✅ Correct Answer:

B. Undefined behavior such as memory corruption or crashes

💡 Explanation:

Accessing elements outside the array bounds can lead to undefined behavior, including memory
corruption or program crashes. C does not perform automatic bounds checking, so it's the
programmer's responsibility to ensure indexes are valid.
https://app.edcafe.ai/share/quiz/arraya-68cc310cb9f9ed8ab157ed83

You might also like