- C++ has two string data types.
- True
- False
- Choose the statement that defines a C-string variable called city that can hold a string of up to 20 characters (this is tricky question, be careful).
- char city[19]
- char city[20]
- char city[21]
- char city[22]
- If s is a C-string, choose the best way to find its length.
- strlen(s)
- s.strlen
- s.strlen()
- strlen.s
- s.length
- s.length()
- If s is an instance of the string class, choose the best way to find its length.
- strlen(s)
- s.strlen
- s.strlen()
- strlen.s
- s.length
- s.length()
- The extraction operator (>>) stops reading a string when it encounters a space.
- True
- False
- Given the variable definition
char name[100];
Write a statement that can read "Cranston Q. Snort" from the console into name.
- Given the variable definition
string name;
Write a statement that can read "Cranston Q. Snort" from the console into name.
- If s1 and s2 are both appropriately defined C-strings, choose the best statement to copy s1 to s2.
- s2 = s1;
- s1 = s2;
- strcpy(s2, s1);
- strcpy(s1, s2);
- If s1 and s2 are both instances of the C++ string class, choose the best statement to copy s1 to s2.
- s2 = s1;
- s1 = s2;
- strcpy(s2, s1);
- strcpy(s1, s2);
- Write a statement that uses a C-string library function to copy the C-string name to the C-string blank, which is defined as
char blank[100];
- The variables name and blank are instances of the C++ string class. Write a statement to copy name to blank.
- Choose an expression that tests two C-strings for equality.
- s1 == s2
- strcmp(s1, s2) == 0
- Choose an expression that tests two instances of the sting class for equality.
- s1 == s2
- strcmp(s1, s2) == 0
- The prototype given in the documentation for C-string concatenation is
strcat(char* s1, const char* s2);
Which of the following is the best definition AND initialization (to an empty string) of the variable s1?
- char *s1 = "";
- char s1[] = "";
- char s1[100] = "";
- None of the above
- When accessing a character in a C-string, C++ always checks the index and validates that it is within bounds.
- true
- false
- When accessing a character in an instance of the string class, C++ always checks the index and validates that it is within bounds (caution, this is a trick question).
- true
- false
- What does the following C++ code fragment print?
int n = 3;
cout << (char)(n + '0') << endl;
- The code will not compile because you can't add an int and char.
- 51
- 33
- 3
- What does the following C++ code fragment print?
char c = 'D';
cout << (c - 'A') << endl;
- The code will not compile because you can't subtract a char from an int.
- 51
- 33
- 3
- Choose the correct main function definition to allow a program to access command line arguments.
- main(char argc, char argv)
- main(int argc, char* argv[])
- main(char* argv[], int argc)
- main(int argc, char argv[])
- A C++ program named my_program is compiled and run from the command line as
my_program file1 file2 file3 file4
Assuming that main's arguments are traditionally named, what is the value of argc, and what is stored in argv[2]?
- Examine the following code fragment. The character array name is defined as a global variable.
char name[100];
char* get_name()
{
.
.
.
return _______________;
}
From the following, choose the best code to complete the return statement to return the C-string variable name or to describe why the return statement cannot be completed correctly.
- It is impossible to convert an array into a pointer.
- *name
- &name
- name
- Given the definition
string s;
and assuming that variable s is appropriately initialized, which expression best converts s to a value of type double?
- to_string(s)
- stod(s)
- ftoa(f, s, 10)
- atof(s)
- strtod(s, nullptr);
- Given the definition
char* s;
and assuming that variable s is appropriately initialized, which expression best converts s to a value of type double?
- to_string(s)
- stod(s)
- ftoa(f, s, 10)
- atof(s)
- strtod(s, nullptr);
- The string class member function end() returns a string iterator that refers to a location in a string that is one character beyond the end of the string. Choose the best explanation for this.
- This is an error in the original implementation, but the behavior has been retained to avoid breaking existing code.
- This is done to reserve space for the null termination character at the end of a string object.
- If str is a string and itr is a string iterator, the expression
itr != str.end()
only becomes false after the iterator is advanced beyond the last character in the string.
- It allows the compiler to delete the memory allocated to hold the string.
- If itr is a string iterator, write the expression to access the character currently referenced by the iterator (just get the character, don't increment or decrement the iterator).
- If itr is a string iterator, write the expression to increment the iterator to the next character in the string (just advance the iterator to the next character, don't get the referenced character).
- Explain what, if anything, is wrong with the following code (assume all header files are included and that the user enters fewer than 100 characters). The question asks what, if anything, is wrong, NOT how to fix the code if needed.
char* read()
{
static char line[100];
cin.getline(line, 100);
return line;
}
- Explain what, if anything, is wrong with the following code fragment (assume all header files are included and that the user enters fewer than 100 characters; also assume that the dynamic memory is deallocated elsewhere). The question asks what, if anything, is wrong, NOT how to fix the code if needed.
char* read()
{
char* line = new char[100];
cin.getline(line, 100);
return line;
}
- Explain what, if anything, is wrong with the following code fragment (assume all header files are included and that the user enters fewer than 100 characters). The question asks what, if anything, is wrong, NOT how to fix the code if needed.
char* read(char* line)
{
cin.getline(line, 100);
return line;
}
void my_function()
{
char data[100];
read(data);
// do something with line
}
- Explain what, if anything, is wrong with the following code (assume all header files are included and that the user enters fewer than 100 characters). The question asks what, if anything, is wrong, NOT how to fix the code if needed.
char* read()
{
char line[100];
cin.getline(line, 100);
return line;
}
- A programmer attempts to write a program that reads names from the console, stores them in an array, and prints them on the screen.
#include <iostream>
using namespace std;
char* get_name()
{
static char name[100];
cout << "Please enter a name: ";
cin.getline(name, 100);
return name;
} |
int main()
{
char* array[3];
for (int i = 0; i < 3; i++)
array[i] = get_name();
for (int i = 0; i < 3; i++)
cout << array[i] << endl;
return 0;
} |
The program compiles and runs without crashing, producing the following output.
Input | Output |
Albert Einstein
Isaac Newton
Cranston Snort |
Cranston Snort
Cranston Snort
Cranston Snort |
What is the problem with the program and how can the programmer correct it?