Machine-translated by Google. Questions are authored in English.
Top

Strings Questions and Answers

71.

Use_______to determine the null-terminated message string that corresponds to the error code errcode.

Answer: A

Correct answer: strerror()

strerror(errcode) returns a pointer to a human-readable message for the given error number - typically the value of errno after a failed library call. perror() is the related function that prints such a message directly.

Please log in or register to report this question.

72.

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;
}

 

Answer: B

Correct answer: The sentence will get printed in reverse order

The loop starts at strlen(sentence)-1, the last character, and counts down to 0, writing each character as it goes - so the line comes out backwards. (Note that gets() cannot limit the input length and has been removed from modern C; fgets() is what real code should use.)

Please log in or register to report this question.

73.

What will be the output of the following C code?

#include <stdio.h>
    #include <string.h>
    int main()
    {
        char *str = "hello, world";
        char str1[9];
        strncpy(str1, str, 9);
        printf("%s %d", str1, strlen(str1));
    }

 

Answer: C

Correct answer: Undefined behaviour

strncpy copies at most n characters and adds a terminating null only if the source is shorter than n. Here the source has 12 characters and n is 9, so all nine bytes of str1 are filled with hello, wo and no null is written. Passing that to printf("%s") and strlen reads past the end of the array - undefined behaviour.

Please log in or register to report this question.

74.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        char str[11] = "hello";
        char *str1 = "world";
        strcat(str, str1);
        printf("%s %d", str, str[10]);
    }

 

Answer: A

Correct answer: helloworld 0

strcat appends world to hello, giving ten characters, and writes the terminating null at index 10 - the last element of the eleven-byte array. So str[10] is '\0', and %d prints its value, 0. The array is exactly big enough; a char str[10] would have overflowed.

Please log in or register to report this question.

75.

Which function will you choose to join two words?

Answer: B

Correct answer: strcat()

strcat() appends the second string to the end of the first, overwriting the first one’s terminating null and adding a new one. strcpy() would replace the first string rather than extend it, and the other two names do not exist. The destination must have room for both.

Please log in or register to report this question.

76.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[7] = "IndiaBIX";
    printf("%s\n", str);
    return 0;
}

 

Answer: C

Correct answer: Cannot predict

The initialiser needs nine bytes - eight characters and a null - but the array has only seven. A compiler will warn, and where it accepts the code it truncates, leaving IndiaB with no terminating null. Printing that runs past the end of the array, so what appears depends on whatever bytes follow.

Please log in or register to report this question.

Answer: A

Correct answer: copies n characters from src to dest

memcpy copies n bytes from src to dest - destination first, as throughout the library. The source is const because it is only read, and the return value is the destination pointer. The two regions must not overlap; memmove is the version that handles overlap correctly.

Please log in or register to report this question.

78.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        char str[10] = "hello";
        char *str1 = "world";
        strncat(str, str1, 9);
        printf("%s", str);
    }

 

Answer: A

Correct answer: Undefined behaviour

Count the bytes: str is 10 long and already holds hello. strncat appends up to 9 characters from world - all five - and then adds a terminating null, so it writes to indices 5 through 10. Index 10 is one past the end of the array, so this is a buffer overflow and the behaviour is undefined; it may well print helloworld, but only by accident. The n in strncat limits the source, not the destination, so it is no protection here: char str[11] is what the call needs.

Please log in or register to report this question.

79.

If char=1, int=4, and float=4 bytes size, What will be the output of the program ?

#include <stdio.h>

int main()
{
    char ch = 'A';
    printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
    return 0;
}

 

Answer: B

Correct answer: 1, 4, 4

sizeof(ch) is 1, the size of a char. sizeof('A') is 4, because a character constant in C has type int. And 3.14f carries the f suffix, making it a float, so 4 - without the suffix it would be a double.

Please log in or register to report this question.

80.

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;
}

 

Answer: B

Correct answer: India

The initialiser contains an explicit \0 after India, and %s prints up to the first null. So only India appears, even though the array holds more characters after it. (\B is not a recognised escape, so it is simply the letter B.)

Please log in or register to report this question.

Loading…
Tags: Strings Questions and Answers || Strings MCQ Questions and Answers || Strings GK Questions and Answers || Strings GK MCQ Questions || Strings Multiple Choice Questions and Answers || Strings GK || GK on Strings || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming