Exercise: Strings

Questions for: Strings

What will be the output of the program ?
#include<stdio.h>

int main()
{
    char str[7] = "ExamAdept";
    printf("%s\n", str);
    return 0;
}
A:
Error
B:
ExamAdept
C:
Cannot predict
D:
None of above
Answer: C
Here str[] has declared as 7 character array and into a 8 character is stored. This will result in overwriting of the byte beyond 7 byte reserved for '\0'.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("India", "BIX\n");
    return 0;
}
A:
Error
B:
India BIX
C:
India
D:
BIX
Answer: C

printf("India", "BIX\n"); It prints "India". Because ,(comma) operator has Left to Right associativity. After printing "India", the statement got terminated.

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
Answer: D

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

What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    char str[] = "India\0\BIX\0";
    printf("%s\n", str);
    return 0;
}
A:
BIX
B:
India
C:
India BIX
D:
India\0BIX
Answer: B

A string is a collection of characters terminated by '\0'.

Step 1: char str[] = "India\0\BIX\0"; The variable str is declared as an array of characters and initialized with value "India"

Step 2: printf("%s\n", str); It prints the value of the str.

The output of the program is "India".

What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf(5+"Good Morning\n");
    return 0;
}
A:
Good Morning
B:
Good
C:
M
D:
Morning
Answer: D

printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.

Hence the output is "Morning"

Ad Slot (Above Pagination)
Quiz