Study Guide 8: Strings and C-Strings

  1. C++ has two string data types.
    1. True
    2. False
  2. 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).
    1. char city[19]
    2. char city[20]
    3. char city[21]
    4. char city[22]
  3. If s is a C-string, choose the best way to find its length.
    1. strlen(s)
    2. s.strlen
    3. s.strlen()
    4. strlen.s
    5. s.length
    6. s.length()
  4. If s is an instance of the string class, choose the best way to find its length.
    1. strlen(s)
    2. s.strlen
    3. s.strlen()
    4. strlen.s
    5. s.length
    6. s.length()
  5. The extraction operator (>>) stops reading a string when it encounters a space.
    1. True
    2. False
  6. Given the variable definition
    char name[100];
    Write a statement that can read "Cranston Q. Snort" from the console into name.
  7. Given the variable definition
    string name;
    Write a statement that can read "Cranston Q. Snort" from the console into name.
  8. If s1 and s2 are both appropriately defined C-strings, choose the best statement to copy s1 to s2.
    1. s2 = s1;
    2. s1 = s2;
    3. strcpy(s2, s1);
    4. strcpy(s1, s2);
  9. If s1 and s2 are both instances of the C++ string class, choose the best statement to copy s1 to s2.
    1. s2 = s1;
    2. s1 = s2;
    3. strcpy(s2, s1);
    4. strcpy(s1, s2);
  10. 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];
  11. The variables name and blank are instances of the C++ string class. Write a statement to copy name to blank.
  12. Choose an expression that tests two C-strings for equality.
    1. s1 == s2
    2. strcmp(s1, s2) == 0
  13. Choose an expression that tests two instances of the sting class for equality.
    1. s1 == s2
    2. strcmp(s1, s2) == 0
  14. 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?
    1. char *s1 = "";
    2. char s1[] = "";
    3. char s1[100] = "";
    4. None of the above
  15. When accessing a character in a C-string, C++ always checks the index and validates that it is within bounds.
    1. true
    2. false
  16. 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).
    1. true
    2. false
  17. What does the following C++ code fragment print?
    int n = 3;
    cout << (char)(n + '0') << endl;
    1. The code will not compile because you can't add an int and char.
    2. 51
    3. 33
    4. 3
  18. What does the following C++ code fragment print?
    char c = 'D';
    cout << (c - 'A') << endl;
    1. The code will not compile because you can't subtract a char from an int.
    2. 51
    3. 33
    4. 3
  19. Choose the correct main function definition to allow a program to access command line arguments.
    1. main(char argc, char argv)
    2. main(int argc, char* argv[])
    3. main(char* argv[], int argc)
    4. main(int argc, char argv[])
  20. 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]?
  21. 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.
    1. It is impossible to convert an array into a pointer.
    2. *name
    3. &name
    4. name
  22. Given the definition string s; and assuming that variable s is appropriately initialized, which expression best converts s to a value of type double?
    1. to_string(s)
    2. stod(s)
    3. ftoa(f, s, 10)
    4. atof(s)
    5. strtod(s, nullptr);
  23. Given the definition char* s; and assuming that variable s is appropriately initialized, which expression best converts s to a value of type double?
    1. to_string(s)
    2. stod(s)
    3. ftoa(f, s, 10)
    4. atof(s)
    5. strtod(s, nullptr);
  24. 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.
    1. This is an error in the original implementation, but the behavior has been retained to avoid breaking existing code.
    2. This is done to reserve space for the null termination character at the end of a string object.
    3. 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.
    4. It allows the compiler to delete the memory allocated to hold the string.
  25. 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).
  26. 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).
  27. Reviewing C-Strings and Scope will help you answer the following questions.

  28. 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;
    }
  29. 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;
    }
  30. 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
    }
  31. 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;
    }
  32. 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.
    InputOutput
    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?