1. Which assignment statements will copy the value \"toaster\" into a string var
ID: 3549075 • Letter: 1
Question
1. Which assignment statements will copy the value "toaster" into a string variable (str1)?
a. strcpy(str1,"toaster");
b. str1 = "toaster";
c. str1 = toaster;
d. str1 += toaster;
2. If the name of a file to open is in the string variable name fileName, which of the following will correctly open the file for output?
a. out_file.open(fileName);
b. out_file.open("fileName");
c. fileName.open(outfile);
d. out_file.open(fileName.c_str());
3. Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line?
a. getline(fin, line);
b. fin.getline(line);
c. fin.getline(line,' ');
d. fin.getline(line,80);
4. The notation vector<Base_Type> means that the vector is a(n):
a. array.
b. template class.
c. primitive data type.
d. All of the choices apply..
5. The base type for a vector can be:
a. int.
b. float or double.
c. char.
d. any data type.
6. What is the proper way to declare a vector of strings named names?
a. vector strings names;
b. vector<names> string;
c. vector<string> names;
d. All of the choices apply.
7. What is the value of numbers.size() after the following code?
vector<float> numbers;
numbers.reserve(100)
a. 0
b. 10
c. 100
d. Unknown
8. When a vector is assigned to another vector:
a. only the location of the vector is copied.
b. all the values in the vector are copied.
c. if there is not enough room in the left-hand vector, then not all the values from the right side are copied.
d. None of the choices apply.
9. Given the following code, what is the correct statement to insert the string str2 into str1, directly after the 'd'?
string str1="abcdefg";
string str2="ABCDE";
a. str1.insert(4,str2);
b. str2.insert(4,str1);
c. insert(str1,4)=str2;
d. insert(str2,4)=str1;
10. Which of the following are correct statements about vector member functions studied in this chapter?
a. push_front (baseTypeObject) puts object on the front of a vector.
b. Indexing uses index values 0 through size( ).
c. Size( ) tells how many base type objects have been inserted into the vector.
d. When a vector runs out of space all implementations are required to double the amount of allocated space.
11. Which of the following correctly declare 3 integer pointers?
a. int* p1, p2, p3;
b. int *p1, p2, p3;
c. int *p1, *p2, *p3;
d. All of the choices apply.
12. What is the output of the following code fragment?
int v1=2, v2=-1, *p1, *p2;
p1 = & v1;
p2= & v2;
p2=p1;
cout << *p2 << endl;
a. 2
b. -1
c. -2
d. 1
13. Which of the following statements correctly prints out the value that is in the memory address that the pointer p1 is pointing to?
a. cout << &p1;
b. cout << p1;
c. cout << int* p1;
d. cout << *p1;
14. What is the output of the following code?
int *p1, *p2;
p1 = new int;
p2 = new int;
*p1=11;
*p2=0;
p2=p1;
cout << *p1 <<" " << *p2 << endl;
a. 11 0
b. 0 11
c. 11 11
d. 0 0
15. Which of the following correctly declares a user-defined data type that defines a pointer to a float?
a. float* floatPtr;
b. typedef float* floatPtr;
c. typedef floatPtr *float;
d. typedef floatPtr* float;
16. Given that a typedef for IntPtr defines a pointer to an integer, what would be the correct declaration for a function that expects a reference to an integer pointer?
a. void f1 (IntPtr& ptr);
b. void f1 (IntPtr&* ptr);
c. void f1 (IntPtr*& ptr);
d. All of the choices apply.
17. Which of the following correctly declares a dynamic array of strings?
a. p1 = new string(13);
b. p1 = new string[];
c. p1 = new string[13];
d. p1 = new stringArray(13);
18. Assuming that the pointer variable p1 is of the correct type and size is an integer with some value > 1, which of the following declarations are legal?
a. p1 = new string[size]
b. p1 = new ifstream[size]
c. p1 = new char[size*size]
d. All of the choices apply.
19. If a program requires a dynamically allocate two-dimensional array, you would allocate the memory by using:
a. p1 = new int*[numRows];
for(int i=0; i < numRows; i++)
p1[i] = new int[numColumns];
b. p1 = new int*[numRows][numColumns];
c. p1 = new[numRows][numColumns]int;
d. None of the choices apply.
20. In which case would you consider using a dynamic array?
a. If the array is small, and the size is known before the program runs
b. If the program needs to get the size of the array from the user
c. If the array size is big, but known at compile time
d. You should always use a dynamic array.
21. The following code declares a vector of characters: vector characters<char>
a. True
b. False
22. The following code declares a vector of integers named numbers that reserves space for 100 integers: vector<int> numbers(100);
a. True
b. False
23. Vectors can have any type as the base type.
a. True
b. False
24. Vectors and arrays are the same data type.
a. True
b. False
25. Using the == operator on a string variable results in the same value as using strcmp on two c-strings.
a. True
b. False
26. Using the [i] on a string variable does not check for illegal values of i.
a. True
b. False
27. A string variable and a c-string are the same data type.
a. True
b. False
28. The function used to 'put two c-strings together into one" is called strcat.
a. True
b. False
29. The following declares a c-string and initializes it to "speaker:" char str[]="speaker";
a. True
b. False
30. The following declares a c-string variable that will hold 10 letters: char str[10];
a. True
b. False
Explanation / Answer
1 a; 2.b; 3.b; 4.d; 5.d; 6.c; 7.c; 8.a; 9. a; 10. c ;11.c; 12.a; 13.d;14.c; 15.a; 16.b ;17.c; 18.d; 19.d; 20.b; 21.b; 22.a ;23.a; 24.b; 25.b 26.b; 27.b; 28.a; 29.b; 30.a
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.