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

Strings Questions and Answers

91.

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

 

Answer: B

Correct answer: IndiaBIX IndiaBIX

strcat appends to its first argument and returns that same pointer, so str3 and str1 end up pointing at the same characters - and both print IndiaBIX. Note that str1 points to a string literal with no room after it, so appending to it is undefined behaviour; the old DOS compiler this question assumes allowed it, but a modern one will usually crash.

Please log in or register to report this question.

92.

This function offers the quickest way to determine whether two character sequences of the same known length match character for the character up to and including any null character in both.

Answer: C

Correct answer: memcmp()

memcmp() is the one to use when both lengths are known: it compares a fixed number of bytes and stops only at a difference, taking no interest in null characters - so nulls inside the data are compared like any other byte. strcmp and strncmp both stop at a terminating null, so neither compares “up to and including” it.

Please log in or register to report this question.

93.

What will be the output of the program ?

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

int main()
{
    static char s[] = "Hello!";
    printf("%d\n", *(s+strlen(s)));
    return 0;
}

 

Answer: D

Correct answer: 0

strlen(s) is 6, so s + 6 points at the byte just past Hello! - which is the terminating null. Dereferencing gives the character '\0', and %d prints its numeric value, 0.

Please log in or register to report this question.

94.

What will be the output of the program ?

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

int main()
{
    printf("%d\n", strlen("123456"));
    return 0;
}

 

Answer: A

Correct answer: 6

strlen counts the characters before the terminating null. "123456" holds six digit characters - their being digits is irrelevant - so the answer is 6. sizeof on the same literal would give 7.

Please log in or register to report this question.

95.

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

 

Answer: A

Correct answer: Unpredictable integer value

Two things make the result unpredictable. The buffers are only five bytes and gets() cannot limit what it reads, so any input of five characters or more overruns them. And even with short input, strcmp returns a value whose sign is defined but whose magnitude is not - some libraries return the difference of the first differing characters, others just -1, 0 or 1. Code should test the sign, never a specific number.

Please log in or register to report this question.

96.

What does the following fragment of C-program print?

char c[] = "GATE2011";  
char *p =c;  
printf("%s", p + p[3] - p[1]) ; 

 

Answer: C

Correct answer: 2011

The subscripts give character codes: p[3] is 'E' = 69 and p[1] is 'A' = 65, so the expression is

\[ p + 69 - 65 = p + 4 \]

Printing from index 4 of "GATE2011" gives 2011.

Please log in or register to report this question.

Answer: C

Correct answer: manipulates sequence of non – null characters.

The strn functions take an explicit count and process at most that many characters, so they do not depend on a terminating null being present. That is what makes them the safer choice with a fixed-size buffer - though strncpy in particular does not add a null of its own if the source fills the whole n.

Please log in or register to report this question.

98.

What will be the output of the following C code?

char str1[] = "Helloworld ";
char str2[] = "Hello";
len = strspn(str1, str2);
printf("Length of initial segment matching %d\n", len );

 

Answer: B

Correct answer: 5

strspn returns the length of the initial run of str1 made up only of characters from str2. The set is {H, e, l, o}, and Hello uses nothing else - but the sixth character, w, is not in the set, so the run stops there. The answer is 5.

Please log in or register to report this question.

99.

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

 

Answer: B

Correct answer: The string is empty

a holds a single null character, so printf("%s", a) writes nothing and returns 0 - the count of characters printed. Zero is false, so the else branch runs and the message happens to be correct. (Compare the variant where the two branches are the other way round, and the same logic prints the wrong message.)

Please log in or register to report this question.

100.

Which of the following function sets first n characters of a string to a given character?

Answer: B

Correct answer: strnset()

strnset(s, ch, n) sets the first n characters of the string to the given character, while strset sets every character to the end. Both are non-standard, from the Turbo C and Microsoft libraries; the portable equivalent is memset.

Please log in or register to report this question.

Loading…

Other Subjects

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