Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

197 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 111–120 of 197
Topics in C Programming
Q.111 Hard Arrays & Strings
What is the relationship between pointer arithmetic and array indexing in C?
A Pointer arithmetic is faster than array indexing
B arr[i] is equivalent to *(arr + i) - both are mathematically identical
C Array indexing is only for static arrays, pointer arithmetic for dynamic
D No relationship - they are completely different operations
Correct Answer:  B. arr[i] is equivalent to *(arr + i) - both are mathematically identical
EXPLANATION

C standard defines arr[i] as *(arr + i). Both notations access the same memory location and perform identically.

Test
Q.112 Hard Arrays & Strings
What is the most efficient way to copy one array to another in C?
A memcpy(dest, src, sizeof(src));
B strcpy(dest, src);
C for loop with element-by-element assignment
D Array assignment dest = src;
Correct Answer:  A. memcpy(dest, src, sizeof(src));
EXPLANATION

memcpy() is the most efficient as it performs block memory copy. strcpy() is only for strings, and array assignment doesn't work in C.

Test
Q.113 Hard Arrays & Strings
How should dynamic 1D array of strings be declared for 5 strings of length 20?
A char **str = (char**)malloc(5 * sizeof(char*)); for loop to allocate each string
B char str[5][20];
C char *str[5];
D Both A and C after proper allocation
Correct Answer:  A. char **str = (char**)malloc(5 * sizeof(char*)); for loop to allocate each string
EXPLANATION

For dynamic allocation, use double pointer char** and allocate memory for each string separately. Options B and C are static allocations.

Test
Q.114 Hard Arrays & Strings
What will the following code print?
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d %d", *p++, *++p);
A 10 20
B 10 30
C 20 20
D Undefined behavior
Correct Answer:  D. Undefined behavior
EXPLANATION

This code exhibits undefined behavior due to lack of sequence points between modifications and access of p. Different compilers may produce different results.

Test
Q.115 Hard Arrays & Strings
What happens with this code?
char *ptr;
char str[] = "Programming";
ptr = str;
ptr[2] = 'X';
A Compilation error
B String becomes 'PrXgramming'
C Undefined behavior
D No change to str
Correct Answer:  B. String becomes 'PrXgramming'
EXPLANATION

ptr points to the character array str. Modifying ptr[2] modifies str[2], changing 'o' to 'X', resulting in 'PrXgramming'.

Test
Q.116 Hard Arrays & Strings
What will happen?
char arr[5] = "Hello";
printf("%d", strlen(arr));
A 5
B 6
C Compilation error
D Undefined behavior
Correct Answer:  C. Compilation error
EXPLANATION

Array is too small. "Hello" needs 6 bytes including null terminator, but only 5 are allocated. This causes buffer overflow.

Test
Q.117 Hard Arrays & Strings
In a dynamic 2D array created as: int **arr = (int**)malloc(rows * sizeof(int*)); What is the next step?
A Directly assign values to arr[i][j]
B Allocate memory for each row
C Call free() immediately
D Use strdup() to copy strings
Correct Answer:  B. Allocate memory for each row
EXPLANATION

After allocating the array of pointers, each row must be allocated separately.

Test
Q.118 Hard Arrays & Strings
In the declaration int *arr[10], what is arr?
A Array of 10 pointers to int
B Pointer to array of 10 integers
C Array of 10 integers
D Pointer to 10 pointers
Correct Answer:  A. Array of 10 pointers to int
EXPLANATION

int *arr[10] declares an array of 10 pointers to int (subscript binds tighter than dereference). Use int (*arr)[10] for pointer to array.

Test
Q.119 Hard Arrays & Strings
What is the correct way to safely copy strings in modern C?
A strcpy(dest, src)
B strncpy(dest, src, sizeof(dest)-1); dest[sizeof(dest)-1]='\0';
C sprintf(dest, "%s", src)
D memcpy(dest, src, strlen(src))
Correct Answer:  B. strncpy(dest, src, sizeof(dest)-1); dest[sizeof(dest)-1]='\0';
EXPLANATION

strncpy with buffer size check and explicit null termination is the standard safe approach. Option A lacks bounds checking; C and D have issues with null termination.

Test
Q.120 Hard Arrays & Strings
If ptr is a pointer to int array and ptr = arr, what does ptr[3] represent?
A The 4th element of the array (same as arr[3])
B The address 3 positions ahead in memory
C 12 bytes ahead of array start (assuming 4-byte int)
D Both A and C are true
Correct Answer:  D. Both A and C are true
EXPLANATION

ptr[3] is equivalent to arr[3] and accesses the element at address *(ptr+3). For int*, ptr+3 moves 12 bytes (3×4 bytes) in memory.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips