Q#1: What is a C-style string in C++?
(A) A class object
(B) An array of characters ending with null character ‘\0’
(C) A dynamic array of integers
(D) A pointer to integer
Answer: (B) An array of characters ending with null character ‘\0’
Q#2: How do you declare a character array of size 10?
(A) char str(10);
(B) char str[10];
(C) char str{10};
(D) char str = 10;
Answer: (B) char str[10];
Q#3: Which of the following initializes a C-style string correctly?
(A) char str[6] = “Hello”;
(B) char str[5] = “Hello”;
(C) char str[6] = {‘H’,’e’,’l’,’l’,’o’};
(D) char str[5] = {‘H’,’e’,’l’,’l’,’o’,’\0′};
Answer: (A) char str[6] = “Hello”;
Q#4: What is the value of the last element in a C-style string initialized as “Hello”?
(A) ‘o’
(B) ‘\0’
(C) NULL
(D) Garbage
Answer: (B) ‘\0’
Q#5: How do you print a C-style string using cout?
(A) cout << str;
(B) cout << &str;
(C) cout << str[0];
(D) cout << *str;
Answer: (A) cout << str;
Q#6: Which of the following is invalid?
(A) char str[5] = “Hello”;
(B) char str[6] = “Hello”;
(C) char str[] = “Hello”;
(D) char str[10] = “Hi”;
Answer: (A) char str[5] = “Hello”;
Q#7: What is the output?
char str[6] = “Hello”;
cout << str[0];
(A) H
(B) e
(C) Hello
(D) \0
Answer: (A) H
Q#8: How do you find the length of a C-style string?
(A) strlen(str)
(B) str.length()
(C) sizeof(str)
(D) length(str)
Answer: (A) strlen(str)
Q#9: What header file is required for strlen()?
(A) <string>
(B) <cstring>
(C) <iostream>
(D) <cstdlib>
Answer: (B) <cstring>
Q#10: Which of the following copies one string into another?
(A) strcpy(dest, src)
(B) strcopy(dest, src)
(C) strncpy(dest, src, n)
(D) strcopy(dest, src, n)
Answer: (A) strcpy(dest, src)
Q#11: What is the output?
char str[] = “C++”;
cout << str[3];
(A) +
(B) \0
(C) C
(D) Error
Answer: (B) \0
Q#12: How do you concatenate two C-style strings?
(A) strcat(str1, str2)
(B) str1 + str2
(C) str.concat(str2)
(D) str.append(str2)
Answer: (A) strcat(str1, str2)
Q#13: Which of the following is true about strcpy?
(A) Copies source string including ‘\0’
(B) Copies only characters, not ‘\0’
(C) Returns the length of copied string
(D) Appends source to destination
Answer: (A) Copies source string including ‘\0’
Q#14: What is the output of this code?
char str[] = “Hello”;
str[0] = ‘J’;
cout << str;
(A) Jello
(B) Hello
(C) J
(D) Error
Answer: (A) Jello
Q#15: Which of the following compares two C-style strings?
(A) strcmp(str1, str2)
(B) str1 == str2
(C) str.equals(str2)
(D) str1.compare(str2)
Answer: (A) strcmp(str1, str2)
Q#16: What is returned by strcmp if strings are equal?
(A) 1
(B) 0
(C) -1
(D) NULL
Answer: (B) 0
Q#17: Which header file is needed for strcmp?
(A) <cstring>
(B) <string>
(C) <iostream>
(D) <cstdlib>
Answer: (A) <cstring>
Q#18: Can you assign one C-style string to another directly?
(A) Yes
(B) No, must use strcpy
(C) Only for global arrays
(D) Only if sizes match
Answer: (B) No, must use strcpy
Q#19: What happens if you forget ‘\0’ in a C-style string?
(A) Compiler error
(B) Undefined behavior
(C) String works normally
(D) Runtime exception
Answer: (B) Undefined behavior
Q#20: How do you declare a pointer to a C-style string?
(A) char str;
(B) char str;
(C) char &str;
(D) char str;
Answer: (A) char *str;
Q#21: What is the output?
char str[] = “ABC”;
cout << sizeof(str);
(A) 3
(B) 4
(C) Depends on compiler
(D) Error
Answer: (B) 4
Q#22: Which of the following safely copies n characters of a string?
(A) strncpy(dest, src, n)
(B) strcpy(dest, src)
(C) strcat(dest, src)
(D) strcopy(dest, src, n)
Answer: (A) strncpy(dest, src, n)
Q#23: What is the output of this code?
char str[5] = {‘H’,’i’};
cout << str[2];
(A) i
(B) \0
(C) Garbage
(D) Error
Answer: (B) \0
Q#24: Which of the following is illegal?
(A) char str[] = “Hello”;
(B) char str[3] = “Hello”;
(C) char str[6] = “Hello”;
(D) char str[10] = “Hi”;
Answer: (B) char str[3] = “Hello”;
Q#25: What is the purpose of ‘\0’ in C-style strings?
(A) Marks end of string
(B) Stores first character
(C) Marks string length
(D) Is optional
Answer: (A) Marks end of string
Q#26: How do you read a line of text into a C-style string safely?
(A) cin >> str
(B) cin.getline(str, size)
(C) gets(str)
(D) scanf(“%s”, str)
Answer: (B) cin.getline(str, size)
Q#27: Which function concatenates only n characters of a string?
(A) strncat(dest, src, n)
(B) strcat(dest, src)
(C) strncpy(dest, src, n)
(D) strcpy(dest, src)
Answer: (A) strncat(dest, src, n)
Q#28: What is the output?
char str[6] = “Hello”;
str[5] = ‘!’;
cout << str;
(A) Hello!
(B) Hello
(C) Undefined behavior
(D) Compiler error
Answer: (C) Undefined behavior
Q#29: Which of the following is true about C-style strings?
(A) Size must be specified for automatic arrays
(B) Can use pointer arithmetic
(C) End with ‘\0’
(D) All of the above
Answer: (D) All of the above
Q#30: How do you safely compare two strings for equality?
(A) if(strcmp(str1, str2) == 0)
(B) if(str1 == str2)
(C) if(str1.equals(str2))
(D) if(str1.compare(str2) == 0)
Answer: (A) if(strcmp(str1, str2) == 0)
Q#31: What will the output?
char str[10];
strcpy(str, “Hi”);
cout << str[2];
(A) i
(B) \0
(C) Garbage
(D) H
Answer: (B) \0