1. What is a string in C?
Answer:
Explanation:
In C, a string is a collection of characters terminated by a null character '\0'.
2. How is a string typically represented in C?
Answer:
Explanation:
Strings in C are typically represented as arrays of characters, ending with a null character '\0'.
3. What is the null character in C?
Answer:
Explanation:
The null character in C is '\0', which is used to terminate strings.
4. Which of the following correctly initializes a string in C?
Answer:
Explanation:
All of these are correct ways to initialize a string in C.
5. How do you find the length of a string in C?
Answer:
Explanation:
The strlen() function, defined in the string.h library, is used to find the length of a string in C (not including the null character).
6. What is the output of the following C code?
char str[] = "Hello";
printf("%s", str);
Answer:
Explanation:
The string "Hello" is correctly initialized and terminated with a null character, so it prints "Hello".
7. Which function is used to concatenate two strings in C?
Answer:
Explanation:
The strcat() function, defined in the string.h library, concatenates two strings.
8. What is the return type of the strlen() function in C?
Answer:
Explanation:
The strlen() function returns the length of the string as a size_t type.
9. Which of the following is a correct way to declare a string in C?
Answer:
Explanation:
All these declarations are correct ways to declare a string in C.
10. How do you compare two strings in C?
Answer:
Explanation:
The strcmp() function, defined in the string.h library, is used to compare two strings.
11. Which function is used to copy one string into another in C?
Answer:
Explanation:
The strcpy() function is used to copy the content of one string into another.
12. What does the strcpy() function return in C?
Answer:
Explanation:
The strcpy() function returns a pointer to the destination string.
13. How is a string literal stored in C?
Answer:
Explanation:
String literals in C are stored in the data segment of the program.
14. What happens if a string array is initialized with fewer characters than its size in C?
Answer:
Explanation:
If a string array is initialized with fewer characters than its size, the rest of the array is automatically filled with null characters.
15. Can strings in C be modified if they are initialized with a string literal?
Answer:
Explanation:
Strings initialized with string literals are stored in the read-only part of the data segment, so they cannot be modified.
16. What is the output of the following C code?
char str1[6] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
Answer:
Explanation:
strcat() causes a buffer overflow since str1 does not have enough space to hold both "Hello" and "World".
17. Which of the following correctly initializes an empty string in C?
Answer:
Explanation:
Both a) and c) correctly initialize an empty string in C, which is just a null character.
18. What is string interpolation in C?
Answer:
Explanation:
C does not support string interpolation natively. It is usually achieved using functions like sprintf().
19. What is the output of the following C code?
char str[20];
strcpy(str, "Hello");
strcat(str, " World");
printf("%s", str);
Answer:
Explanation:
The string "Hello" is copied to str, and then " World" is concatenated, resulting in "Hello World".
20. Which header file must be included to use string functions like strcpy() and strcat() in C?
Answer:
Explanation:
The <string.h> header file contains declarations for common string handling functions like strcpy() and strcat().
Comments
Post a Comment
Leave Comment