C String Quiz - MCQ Questions and Answers

1. What is a string in C?

a) A collection of characters
b) A single character
c) An integer data type
d) A special function

Answer:

a) A collection of characters

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?

a) As an array of integers
b) As an array of characters
c) As a single character variable
d) As a structure

Answer:

b) As an array of characters

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?

a) '0'
b) '\0'
c) 'NULL'
d) 0

Answer:

b) '\0'

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?

a) char str[] = "Hello, World!";
b) char str[50] = "Hello, World!";
c) char *str = "Hello, World!";
d) All of the above

Answer:

d) All of the above

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?

a) Using the length() function
b) Using the sizeof operator
c) Using the strlen() function
d) By manually counting the characters

Answer:

c) Using the strlen() function

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);
a) Hello
b) Hello followed by garbage values
c) An error message
d) Undefined behavior

Answer:

a) Hello

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?

a) strcat()
b) strcon()
c) stradd()
d) stringcat()

Answer:

a) strcat()

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?

a) int
b) size_t
c) long
d) char*

Answer:

b) size_t

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?

a) char str[5] = "Hello";
b) char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
c) char *str = "Hello";
d) All of the above

Answer:

d) All of the above

Explanation:

All these declarations are correct ways to declare a string in C.

10. How do you compare two strings in C?

a) Using the == operator
b) Using the strcmp() function
c) Using the compare() function
d) By manually comparing each character

Answer:

b) Using the strcmp() function

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?

a) strcopy()
b) strcpy()
c) strpaste()
d) copystr()

Answer:

b) strcpy()

Explanation:

The strcpy() function is used to copy the content of one string into another.

12. What does the strcpy() function return in C?

a) The number of characters copied
b) The destination string
c) A pointer to the destination string
d) NULL

Answer:

c) A pointer to the destination string

Explanation:

The strcpy() function returns a pointer to the destination string.

13. How is a string literal stored in C?

a) In the stack
b) In the heap
c) In the data segment
d) In the code segment

Answer:

c) In the data segment

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?

a) The remaining elements are filled with null characters
b) The remaining elements are left uninitialized
c) It causes an error
d) The program crashes

Answer:

a) The remaining elements are filled with null characters

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?

a) Yes, always
b) No, never
c) Only if they are not constant
d) Only in some compilers

Answer:

b) No, never

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);
a) HelloWorld
b) Hello World
c) Hello followed by undefined characters
d) Buffer overflow

Answer:

d) Buffer overflow

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?

a) char str[] = "";
b) char str[] = " ";
c) char str[] = {'\0'};
d) Both a) and c)

Answer:

d) Both a) and c)

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?

a) Combining multiple strings into one
b) Inserting variables into a string
c) Replacing parts of a string
d) There is no such concept in C

Answer:

d) There is no such concept in C

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);
a) Hello
b) Hello World
c) HelloWorld
d) An error

Answer:

b) Hello World

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?

a) #include <stdio.h>
b) #include <string.h>
c) #include <stdlib.h>
d) #include <strings.h>

Answer:

b) #include <string.h>

Explanation:

The <string.h> header file contains declarations for common string handling functions like strcpy() and strcat().

Comments