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

Strings Questions and Answers

101.

What will the following C code do?

void *memset(void *s, int c, size_t n)
{
    unsigned char ch = c;
    unsigned char *su;
    for (su = s; 0 < n; ++su, --n)
        *su = ch;
    return (s);
}

Answer: A

Correct answer: store c throughout unsigned char s[n]

This is the textbook implementation of memset. The byte value c is copied into an unsigned char, and the loop walks n bytes from s, writing that value into each. Using unsigned char matters: it is the type guaranteed to address every byte of an object without sign issues.

Please log in or register to report this question.

102.

The function strspn() is the complement of strcspn().

Answer: A

Correct answer: true

The two are complementary. strspn(s, set) counts the initial run of characters that are in the set; strcspn(s, set) counts the initial run of those that are not. Used together they let a string be walked field by field - skip the delimiters, then take the token.

Please log in or register to report this question.

103.

What will be the value of var for the following C statement?

var = strcmp("Hello", "World");

 

Answer: A

Correct answer: -1

strcmp compares character by character, and the first pair decides it: 'H' is 72 while 'W' is 87, so the first string sorts before the second and the result is negative. The standard promises only the sign, not the magnitude - some libraries return the difference, here -15 - so code should test < 0 rather than == -1.

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