Strings - Find Output of Program Multiple Questions and Answers.
Exercise Questions ::
Strings
1. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20 ] = "Hello" , str2[20 ] = " World" ;
printf("%s\n" , strcpy(str2, strcat(str1, str2)));
return 0 ;
}
A. Hello B. World C. Hello World D. WorldHello
View Answer
Discuss forum
Workplace
Report
Answer: Option C
Explanation:
Step 1 : char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of characters and initialized with value "Hello" and " World" respectively.
Step 2 : printf("%s\n", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1 . The result will be stored in str1 . Therefore str1 contains "Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2 .
Hence it prints "Hello World".
See More Information
Tutorial Link:
Published by:Michael Daani
Answer: Option A
Explanation:
Step 1 : char p[] = "%d\n"; The variable p is declared as an array of characters and initialized with string "%d".
Step 2 : p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes "%c".
Step 3 : printf(p, 65); becomes printf("%c", 65);
Therefore it prints the ASCII value of 65. The output is 'A'.
See More Information
Tutorial Link:
Published by:Michael Daani
3. What will be the output of the program?
#include<stdio.h>
#include<string.h>
int main()
{
printf("%d\n" , strlen("123456" ));
return 0 ;
}
A. 6 B. 2 C. 12 D. 7
View Answer
Discuss forum
Workplace
Report
Answer: Option 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".
See More Information
Tutorial Link:
Published by:Michael Daani
Answer: Option D
Explanation:
printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.
Hence the output is "Morning"
See More Information
Tutorial Link:
Published by:Michael Daani
5. What will be the output of the program If characters 'a', 'b' and 'c' enter are supplied as input?
#include<stdio.h>
int main()
{
void fun();
fun();
printf("\n" );
return 0 ;
}
void fun()
{
char c;
if ((c = getchar())!= '\n' )
fun();
printf("%c" , c);
}
A. abc abc B. bca C. Infinite loop D. cba
View Answer
Discuss forum
Workplace
Report
Answer: Option D
Explanation:
Step 1 : void fun(); This is the prototype for the function fun() .
Step 2 : fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The given input characters are "abc"
Output : cba
See More Information
Tutorial Link:
Published by:Michael Daani
»