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

Strings Questions and Answers

11.

Predict the output of the following program:

#include <stdio.h> 
int main() 
{ 
    char str[] = "%d %c", arr[] = "GeeksQuiz"; 
    printf(str, 0[arr], 2[arr + 3]); 
    return 0; 
} 

 

Answer: C

Correct answer: 71 Q

Both subscripts are written backwards, which is legal: 0[arr] is arr[0] = 'G', and 2[arr+3] is *(arr+3+2) = arr[5] = 'Q'. The format string, itself held in an array, prints the first with %d - giving the character code 71 - and the second with %c, giving Q.

Please log in or register to report this question.

12.

Output of following program

#include <stdio.h> 
int fun(char *p) 
{ 
    if (p == NULL || *p == '\0') return 0; 
    int current = 1, i = 1; 
    while (*(p+current)) 
    { 
        if (p[current] != p[current-1]) 
        { 
            p[i] = p[current]; 
            i++; 
        } 
        current++; 
    } 
    *(p+i)='\0'; 
    return i; 
} 
  
int main() 
{ 
    char str[] = "geeksskeeg"; 
    fun(str); 
    puts(str); 
    return 0; 
} 

 

Answer: A

Correct answer: gekskeg

The function removes adjacent duplicates: current scans the string while i marks where to write, and a character is kept only when it differs from the one before it. On geeksskeeg the second e, the second s and the second e of ee are dropped, leaving gekskeg. The result is written back in place and terminated with a null.

Please log in or register to report this question.

13.

Assume that a character takes 1 byte. Output of following program?

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

 

Answer: C

Correct answer: 20

sizeof reports the size of the array as declared, not the length of the string in it. The array was given 20 elements, so the answer is 20, of which the first ten hold "GeeksQuiz" and its null and the remaining ten are zero-filled. strlen(str) would give 9.

Please log in or register to report this question.

14.

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

15.

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.

16.

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.

17.

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.

Answer: B

Correct answer: compares the string

strcmp() compares two null-terminated strings and returns a negative value, zero, or a positive value according to whether the first sorts before, equal to, or after the second. The comparison is by character code, so it is not the same as alphabetical order for mixed case. The first n characters version is strncmp().

Please log in or register to report this question.

Answer: A

Correct answer: int strcoll(const char *s1,const char *s2)

strcoll compares two strings, so it takes two const char * arguments and returns an int in the manner of strcmp. The const qualifiers state that neither string is modified.

Please log in or register to report this question.

Answer: A

Correct answer: compares the string, result is dependent on the LC_COLLATE

strcoll compares two strings according to the collating sequence of the current locale, set by LC_COLLATE. That matters wherever sorting order is not simply the order of the character codes - accented letters in French, or the treatment of case - which is exactly where strcmp gives the wrong answer for a human reader.

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