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

Strings Questions and Answers

51.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
    int i;
    char *t;
    t = names[3];
    names[3] = names[4];
    names[4] = t;
    for(i=0; i<=4; i++)
        printf("%s,", names[i]);
    return 0;
}

 

Answer: B

Correct answer: Suresh, Siva, Sona, Ritu, Baiju

The array holds pointers, and the code exchanges two of them through the temporary t - the strings themselves are never touched or copied. So elements 3 and 4 swap places, giving Suresh, Siva, Sona, Ritu, Baiju. Swapping pointers rather than character arrays is exactly why an array of pointers is preferred for sorting strings.

Please log in or register to report this question.

52.

The C library function _________ breaks string s1 into a series of tokens using the delimiter s2.

Answer: A

Correct answer: char *strtok(char *s1,const char *s2);

strtok splits s1 into tokens separated by any of the characters in s2. The first call passes the string; every later call passes NULL to continue where it left off. Note that it modifies the string, writing a null over each delimiter, and keeps state between calls, which makes it unsuitable for reentrant or threaded code.

Please log in or register to report this question.

Answer: B

Correct answer: manipulating objects as character arrays

The mem functions treat their arguments as blocks of bytes of a stated length, with no special meaning for the null character. That is what lets them copy, compare, search and fill arbitrary data - structures, integers, binary buffers - and not just text.

Please log in or register to report this question.

54.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    static char s[25] = "The cocaine man";
    int i=0;
    char ch;
    ch = s[++i];
    printf("%c", ch);
    ch = s[i++];
    printf("%c", ch);
    ch = i++[s];
    printf("%c", ch);
    ch = ++i[s];
    printf("%c", ch);
    return 0;
}

 

Answer: A

Correct answer: hhe!

Take the four statements in turn, with s = "The cocaine man" and i = 0.

  • s[++i] - i becomes 1 first, so this is s[1] = h.
  • s[i++] - the old i, still 1, so h again; i becomes 2.
  • i++[s] - the same as s[i++], so s[2] = e; i becomes 3.
  • ++i[s] - subscript binds tighter than the prefix ++, so this increments the character s[3], a space (32), to 33 = !.

Output: hhe!

Please log in or register to report this question.

55.

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.

56.

What does the function void *memmove(void *s1, const void *s2, size_t n); return?

Answer: A

Correct answer: returns the value of s1

memmove returns its destination pointer, s1 - as memcpy, strcpy and strcat all do. Returning the destination lets a call be used directly as an argument to another function. Its distinctive property is that it works correctly even when the two regions overlap, which memcpy does not guarantee.

Please log in or register to report this question.

Answer: A

Correct answer: length of the transformed string, not including the terminating null-character

strxfrm transforms a string so that comparing two transformed strings with strcmp gives the same order as comparing the originals with strcoll. It returns the length of the transformed string, excluding the null - so a caller can size the buffer by calling it once with a length of 0.

Please log in or register to report this question.

58.

Which among the given options compares at most n characters of string ch to string s?

Answer: A

Correct answer: int strncmp(ch, s, n)

strncmp is the bounded form of strcmp: it compares at most n characters, stopping early at a difference or at a terminating null. The order of the first two arguments matters only for the sign of the result, but the question asks for ch compared to s, so ch comes first.

Please log in or register to report this question.

59.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str = "IndiaBIX";
    printf("%s\n", str);
    return 0;
}

 

Answer: A

Correct answer: Error

char str declares a single character, and a string literal cannot be assigned to one - the initialiser is a pointer to an array of characters. The declaration would have to be char str[] = "IndiaBIX"; or char *str = "IndiaBIX";.

Please log in or register to report this question.

60.

What will be the output of the program ?

#include <stdio.h>
void swap(char *, char *);

int main()
{
    char *pstr[2] = {"Hello", "IndiaBIX"};
    swap(pstr[0], pstr[1]);
    printf("%s\n%s", pstr[0], pstr[1]);
    return 0;
}
void swap(char *t1, char *t2)
{
    char *t;
    t=t1;
    t1=t2;
    t2=t;
}

 

Answer: C

Correct answer: Hello IndiaBIX

swap receives copies of the two pointers and exchanges the copies, which vanish when the function returns. The array in main is untouched, so the strings print in their original order. To swap the caller’s pointers the function would have to take char ** and be called as swap(&pstr[0], &pstr[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