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.

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.

4.

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.

5.

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.

6.

What does the following fragment of C-program print?

char c[] = "GATE2011";  
char *p =c;  
printf("%s", p + p[3] - p[1]) ; 

 

Answer: C

Correct answer: 2011

The subscripts give character codes: p[3] is 'E' = 69 and p[1] is 'A' = 65, so the expression is

\[ p + 69 - 65 = p + 4 \]

Printing from index 4 of "GATE2011" gives 2011.

Please log in or register to report this question.

7.

#include <stdio.h> 
int main() 
{ 
    char str[] = "GeeksQuiz"; 
    printf("%s %s %s\n", &str[5], &5[str], str+5); 
    printf("%c %c %c\n", *(str+6), str[6], 6[str]); 
    return 0; 
} 

 

Answer: D

Correct answer: Quiz Quiz Quiz u u u

Subscripting is defined as a[i] == *(a + i), and since addition commutes, i[a] means the same thing. So &str[5], &5[str] and str+5 are all the address of the sixth character - each prints Quiz. Likewise *(str+6), str[6] and 6[str] are all the character u.

Please log in or register to report this question.

8.

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.

9.

Output?

int main() 
{ 
    char a[2][3][3] = {'g','e','e','k','s','q','u','i','z'}; 
    printf("%s ", **a); 
    return 0; 
} 

 

Answer: C

Correct answer: geeksquiz

The array is 2×3×3 = 18 bytes in one contiguous block. The nine initialisers fill the first nine, and the remaining bytes are zero-filled, as a partly initialised array always is. **a is the address of the first byte, and %s prints from there to the first null - which is byte 10. So the whole run of nine characters comes out, with no garbage after it.

Please log in or register to report this question.

10.

#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.

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