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

Strings Questions and Answers

21.

#include <stdio.h> 
   
void my_toUpper(char* str, int index) 
{ 
    *(str + index) &= ~32; 
} 
   
int main() 
{ 
    char* arr = "geeksquiz"; 
    my_toUpper(arr, 0); 
    my_toUpper(arr, 5); 
    printf("%s", arr); 
    return 0; 
} 

 

Answer: C

Correct answer: Compiler dependent

arr points to a string literal, and modifying one is undefined behaviour. Most compilers place literals in read-only memory, so the write faults at run time; others allow it, and the program prints GeeksQuiz. The fix is char arr[] = "geeksquiz";, which gives a modifiable copy. (The &= ~32 trick works because upper and lower case differ by exactly bit 5.)

Please log in or register to report this question.

22.

In below program, what would you put in place of “?” to print “Quiz”?

#include <stdio.h> 
int main()  
{  
  char arr[] = "GeeksQuiz";  
  printf("%s", ?);  
  return 0;  
} 

 

Answer: B

Correct answer: (arr+5)

"GeeksQuiz" has Q at index 5, counting from 0, so arr+5 points at the start of Quiz and %s prints from there to the terminating null. arr+4 would give sQuiz.

Please log in or register to report this question.

23.

Which of the following functions returns a pointer to the located string or a null pointer if string is not found.

Answer: B

Correct answer: strstr()

strstr(haystack, needle) searches for the first occurrence of one string inside another and returns a pointer to where it begins, or NULL if it is not there. strchr finds a single character rather than a string.

Please log in or register to report this question.

24.

What will be the output of the program ?

#include <stdio.h>

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

 

Answer: D

Correct answer: Morning

A string literal in an expression yields a pointer to its first character, so 5+"Good Morning\n" is a pointer five characters further along. Counting G-o-o-d-space, index 5 is the M, and printf prints from there: Morning.

Please log in or register to report this question.

25.

What will be the output of the program ?

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

int main()
{
    char str1[20] = "Hello", str2[20] = " World";
    printf("%s\n", strcpy(str2, strcat(str1, str2)));
    return 0;
}

 

Answer: C

Correct answer: Hello World

Work outwards. strcat(str1, str2) appends " World" to "Hello", making str1 hold "Hello World" and returning a pointer to it. strcpy(str2, ...) then copies that into str2 and returns str2. Both arrays are 20 bytes, so there is room, and the printed string is Hello World.

Please log in or register to report this question.

26.

Which of the following is the right syntax to copy n characters from the object pointed to by s2 into the object pointed to by s1?

Answer: A

Correct answer: void *memcpy(void *s1,const void *s2,size_t n);

The destination comes first, as it does throughout the library - the same order as an assignment. The source is const, since it is only read, and the function returns void *, the destination pointer, so calls can be chained. The two regions must not overlap; memmove is the version that copes if they do.

Please log in or register to report this question.

27.

The library function used to find the last occurrence of a character in a string is

Answer: C

Correct answer: strrchr()

strrchr() searches from the end and returns a pointer to the last occurrence of the character, or NULL. It is the standard way of finding a file extension or the last directory separator in a path.

Please log in or register to report this question.

Answer: B

Correct answer: return pointer to first occurrence of c in ch or NULL if not present

strchr(ch, c) searches the string ch for the character c and returns a pointer to the first occurrence, or NULL if there is none. Returning a pointer rather than an index is what lets the result be used directly as the start of a substring. The terminating null counts as part of the string, so searching for '\0' finds the end.

Please log in or register to report this question.

29.

If the size of pointer is 32 bits What will be the output of the program ?

#include <stdio.h>

int main()
{
    char a[] = "Visual C++";
    char *b = "Visual C++";
    printf("%d, %d\n", sizeof(a), sizeof(b));
    printf("%d, %d", sizeof(*a), sizeof(*b));
    return 0;
}

 

Answer: C

Correct answer: 11, 4 1, 1

a is an array, so sizeof(a) is the whole thing: 10 characters plus the terminating null, 11. b is a pointer, so sizeof(b) is 32 bits, 4 bytes. Both *a and *b are single chars, so each is 1.

Please log in or register to report this question.

30.

What will the given C code do?

int memcmp(const void *str1, const void *str2, size_t n)

 

Answer: A

Correct answer: compares the first n bytes of str1 and str2

memcmp compares the first n bytes of the two objects, returning a negative, zero or positive value like strcmp. It copies nothing, and unlike the str functions it takes no notice of null bytes - it compares exactly n bytes whatever they contain.

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