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

Strings Questions and Answers

31.

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.

Answer: A

Correct answer: first group names beginning with str; second group names beginning with mem

<string.h> holds two families. The str functions work on null-terminated strings, stopping at the terminator. The mem functions work on objects of a given length, taking a byte count and paying no attention to nulls - which is why they can copy or compare arbitrary binary data.

Please log in or register to report this question.

33.

What will be the output of the following C code?

const char str1[]="ABCDEF1234567";
const char str2[] = "269";
len = strcspn(str1, str2);
printf("First matching character is at %d\n", len + 1);

 

Answer: A

Correct answer: First matching character is at 8

strcspn returns the length of the initial segment of str1 containing no character from str2. Scanning ABCDEF1234567 against 269, the first seven characters ABCDEF1 are all absent from the set, and the eighth, 2, is present. So the function returns 7, and the program prints 7 + 1 = 8 - the 1-based position of the first match.

Please log in or register to report this question.

34.

Which of the given function is used to return a pointer to the located character?

Answer: D

Correct answer: strchr()

strchr() returns a pointer to the located character - the first occurrence of it in the string - or NULL. strrchr finds the last occurrence, memchr searches a fixed-length block rather than a string, and strxfrm transforms rather than searches.

Please log in or register to report this question.

35.

String operation such as strcat(s, t), strcmp(s, t), strcpy(s, t) and strlen(s) rely heavily upon

Answer: A

Correct answer: Presence of NULL character

Every one of them finds the end of a string by looking for the terminating null - there is no stored length. A string that has lost its null makes all of them read past the end of the array, which is the single commonest cause of memory corruption in C.

Please log in or register to report this question.

36.

How will you print \n on the screen?

Answer: D

Correct answer: printf("\\n");

Inside a string literal a backslash starts an escape sequence, so a literal backslash must itself be escaped: "\\n" is the two characters backslash and n. "\n" would be the single newline character, and single quotes make a character constant rather than a string.

Please log in or register to report this question.

37.

If the two strings are identical, then strcmp() function returns

Answer: D

Correct answer: 0

strcmp returns 0 when the strings are identical, a negative value when the first sorts before the second, and a positive one when it sorts after. Returning 0 for a match is what makes if (strcmp(a, b)) such a common mistake - it is true when the strings differ.

Please log in or register to report this question.

38.

The______function returns a pointer to the first character of a token.

Answer: D

Correct answer: strtok()

strtok() returns a pointer to the first character of the next token, or NULL when there are none left. It finds a token by skipping leading delimiters and then writing a null over the delimiter that ends it.

Please log in or register to report this question.

39.

Does strcat() add a terminating null character?

Answer: B

Correct answer: Always

strcat always writes a terminating null after the characters it appends - the result is a proper C string. That is why the destination must have room for the two strings and one more byte, and why the function cannot check whether that room exists.

Please log in or register to report this question.

40.

Which of the following function compares 2 strings with case-insensitively?

Answer: C

Correct answer: strcasecmp(s, t)

strcasecmp compares two strings ignoring the difference between upper and lower case. It is a POSIX function rather than a standard C one - the Microsoft spelling is _stricmp - but it is available almost everywhere. strcmp compares by character code, so "abc" and "ABC" come out unequal.

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