Strings - Find Output of Program Multiple Questions and Answers.
Exercise Questions ::
Strings
26. What will be the output of the program?
#include<stdio.h>
#include<string.h>
int main()
{
char str1[5 ], str2[5 ];
int i;
gets(str1);
gets(str2);
i = strcmp(str1, str2);
printf("%d\n" , i);
return 0 ;
}
A. 0 B. -1 C. Error D. Unpredictable integer value
View Answer
Discuss forum
Workplace
Report
Answer: Option D
Explanation:
gets() gets collects a string of characters terminated by a new line from the standard input stream stdin .
The gets(str1) read the input string from user and store in variable str1 .
The gets(str2) read the input string from user and store in variable str2 .
The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other negative or positive values. So the value of i is "unpredictable integer value".
printf("%d\n", i); It prints the value of variable i .
See More Information
Tutorial Link:
Published by:Michael Daani
27. What will be the output of the program in Turbo C?
#include<stdio.h>
int main()
{
char str[10 ] = "Pak" ;
str[6 ] = "MCQS" ;
printf("%s\n" , str);
return 0 ;
}
A. PakMCQS B. MCQS C. PAK D. Error
View Answer
Discuss forum
Workplace
Report
Answer: Option D
Explanation:
str[6] = "MCQS"; - Nonportable pointer conversion.
See More Information
Tutorial Link:
Published by:Michael Daani
28. What will be the output of the program?
#include<stdio.h>
int main()
{
char str1[] = "Hello" ;
char str2[] = "Hello" ;
if (str1 == str2)
printf("Equal\n" );
else
printf("Unequal\n" );
return 0 ;
}
A. Equal B. Unequal C. Error D. None of above
View Answer
Discuss forum
Workplace
Report
Answer: Option B
Explanation:
Step 1 : char str1[] = "Hello"; The variable str1 is declared as an array of characters and initialized with a string "Hello".
Step 2 : char str2[] = "Hello"; The variable str2 is declared as an array of characters and initialized with a string "Hello".
We have use strcmp(s1,s2) function to compare strings.
Step 3 : if(str1 == str2) here the address of str1 and str2 are compared. The address of both variable is not same. Hence the if condition is failed.
Step 4 : At the else part it prints "Unequal".
See More Information
Tutorial Link:
Published by:Michael Daani
29. What will be the output of the program?
#include<stdio.h>
int main()
{
char t;
char *p1 = "PAK" , *p2;
p2=p1;
p1 = "MCQS" ;
printf("%s %s\n" , p1, p2);
return 0 ;
}
A. MCQS PAK B. PAK MCQS C. PAK D. MCQS
View Answer
Discuss forum
Workplace
Report
Published by:Michael Daani
30. What will be the output of the program?
#include<stdio.h>
#include<string.h>
int main()
{
printf("%c\n" , "abcdefgh" [4 ]);
return 0 ;
}
A. abcdefgh B. e C. d D. Error
View Answer
Discuss forum
Workplace
Report
Answer: Option B
Explanation:
printf("%c\n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".
Hence the output is 'e'.
See More Information
Tutorial Link:
Published by:Michael Daani
« »