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

Strings Questions and Answers

1.

Is there any function declared as strstr()?

Answer: A

This function returns a pointer to the first occurrence in s1 of any of the entire sequence of characters specified in s2, or a null pointer if the sequence is not present in s1.
char *strstr(const char *s1, const char *s2)

Please log in or register to report this question.

2.

Which among the following is Copying function?

Answer: A

Correct answer: memcpy()

memcpy() copies a given number of bytes from one object to another. The others do not exist: the string copy is strcpy(), not strcopy(), and there is no memcopy() or strxcpy().

Please log in or register to report this question.

3.

The ______ function appends not more than n characters.

Answer: C

Correct answer: strncat()

strncat() appends at most n characters from the source, and always adds a terminating null - so the destination needs n+1 bytes free. The bounded form exists to keep an append from running past the end of the buffer.

Please log in or register to report this question.

4.

Which of the following function is used to find the first occurrence of a given string in another string?

Answer: C

Correct answer: strstr()

strstr(haystack, needle) finds the first occurrence of one string inside another and returns a pointer to where it starts. strchr and strrchr search for a single character, not a substring.

Please log in or register to report this question.

5.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        char *str = "hello, world";
        char *str1 = "hello, world";
        if (strcmp(str, str1))
            printf("equal");
        else
            printf("unequal");
    }

 

Answer: B

Correct answer: unequal

This is the trap in strcmp: it returns 0 when the strings are equal, and 0 is false. The two strings here have the same contents, so the condition fails and the else branch runs, printing unequal. The test should be written if (strcmp(a, b) == 0).

Please log in or register to report this question.

6.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    static char mess[6][30] = {"Don't walk in front of me...", 
                               "I may not follow;", 
                               "Don't walk behind me...", 
                               "Just walk beside me...", 
                               "And be my friend." };

    printf("%c, %c\n", *(mess[2]+9), *(*(mess+2)+9));
    return 0;
}

 

Answer: B

Correct answer: k, k

mess[2] is "Don't walk behind me...", and index 9 counts D-o-n-'-t-space-w-a-l-k - giving k. The second expression, *(*(mess+2)+9), is the same thing written in pointer form, since mess[i] is *(mess+i). So both print k.

Please log in or register to report this question.

7.

Which of the following function is more appropriate for reading in a multi-word string?

Answer: C

Correct answer: gets()

gets() reads characters up to the newline, so it takes a whole line including the spaces between words. scanf("%s", ...) stops at the first whitespace and would read only the first word. printf and puts write rather than read. Note that gets() cannot limit the input length and has been removed from modern C for that reason; fgets() is what should be used in real code.

Please log in or register to report this question.

9.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        char str[10] = "hello";
        char *p = strrchr(str, 'l');
        printf("%c\n", *(++p));
    }

 

Answer: B

Correct answer: o

strrchr finds the last 'l', which in hello is at index 3. The pre-increment moves the pointer on one, to index 4, and that character is o.

Please log in or register to report this question.

Answer: B

Correct answer: places character c into first n characters of s, return s

Read the arguments in order: memset(s, c, n) fills the first n bytes of s with the byte c, and returns s. It is the usual way of zeroing a buffer, as memset(buf, 0, sizeof buf). Note that c is converted to unsigned char, so it sets bytes, not wider values.

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