CDS Q7
CDS Q7
#include <stdio.h>
#define R 10
#define C 20
int main()
{
int (*p)[R][C];
printf("%d", sizeof(*p));
getchar();
return 0;
}
2. Output of following C program? Assume that all necessary header files are included.
int main()
{
char *s1 = (char *)malloc(50);
char *s2 = (char *)malloc(50);
strcpy(s1, "Ravindra");
strcpy(s2, "Ravula");
strcat(s1, s2);
printf("%s", s1);
return 0;
}
3. 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 “Ravindra Ravula”. The program compiles
fine but produces segmentation fault when run.
1
#include <stdio.h>
#include<string.h>
int main()
{
char *str1 = "Ravindra ";
char *str2 = "Ravula";
myStrcat(str1, str2);
printf("%s ", str1);
return 0;
}
Which of the following changes can correct the program so that it prints “Geeks Quiz”?
# include <stdio.h>
int main()
{
char str1[] = "Ravindras";
char str2[] = {'R', 'a', 'v', 'i', 'n', 'd', 'r', 'a', 's'};
int n1 = sizeof(str1)/sizeof(str1[0]);
2
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1 = %d, n2 = %d", n1, n2);
return 0;
}
#include<stdio.h>
int main()
{
char *str1 = "Ravindra";
char *str2 = "ravula";
swap(str1, str2);
printf("str1 is %s, str2 is %s", str1, str2);
return 0;
}
(a) str1 is ravula, str2 is Ravindra (b) str1 is Ravindra, str2 is ravula
(c) str1 is Ravindra, str2 is Ravindra (d) str1 is ravula, str2 is ravula
#include <stdio.h>
int main()
{
char *str = "Ravindrar";
printf("%d", fun(str));
return 0;
}
(a) 10 (b) 9
(c) 8 (d) Random Number
8.
#include<stdio.h>
int main()
{
char str[] = "ravindrar";
printf("%s %s %s\n", &str[5], &5[str], str+5);
printf("%c %c %c\n", *(str+6), str[6], 6[str]);
return 0;
}
4
9. In below program, what would you put in place of “?” to print “Quiz”?
#include <stdio.h>
int main()
{
char arr[] = "GatesQuiz";
printf("%s", ?);
return 0;
}
10. Output?
int main()
{
char a[2][3][3] = {'g','a','t','e','s','q','u','i','z'};
printf("%s ", **a);
return 0;
}
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);
5
(c) string (d) no output is printed
12.
#include <stdio.h>
int main()
{
char* arr = "gatesquiz";
my_toUpper(arr, 0);
my_toUpper(arr, 5);
printf("%s", arr);
return 0;
}
#include <stdio.h>
int main()
{
char str[] = "%d %c", arr[] = "GatesQuiz";
printf(str, 0[arr], 2[arr + 3]);
return 0;
}
(a) G Q (b) 71 81
(c) 71 Q (d) Compile-time error
int main()
{
char str[] = "geeksskeeg";
fun(str);
puts(str);
return 0;
}
15.
int main()
{
char p[] = "gatesquiz";
char t;
int i, j;
for(i=0,j=strlen(p); i<j; i++)
{
7
t = p[i];
p[i] = p[j-i];
p[j-i] = t;
}
printf("%s", p);
return 0;
}
Output?
#include<stdio.h>
int main()
{
char str[20] = "GatesQuiz";
printf ("%d", sizeof(str));
return 0;
}
(a) 9 (b) 10
17. Predict the output of following program, assume that a character takes 1 byte and pointer
takes 4 bytes.
#include <stdio.h>
int main()
{
char *str1 = "GatesQuiz";
char str2[] = "GatesQuiz";
printf("sizeof(str1) = %d, sizeof(str2) = %d",
sizeof(str1), sizeof(str2));
8
return 0;
}
#include <stdio.h>
char str1[100];
char *fun(char str[])
{
static int i = 0;
if (*str)
{
fun(str+1);
str1[i] = *str;
i++;
}
return str1;
}
int main()
{
char str[] = "GATE CS 2016 Mock Test";
printf("%s", fun(str));
return 0;
}
(a) GATE CS 2016 Mock Test (b) tseT kcoM 6102 SC ETAG
(c) Nothing is printed on screen (d) Segmentation Fault
# include <stdio.h>
int main( )
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '\0' ;
printf ("%s", s1);
}
10