CIS22B - Chapter 5 Questions (FINAL)
What is the ending value of the element at index 0? int numbers[10]; numbers[0] = 35; numbers[1] = 37; numbers[1] = numbers[0] + 4; a. 0 b. 35 c. 37 d. 39
b. 35
What is the index of the last element? a. 0 b. 49 c. 50 d. Unknown, because the array has not been intialized.
b. 49
What is the ending value of myStr? char myStr[5] = "Hi"; strcpy(myStr, "Hey"); a. Hi b. Hey c. HiHey d HeyHi
b. Hey
Which assigns the vector's last element with 99? vector<int> myVector(15); a. my.Vector.at(0) = 99; b. my.Vector.at(14) = 99; c. my.Vector.at(15) = 99; d. my.Vector.at(16) = 99;
b. my.Vector.at(14) = 99;
Which XXX / YYY declare a vector having MAX_SIZE elements and initializes all elements with -1? const int MAX_SIZE = 4; vecotr<int> myNumbers(XXX); int i; for (i = 0; i < YYY; ++i) { myNumbers.at(i) = -1; } a. MAX_SIZE / MAX_SIZE b. MAX_SIZE / MAX_SIZE - 1 c. MAX_SIZE - 1 / MAX_SIZE d. MAX_SIZE - 1 / MAX_SIZE - 1
a MAX_SIZE / MAX_SIZE
What are the ending values in vector origValues? Note the question asks for origValues, not newValues. const int NUM_ELEMENTS = 4; vector<int> origValues(NUM_ELEMENTS); vector<int> newValues(NUM_ELEMENTS); origValues.at(0) = 10; origValues.at(1) = 20; origValues.at(2) = 30; origValues.at(3) = 40; newValues = origValues; newValues.at(2) = 21; newValues.at(3) = 22; a. 10, 20, 30, 40 b. 0, 0, 21, 22 c. 10, 20, 21, 22 d. Error: Invalid assignment of vectors
a. 10, 20, 30, 40
What is the output? const int MAX_LEN = 10; char userStr[MAX_LEN] = "PASS123"; int i; for (i = 0; userStr[i] != '\0'; ++i) { userStr[i] = tolower(userStr[i]); } a. pass123 b. Pass123 c. PASS123 d. Error: The string contains a digit that cannot be converted to lowercase
a. pass123
What code for XXX, YYY correctly swaps a and b? Choices are written as XXX / YYY. XXX; a = b; YYY; b = tmp; a. tmp = a / (nothing) b. tmp = a / tmp = b c. tmp = b / (nothing) d. (nothing) / tmp = a
a. tmp = a / (nothing)
What are the ending values in vector newValues? const int NUM_ELEMENTS = 4; vector<int> origValues(NUM_ELEMENTS); vector<int> newValues(NUM_ELEMENTS); origValues.at(0) = 10; origValues.at(1) = 20; origValues.at(2) = 30; origValues.at(3) = 40; newValues = origValues; newValues.at(2) = 21; newValues.at(3) = 22; a. 10, 20, 30, 40 b. 0, 0, 21, 22 c. 10, 20, 21, 22 d. c. 10, 20, 21, 22
c. 10, 20, 21, 22
Given vector userVal has 4 elements. What is the size of userVal after the following? userVal.resize(5); a. 1 b. 4 c. 5 d. 9
c. 5
Which XXX outputs all the elements of vector myVals? for (i = 0; XXX; ++i) { cout << myVals.at(i) << " "; } a. i < myVals.size; b. i <= myVals.size(); c. i < myVals.size(); d. No such expression as the number of elements is unknown.
c. i < myVals.size();
Which best describes what is output? Assume v is a large vector of ints. int i; int s; s = v.at(0); for (i = 0; i < v.size(); ++i) { if (s > v.at(i)) { s = v.at(i); } } cout << s; a. first value in v b. max value in v c. min value in v d. last value in v
c. min value in v
Which assigns the vector's first element with 99? vector<int> myVector(4); a. myVector.at() = 99; b. myVector.at(-1) = 99; c. myVector.at(0) = 99; d. myVector.at(1) = 99;
c. myVector.at(0) = 99;
Given two vectors, studentNames that maintains a list of students, and studentScores that has a list of the scores for each student, which XXX and YYY prints out only the student names whose score is above 80? Choices are in the form XXX/ YYY. vector<string> studentNames(NUM_STUDENTS); vector<int> studentScores(NUM_STUDENTS); unsigned int i; for (i = 0; i < studentScores.size(); ++i) { if (XXX > 80) { cout << YYY << " "; } } a. studentNames.at(i) / studentNames.at(i) b. studentNames.at(i) / studentScores.at(i) c. studentScores.at(i) / studentNames.at(i) d. studentScores.at(i) / studentScores.at(i)
c. studentScores.at(i) / studentNames.at(i)
Which XXX and YYY will find the minimum value of all the elements in the array? Choices are in the form XXX / YYY. int userVals[NUM_ROWS][NUM_COLS]; int minVal = userVals[0][0]; for (i = 0; i < NUM_ROWS; ++i) { for (j = 0; j < NUM_COLS; ++j) { if (XXX) { YYY; } } } a. userVals[i] < minVal / minVal = userVals[i] b. userVals[j] < minVal / minVal = userVals[j] c. userVals[i][j] < minVal / minVal = userVals[i][j] d. userVals[i][j] > minVal / minVal = userVals[i][j]
c. userVals[i][j] < minVal / minVal = userVals[i][j]
For the given program, which XXX iterates through the vector to find the state that is input (userState) until a match is found? vector<string> stateNames(NUM_STATES); vector<int> statePop(NUM_STATES); string userState; bool foundState = false; unsigned int i; cin >> userState; foundState = false; for (i = 0; XXX; ++i) { if (stateNames.at(i) == userState) { foundState = true; cout << userState << ", " << statePop.at(i) << endl; } } a. (i < NUM_STATES - 1) b. (i < NUM_STATES) c. (i < NUM_STATES - 1) && (!foundState) d. (i < NUM_STATES) && (!foundState)
d. (i < NUM_STATES) && (!foundState)
How many elements does the vector declaration create? vector<int> scores(10); // Vector declaration scores.at(0) = 25; scores.at(1) = 22; scores.at(2) = 18; scores.at(3) = 28; a. 0 b. 4 c. 9 d. 10
d. 10
What are the ending contents of the vector? Choices show elements in index order 0, 1, 2. vector<int> yearsList(3); yearsList.at(0) = 5; yearsList.at(1) = yearsList.at(0); yearsList.at(0) = 10; yearsList.at(2) = yearsList.at(1); a. 5, 5, 5 b. 5, 10, 10 c. 10, 10, 10 d. 10, 5, 5
d. 10, 5, 5
What is the ending value of userNum? vector<int> userValues; int userNum; userNum = 0; userValues.push_back(45); userValues.push_back(55); userNum = userValues.pop_back(); a. 0 b. 45 c. 55 d. Error: pop_back does not return a value
d. Error: pop_back does not return a value
Which is true for the following code? char userText[10]; userText[0] = 'B'; userText[1] = 'o'; userText[2] = 'o'; userText[3] = 'k'; userText[4] = '\0'; ... userText[3] = 't'; a. The first four characters of the array userText are Book. b. The first five characters of the array userText are Bookt. c. The compiler generates an error because element 3 can't be overwritten. d. Printing the array userText will work fine because the new string is 4 characters and the array size is 10.
d. Printing the array userText will work fine because the new string is 4 characters and the array size is 10.
The following program generates an error. Why? const int NUM_ELEMENTS = 5; vector<int> userVals(NUM_ELEMENTS); unsigned int i; userVals.at(0) = 1; userVals.at(1) = 7; userVals.at(2) = 4; for (i = 0; i <= NUM_ELEMENTS; ++i) { cout << userVals.at(i) << endl; } a. Variable i is declared as an unsigned integer. b. The vector userVals has 5 elements, but only 3 have values assigned. c. The integer NUM_ELEMENTS is declared as a constant d. The for loop tries to access an index that is out of the vector's valid range.
d. The for loop tries to access an index that is out of the vector's valid range.
Given an integer vector of size NUM_ELEMENTS, which XXX, YYY, and ZZZ will count the number of times the value 4 is in the vector? Choices are in the form XXX / YYY / ZZZ. vector<int> myVect(NUM_ELEMENTS); int cntFours; XXX for (i = 0; YYY; ++i) { if (myVals.at(i) == 4) { ZZZ; } } a. cntFours = myVect.at(0); / i > myVect.size(); / cntFours = myVect.at(i); b. cntFours = myVect.at(1); / i < myVect.size(); / cntFours = myVect.at(i) + 1; c. cntFours = 1; / i > NUM_ELEMENTS; / cntFours = cntFours + 1; d. cntFours = 0; / i < NUM_ELEMENTS; / ++cntFours;
d. cntFours = 0; / i < NUM_ELEMENTS; / ++cntFours;
Which XXX prints the message only if str1 and str2 are equal? if (XXX) { // Print "strings are equal" } a. str1 == str2 b. strcat(str1, str2) == 0 c. strcpy(str1, str2) == 0 d. strcmp(str1, str2) == 0
d. strcmp(str1, str2) == 0