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 "Geeks Quiz". 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 = \"Geeks \";
char *str2 = \"Quiz\";
myStrcat(str1, str2);
printf(\"%s \", str1);
return 0;
}
Which of the following changes can correct the program so that it prints "Geeks Quiz"?
Question 2
# 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;
}
Question 3
#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;
}
Question 4
#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;
}
Question 5
char c[] = \"GATE2011\";
char *p =c;
printf(\"%s\", p + p[3] - p[1]) ;
Question 6
#include<stdio.h>
int main()
{
char str[] = \"GeeksQuiz\";
printf(\"%s %s %s \", &str[5], &5[str], str+5);
printf(\"%c %c %c \", *(str+6), str[6], 6[str]);
return 0;
}
Question 7
int main()
{
char a[2][3][3] = {\'g\',\'e\',\'e\',\'k\',\'s\',\'q\',\'u\',\'i\',\'z\'};
printf(\"%s \", **a);
return 0;
}
Question 8
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);
Question 9
The Output of the following C code will be?
#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;
}
Question 10
#include <stdio.h>
int main()
{
char str[] = \"%d %c\", arr[] = \"GeeksQuiz\";
printf(str, 0[arr], 2[arr + 3]);
return 0;
}
There are 21 questions to complete.