0% found this document useful (0 votes)
614 views5 pages

Strings

The document contains 15 multiple choice questions related to strings in C programming. It covers topics like string declarations, string comparisons, string functions like strcpy(), strcat(), and sizeof operators on strings. The answers section provides the correct option for each question along with some additional explanations.

Uploaded by

hydownstar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
614 views5 pages

Strings

The document contains 15 multiple choice questions related to strings in C programming. It covers topics like string declarations, string comparisons, string functions like strcpy(), strcat(), and sizeof operators on strings. The answers section provides the correct option for each question along with some additional explanations.

Uploaded by

hydownstar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

***********************************************************************

*************************** STRINGS ******************************


***********************************************************************

1] What would be the output of the following program ?


main()
{
printf(5 + "Fascimile");
}
OPTIONS:
(a) Error
(b) Fascimile
(c) mile
(d) None of the above

2] What would be the output of the following program ?


main()
{
char str1[] = "Hello";
char str2[] = "Hello";
if (str1 == str2)
printf("\n Equal");
else
printf("\n Unequal");
}
OPTIONS:
(a) Equal
(b) Unequal
(c) Error
(d) None of the above

3] What would be the output of the following program ?


main()
{
printf("%c","abcdefgh"[4]);
}
OPTIONS:
(a) Error
(b) d
(c) e
(d) abcdefgh

4] What would be the output of the following program ?


main()
{
char str[7] = "Strings";
printf("%s",str);
}
OPTIONS:
(a) Error
(b) Strings
(c) Cannot predict
(d) None of the above

5] How would you output '\n' on the screen ?

6] What would be the output of the following program ?


main()
{
char ch = 'A';
printf("%d %d",sizeof(ch),sizeof('A'));
}
OPTIONS:
(a) 1 1
(b) 1 2
(c) 2 2
(d) 2 1

7] What would be the output of the following program ?


main()
{
printf("\n%d %d %d",sizeof('3'),sizeof("3"),sizeof(3));
}
OPTIONS:
(a) 1 1 1
(b) 2 2 2
(c) 1 2 2
(d) 1 2 1

8] Is the following program correct ? < Yes / No>


main()
{
char *str1 = "United";
char *str2 = "Front";
char *str3;
str3 = strcat(str1,str2);
printf("\n %s",str3);
}

9] How would you improve the code in (8) above ?

10] In the following code which function would get called, the user-def
ined strcpy() or the one in the standard library ?
main()
{
char tr1[] = "keep India Beautiful ...emigrate!");
char str2[40];
strcpy(str2,str1);
printf("\n %s",str2);
}
strcpy(char *t,char *s)
{
while(*s)
{
*t = *s;
t++;
s++;
}
*t = "\0";
}

11] Can you compact the code in strcpy() into one line ?

12] Can you compact the code in strcpy() into one line ?
main()
{
char *str[] = {"Frogs","Do","Not","Die.","They","Crock!"};
printf("%d %d",sizeof(str),sizeof(str[0]));
}

13] How would you find the length of each string in the program (12) abo
ve ?

14] What is the difference in the following declarations ?


char *p = "Samuel";
char a[] = "Samuel";

15] While handling a string do we always have to process it characte


r by character or there exists a method to process the entire string as one uni
t.

***********************************************************************
************************* ANSWERS *******************************
***********************************************************************
1] C

2] B

3] C

4] C. Here 'str[]' has been declared a 7 character array and into it


a 8 character string has been stored. This would result into overwriting
of the byte beyond the seventh byte reserved for the array with a '\0'. There
is always a possibility that something important gets overwritten which would
be unsafe.

5] printf("\\n");

6] B

7] B

8] No, since what is present in memory beyond 'United' is not known an


d we are attaching 'Front' at the end of 'United', thereby overwriting some
thing, which is an unsafe thing to do.

9] main()
{
char str1[15] = "United";
char *str2 = "Front";
char *str3;
str3 = strcat(str1,str2);
printf("\n %s",str3);
}

10] User-defined tyrcpy()

11] strcpy(char *t,char *s)


{
while(*t++ = *s++)
}

12] 12 2

13] main()
{
char *str[] = {"Frogs","Do","Not","Die","They","Croak!");
int i;
for(i = 0;i <= 5;i++)
printf("%s %d",str[i],strlen(str[i]));
}

14] Here 'a' is an array big enough to hold the message and
the '\0' following the message. Individual characters within the array can be
changed but the address of the array would remain same.
On the other hand, 'p' is a pointer, initialized to point to
a string constant. The pointer 'p' may be nodified to point to another string,
but if you attempt to modify the string at which 'p' is pointing the resu
lt is undefined.

15] A string can be processed only on a character by character basis.

You might also like