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

Strings Questions and Answers

41.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    printf(5+"IndiaBIX\n");
    return 0;
}

 

Answer: C

Correct answer: BIX

A string literal in an expression yields a pointer to its first character, so 5+"IndiaBIX\n" points five characters in. Counting I-n-d-i-a, index 5 is the B, and printf prints from there: BIX.

Please log in or register to report this question.

42.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[25] = "IndiaBIX";
    printf("%s\n", &str+2);
    return 0;
}

 

Answer: A

Correct answer: Garbage value

str and &str hold the same address but have different types: str decays to char *, while &str is char (*)[25], a pointer to the whole array. So &str + 2 advances by two whole arrays - 50 bytes, not two characters, landing well past the string. Printing from there reads whatever happens to be in memory.

Please log in or register to report this question.

43.

The______ function returns the number of characters that are present before the terminating null character.

Answer: B

Correct answer: strlen()

strlen() counts the characters up to, but not including, the terminating null. So strlen("abc") is 3 while sizeof("abc") is 4. The other names do not exist.

Please log in or register to report this question.

44.

What is the output of following program?

# include <stdio.h>
  
int main() 
{ 
   char str1[] = "GeeksQuiz"; 
   char str2[] = {'G', 'e', 'e', 'k', 's', 'Q', 'u', 'i', 'z'}; 
   int n1 = sizeof(str1)/sizeof(str1[0]); 
   int n2 = sizeof(str2)/sizeof(str2[0]); 
   printf("n1 = %d, n2 = %d", n1, n2); 
   return 0; 
} 

 

Answer: A

Correct answer: n1 = 10, n2 = 9

str1 is initialised from a string literal, so the compiler adds a terminating null: 9 letters plus '\0' is 10 bytes. str2 is initialised from an explicit list of nine characters, with no null added, so it is 9 bytes - and, lacking a terminator, it is an array of characters rather than a usable C string.

Please log in or register to report this question.

45.

What will be the output of the program ?

#include <stdio.h>
#include <string.h>

int main()
{
    static char str1[] = "dills";
    static char str2[20];
    static char str3[] = "Daffo";
    int i;
    i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
    printf("%d\n", i);
    return 0;
}

 

Answer: D

Correct answer: 0

Work outwards. strcpy(str2, str1) puts "dills" into str2 and returns it; strcat(str3, ...) appends that to "Daffo", making "Daffodills". Comparing that with "Daffodills" gives 0, since the strings are identical. (str3 is static and sized by its initialiser, so this relies on there being room after it - not something to imitate.)

Please log in or register to report this question.

46.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str1[] = "Hello";
    char str2[10];
    char *t, *s;
    s = str1;
    t = str2;
    while(*t=*s)
        *t++ = *s++;
    printf("%s\n", str2);
    return 0;
}

 

Answer: A

Correct answer: Hello

The condition *t = *s is an assignment, not a comparison: it copies the character and yields it, so the loop continues while that character is non-zero. Each character is therefore written twice - once by the condition and once by the body - but to the same place, and the pointers advance only in the body. When the terminating null is reached the condition copies it and evaluates to 0, ending the loop with str2 properly terminated: Hello.

Please log in or register to report this question.

47.

What will be the output of the following C code?

char str1[15];
char str2[15];
int mat;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
mat = strncmp(str1, str2, 4);
if(mat < 0)
    printf("str1 is not greater than str2");
else if(mat > 0)
    printf("str2 is not greater than str1");
else
    printf("both are equal");

Answer: B

Correct answer: str2 is not greater than str1

strncmp compares the first four characters, and the very first pair settles it: 'a' is 97 while 'A' is 65, so the result is positive. The first test mat < 0 therefore fails and the else if (mat > 0) succeeds. Case matters because the comparison is by character code, not alphabetically.

Please log in or register to report this question.

48.

What will be the output of the program ?

#include <stdio.h>
#include <string.h>

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

 

Answer: C

Correct answer: 5

strlen stops at the first null, and the initialiser puts one immediately after India. So the count is 5, even though the array holds several more characters. Compare sizeof, which would report the whole array.

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.

50.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char p[] = "%d\n";
    p[1] = 'c';
    printf(p, 65);
    return 0;
}

 

Answer: A

Correct answer: A

The format string is an ordinary array here, so it can be modified: setting p[1] to 'c' turns "%d\n" into "%c\n". The argument 65 is then printed as a character, and 65 is the code for A.

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