WorksheetsC Array Basics Quiz
Total questions: 49
Worksheet time: 25mins
How do you declare an integer array of size 5 in C?
int arr;
int arr();
array arr;
int arr;
What is the index of the first element in a C array?
0
1
-1
Depends on compiler
To access the 4th element in an array arr, which syntax is correct?
arr
arr
arr(3)
arr
What will sizeof(arr) return if int arr;?
10
40 (assuming 4 bytes per int)
Number of elements
Size of pointer
How do you initialize an array with values 1, 2, 3?
int arr[] = {1, 2, 3};
int arr(3) = {1,2,3};
int arr{1,2,3};
int arr = ;
What happens if you access arr in int arr;?
Gives first element
Undefined behavior (out-of-bounds)
Compiler Error
Returns 0
Which loops can be used to iterate through all elements of an array?
for loop
while loop
do-while loop
All of the above
What is the proper way to print all elements of int arr;?
Loop with printf("%d", arr[i]);
printf(arr);
print(arr);
scanf("%d", arr);
How do you declare a two-dimensional array with 3 rows and 4 columns?
int arr;
int arr;
int arr;
int arr(3,4);
First element of 2D array arr is accessed by?
arr
arr
arr
arr
How to find the length of an array in C?
sizeof(arr) / sizeof(arr)
length(arr)
arr.length
Using loop count
Which header file is needed to use printf for arrays?
#include
#include
#include
#include
How do you pass an array to a function?
Just the name of the array: function(arr);
function(&arr);
function(arr[])
Both A and C are valid
How do you declare an array of character with 10 elements?
char arr;
int arr;
char arr();
float arr;
How to initialize a character array with "hello"?
char arr[] = "hello";
char arr[] = {'h','e','l','l','o'};
Both A and B
None of the above
What does arr[i] represent in C?
Pointer to element i
Value at index i
Address of element i
Size of array
What is the default value of uninitialized elements in a local array?
0
Garbage value
NULL
Depends on compiler
How to copy an array in C?
Using loop to copy element-wise
Using strcpy
Assign array directly
Use memcpy or loop
Which of these can index an array?
Integer
Float
String
Character
How to declare an array when size is unknown and will be input?
Use dynamic allocation (malloc)
Fixed length declaration
Use array of size 0
Use pointer without allocation
How do you print an array's address in C?
printf("%p", arr);
printf("%d", arr);
printf(arr);
printf("%s", arr);
Expression arr++ in C?
Increments pointer to next element
Invalid, array name can't be incremented
Moves array start by 1
None of the above
Which keyword defines a constant array?
const
static
volatile
register
How to declare multi-dimensional array dynamically?
Use pointers to pointers
int arr[][];
Fixed declaration
Use #define
Which function helps clear array content?
memset
memcpy
strcpy
free
What is the output of this code snippet? int arr[3] = {1, 2}; printf("%d", arr[2]);
0
2
Garbage
Compilation error
Which is valid index access for int arr;?
arr
arr
arr[-1]
arr
How to initialize all elements to zero while declaring?
int arr = {0};
int arr = 0;
int arr; memset(arr, 0, sizeof(arr));
Both A and C
Size of type int in array depends on?
Compiler and machine architecture
Array size
Number of elements
None of above
How to declare an array and assign values later?
int arr; arr=5;
int arr[];
arr = {1,2,3};
array arr;
What is the result of this code snippet? int arr[3] = {0}; printf("%d", arr[1]);
Garbage value
0
Compiler error
Undefined
Which operator can be used as an alternative to array indexing arr[i]?
*arr[i]
*(arr + i)
arr + i
&arr[i]
How do you declare a pointer to an array?
int *ptr;
int (*ptr);
int ptr;
int ptr();
What will happen if the array is passed to a function and modified inside it?
Changes reflect in the original array
Changes do not reflect
Compiler error
Undefined behavior
How to correctly scan input into an array of size 5?
Using loop with scanf("%d", &arr[i]);
scanf("%d", arr);
scanf("%d %d %d %d %d", arr);
None
How to declare a global array?
Outside all functions
Inside main()
Inside a function with static
Both A and C
How can you increase the size of an array after declaration?
Use realloc() with dynamic arrays
Increase size directly
Use malloc() again
Not possible in static arrays
Initializing an integer array all elements to 5 directly?
int arr = {5};
Using a loop
memset(arr, 5, sizeof(arr));
None of the above
What is the role of #define SIZE 10 in arrays?
Defines size constant
Declares array
Sets initial values
None
Which header is required for memory allocation functions for dynamic arrays?
#include
#include
#include
#include
How do you define an array inside a structure?
struct S { int arr[5]; };
struct S arr;
int arr in struct
None
What is an array initializer list?
List of values used when declaring array
Loop for array input
Function call
Pointer to array
The expression *(arr + 2) is?
Access 2nd element (index 2)
Access 1st element
Compiler error
Address of element 2
What happens to array elements if declared static inside a function?
Retains value between calls
Resets each time
Garbage values
Compiler error
Which function can be used to copy arrays?
memcpy()
strcpy()
malloc()
free()
How do you declare an array of 10 floats?
float arr;
int arr;
float arr();
double arr;
Can you assign a new array to an existing array variable?
No
Yes, with assignment
Yes, using pointers
Both B and C
What is the output of the code? int arr[4] = {1,2,3,4}; printf("%d", *arr);
1
Address of arr
4
Garbage
Why do arrays decay to pointers in C?
To pass arrays to functions easily
For arithmetic
To save memory
None
