Find Output Of Program

Question 21 :

What will be the output of the program ?

#include < stdio.h >

int main()
{
    char str[25] = "IndiaPARINAM";
    printf("%s\n", &str+2);
    return 0;
}


A). Garbage value
B). Error
C). No output
D). diaPARINAM
Answer : Option A

Explanation :

Step 1: char str[25] = "IndiaPARINAM"; The variable str is declared as an array of characteres and initialized with a string "IndiaPARINAM".
Step 2: printf("%s\n", &str+2);
=> In the printf statement %s is string format specifier tells the compiler to print the string in the memory of &str+2
=> &str is a location of string "IndiaPARINAM". Therefore &str+2 is another memory location.

Hence it prints the Garbage value.


Question 22 :

What will be the output of the program ?

#include < stdio.h >

int main()
{
    char str = "IndiaPARINAM";
    printf("%s\n", str);
    return 0;
}


A). Error
B). IndiaPARINAM
C). Base address of str
D). No output
Answer : Option A

Explanation :

The line char str = "IndiaPARINAM"; generates "Non portable pointer conversion" error.
To eliminate the error, we have to change the above line to
char *str = "IndiaPARINAM"; (or) char str[] = "IndiaPARINAM";

Then it prints "IndiaPARINAM".


Question 23 :

What will be the output of the program ?

#include < stdio.h >

int main()
{
    char str[] = "Nagpur";
    str[0]='K';
    printf("%s, ", str);
    str = "Kanpur";
    printf("%s", str+1);
    return 0;
}


A). Kagpur, Kanpur
B). Nagpur, Kanpur
C). Kagpur, anpur
D). Error
Answer : Option D

Explanation :

The statement str = "Kanpur"; generates the LVALUE required error. We have to use strcpy function to copy a string.
To remove error we have to change this statement str = "Kanpur"; to strcpy(str, "Kanpur");
The program prints the string "anpur"


Question 24 :

What will be the output of the program ?

#include < stdio.h >

int main()
{
    printf(5+"IndiaPARINAM\n");
    return 0;
}


A). Error
B). IndiaPARINAM
C). PARINAM
D). None of above
Answer : Option C

Explanation :

printf(5+"IndiaPARINAM\n"); In the printf statement, it skips the first 5 characters and it prints "PARINAM"


Question 25 :

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 above
Answer : Option B