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"?
char *str1 = "Geeks "; can be changed to char str1[100] = "Geeks ";
char *str1 = "Geeks "; can be changed to char str1[100] = "Geeks "; 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()
Question 2
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;
}
n1 = 10, n2 = 9
n1 = 10, n2 = 10
n1 = 9, n2 = 9
n1 = 9, n2 = 10
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;
}
Runtime Error
Compiler Error
uiz uiz uiz u u u
Quiz Quiz Quiz u u u
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
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
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;
}
GeeksQuiz
geeksquiz
Compiler dependent
Runtime Error
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.