Self-Test Exercises - Introduction to problem solving and programming | CECS 174, Exams of Computer Science

Material Type: Exam; Class: Programming & Problem Solving I; Subject: Computer Engr & Computer Sci; University: California State University - Long Beach; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-pq2
koofers-user-pq2 🇺🇸

1

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 11 Self Test Exercises
1. Which of the following declarations are equivalent?
char string_var[10] = “Hello”;
char string_var[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
char string_var[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’};
char string_var[6] = “Hello”;
char string_var[] = “Hello”;
2. What C string will be stored in singing_string after the following code is run?
char singing_string[20] = “DoBeDo”;
cout strcat(singing_string, “ to you”);
Assume that the code is embedded in a complete and correct program and that an include
directive for <cstring> is in the program file.
3. What (if anything) is wrong with the following code?
char string_var[] = “Hello”;
strcat(string_var, “ and Good-bye.”);
cout << string_var;
Assume that the code is embedded in a complete and correct program and that an include
directive for <cstring> is in the program file.
4. Suppose the function strlen (which returns the length of its string argument) was not
already defined for you. Give a function definition for strlen. Note that strlen has
only one argument, which is a C string. Do not add additional arguments; they are not
needed.
5. What is the length (maximum) of a string that can be placed in the string variable declared
by the following declaration? Explain.
char s[6];
6. How many characters are in each of the following character and string constants?
a.‘\n’
b.‘n’
c.“Mary”
d.“M”
e.“Mary\n”
7. Since character strings are just arrays of char, why does the test caution you not to confuse
the following declaration and initialization?
char short_string[] = “abc”;
char short_string[] = {‘a’, ‘b’, ‘c’};
pf3

Partial preview of the text

Download Self-Test Exercises - Introduction to problem solving and programming | CECS 174 and more Exams Computer Science in PDF only on Docsity!

Chapter 11 Self Test Exercises

  1. Which of the following declarations are equivalent? char string_var[10] = “Hello”; char string_var[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; char string_var[10] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’}; char string_var[6] = “Hello”; char string_var[] = “Hello”;
  2. What C string will be stored in singing_string after the following code is run? char singing_string[20] = “DoBeDo”; cout strcat(singing_string, “ to you”); Assume that the code is embedded in a complete and correct program and that an include directive for is in the program file.
  3. What (if anything) is wrong with the following code? char string_var[] = “Hello”; strcat(string_var, “ and Good-bye.”); cout << string_var; Assume that the code is embedded in a complete and correct program and that an include directive for is in the program file.
  4. Suppose the function strlen (which returns the length of its string argument) was not already defined for you. Give a function definition for strlen. Note that strlen has only one argument, which is a C string. Do not add additional arguments; they are not needed.
  5. What is the length (maximum) of a string that can be placed in the string variable declared by the following declaration? Explain. char s[6];
  6. How many characters are in each of the following character and string constants? a.‘\n’ b.‘n’ c.“Mary” d.“M” e.“Mary\n”
  7. Since character strings are just arrays of char, why does the test caution you not to confuse the following declaration and initialization? char short_string[] = “abc”; char short_string[] = {‘a’, ‘b’, ‘c’};
  1. Given the following declaration and initialization of the string variable, write a loop to assign ‘X’ to all positions of this string variable, keeping the length the same. char our_string[15] = “Hi there!”;
  2. Given the declaration of a C-string variable, where SIZE is a defined constant: char our_string[SIZE]; The C-string variable our_string has been assigned in code not shown here. For correct C- string variables, the following loop reassigns all positions of our_string the value ‘X’, leaving the length the same as before. Assume this code fragment is embedded in an otherwise complete and correct program. Answer the questions following this code fragment: int index = 0; while (our_string[index] != ‘\0’) { our_string[index] = ‘X’; index++; } a. Explain how this code can destroy the contents of memory beyond the end of the array. b. Modify this loop to pretect against inadvertently changing memory beyond the end of the array. 10.Write code using a library function to copy the string constant “Hello” into the string variable declared below. Be sure to #include the necessary header file to get the declaration of the function you use. char a_string[10]; 11.What string will be output when this code is run? (Assume, as always, that this code is embedded in a complete, correct program.) char song[10] = “I did it “; char franks_song[20]; strcpy(franks_song, song); strcat(franks_song, “my way!”); cout << franks_song << endl; 12.What is the problem (if any) with this code? char a_string[20] = “How are you? “; strcat(a_string, “Good, I hope.”); 13.Consider the following code (and assume it is embedded in a complete and correct program and then run): char a[80], b[80]; cout << “Enter some input:\n”; cin >> a >> b; cout << a << ‘-‘ << b << “END OF OUTPUT\n”;