Strings - Point Out Correct Statements Multiple Questions and Answers.
Exercise Questions ::
Strings
1. | Which of the following statements are correct about the program below? |
|
#include<stdio.h>
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;
}
|
| A. | The code converts a string in to an integer |
| B. | The code converts lower case character to upper case |
| C. | The code converts upper case character to lower case |
| D. | Error in code |
|
|
| Answer: Option B |
|
|
| Explanation: |
|
This program converts the given string to upper case string.
Output:
Enter a string: pakmcqs
PAKMCQS |
| See More Information |
|
|
|
Tutorial Link: |
Published by:Michael Daani
2. | Which of the following statements are correct ? |
| A string is a collection of characters terminated by '\0'.
The format specifier %s is used to print a string.
The length of the string can be obtained by strlen().
The pointer CANNOT work on string. |
| A. | 1, 2 |
| B. | 1, 2, 3 |
| C. | 2, 4 |
| D. | 3, 4 |
|
|
| Answer: Option 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 = "PakMcqs". |
| See More Information |
|
|
|
Tutorial Link: |
Published by:Michael Daani
3. | 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 |
| C. | strcmp(s1, s2) returns 0 if s1==s2 |
| D. | strcmp(s1, s2) returns 1 if s1==s2 |
|
|
| Answer: Option 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. |
| See More Information |
|
|
|
Tutorial Link: |
Published by:Michael Daani