Question 1
WRONG
|
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.
Which of the following changes can correct the program so that it prints "Geeks Quiz"?
#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; } |
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()
|
Discuss it
Question 1 Explanation:
See following for explanation. http://www.geeksforgeeks.org/storage-for-strings-in-c/
Question 2
****CORRECT
|
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
|
Discuss it
Question 2 Explanation:
The size of str1 is 10 and size of str2 9. When an array is initialized with string in double quotes, compiler adds a '\0' at the end.
Question 3
WRONG
|
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; } |
str1 is Quiz, str2 is Geeks
| |
str1 is Geeks, str2 is Quiz
| |
str1 is Geeks, str2 is Geeks
| |
str1 is Quiz, str2 is Quiz
|
Discuss it
Question 3 Explanation:
The above swap() function doesn't swap strings. The function just changes local pointer variables and the changes are not reflected outside the function. See following for more details. http://www.geeksforgeeks.org/swap-strings-in-c/
Question 4
CORRECT
|
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; } |
10
| |
9
| |
8
| |
Random Number
|
Discuss it
Question 4 Explanation:
The function fun() basically counts number of characters in input string. Inside fun(), pointer str2 is initialized as str1. The statement while(*++str1); increments str1 till ‘\0’ is reached. str1 is incremented by 9. Finally the difference between str2 and str1 is returned which is 9.
Question 5
CORRECT
|
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
|
Discuss it
Question 5 Explanation:
See comments for explanation.
char c[] = "GATE2011"; // p now has the base address string "GATE2011" char *p = c; // p[3] is 'E' and p[1] is 'A'. // p[3] - p[1] = ASCII value of 'E' - ASCII value of 'A' = 4 // So the expression p + p[3] - p[1] becomes p + 4 which is // base address of string "2011" printf("%s", p + p[3] - p[1]); // prints 2011
Question 6
WRONG
|
#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; } |
Runtime Error
| |
Compiler Error
| |
uiz uiz uiz u u u
| |
Quiz Quiz Quiz u u u
|
Discuss it
Question 6 Explanation:
The program has no error. All of the following expressions mean same thing &str[5] &5[str] str+5 Since compiler converts the array operation in pointers before accessing the array elements, all above result in same address. Similarly, all of the following expressions mean same thing. *(str+6) str[6] 6[str]
Question 7
CORRECT
|
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; } |
arr
| |
(arr+5)
| |
(arr+4)
| |
Not possible
|
Discuss it
Question 7 Explanation:
Since %s is used, the printf statement will print everything starting from arr+5 until it finds ‘\0’
Question 8
WRONG
|
Output?
int main() { char a[2][3][3] = { 'g' , 'e' , 'e' , 'k' , 's' , 'q' , 'u' , 'i' , 'z' }; printf ( "%s " , **a); return 0; } |
Compiler Error
| |
geeksquiz followed by garbage characters
| |
geeksquiz
| |
Runtime Error
|
Discuss it
Question 8 Explanation:
We have created a 3D array that should have 2*3*3 (= 18) elements, but we are initializing only 9 of them. In C, when we initialize less no of elements in an array all uninitialized elements become ‘\0′ in case of char and 0 in case of integers.
Question 9
CORRECT
|
Consider the following C program segment:
The output of the program is? (GATE CS 2004)
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); |
gnirts
| |
gnirt
| |
string
| |
no output is printed
|
Discuss it
Question 9 Explanation:
Let us consider below line inside the for loop p[i] = s[length — i]; For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0′ So p[0] becomes ‘\0’. It doesn't matter what comes in p[1], p[2]….. as P[0] will not change for i >0. Nothing is printed if we print a string with first character ‘\0′
Question 10
WRONG
|
#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
|
Discuss it
Question 10 Explanation:
The memory for the string arr is allocated in the read/write only area of data section. The choice is compiler dependent. In the newer version of compilers, the memory is allocated in the read only section of the data area. So any modification in the string is not possible. In older version compilers like Turbo-C, modification is possible.
Question 11
WRONG
|
Predict the output of the following program:
#include <stdio.h> int main() { char str[] = "%d %c" , arr[] = "GeeksQuiz" ; printf (str, 0[arr], 2[arr + 3]); return 0; } |
G Q
| |
71 81
| |
71 Q
| |
Compile-time error
|
Discuss it
Question 11 Explanation:
The statement printf(str, 0[arr], 2[arr + 3]); boils down to: printf("%d %c, 0["GeeksQUiz"], 2["GeeksQUiz" + 3]); Which is further interpreted as: printf("%d %c, *(0 + "GeeksQUiz"), *(2 + "GeeksQUiz" + 3)); Which prints the ascii value of 'G' and character 'Q'.
Question 12
CORRECT
|
Output of following program
#include <stdio.h> int fun( char *p) { if (p == NULL || *p == '\0' ) return 0; int current = 1, i = 1; while (*(p+current)) { if (p[current] != p[current-1]) { p[i] = p[current]; i++; } current++; } *(p+i)= '\0' ; return i; } int main() { char str[] = "geeksskeeg" ; fun(str); puts (str); return 0; } |
gekskeg
| |
geeksskeeg
| |
geeks
| |
Garbage Values
|
Discuss it
Question 12 Explanation:
The function mainly replaces more than once consecutive occurrences of a character with one one occurrence.
Question 13
CORRECT
|
int main() { char p[] = "geeksquiz" ; char t; int i, j; for (i=0,j= strlen (p); i<j; i++) { t = p[i]; p[i] = p[j-i]; p[j-i] = t; } printf ( "%s" , p); return 0; } |
ziuqskeeg
| |
Nothing is printed on the screen
| |
geeksquiz
| |
gggggggg
|
Discuss it
Question 13 Explanation:
The string termination character '\0' is assigned to first element of array p[]
Question 14
WRONG
|
Assume that a character takes 1 byte. Output of following program?
#include<stdio.h> int main() { char str[20] = "GeeksQuiz" ; printf ( "%d" , sizeof (str)); return 0; } |
9
| |
10
| |
20
| |
Garbage Value
|
Discuss it
Question 14 Explanation:
Note that the sizeof() operator would return size of array. To get size of string stored in array, we need to use strlen(). The following program prints 9.
#include <stdio.h> #include <string.h> int main() { char str[20] = "GeeksQuiz"; printf ("%d", strlen(str)); return 0; }
Question 15
WRONG
|
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 = "GeeksQuiz" ; char str2[] = "GeeksQuiz" ; printf ( "sizeof(str1) = %d, sizeof(str2) = %d" , sizeof (str1), sizeof (str2)); return 0; } |
sizeof(str1) = 10, sizeof(str2) = 10
| |
sizeof(str1) = 4, sizeof(str2) = 10
| |
sizeof(str1) = 4, sizeof(str2) = 4
| |
sizeof(str1) = 10, sizeof(str2) = 4
|
Discuss it
Question 15 Explanation:
str1 is a pointer and str2 is an array.
Question 16
CORRECT
|
The output of following C program is
#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 2015 Mock Test" ; printf ( "%s" , fun(str)); return 0; } |
GATE CS 2015 Mock Test
| |
tseT kcoM 5102 SC ETAG
| |
Nothing is printed on screen
| |
Segmentation Fault
|
Discuss it
Question 16 Explanation:
The function basically reverses the given string.
Question 17
CORRECT
|
Consider the following function written in the C programming language. The output of the above function on input “ABCD EFGH” is
void foo ( char *a) { if (*a && *a != ` `) { foo(a+1); putchar (*a); } } |
ABCD EFGH
| |
ABCD
| |
HGFE DCBA
| |
DCBA
|
Discuss it
Question 17 Explanation:
The program prints all characters before ' ' or '\0' (whichever comes first) in reverse order.
Question 18
CORRECT
|
Consider the following C program segment.
What will be printed by the program?
# include <stdio.h> int main( ) { char s1[7] = "1234" , *p; p = s1 + 2; *p = '\0' ; printf ( "%s" , s1); } |
12
| |
120400
| |
1204
| |
1034
|
Discuss it
Question 18 Explanation:
char s1[7] = "1234", *p; p = s1 + 2; // p holds address of character 3 *p = '\0' ; // memory at s1 now becomes "12\034" printf ("%s", s1); // All characters till \0 are printed
Question 19
CORRECT
|
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, "Geeks" ); strcpy (s2, "Quiz" ); strcat (s1, s2); printf ( "%s" , s1); return 0; } |
GeeksQuiz
| |
Geeks
| |
Geeks Quiz
| |
Quiz
|
Discuss it
No comments:
Post a Comment