Home Subjects C Programming Arrays & Strings

C Programming
Arrays & Strings

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 51–60 of 100
Topics in C Programming
Q.51 Medium Arrays & Strings
Consider: char str[10]; strcpy(str, "Hello"); What is the state of memory after this?
A str[5] contains '\0', str[6] is uninitialized
B All elements from str[0] to str[9] contain characters
C str[0] to str[4] have 'H','e','l','l','o', str[5] has '\0'
D Runtime error occurs
Correct Answer:  C. str[0] to str[4] have 'H','e','l','l','o', str[5] has '\0'
EXPLANATION

strcpy copies "Hello" and adds null terminator at position 5. Positions 6-9 remain uninitialized.

Take Test
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.

Take Test
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.

Take Test
Q.54 Medium Arrays & Strings
What does the following code do?
char str[] = "HELLO";
for(int i = 0; str[i]; i++) str[i] = str[i] + 32;
A Converts uppercase to lowercase
B Converts lowercase to uppercase
C Shifts ASCII values by 32
D Causes compilation error
Correct Answer:  A. Converts uppercase to lowercase
EXPLANATION

Adding 32 to uppercase ASCII values converts them to lowercase (A=65, a=97, difference=32).

Take Test
Predict the output:
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("%d", arr[1][2]);
A 5
B 6
C 8
D 9
Correct Answer:  B. 6
EXPLANATION

arr[1][2] refers to row 1, column 2, which contains 6.

Take Test
Q.56 Medium Arrays & Strings
What is the correct syntax to concatenate two strings safely in modern C?
A strcat(dest, src)
B strncat(dest, src, n)
C sprintf(dest, "%s%s", dest, src)
D Both B and C are safer options
Correct Answer:  D. Both B and C are safer options
EXPLANATION

strcat() is unsafe due to buffer overflow risk. strncat() and sprintf() provide bounds checking.

Take Test
Q.57 Medium Arrays & Strings
Which of the following will correctly compare two strings?
A if (str1 == str2)
B if (strcmp(str1, str2) == 0)
C Both A and B
D Neither A nor B
Correct Answer:  B. if (strcmp(str1, str2) == 0)
EXPLANATION

A compares pointers, not string content. strcmp() compares actual string values character by character.

Take Test
What will be printed?
char *str = "Hello";
printf("%c", str[1]);
A H
B e
C l
D Compilation error
Correct Answer:  B. e
EXPLANATION

str[1] accesses the second character of the string, which is 'e'.

Take Test
Q.59 Medium Arrays & Strings
Consider the code: int *p; int arr[5] = {10, 20, 30, 40, 50}; p = arr; What is *(p+2)?
A 20
B 30
C 40
D 50
Correct Answer:  B. 30
EXPLANATION

p points to arr[0]. p+2 points to arr[2], which contains 30.

Take Test
Q.60 Medium Arrays & Strings
What will be the output?
char arr[] = "ABC";
printf("%d", sizeof(arr));
A 3
B 4
C Depends on compiler
D Runtime error
Correct Answer:  B. 4
EXPLANATION

sizeof includes the null terminator. "ABC" has 4 bytes (A, B, C, \0).

Take Test
IGET
iget AI
Online · Ask anything about exams
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