Exercise: Questions on String


Questions on String : Question 1 :
Consider the following code. The function myStrcat concatenates two strings. It appends all characters of b to end of a. So the expected output is "PRO coder". The program compiles fine but produces segmentation fault when run.
 #include <stdio.h>
 
 void myStrcat(char *a, char *b)
 {
     int m = strlen(a);
     int n = strlen(b);
     int i;
     for (i = 0; i <= n; i++)
        a[m+i]  = b[i];
 }
 
 int main()
 {
     char *str1 = "PRO ";
     char *str2 = "coder";
     myStrcat(str1, str2);
     printf("%s ", str1);
     return 0;
 }
 
Which of the following changes can correct the program so that it prints "PRO coder"?

char *str1 = "PRO "; can be changed to char str1[100] = "PRO ";
char *str1 = "PRO "; can be changed to char str1[100] = "PRO "; and a line a[m+n-1] = '\0' is added at the end of myStrcat
A line a[m+n-1] = '\0' is added at the end of myStrcat
A line 'a = (char *)malloc(sizeof(char)*(strlen(a) + strlen(b) + 1)) is added at the beginning of myStrcat()
Show Answer
Questions on String : Question 2 :
What is the output of following program?
 # include <stdio.h>
 
 int main()
 {
    char str1[] = "PROcoders";
    char str2[] = {'P', 'R', 'O', 'c', 'o', 'd', 'e', 'r', 's'};
    int n1 = sizeof(str1)/sizeof(str1[0]);
    int n2 = sizeof(str2)/sizeof(str2[0]);
    printf("n1 = %d, n2 = %d", n1, n2);
    return 0;
 }
 

n1 = 10, n2 = 9
n1 = 10, n2 = 10
n1 = 9, n2 = 9
n1 = 9, n2 = 10
Show Answer
Questions on String : Question 3 :
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 = "PRO";
   char *str2 = "coder";
   swap(str1, str2);
   printf("str1 is %s, str2 is %s", str1, str2);
   return 0;
 }
 

str1 is Quiz, str2 is PRO
str1 is PRO, str2 is coder
str1 is PRO, str2 is PRO
str1 is coder, str2 is coder
Show Answer
Questions on String : Question 4 :
Predict the output?
 #include <stdio.h>
 int fun(char *str1)
 {
   char *str2 = str1;
   while(*++str1);
   return (str1-str2);
 }
 
 int main()
 {
   char *str = "PROcoders";
   printf("%d", fun(str));
   return 0;
 }
 

10
9
8
Random Number
Show Answer
Questions on String : Question 5 :
What does the following fragment of C-program print?
 char c[] = "GATE2011"; 
 char *p =c; 
 printf("%s", p + p[3] - p[1]) ;

GATE2011
E2011
2011
011
Show Answer
Questions on String : Question 6 :
 #include<stdio.h>
 int main()
 {
     char str[] = "PROcoders";
     printf("%s %s %s\n", &str[5], &5[str], str+5);
     printf("%c %c %c\n", *(str+6), str[6], 6[str]);
     return 0;
 }
 

Runtime Error
Compiler Error
ers ers ers u u u
ders ders ders u u u
Show Answer
Questions on String : Question 7 :
In below program, what would you put in place of '?' to print 'ders'?
 #include <stdio.h>
 int main() 
 { 
   char arr[] = "PROcoders"; 
   printf("%s", ?); 
   return 0; 
 }
 

arr
(arr+5)
(arr+4)
Not possible
Show Answer
Questions on String : Question 8 :
Output?
 int main()
 {
     char a[2][3][3] = {'p','r','o','c','o','d','e','r','s'};
     printf("%s ", **a);
     return 0;
 }
 

Compiler Error
procoders followed by garbage characters
procoders
Runtime Error
Show Answer
Questions on String : Question 9 :
Consider the following C program segment:
 char p[20]; 
 char *s = "string"; 
 int length = strlen(s); 
 int i; 
 for (i = 0; i < length; i++) 
     p[i] = s[length — i]; 
 printf("%s", p);
The output of the program is? (GATE CS 2004)

gnirts
gnirt
string
no output is printed
Show Answer
Questions on String : Question 10 :
 #include <stdio.h>
  
 void my_toUpper(char* str, int index)
 {
     *(str + index) &= ~32;
 }
  
 int main()
 {
     char* arr = "procoders";
     my_toUpper(arr, 0);
     my_toUpper(arr, 5);
     printf("%s", arr);
     return 0;
 }
 

ProcoDers
procoders
Compiler dependent
Show Answer