Exam 1 practice (eCampus)
我是个没有感情的搬运工具人✌️
1. Given a function with one vector parameter ages. How should the parameter be defined if ages may be very large, and the function will not modify the parameter?
a. Constant, and pass by reference
b. Constant, and pass by value
c. Not constant, and pass by reference
d. Not constant, and pass by value
2. The numNegatives variable counts the number of negative values in the vector userVals. What should numNegatives be initialized to?
vector userVals(20);
unsigned int i;
numNegatives = XXX;
for (i = 0; i < userValues.size(); ++i) {
if (userValues.at(i) < 0) {
numNegatives = numNegatives + 1;
}
}
a. No initialization needed
b. 0
c. -1
d. userVals.at(0)
3. Which expression for XXX outputs "Modern era" for any year 1980 and later?
if (year < 2000) {
// Output "Past"
} else if (XXX) {
// Output "Modern era"
}
a. year > 1980
b. year >= 1980
c. year >= 2000
d. (No such expression exists)
4. What is output?
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFS;
ifstream inFS;
string str;
outFS.open("myfile.txt");
outFS << "This is a test data.";
if (!outFS.is_open()) {
cout << "Could not open file myoutfile.txt." << endl;
return 1;
}
outFS.close();
inFS.open("myfile.txt");
cout << "Reading from file..." << endl;
inFS >> str;
cout << str;
return 0;
}
Reading from file...
This
5. Given integer vector x has elements 5, 10, 15, 20. What is the output?
int i;
for (i = 0; i < x.size(); ++i) {
cout << x.at(i) + x.at(i + 1);
}
Error //"out of range"
6. Which assigns the vector's first element with 99?
vector myVector(4);
a. myVector.at() = 99;
b. myVector.at(-1) = 99;
c. myVector.at(0) = 99;
d. myVector.at(1) = 99;
7. Which XXX generates "Adam is 30 years old." as the output?
#include <iostream>
using namespace std;
int main() {
string name = "Adam";
int age = 30;
XXX
return 0;
}
a. cout << name << " is " << age << " years old.";
b. cout << "name" << " is " << "age" << " years old .";
c. cout >> name >> " is " >> age >> " years old .";
d. cout >> "name" >> " is " >> "age" >> " years old .";
8. Which declares two related integer vectors named personName and personAge each with 50 elements?
a. vector personName, personAge;
b. vector personName, personAge = 50;
c. vector personName = 50; vector personAge = 50;
d. vector personName(50); vector personAge(50);
Answer: vector<int>personName(50); vector<int>personAge(50);
9. Given string s is "Sunday", what is the ending value of char myChar?
myChar = s.at(s.size() - 1);
a. a
b. y
c. "
d. No such character
10. myString is declared as a string and is the only variable declared. Which is a valid assignment?
a. myString = 'Hello';
b. myString = Hey;
c. myString = "H";
d. myString = [Hoy];
11. What value of x outputs "Junior"?
if (x < 56) {
// Output "Sophomore"
} else if (x > 56) {
// Output "Senior"
} else {
// Output "Junior"
}
a. Value 56
b. Values 57 or larger
c. Values 55 or 57
d. No such value
12. The ostringstream member function __ returns the contents of an ostringstream buffer as a string.
a. str()
b. setw()
c. setfill()
d. fixed()
13. What is the ending value of numItems if myChar = 'X'?
switch (myChar) {
case 'W':
numItems = 1;
case 'X':
numItems = 2;
case 'Y':
numItems = 3;
case 'Z':
numItems = 4;
}
2
14. Which XXX will search the input name in the GuestList.txt file?
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFS;
string userName;
string listName;
int flag = 0;
inFS.open("GuestList.txt");
if (!inFS.is_open()) {
cout << "Could not open file numFile.txt." << endl;
return 1;
}
cout << "Enter guest name: ";
cin >> userName;
while (!inFS.eof()) {
inFS >> listName;
XXX {
if(listName == userName) {
flag = 1;
}
}
}
if(flag == 1){
cout << userName << " is in the guest list" << endl;
} else {
cout << userName << " not in the guest list" << endl;
}
inFS.close();
return 0;
}
A. if(!inFS.fail)
B. if(inFS.fail)
C. if(inFS.fail())
D. if(!inFS.fail())
15. What is output?
#include <iostream>
#include <ios>
#include <iomanip>
using namespace std;
int main() {
int num = 5;
for(int i = 0; i < 3; i++) {
cout << num << setw(5) << setfill('*') << num << endl;
num = num * 5;
}
return 0;
}
5****5
25***25
125**125
16. What is y after executing the statements?
x = 5;
y = x + 1;
y = y * 2;
y = 12
17. Which value of x results in short circuit evaluation, causing y < 4 to not be evaluated?(x >= 7) && (y < 4)?
Answer: x < 7
18. What is the output?
int a = 10;
do {
cout << a << " ";
a = a + 1;
} while (a < 15);
10 11 12 13 14 15
19. What is the output, if the input is 3 2 1 0?
cin >> x;
while (x > 0) {
cout << 2 * x << " ";
}
6 4 2
20. What is the ending value of z?
x = 0;
y = 3;
z = pow(x + 2, y);
z = 8
21. Both must be true for a person to ride:
(1) At least 5 years old, (2) Taller than 36 inches.
Which expression evaluates to true if a person can ride?
yearold >= 5 && height > 36;
22. What is the output if 123ABC is the input?
#include <iostream>
using namespace std;
int main() {
int number;
cin >> number;
cout << number;
}
//123
23. which assignment would yield a non-floating-point number for y?
y = 32.0 + (x / (z + 1.0));
a. x = 0.0; z = 1.0;
b. x = -32.0; z = 1.0;
c. x = 0.0; z = 0.0;
d. x = 1.0; z = -1.0;
24. what is the output?
letter1 = 'p';
while (letter1 <= 'q') {
letter2 = 'x';
while (letter2 <= 'y') {
cout << letter1 << letter2 << " ";
++letter2;
}
++letter1;
}
px py qx qy ;
25. Which is true regarding how functions work?
a. After a function returns, its local variables keep their values, which serve as their initial calues the next time the function is called
b. A function's local variables are discarded upon a function's return; each new call creats new local variables in memory
c. A return address indicates the value rturned by the function
d. If a function returns a variable, the function stores the variable's value until the function is called again
26. The _____ function reads an input line into a string.
a. str();
b. getline();
c. getchar();
d. clear();
27. A character variable's value is stored in memory as _____ .
a. a graphical symbol
b. a string
c. a floating-point value
d. an interger value
28. What is the output?
int main() {
for (int i = 0; i < 3; ++i) {
cout << i;
}
cout << i;
return 0;
}
Error: The variable i is not declared in the right scope
i 在for loop里面定义的,外面不能用调用。如果i在外面定义,output为0123
29. 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)
Last updated
Was this helpful?