String Questions with Answers

15 practice questions on String (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Medium

What will be the output of the program?

#include <stdio.h>
#include <string.h>
int main()
{
    char str[] = "COMP\0\RIDER\0";
    printf("%d\n", strlen(str));
    return 0;
}
  1. 12
  2. 5
  3. 4
  4. 15

Answer: C. 4

Explanation

The function strlen returns the number of characters int the given string.
Therefore,

strlen(str) becomes strlen("COMP")

contains 4 characters.
A string is a collection of characters terminated by '\0'.The output of the program is "4".

Step-by-step solution

  1. The string literal is COMP, then a NUL character, then R, I, D, E, R and another NUL.
  2. Strlen counts characters only up to the first NUL, not to the end of the array.
  3. It counts C, O, M and P, which is 4.
  4. The answer is 4.

Why the other options are wrong

  • A. 12 is not the count, because strlen stops at the first NUL.
  • B. 5 would count the NUL, but strlen stops before it.
  • D. 15 is not the count, because strlen stops at the first NUL.

Tip: Strlen stops at the first zero byte, so an embedded zero cuts the string short.

Question 2Medium

What will be the output of the program?

#include <stdio.h>
#include <string.h>
int main()
{
  printf("%d\n", strlen("123456"));
  return 0;
}
  1. 6
  2. 12
  3. 7
  4. 2

Answer: A. 6

Explanation

The function strlen returns the number of characters in the given string.
Therefore, strlen("123456") returns 6.
Hence the output of the program is "6".

Step-by-step solution

  1. Strlen counts characters in a string.
  2. The string '123456' has 6 characters.
  3. Output: 6.

Why the other options are wrong

  • B. Incorrect output. The correct answer is 6.
  • C. Incorrect output. The correct answer is 6.
  • D. Incorrect output. The correct answer is 6.

Question 3Medium

Which of the following statement is correct?

  1. strcmp(s1, s2)

    returns a number less than 0 if s1 > s2

  2. strcmp(s1, s2)

    returns a number greater than 0 if s1 < s2

  3. strcmp(s1, s2)

    returns 0 if s1==s2

  4. strcmp(s1, s2)

    returns 1 if s1==s2

Answer: C.

Explanation

The strcmp return an int value that is if s1 < s2 returns a value < 0
if s1 == s2 returns 0
if s1 > s2 returns a value > 0
From the above statements, that the third statement is only correct.

Step-by-step solution

  1. Strcmp compares strings character by character using character codes.
  2. It returns a negative number when s1 comes before s2, and a positive number when s1 comes after s2.
  3. It returns 0 when the two strings are equal.
  4. The correct statement is: strcmp(s1, s2) returns 0 if s1==s2.

Why the other options are wrong

  • A. A negative result means s1 is less than s2; s1 greater than s2 gives a positive value.
  • B. A positive result means s1 is greater than s2; s1 less than s2 gives a negative value.
  • D. Strcmp returns 0 for equal strings, not 1.

Tip: Strcmp returns 0 when strings match; negative or positive shows the order.

Question 4Medium

How will you print \n on the screen?

  1. printf("\n");
  2. echo "\\n";
  3. printf('\n');
  4. printf("\\n");

Answer: D. printf("\\n");

Explanation

The statement printf("\\n"); prints '\n' on the screen.

Step-by-step solution

  1. Inside a C string, a backslash starts an escape sequence, so a single backslash followed by n prints a newline.
  2. To print a backslash followed by n as text, the backslash itself must be doubled in the string.
  3. The code has a doubled backslash, so it prints one backslash followed by n.
  4. So the answer is printf("\\n");

Why the other options are wrong

  • A. A single backslash followed by n is a newline escape, so it prints a line break and not the text.
  • B. Echo is a shell command, not printf in C, and it uses different quoting.
  • C. Single quotes make a character constant, not a string, so this does not print the text.

Tip: Double a backslash inside a C string to print one backslash.

Question 5Medium

Which of the following statements are correct?

- A. A string is a collection of characters terminated by '\0'.
- B. The format specifier %s is used to print a string.
- C. The length of the string can be obtained by strlen().
- D. The pointer CANNOT work on string.

  1. A, B
  2. A, B, C
  3. B, D
  4. C, D

Answer: B. A, B, C

Explanation

Clearly, we know first three statements are correct, but fourth statement is wrong. because we can use pointer on strings.

Eg. char *p = "CompRider".

Step-by-step solution

  1. A is true: a C string is a char array that ends with the null character.
  2. B is true: the %s format prints a string with printf.
  3. C is true: strlen returns the length of the string without the terminating null character.
  4. D is false: pointers work on strings all the time, as in char *p = a string literal.
  5. So the answer is A, B, C.

Why the other options are wrong

  • A. This leaves out C, which is correct because strlen gives the length.
  • C. This includes D, which is false because pointers do work on strings.
  • D. This includes D, which is false, and leaves out A and B, which are true.

Tip: Pointers work on strings, and strlen counts characters up to the null character.

Question 6Medium

What will be the output of the program?

#include <stdio.h>
int main()
{
    char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
    int i;
    char *t;
    t = names[3];
    names[3] = names[4];
    names[4] = t;
    for(i=0; i<=4; i++)
        printf("%s,", names[i]);
    return 0;
}
  1. Suresh, Siva, Sona, Baiju, Ritu
  2. Suresh, Siva, Sona, Ritu, Baiju
  3. Suresh, Siva, Baiju, Sona, Ritu
  4. Suresh, Siva, Ritu, Sona, Baiju

Answer: B. Suresh, Siva, Sona, Ritu, Baiju

Explanation

Step 1:

char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};

The variable names is declared as an pointer to a array of strings.

Step 2:

int i;

The variable i is declared as an integer type.

Step 3:

char *t;

The variable t is declared as pointer to a string.

Step 4:

t = names[3]; names[3] = names[4]; names[4] = t;

These statements the swaps the 4 and 5 element of the array names.

Step 5:

for(i=0; i<=4; i++) printf("%s,", names[i]);

These statement prints the all the value of the array names.
Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".

Step-by-step solution

  1. The variable names is an array of strings.
  2. The variable i is used as a loop counter.
  3. The variable t is used for swapping elements.
  4. Element at index 3 (Baiju) is swapped with element at index 4 (Ritu).
  5. The loop prints all elements in the array, now in the order: Suresh, Siva, Sona, Ritu, Baiju.
  6. Output: Suresh, Siva, Sona, Ritu, Baiju.

Why the other options are wrong

  • A. Incorrect swap of elements.
  • C. Incorrect order after swap.
  • D. Incorrect order after swap.

Tip: Remember to use a temporary variable for swapping.

Question 7Medium

Which of the following statements are correct about the below declarations?

char *p = "Sanjay";
char a[] = "Sanjay";

1: There is no difference in the declarations and both serve the same purpose.
2: p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to a non-const pointer.
3: The pointer p can be modified to point to another string,whereas the individual characters within array a can be changed.
4: In both cases the '\0' will be added at the end of the string "Sanjay".

  1. 1, 2
  2. 2, 3, 4
  3. 3, 4
  4. 2, 3

Answer: C. 3, 4

Explanation

statement 2 is false because a is an array, not a const pointer; statements 3 and 4 are true

Step-by-step solution

  1. Statement 1 is false: p is a pointer and a is an array, so they differ.
  2. Statement 2 is false: a is an array, not a const pointer.
  3. Statement 3 is true: p can point elsewhere, and characters of a can be changed.
  4. Statement 4 is true: both strings end with the null character. So the correct set is 3, 4, option C.

Why the other options are wrong

  • A. Statements 1 and 2 are both false.
  • B. Statement 2 is false, since a is an array and not a const pointer.
  • D. Statement 2 is false.

Tip: char *p = literal makes a pointer you can repoint; char a[] = literal makes an array you can edit.

Question 8Medium

What will be the output of the program?

#include <stdio.h>
int main()
{
  char str[7] = "comprider";
  printf("%s\n", str);
  return 0;
}
  1. Error
  2. comprider
  3. Cannot predict
  4. None of above

Answer: C. Cannot predict

Explanation

Here str[] has declared as 7 character array and into a 9 character is stored.
This will result in overwriting of the byte beyond 7 byte reserved for '\0'.

Step-by-step solution

  1. Str has size 7, but the literal comprider needs 10 bytes including the null terminator.
  2. The initializer overflows the array, so the terminating null is dropped.
  3. Printf with %s reads until it finds a zero byte, which is not inside str, so it reads past the array.
  4. This is undefined behavior, so the output cannot be predicted.
  5. The answer is Cannot predict.

Why the other options are wrong

  • A. C accepts this declaration, so it is not a compile error; the problem is undefined behavior at run time.
  • B. The output is not the clean word comprider, because the string has no terminator.
  • D. Option C is correct, so none of the above is wrong.

Tip: A char array needs room for the terminating null character.

Question 9Medium

Which of the following statements are correct about the program below?

int main()
{
  char str[20], *s;
  printf("Enter a string\n");
  scanf("%s", str);
  s=str;
  while(*s != '\0')
  {
    if((*s >= 97 && *s <= 122))
     *s = *s-32;
      s++;
  }
    printf("%s",str);
    return 0;
}
  1. The code converts a string in to an integer
  2. The code converts lower case character to
  3. The code converts upper case character to lower ca
  4. Error in code

Answer: B. The code converts lower case character to

Explanation

This program converts the given string to upper case string.
Output:
Enter a string: comprider
COMPRIDER

Step-by-step solution

  1. The while loop runs through each character until the terminating null.
  2. If a character is between 97 and 122, which is a to z, the code subtracts 32.
  3. Subtracting 32 turns a lower case letter into its upper case letter, so a at 97 becomes A at 65.
  4. So the answer is The code converts lower case character to upper case.

Why the other options are wrong

  • A. No integer is made, because the characters are only changed in place.
  • C. The code goes the other way, so it does not convert upper case to lower case.
  • D. The code compiles and runs without an error.

Tip: ASCII codes 97 to 122 are the lower case letters, and 32 is the gap to upper case.

Question 10Medium

The library function used to find the last occurrence of a character in a string is

  1. strnstr()
  2. laststr()
  3. strrchr()
  4. strstr()

Answer: C. strrchr()

Explanation

Declaration:

char *strrchr(const char *s, int c);

It scans a string s in the reverse direction, looking for a specific character c.

#include <string.h>
#include <stdio.h>

int main(void)
{
   char text[] = "CompRider";
   char *ptr, c = 'r';

   ptr = strrchr(text, c);
   if (ptr)
      printf("The position of '%c' is: %d\n", c, ptr-text);
   else
      printf("The character was not found\n");
   return 0;
}
Output:

The position of 'r' is: 8

Step-by-step solution

  1. strnstr() finds the first occurrence of a substring.
  2. laststr() does not exist in C.
  3. strrchr() finds the last occurrence of a character.
  4. strstr() finds the first occurrence of a substring.

Why the other options are wrong

  • A. It searches for a substring, not a single character.
  • B. This function does not exist in C.

Tip: Remember: strrchr() for last char, strstr() for substring.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics