Exercise: Strings

Questions for: Strings

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: C

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).

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 = "BIX";
    char *str3;
    str3 = strcat(str1, str2);
    printf("%s %s\n", str3, str1);
    return 0;
}
A:
ExamAdept India
B:
ExamAdept ExamAdept
C:
India India
D:
Error
Answer: B

It prints 'ExamAdept ExamAdept' in TurboC (in 16 bit platform).

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

What will be the output of the program ?
#include<stdio.h>
void swap(char *, char *);

int main()
{
    char *pstr[2] = {"Hello", "ExamAdept"};
    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:
ExamAdept
Hello
B:
Address of "Hello" and "ExamAdept"
C:
Hello
ExamAdept
D:
Iello
HndiaBIX
Answer: C

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", "ExamAdept"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to

pstr[0] = "Hello", pstr[1] = "ExamAdept"

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
ExamAdept

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: B
No answer description is available. Let's discuss.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf(5+"ExamAdept\n");
    return 0;
}
A:
Error
B:
ExamAdept
C:
BIX
D:
None of above
Answer: C

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

Ad Slot (Above Pagination)
Quiz