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

Strings Questions and Answers

61.

What will be the output of the program in 16-bit platform (Turbo C under DOS) ?

#include <stdio.h>

int main()
{
    printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));
    return 0;
}

 

Answer: B

Correct answer: 4, 2, 8

On a 16-bit Turbo C target: 3.0f is a float, 4 bytes; '3' is a character constant, which has type int in C, so 2 bytes on that platform - not 1; and 3.0 with no suffix is a double, 8 bytes. The character constant is the trap: it is an int in C and a char only in C++.

Please log in or register to report this question.

62.

What is the output of following program?

#include <stdio.h> 
void swap(char *str1, char *str2) 
{ 
  char *temp = str1; 
  str1 = str2; 
  str2 = temp; 
}   
    
int main() 
{ 
  char *str1 = "Geeks"; 
  char *str2 = "Quiz"; 
  swap(str1, str2); 
  printf("str1 is %s, str2 is %s", str1, str2); 
  return 0; 
} 

 

Answer: B

Correct answer: str1 is Geeks, str2 is Quiz

swap receives copies of the two pointers and exchanges the copies; the originals in main are untouched. To swap the caller’s pointers the function would have to take char ** and be called as swap(&str1, &str2).

Please log in or register to report this question.

63.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[] = "Nagpur";
    str[0]='K';
    printf("%s, ", str);
    str = "Kanpur";
    printf("%s", str+1);
    return 0;
}

 

Answer: D

Correct answer: Error

The first part is fine: str is a modifiable array, so str[0] = 'K' works. The failure is str = "Kanpur"; - an array name is not a modifiable lvalue, so it cannot be reassigned. Copying into it would need strcpy(str, "Kanpur"), and even then only if there were room.

Please log in or register to report this question.

64.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is empty\n");
    else
        printf("The string is not empty\n");
    return 0;
}

 

Answer: B

Correct answer: The string is not empty

a holds a single null character, so printf("%s", a) writes nothing and returns 0 - the count of characters printed. Zero is false, so the else branch runs. The message is misleading, which is the point of the question: the condition tests how much was printed, not whether the string was empty.

Please log in or register to report this question.

65.

The strpbrk() function is used to return a pointer to the character, or a null pointer if no character from s2 occurs in s1.

Answer: A

Correct answer: true

strpbrk(s1, s2) - “pointer break” - scans s1 for the first character that appears anywhere in s2, and returns a pointer to it, or NULL if none of them occurs. It is the pointer-returning counterpart of strcspn, which returns the same position as a length.

Please log in or register to report this question.

66.

Predict the output?

#include <stdio.h> 
int fun(char *str1) 
{ 
  char *str2 = str1; 
  while(*++str1); 
  return (str1-str2); 
} 
  
int main() 
{ 
  char *str = "GeeksQuiz"; 
  printf("%d", fun(str)); 
  return 0; 
} 

 

Answer: B

Correct answer: 9

while(*++str1); pre-increments before testing, so the character at index 0 is never examined and the pointer stops on the terminating null at index 9. The difference str1 - str2 is therefore 9 - which happens to be the length here, but only because the string is non-empty; on an empty string this code would run past the end.

Please log in or register to report this question.

67.

What will be the output of the following C code?

const char str1[] = "abcdef";
const char str2[] = "fgha";
char *mat;
mat= strpbrk(str1, str2);
if(mat)
printf("First matching character: %c\n", *mat);
else
printf("Character not found");

 

Answer: D

Correct answer: a

strpbrk returns a pointer to the first character of str1 that occurs anywhere in str2. Scanning abcdef against the set {f, g, h, a}, the very first character, a, is already in the set - so the search stops there. The f would only be reached if a were not a member.

Please log in or register to report this question.

68.

What will be the output of the following C code?

const char pla[] = "string1";
const char src[] = "string2";
printf("Before memmove place= %s, src = %s\n", pla, src);
memmove(pla, src, 7);
printf("After memmove place = %s, src = %s\n", pla, src);

 

Answer: A

Correct answer: Before memmove place= string1, src = string2 After memmove place = string2, src = string2

memmove copies 7 bytes from src to pla, that is the characters string2 without its terminating null - but pla already has a null in position 7, so it reads correctly as string2 afterwards. The source is not altered, which is why src is unchanged. (Note that both arrays are declared const, so writing to one is not something a program should do.)

Please log in or register to report this question.

69.

What will be the output of the following C code?

const char str1[10]="Helloworld";
const char str2[10] = "world";
char *mat;
mat = strstr(str1, str2);
printf("The substring is:%s\n", mat);

 

Answer: A

Correct answer: The substring is:world

strstr(str1, str2) finds the first occurrence of "world" inside "Helloworld" and returns a pointer to where it begins - not a copy. Printing from that pointer therefore gives world, the rest of the string from the match onwards.

Please log in or register to report this question.

Answer: C

Correct answer: return length of prefix of c consisting of characters not in s

strcspn(c, s) - the c is for complement - returns the length of the initial run of c made up of characters not in s. In other words it gives the index of the first character of c that does appear in the set. Its counterpart strspn counts the run of characters that are in the set.

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