0% found this document useful (0 votes)
21 views20 pages

21 String Handling Functions MCQ

The document contains a series of C programming questions and answers related to string handling functions, including string manipulation, concatenation, and comparison. It explains the output of various code snippets and provides correct answers along with explanations for each question. The document also covers the usage of specific functions like strcpy, strcat, strlen, and strcmp.

Uploaded by

sv2938394
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

21 String Handling Functions MCQ

The document contains a series of C programming questions and answers related to string handling functions, including string manipulation, concatenation, and comparison. It explains the output of various code snippets and provides correct answers along with explanations for each question. The document also covers the usage of specific functions like strcpy, strcat, strlen, and strcmp.

Uploaded by

sv2938394
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String Handling Functions

What will be the output?


#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "REC", str2[20] = "RIT";
printf("%s", strcpy(str1, strcat(str2, str1)));
return 0;
}

A. RECRIT
B. RITREC
C. RECREC
D. RITRIT

ANSWER: B

str1=REC, str2=RIT. After strcat(str2, str1), str2= RITREC. And strcpy makes str1= RITREC
What will be the output?
#include<stdio.h>
#include<string.h>
int main()
{
char p[] = "assignment";
char t;
int i, j;
for (i = 0, j = strlen(p); i < j; i++)
{
t = p[i];
p[i] = p[j - i];
p[j - i] = t;
}
printf("%s", p);
return 0;
}

A. assignment
B. tnemngissa
C. nothing will be printed
D. tttttttttt

ANSWER: C

nothing will be printed as the string termination character ‘\0’ is assigned to first element of
array p[].

2 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the following program?
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0;
char c;
char str[] = "i love c";
while (str[i] != ' ')
{
putchar(toupper(str[i]));
i++;
}
return 0;
}

A. I LOVE C
B. I love c
C. I
D. I Love C

ANSWER: C

In the while loop the condition is checked whether the character is ‘ ’ (i.e. space). Since after ‘i’
there is a space in “i love c" so the program flow exits the loop and hence only I is printed [ i is
made capital by the toupper function()]

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 3


What will be the output?
#include <stdio.h>
#include <string.h>
int main()
{
char p[] = "string";
char t;
int i, j;
for (i = 0, j = strlen(p); i < j; i++)
{
t = p[i];
p[i] = p[j - i];
p[j - i] = t;
}
printf("%s", p);
return 0;
}

A. string
B. gnirts
C. no output
D. compilation error

ANSWER: C

In the for loop, while swapping the last character i.e. ‘\0’ is made the first character. Hence,
nothing will be printed.

4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output?
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "hello", str2[20] = " world";
printf("%s", strcpy(str2, strcat(str1, str2)));
return 0;
}

A. hello
B. world
C. world hello
D. hello world

ANSWER: D

str1=hello, str2=world.
After strcat(str1,str2), str1=hello world. And strcpy makes str2=hello world.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 5


What will be the correct output of the following program?
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "C EXAMINATION", rev[17];
int i = strlen(str), j = 0;
for (; i >= 0; rev[j++] = str[i--])
rev[j] = str[j];
puts(rev);
return 0;
}

A. NOITANIMAXE C
B. NOITANIMAXE
C. C
D. No output at all
E. Syntax error
ANSWER: D

Explanation:
As i is with the length of string and hence in array rev the contents are copied as "\0
NOITANIMAXE C". But while displaying those contents with puts function it stops because of
the first character '\0' and hence no output.

6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


String concatenation means _____.
A. Combining two strings.
B. Extracting a substring out of a string.
C. Partitioning the string into two strings.
D. Merging two strings.
E. Comparing the two strings to define the larger one.

ANSWER: A

Explanation:
Concatenation means combining two strings together (appending the second at the end of first
one).

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 7


What is the return value of the following statement if it is placed in C program?
strcmp("ABC", "ABC");
A. 33
B. -1
C. 1
D. 0
E. Compilation Error
ANSWER: D

Explanation:
strcmp(s1, s2);
returns int as follows:
== 0, when s1 == s2
< 0, when s1 < s2
> 0, when s1 > s2
int a[5] = {1,2,3}

8 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


Which of the following function sets first n characters of a string to a given character?
A. strinit()
B. strnset()
C. strset()
D. strcset()
ANSWER: B

Explanation:
Declaration:
char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';

printf("string before strnset: %s\n", string);


strnset(string, letter, 13);
printf("string after strnset: %s\n", string);

return 0;
}

Output:
string before strnset: abcdefghijklmnopqrstuvwxyz
string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 9


If the two strings are identical, then strcmp() function returns _____.
A. -1
B. 1
C. 0
D. Yes
ANSWER: C

Explanation:
Declaration: strcmp(const char *s1, const char*s2);
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

10 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


How will you print \n on the screen?
A. printf("\n");
B. echo "\\n";
C. printf('\n');
D. printf("\\n");
ANSWER: D

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

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 11


The library function used to find the last occurrence of a character in a string is _____.
A. strnstr()
B. laststr()
C. strrchr()
D. strstr()
ANSWER: C

Explanation:
Declaration: char *strrchr(const char *s, int c);
It scans a string s in the reverse direction, looking for a specific character c.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char text[] = "I learn through www.rajalakshmi.org";
char *ptr, c = 'i';
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 'i' is: 30

12 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


Which of the following function is used to find the first occurrence of a given string in another
string?
A. strchr()
B. strrchr()
C. strstr()
D. strnset()
ANSWER: C

Explanation:
The function strstr() Finds the first occurrence of a substring in another string
Declaration: char *strstr(const char *s1, const char *s2);
Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
On error (if s2 does not occur in s1), strstr returns null.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char *str1 = "IndiaREC", *str2 = "ia", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %s\n", ptr);
return 0;
}
Output: The substring is: iaREC

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 13


Which of the following function is more appropriate for reading in a multi-word string?
A. printf();
B. scanf();
C. gets();
D. puts();
ANSWER: C

Explanation:
gets(); collects a string of characters terminated by a new line from the standard input stream
stdin
#include <stdio.h>
#include <string.h>
int main()
{
char string[80];
printf("Enter a string:");
gets(string);
printf("The string input was: %s\n", string);
return 0;
}Output:
Enter a string: IndiaREC
The string input was: IndiaREC

14 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


Which of the following function is correct that finds the length of a string?
A. int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
{ length++; s++; }
return (length);
}
B. int xstrlen(char s)
{
int length=0;
while(*s!='\0')
length++; s++;
return (length);
}
C. int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
length++;
return (length);
}
D. int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
s++;
return (length);
}
ANSWER: A
Explanation:
Option A is the correct function to find the length of given string.
Example:
#include <stdio.h>
int xstrlen(char *s)
{
int length = 0;
while (*s != '\0')
{
length++;
s++;
}
return (length);
}
int main()
{
char d[] = "IndiaREC";
printf("Length = %d\n", xstrlen(d));
return 0;
}
Output: Length = 8

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 15


What will be the output of the program?
#include <stdio.h>
#include <string.h>
int main()
{
printf("%d", strlen("123456"));
return 0;
}

A. 6
B. 12
C. 7
D. 2
ANSWER: A

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".

16 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


What will be the output of the program?
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "India\0\REC\0";
printf("%d", strlen(str));
return 0;
}
A. 10
B. 6
C. 5
D. 11
ANSWER: C

Explanation:
The function strlen returns the number of characters int the given string.
Therefore, strlen(str) becomes strlen("India") contains 5 characters. A string is a collection of
characters terminated by '\0'.
The output of the program is "5".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 17


What will be the output of the program?
#include <stdio.h>
#include <string.h>
int main()
{
char sentence[80];
int i;
printf("Enter a line of text\n");
gets(sentence);
for (i = strlen(sentence) - 1; i >= 0; i--)
putchar(sentence[i]);
return 0;
}
A. The sentence will get printed in same order as it entered
B. The sentence will get printed in reverse order
C. Half of the sentence will get printed
D. None of the mentioned

ANSWER: B

18 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


Which of the following statements are correct?
1: A string is a collection of characters terminated by '\0'.
2: The format specifier %s is used to print a string.
3: The length of the string can be obtained by strlen().
4: The pointer CANNOT work on string.
A. 1, 2
B. 1, 2, 3
C. 2, 4
D. 3, 4

ANSWER: B

Explanation:
Clearly, we know first three statements are correct, but fourth statement is wrong. because
we can use pointer on strings. Eg. char *p = "IndiaREC".

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 19


Which of the following statement is correct?
A. strcmp(s1, s2) returns a number less than 0 if s1>s2
B. strcmp(s1, s2) returns a number greater than 0 if s1<s2
C. strcmp(s1, s2) returns 0 if s1==s2
D. 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.

20 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College

You might also like