Find Output Of Program

Question 26 :

What will be the output of the program ?

#include < stdio.h >
void swap(char *, char *);

int main()
{
    char *pstr[2] = {"Hello", "IndiaPARINAM"};
    swap(pstr[0], pstr[1]);
    printf("%s\n%s", pstr[0], pstr[1]);
    return 0;
}
void swap(char *t1, char *t2)
{
    char *t;
    t=t1;
    t1=t2;
    t2=t;
}


A).
IndiaPARINAM
Hello
B).
Address of "Hello" and "IndiaPARINAM"
C).
Hello
IndiaPARINAM
D).
Iello
HndiaPARINAM
Answer : Option C

Explanation :

Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything.
Step 2: char *pstr[2] = {"Hello", "IndiaPARINAM"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to
pstr[0] = "Hello", pstr[1] = "IndiaPARINAM"
Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program.
If the swap function is "called by reference" it will affect the variable pstr.
Step 4: printf("%s\n%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].

Hence the output of the program is
Hello
IndiaPARINAM


Question 27 :

What will be the output of the program (Turbo C in 16 bit platform DOS) ?

#include < stdio.h >
#include < string.h >

int main()
{
    char *str1 = "India";
    char *str2 = "PARINAM";
    char *str3;
    str3 = strcat(str1, str2);
    printf("%s %s\n", str3, str1);
    return 0;
}


A). IndiaPARINAM India
B). IndiaPARINAM IndiaPARINAM
C). India India
D). Error
Answer : Option B

Explanation :

It prints 'IndiaPARINAM IndiaPARINAM' in TurboC (in 16 bit platform).
It may cause a 'segmentation fault error' in GCC (32 bit platform).


Question 28 :

If the size of pointer is 4 bytes then What will be the output of the program ?

#include < stdio.h >

int main()
{
    char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};
    printf("%d, %d", sizeof(str), strlen(str[0]));
    return 0;
}


A). 22, 4
B). 25, 5
C). 24, 5
D). 20, 2
Answer : Option C

Explanation :

Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; The variable str is declared as an pointer to the array of 6 strings.
Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));
sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'
strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';

Hence the output of the program is 24, 5
Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be 12, 5. Because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit platform), the output will be 24, 5 (because the size of pointer is 4 bytes).


Question 29 :

What will be the output of the program ?

#include < stdio.h >

int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is not empty\n");
    else
        printf("The string is empty\n");
    return 0;
}


A). The string is not empty
B). The string is empty
C). No output
D). 0
Answer : Option B

Explanation :

The function printf() returns the number of charecters printed on the console.
Step 1: char a[] = '\0'; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.
Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.
In the else part it prints "The string is empty".


Question 30 :

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). Unpredictable integer value
B). 0
C). -1
D). Error
Answer : Option A

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.