String in C MCQ
Question 1: What is the correct way to declare a string in C? a) string str;
b) char* str;
c) str string;
d) char str[100];
Answer 1: b) char* str;
Question 2: How do you initialize a string in C?
Answer 1: b) char* str;
Question 2: How do you initialize a string in C?
a) string s = "Hello";
b) char s[] = "Hello";
c) s = "Hello";
d) char* s = "Hello";
Answer 2: b) char s[] = "Hello";
Question 3: What is the function used to find the length of a string in C?
Answer 2: b) char s[] = "Hello";
Question 3: What is the function used to find the length of a string in C?
a) len()
b) length()
c) strlen()
d) strlength()
Answer 3: c) strlen()
Question 4: In C, how do you compare two strings?
a) compare(str1, str2);
b) strcomp(str1, str2);
c) strcmp(str1, str2);
d) strcompare(str1, str2);
Answer 4: c) strcmp(str1, str2);
Question 5: What is the function used to concatenate two strings in C?
Answer 4: c) strcmp(str1, str2);
Question 5: What is the function used to concatenate two strings in C?
a) concat()
b) strcat()
c) join()
d) strjoin()
Answer 5: b) strcat()
Question 6: How do you input a string from the user in C?
Answer 5: b) strcat()
Question 6: How do you input a string from the user in C?
a) scanf("%s", str);
b) gets(str);
c) read(str);
d) input(str);
Answer 6: a) scanf("%s", str);
Question 7: Which header file is required for string functions in C?
Answer 6: a) scanf("%s", str);
Question 7: Which header file is required for string functions in C?
a) <stdlib.h>
b) <math.h>
c) <string.h>
d) <stdio.h>
Answer 7: c) <string.h>
Question 8: What does the function strcpy() do in C?
Answer 7: c) <string.h>
Question 8: What does the function strcpy() do in C?
a) Concatenates two strings
b) Compares two strings
c) Copies one string to another
d) Finds the length of a string
Answer 8: c) Copies one string to another
Question 9: How do you convert a string to an integer in C?
Answer 8: c) Copies one string to another
Question 9: How do you convert a string to an integer in C?
a) atoi()
b) strtoi()
c) intToStr()
d) stringToNum()
Answer 9: a) atoi()
Question 10: What is the output of the following code snippet?
a) Hello
Answer 9: a) atoi()
Question 10: What is the output of the following code snippet?
cchar str1[] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
a) Hello
b) World
c) HelloWorld
d) Compiler Error
Answer 10: d) Compiler Error (Buffer Overflow - str1 is not large enough to hold the concatenated string)
Answer 10: d) Compiler Error (Buffer Overflow - str1 is not large enough to hold the concatenated string)
0 Comments