Showing 91–100 of 100 questions
in Arrays & Strings
What will be printed by: printf("%s", "Hello\0World");?
A
HelloWorld
B
Hello
C
World
D
Hello\0World
EXPLANATION
\0 is the null terminator which marks the end of a string. %s stops printing at the first null character.
What is the output of the following code?
int arr[] = {10, 20, 30};
printf("%d", sizeof(arr)/sizeof(arr[0]));
EXPLANATION
sizeof(arr) gives total bytes of array. sizeof(arr[0]) gives bytes of one element. Division gives number of elements: 3.
Which function is used to reverse a string in C library?
A
reverse()
B
strrev()
C
String reversal is not in standard C library
D
revstr()
Correct Answer:
C. String reversal is not in standard C library
EXPLANATION
There is no standard library function for string reversal. strrev() is non-standard (available in some compilers). Manual reversal or custom functions are needed.
What is the difference between char str[] = "Test" and char *str = "Test"?
A
No difference
B
First is array, second is pointer; first is modifiable, second is read-only
C
First is pointer, second is array
D
First causes error, second is valid
Correct Answer:
B. First is array, second is pointer; first is modifiable, second is read-only
EXPLANATION
char[] creates a modifiable array with its own storage. char* points to read-only string literal in memory.
What will be the output of the following code?
char str[] = "Code";
printf("%c", str[2]);
EXPLANATION
String "Code" has indices: C(0), o(1), d(2), e(3). str[2] refers to 'd'.
Consider a 2D array declared as: int matrix[3][4]. How many elements does it have?
EXPLANATION
A 2D array with dimensions 3x4 has 3*4 = 12 total elements.
What does the following code do? strcpy(dest, src);
A
Compares two strings
B
Copies string src to dest
C
Concatenates two strings
D
Finds length of string
Correct Answer:
B. Copies string src to dest
EXPLANATION
strcpy() copies the contents of the source string to the destination string.
Which of the following is NOT a valid string initialization?
A
char str[] = "Program";
B
char str[10] = {'P','r','o','g'};
C
char str[10]; str = "Program";
D
char *str = "Program";
Correct Answer:
C. char str[10]; str = "Program";
EXPLANATION
Array names are constant pointers and cannot be reassigned. Direct assignment to char array after declaration is invalid.
What will be the output of strlen("Hello") in C?
EXPLANATION
strlen() returns the length of the string excluding the null terminator '\0'. "Hello" has 5 characters.
What is the correct way to declare a 1D array of 10 integers in C?
A
int arr[10];
B
int arr(10);
C
int arr{10};
D
int [10]arr;
Correct Answer:
A. int arr[10];
EXPLANATION
In C, arrays are declared with the syntax: datatype arrayname[size]. Option A follows the correct syntax.