6
www.fellowbuddy.com Simplifying Students Life C String 1. What would be the output of the following program? main() { char *str*+ = ,“Frog”,”Do”,”Not”, “Die.”,”They”,”Croak!”-; printf(“%d %d”, sizeof (str), sizeof (str*0+)); } a) 3 12 b) 2 12 c) 12 3 d) 12 2 2. What would be the output of the following program? main() { char str1*+ = “Hello”; char str2*+ = “Hello”; if(str1 == str2) printf(“Equal”); else printf(“\nUnequal”); } a) Error. b) Equal. c) Unequal. d) None. 3. Would the following code give any error on compilation? strcat (string,’!’); a) Yes. b) No. 4. What will happen if I use this code in my program? #define TRACE(n) printf("TRACE: %d\n", n) a) Nothing it is good.

C string

Embed Size (px)

Citation preview

Page 1: C string

www.fellowbuddy.com

Simplifying Students Life

C String

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

main()

{

char *str*+ = ,“Frog”,”Do”,”Not”, “Die.”,”They”,”Croak!”-;

printf(“%d %d”, sizeof (str), sizeof (str*0+));

}

a) 3 12

b) 2 12

c) 12 3

d) 12 2

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

main()

{

char str1*+ = “Hello”;

char str2*+ = “Hello”;

if(str1 == str2)

printf(“Equal”);

else

printf(“\nUnequal”);

}

a) Error.

b) Equal.

c) Unequal.

d) None.

3. Would the following code give any error on compilation?

strcat (string,’!’);

a) Yes.

b) No.

4. What will happen if I use this code in my program?

#define TRACE(n) printf("TRACE: %d\n", n)

a) Nothing it is good.

Page 2: C string

www.fellowbuddy.com

Simplifying Students Life

b) Code is wrong.

c) Error: macro replacement within a string literal

d) None

5. How can I print a '%' character in a printf format string?

a) Use “\%”

b) Use “/%”

c) None

d) Use “%%”

6. String operation such as strcmp(s, t), strcat(s, t), strcpy(s, t) and strlen(s) heavily rely upon

a) Presence of any escape sequence

b) Presence of NULL character

c) Presence of new-line character

d) None of the mentioned

7. What would be the output of the following code?

main()

{

Printf(“%c”,”abcdefghijklmn*4+”);

}

a) d.

b) e.

c) abcdefgh.

d) None

8. What would be the output of the following program?

main()

{

printf (5+“program”);

}

a) ram

b) gram

c) am

d) None

9. In the following program which functions would get called?

Page 3: C string

www.fellowbuddy.com

Simplifying Students Life

main()

{

char str1*+ = “Keep me clean ”;

char str2[40] ;

strcpy(str2,str1);

printf(“n%s”, str2);

}

strcpy(char *t, char *s)

{

while(*s)

{

*t = *s;

t++;

s++;

}

*t =”\0”;

}

a) The library defined strcpy()

b) The user defined strcpy()

c) Both

d) None.

10. What is the difference in the following declaration?

char *p = “Sundaram”;

char a*+ =”Sundaram”;

a) Both are same

b) First pointer initialization with string and other array initialization with string

c) First pointer initialized to point to string constant and other array initialization with string

d) None

11. Is the following program correct?

main()

{

Char *str1 = “Union”;

Char *str2 = “Minister”;

Char *str3;

str3 = strcat(str1, str2);

Printf(“\n%s”, str3);

}

Page 4: C string

www.fellowbuddy.com

Simplifying Students Life

a) Print “Union Minister”

b) Garbage value

c) Print “UnionMinster”

d) Error

12. What would be the output of the following program.

main()

{

Char str*7+ = “Strings”;

printf(“%s”, str);

}

a) Strings

b) Error

c) Cannot predict

d) None

13. What is the output of the following program?

void main()

{

printf("IMEIMEMYSELF"+9);

}

a) IMEI.

b) MYSELF.

c) SELF

d) None

14. If the two strings are identical, then strcmp() function returns?

a) 0

b) 1

c) -1

d) None

15. What would be the output of the following program

main()

{

char ch =’A’;

printf(“%d %d”, sizeof(ch), sizeof(‘A’));

Page 5: C string

www.fellowbuddy.com

Simplifying Students Life

}

a) 2 1

b) 1 1

c) 2 2

d) 1 2

16. What would be the output of the following program?

main()

{

printf(“%d %d %d”, sizeof(‘3’), sizeof(“3”), sizeof(3));

}

a) 1 1 1

b) 2 2 2

c) 1 2 3

d) 1 2 2

17. Which one of the following functions returns the string representation from a pointer to a time_t

value?

a) gettime().

b) seektime().

c) ctime().

d) None

18. Consider the declaration

Static char hello*+ = “hello”;

The output of printf(“%s\n”, hello);

Will be the same as that of

a) Puts(hello).

b) Puts(“hello”).

c) Puts(“hello\n”).

d) Puts(“%s\n”, “hello”).

19. The following program

main()

{

Page 6: C string

www.fellowbuddy.com

Simplifying Students Life

static char a*3+*4+ = ,“abcd”, “mnop”,”fghi”-;

putchar(**a);

}

a) Will not compile successfully

b) Report error at run time

c) Print garbege

d) None

20. A string that is a formal parameter can be declared

a) An array with empty braces

b) A pointer to character

c) A pointer to a character

d) None

Answers

1. ANSWER: D

2. ANSWER: C

3. ANSWER: A

4. ANSWER: C

5. ANSWER: D

6. ANSWER: B

7. ANSWER: B

8. ANSWER: C

9. ANSWER: B

10. ANSWER: C

11. ANSWER: B

12. ANSWER: C

13. ANSWER: C

14. ANSWER: A

15. ANSWER: D

16. ANSWER: B

17. ANSWER: C

18. ANSWER: A, B, D

19. ANSWER: D

20. ANSWER: A, B