C String MCQ

Strings are an essential part of most programming languages, including C. In C, strings are essentially arrays of characters, ending with the null character \0. Let’s see how well you know the basics of C strings with this multiple choice quiz.

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. How is the string “hello” represented in memory?

A) ‘h’, ‘e’, ‘l’, ‘l’, ‘o’
B) ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\n’
C) ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’
D) ‘hello’

Answer:

C) ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’

Explanation:

Strings in C are terminated with a null character to mark the end.

2. What will be the size of the string “CProgramming” in memory?

A) 12
B) 13
C) 11
D) 10

Answer:

B) 13

Explanation:

The size includes all characters plus the null terminator.

3. Which of the following functions is used to compare two strings in C?

A) cmpstr()
B) strcomp()
C) strcmp()
D) compare()

Answer:

C) strcmp()

Explanation:

The strcmp() function from the string.h library is used to compare two strings.

4. Which of the following functions can be used to find the length of a string?

A) strlen()
B) strlength()
C) length()
D) size()

Answer:

A) strlen()

Explanation:

The strlen() function from the string.h library returns the length of the string excluding the null terminator.

5. Which function is used to concatenate two strings?

A) strconcat()
B) strcat()
C) stradd()
D) strapp()

Answer:

B) strcat()

Explanation:

The strcat() function from the string.h library is used to concatenate two strings.

6. What does the strcpy() function do?

A) Compares two strings
B) Counts characters in a string
C) Copies one string to another
D) Splits a string

Answer:

C) Copies one string to another

Explanation:

The strcpy() function copies the content of one string into another.

7. Which of the following will correctly initialize a string with the content “world”?

A) char str[] = “world”;
B) char str[5] = “world”;
C) char *str = “world”;
D) Both A and C

Answer:

D) Both A and C

Explanation:

Both these methods correctly initialize a string. Option B is incorrect because it doesn’t account for the null terminator.

8. In C, which escape sequence represents the null character?

A) \n
B) \0
C) \null
D) \t

Answer:

B) \0

Explanation:

In C, the null character is represented by the escape sequence \0.

9. Which function can be used to convert a string to uppercase?

a) strupr()
b) strtoupper()
c) toupper()
d) to_upper()

Answer:

a) strupr()

Explanation:

The strupr() function converts a string to uppercase.

10. Which of the following functions reverses a string?

a) revstr()
b) strrev()
c) reverse()
d) strreverse()

Answer:

b) strrev()

Explanation:

The strrev() function from the string.h library reverses the string in place.

11. How can you initialize an empty string in C?

a) char str[] = ” “;
b) char str[] = “\0”;
c) char str[] = “”;
d) char str[0];

Answer:

c) char str[] = “”;

Explanation:

An empty string in C can be initialized using empty double quotes. It contains only the null character.

12. Which function will convert the string “123” to an integer?

a) toInt(“123”);
b) atoi(“123”);
c) str2int(“123”);
d) convert(“123”);

Answer:

b) atoi(“123”);

Explanation:

The atoi() function (from the stdlib.h library) converts a string to an integer.

13. Given char str[] = “Hello, World!”;, how do you get the character ‘W’ using the array indexing?

a) str[6]
b) str[7]
c) str[8]
d) str[9]

Answer:

d) str[9]

Explanation:

Strings in C are zero-indexed, so the character ‘W’ is located at index 9 in the given string.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top