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

Strings Questions and Answers

11.

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.

13.

Which code from the given option return pointer to last occurrence of c in ch or NULL if not present?

Answer: B

Correct answer: char *strrchr(ch, c)

The extra r stands for reverse: strrchr returns a pointer to the last occurrence of the character, or NULL if it is absent. It is the usual way of finding a file extension, by searching for the last dot.

Please log in or register to report this question.

14.

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

 

Answer: C

Correct answer: 24, 5

str is an array of six pointers, and each is 4 bytes, so sizeof(str) is \(6 \times 4 = 24\) - the size of the pointer table, not of the strings it points at. strlen(str[0]) counts the characters of Frogs, which is 5.

Please log in or register to report this question.

15.

What will be the output of the following C code?

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

 

Answer: B

Correct answer: Segmentation Fault

str points to a string literal, which almost every compiler places in read-only memory. strcpy then tries to write over it, and the program is killed by the memory protection. Declaring char str[20] = "hello, world"; would give a writable copy.

Please log in or register to report this question.

16.

Which of the following is the variable type defined in header string. h?

Answer: C

Correct answer: size_t

size_t is the unsigned integer type returned by sizeof and used for sizes and counts throughout the library - it is the type of the length that strlen returns and of the n parameter of strncpy. It is declared in <string.h> among other headers. The underscore is part of the name.

Please log in or register to report this question.

17.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        char *str = "hello, world\n";
        printf("%d", strlen(str));
 
    }

 

Answer: C

Correct answer: 13

strlen counts the characters before the terminating null. hello, world is 12 characters, and the \n is a single character, not two - the backslash is an escape in the source, not a character in the string. So the count is 13.

Please log in or register to report this question.

18.

#include <stdio.h> 
int main() 
{ 
    char *str1 = "GeeksQuiz"; 
    char str2[] = "GeeksQuiz"; 
  
    printf("sizeof(str1) = %d, sizeof(str2) = %d", 
           sizeof(str1), sizeof(str2)); 
  
    return 0; 
} 

 

Answer: B

Correct answer: sizeof(str1) = 4, sizeof(str2) = 10

str1 is a pointer, so sizeof gives the size of a pointer - 4 bytes on a 32-bit build. str2 is an array, so sizeof gives the whole array: 9 characters plus the terminating null, 10. The initialiser looks identical in the two cases, which is what makes this worth knowing.

Please log in or register to report this question.

19.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[] = "India\0BIX\0";
    printf("%d\n", sizeof(str));
    return 0;
}

 

Answer: D

Correct answer: 11

sizeof reports the whole array, and the array was sized by its initialiser. That initialiser is I n d i a \0 B I X \0 - ten characters, since the explicit nulls count - and the compiler adds one more terminating null of its own, giving 11. strlen on the same array would return 5.

Please log in or register to report this question.

20.

What will the following C code do?

char *strrchr(const char *s, int c)
{
    char ch = c;
    const char *sc;
    for (sc = NULL; ; ++s)
    {
        if (*s == ch)
            sc = s;
        if (*s == '\0')
            return (char *) sc;
    }
}

Answer: A

Correct answer: find last occurrence of c in char s[ ].

The loop walks the whole string, and every time it meets the character it records the position in sc, overwriting any earlier one. Only when it reaches the terminating null does it return, so what comes back is the position of the last occurrence - or NULL if there was none. Testing for the null after the comparison is what lets a search for '\0' itself succeed.

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