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

Strings Questions and Answers

1.

Consider the following C statements:

char *str1 = "Hello; /* Statement S1 */
char *str2 = "Hello;"; /* Statement S2 */
int *str3 = "Hello"; /* Statement S3 */

Which of the following options is/are correct?

Exam Year(s)

GATE CS - 2026,

Answer: C

The correct option is S1 has a lexical error and S3 has a semantic error.

In S1 the string literal "Hello; is never closed before the end of the line. The lexical analyser cannot form a valid string token, so this is a lexical error.

S2 is valid: the semicolon inside the quotes is part of the string, and the statement ends with its own semicolon.

S3 is well formed syntactically, but it initialises an int * with a string literal, whose type is a char array that decays to char *. That type mismatch is detected during semantic analysis, so it is a semantic error.

Calling S1's error syntactic, or saying S2 has any error, misplaces the phase that catches each fault.

Please log in or register to report this question.

2.

Consider the following C program:

#include \(\lt\)stdio.h\(\gt\)
void stringcopy(char *, char *);
int main(){
char a[30] = "@#Hello World!";
stringcopy(a, a + 2);
printf("%s\n", a);
return 0;
}
void stringcopy(char *s, char *t) {
while(*t)
*s++ = *t++;
}

Which ONE of the following will be the output of the program?

Exam Year(s)

GATE CS - 2025,

Answer: D

The program prints Hello World!d!.

The array holds "@#Hello World!": 14 characters in a[0] to a[13], then the terminator in a[14]. The call stringcopy(a, a + 2) copies from the third character onwards into the start of the array.

The loop while(*t) *s++ = *t++; stops when *t is the terminator, without copying it. So it copies the 12 characters 'Hello World!' into a[0] to a[11], and a[12] and a[13] keep their old values, 'd' and '!'. The original terminator at a[14] is untouched.

printf then prints a[0] to a[13]: Hello World!d!. 'Hello World!' alone would be printed only if the terminator were copied too. The original string and 'ello World!' would need different copy offsets.

Please log in or register to report this question.

3.

Is there any function declared as strstr()?

Answer: A

This function returns a pointer to the first occurrence in s1 of any of the entire sequence of characters specified in s2, or a null pointer if the sequence is not present in s1.
char *strstr(const char *s1, const char *s2)

Please log in or register to report this question.

4.

Which among the following is Copying function?

Answer: A

Correct answer: memcpy()

memcpy() copies a given number of bytes from one object to another. The others do not exist: the string copy is strcpy(), not strcopy(), and there is no memcopy() or strxcpy().

Please log in or register to report this question.

5.

The ______ function appends not more than n characters.

Answer: C

Correct answer: strncat()

strncat() appends at most n characters from the source, and always adds a terminating null - so the destination needs n+1 bytes free. The bounded form exists to keep an append from running past the end of the buffer.

Please log in or register to report this question.

6.

Which of the following function is used to find the first occurrence of a given string in another string?

Answer: C

Correct answer: strstr()

strstr(haystack, needle) finds the first occurrence of one string inside another and returns a pointer to where it starts. strchr and strrchr search for a single character, not a substring.

Please log in or register to report this question.

7.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        char *str = "hello, world";
        char *str1 = "hello, world";
        if (strcmp(str, str1))
            printf("equal");
        else
            printf("unequal");
    }

 

Answer: B

Correct answer: unequal

This is the trap in strcmp: it returns 0 when the strings are equal, and 0 is false. The two strings here have the same contents, so the condition fails and the else branch runs, printing unequal. The test should be written if (strcmp(a, b) == 0).

Please log in or register to report this question.

8.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    static char mess[6][30] = {"Don't walk in front of me...", 
                               "I may not follow;", 
                               "Don't walk behind me...", 
                               "Just walk beside me...", 
                               "And be my friend." };

    printf("%c, %c\n", *(mess[2]+9), *(*(mess+2)+9));
    return 0;
}

 

Answer: B

Correct answer: k, k

mess[2] is "Don't walk behind me...", and index 9 counts D-o-n-'-t-space-w-a-l-k - giving k. The second expression, *(*(mess+2)+9), is the same thing written in pointer form, since mess[i] is *(mess+i). So both print k.

Please log in or register to report this question.

9.

Which of the following function is more appropriate for reading in a multi-word string?

Answer: C

Correct answer: gets()

gets() reads characters up to the newline, so it takes a whole line including the spaces between words. scanf("%s", ...) stops at the first whitespace and would read only the first word. printf and puts write rather than read. Note that gets() cannot limit the input length and has been removed from modern C for that reason; fgets() is what should be used in real code.

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