sample_midterm cs mcq
pdf
keyboard_arrow_up
School
University of California, Riverside *
*We aren’t endorsed by this school
Course
010B
Subject
Computer Science
Date
Feb 20, 2024
Type
Pages
13
Uploaded by BailiffMink3426
eLearn Name ___________________________________________ NetID _______________________ Page 1 of 13 CS 010B - Intro to Computer Science II Fall - 2022
Midterm Exam Sample M.C. Problems You may have on your desks ONLY this exam, your "bubble sheet", use a blue or black pen, or #2 pencil to bubble in your answers.
All electronic devices must be turned off and completely out of sight (including watches). Answer all multiple-choice questions using the bubble sheet provided - a single best choice per question.
If you make a mistake, you may correct it by putting a neat 'X' through the mistaken bubble and then bubbling in the correct answer.
Be neat & careful! Any blotches and smudges and excessive corrections will make your sheet unreadable by the machine - if necessary, ask for a clean bubble sheet and redo your answers!
All written/coding questions must be answered on the exam itself, within the box allotted to it. Please write legibly and erase your mistakes rather than crossing them out. Be sure your name is on each sheet of paper that contains a written answer. Be sure to read each problem carefully. Bubble Sheet: Name
: use name as it is in eLearn ID
: use netID (e.g., tpayn002) Section
: your lecture section number Version
: A
Page 2 of 13 1. When overloading a binary operator function as a member
function, how many explicit parameters must be declared?
a. 0 b. 1 c. 2 d. 3 2. When overloading a binary operator function as a global
function, how many explicit parameters must be declared?
a. 0 b. 1 c. 2 d. 3 3. Assuming fin
is an ifstream object that has been connected to file1.txt
, what will be stored in the string variable str2
?
char ch; string str1 = "abc"; string str2 = "xyz"; fin >> ch; getline(fin, str1); getline(fin, str2); a.
"d" b.
"Why doesn't this work?" c.
"Don't forget the rules for the >> operator." d.
"Why" e.
"abc" f.
The empty string, i.e. "" 4. Assuming the user types the sentence
Try to be a rainbow in someone's cloud. and then pushes the ENTER key, what will the value of ch
be after the following code executes? char ch = 'a'; cin >> ch >> ch >> ch >> ch; a.
'T' b.
't' c.
'o' d.
'a' e.
' ' (space char) 5. A commuter swipes a public transport card at a kiosk and sees his account balance displayed on the screen. The commuter would be viewing an abstraction if _____.
a.
the machine is the only place on which the information is stored b.
there is other information about his account that is not displayed on screen c.
all the available information is displayed onscreen d.
the card is the only place on which the information is stored 6. Assuming these are all member function declarations for a class named Widget, which member function is an accessor function?
a.
void display(); b.
bool drive(int); c.
Widget(); d.
string buzz() const; 7. Given a class named Widget that has a constructor that is passed 2 integer parameters and no other constructors are defined for this class, which of the following statements has a syntax error?
a. return Widget(3, 4); b. Widget item(3, 4); c. Widget item = Widget(3, 4); d. Widget item = (3, 4); d Why doesn't this work? Don't forget the rules for the >> operator. Don't forget the rules for getline. file1.txt O
O
O
Tr
y
e
Name_______________________________________ Username_____________________________________ Page 3 of 13 8. Given a class named Widget that has an int member variable named foo
and a member function named func1
(declared below), which of the following statements would cause a syntax error within the Widget member function func1
? int func1(int bar) const; a.
foo = bar; b.
bar = foo; c.
cout << foo << endl; d.
return foo + bar; 9. Given a class named Widget that has an int member variable named foo
and a member function named func2
(declared below), which of the following statements would cause a syntax error within the Widget member function func2
? int func2(const Widget &bar); a.
foo = bar.foo; b.
bar.foo = foo; c.
cout << foo << endl; d.
return foo + bar.foo; 10. Given a class named Widget that has the following friend function declaration within the class declaration, which implementation (not within the class declaration) has the correct header for this friend function? friend void func3(); a.
friend Widget::void func3() { // does something } b.
friend void Widget::func3() { // does something } c.
void Widget::func3() const { // does something } d.
void func3() { // does something } 11. In object-oriented programming (OOP), _______________ refers to the concept of declaring class member variables private and only allowing users of the class to access them through the public member functions. a.
construction b.
encapsulation c.
inheritance d.
overloading 12. Assuming the variable fin is an ofstream variable connected to a file that contains only integers, which of the following code fragments will always result in the integer variable sum containing the sum of all of the integers within the file? a. int sum = 0; int n = 100; while (!fin.eof()) { fin >> n; sum += n; } b. int sum = 0; int n = 100; while (!fin.eof()) { if (!fin.fail()) { fin >> n; sum += n; } } c. int sum = 0; int n = 100; while (fin >> n) { sum += n; } d. int sum = 0; int n = 100; while (fin >> n) { fin >> n; sum += n; O
O
O
O
O
O
O
O
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Page 4 of 13 Use the following code to answer the next 4 questions. class Person { private: string name; int age; public: Person( ); Person(const string &name, int age); void setName(const string &name); void setAge(int age); const string getName( ) const; const int getAge( ) const; friend ostream & operator<<(ostream &, const Person &); }; int main( ){ Person p1; Person p2 = Person("Brad Pitt", 40); ... return 0; } 13. Which of the following lists only accessor functions of the Person class declared above? a.
getName, getAge, setName, setAge b.
getName, getAge c.
setName, setAge d.
Person(), Person(const string &string, int age) 14. Assigning a value to the member data field age
would cause a syntax
error in which of the following member functions of the Person class declared above? a.
the default constructor b.
getAge c.
setName d.
none of the them 15. Invoking (calling) the setAge
member function would cause a syntax
error in which of the following member functions of the Person class declared above? a.
the default constructor b.
getAge c.
setName d.
none of the them 16. Given the Person class declared above, what type of value is passed to the implicit
parameter for the following function call? p2.setName("Brad Jolie"); a.
Person b.
string c.
const string reference d.
there is no implicit parameter for this function
Name_______________________________________ Username_____________________________________ Page 5 of 13 17. Which of the following statements are true? a.
A struct cannot have member functions. b.
A struct cannot have private member variables. c.
The only difference between a struct and a class is whether the members are private or public by default. d.
A and B are both true.
18. Assuming fin
is an ifstream object that has been connected to file1.txt
, what will be output?
char ch1; string s1, s2; fin >> s1 >> ch1 >> s2; cout << s1 << ch1 << s2; a.
The limits b.
Thelimits c.
Thelimitsof d.
The l imits e.
The l of 19. Assuming fin
is an ifstream object that has been connected to file2.txt
, what will be output?
string str1 = "abc"; string str2 = "xyz"; string str3 = "nop"; getline(fin, str1); fin >> str2; getline(fin, str3); cout << str1 << str2 << str3; a.
What are the rules for the >> operator?Careful!!What about getline? b.
What are the rules for the >> operator?Careful!! c.
What are the rules for the >> operator? Careful!! What about getline? d.
What are the rules for the >> operator? Careful!! e.
Whatarethe 20. Given a class named Widget, which of the following is the correct declaration for a global
function that overloads the + operator to return the sum of 2 Widget objects? a.
const Widget operator+(const Widget &lhs, const Widget &rhs) const; b.
const Widget operator+(const Widget &rhs); c.
const Widget operator+(const Widget &rhs) const; d.
const Widget operator+(const Widget &lhs, const Widget &rhs);
21. Given a class named Widget, which of the following is the correct declaration for a member
function that overloads the + operator to return the sum of 2 Widget objects? a.
const Widget operator+(const Widget &lhs, const Widget &rhs) const; b.
const Widget operator+(const Widget &rhs); c.
const Widget operator+(const Widget &rhs) const; d.
const Widget operator+(const Widget &lhs, const Widget &rhs);
The limits of the possible can only be defined by going beyond them into the impossible. file1.txt What are the rules for the >> operator? Careful!! What about getline? file2.txt I
The
I
X
X
x
X
X
Page 6 of 13 Use the following code to answer the next 4 questions. class Mystery { private: string a; int b; public: Mystery( ); Mystery(const string &, int); void foo(const string &, int); const string bar(int) const; }; int main( ){ Mystery m1; Mystery m2 = Mystery("abc", 42); ... return 0; } 22. The function bar is a(n) _____________________ member function of class Mystery? a.
accessor b.
mutator c.
constructor 23. Assigning an integer value to the member variable b
would cause a syntax
error in which of the following member functions of the Mystery class declared above? a.
the default constructor b.
foo c.
bar d.
none of the them 24. Invoking (calling) the foo
member function would cause a syntax
error in which of the following member functions of the Mystery class declared above? a.
the default constructor b.
foo c.
bar d.
none of the them 25. Invoking (calling) the bar
member function would cause a syntax
error in which of the following member functions of the Mystery class declared above? a.
the default constructor b.
foo c.
bar d.
none of the them O
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Name_______________________________________ NetID_____________________________________ Page 7 of 13 26. Given the file in.dat
contains only integers and given the following declarations:
ifstream fin("in.dat"); int n; int sum = 0; which of the following while loops correctly calculates the sum all of the integers contained within the file in.dat? a.
while (!fin.eof()){ fin >> n; sum += n; } b.
while (fin){ cin >> n; sum += n; } c.
while (fin >> n){ sum += n; } 27. Given a class named Widget, which of the following expressions or statements does not call the Widget’s default constructor
?
e. Widget() f. Widget item; g. Widget item(); h. Widget item = Widget(); 28. If a const accessor (member) function calls a non-const mutator (member) function, a data member of the class may get changed.
a. TRUE b. FALSE 29. C
haracters written to cout are immediately written to a system’s standard output.
a. TRUE b. FALSE 30. Which of the following statements can be used to rid the stream of the newline character left over after an extraction operation (e.g., cin >> n
)?
a. cin.ignore(); b. cin.clear(); c. cin.good(); d. cin.bad(); 31. Assuming all necessary accessor functions are defined for a class, the comparison operator (
<
) can be overloaded for this class as a ________________ function. a.
member b.
friend c.
global (non-member, non-friend) d.
a) and b) only e.
b) and c) only f.
all three (a, b, and c) 32. Assuming all necessary accessor functions are defined for a class, the output stream operator (
<<
) can be overloaded for this class as a ________________ function.
a.
member b.
friend c.
global (non-member, non-friend) d.
a) and b) only e.
b) and c) only f.
all three (a, b, and c) X
O
O
O
O
O
O
O
Page 8 of 13 33. Given the main function header is:
int main(int argc, char *argv[]) and a command line that consists of: ./a.out -w dreams.txt -o out.dat what is the value of argc
? a.
0 b.
1 c.
5 d.
6 e.
7 f.
8 34. Given the same main function header and command line as the above question, what is the value of argv[3]
?
a.
"./" b.
"a.out" c.
"-w" d.
"dreams.txt" e.
"-o" f.
"-o out.dat" 35. How many total int locations are allocated in memory by the following code?
struct Time { int hours; int minutes; int seconds; }; int main() { Time startTime, endTime; ... }
a.
3 b.
0 c.
6 d.
9 e.
2 f.
5 36. Given the struct and variable declarations in the previous question, which of the following statements has a syntax error?
a.
cout << startTime.hours –
endTime.hours; b.
cin >> startTime.minutes; c.
startTime.seconds = 45; d.
int h = startTime.hours; e.
endTime = startTime + 60; f.
All of the above 37. Given a vector of Time objects named sched
, which statement correctly accesses the hours
data field of a Time object?
a.
sched.hours; b.
sched.at(0).hours; c.
sched.hours(0); d.
sched.hours.at(0); T
TAT
O
O
O
O
Name_______________________________________ Username_____________________________________ Page 9 of 13 Use the following class definition for the next 5 questions. class Rational { private: int numer; int denom; public: Rational(); Rational(int); Rational(int, int); const Rational add(const Rational &) const; const Rational operator*(const Rational &) const; void display() const; }; 38. Which of the following is a valid header for the implementation
of the add member function declared above? (assuming it is implemented outside the declaration of the class)
a.
const Rational::Rational add(const Rational &rhs) const b.
const Rational Rational::add(const Rational &) const c.
Rational::Rational add(rhs) d.
const Rational Rational::add(const Rational &rhs) const e.
const Rational add(const Rational &) f.
Rational add(Rational &rhs) 39. Given two Rational objects r1
and r2
, which of the following statements has a syntax error
assuming the statement is within the main function (
not
within a Rational member function)?
a.
cout << r1.display(); b.
cout << r1.numer; c.
cout << r1 * r2; d.
All of the above. 40. Which of the following statements has a syntax error
given each statement is within the definition of the Rational class’ operator*
member function and assuming the explicit parameter for the operator*
function is named rhs
? (Notice that the operator*
function is a const member function.) (This question is not
asking if the statement would be a logic or runtime error.)
a.
display(); b.
cout << rhs.denom; c.
denom *= rhs.denom; d.
All of the above. 41. Which value(s) is/are passed into the add function’s implicit
parameter(s) in the statement:
r1 = r2.add(r3); a.
r1 b.
r2 c.
r3 d.
r2 and r3 42. W
hich value(s) is/are passed into the operator* function’s explicit
parameter(s) in the statement:
r1 = r2 * r3; a.
r1 b.
r2 c.
r3 d.
r2 and r3 43. All the code between #ifndef __MY_CLASS_H__ and #endif will be compiled if __MY_CLASS_H__
is defined. a.
TRUE b.
FALSE 6
O
O
O
O
O
O
O
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Page 10 of 13 44. A const member function may call a non-const member function.
a.
TRUE b.
FALSE 45. Given a class named Student with data members name and sid, and the public member function: void setSID(int sid); which of the following lines within the setSID function correctly sets the sid data member to the value passed into the explicit parameter sid? a.
sid = sid; b.
this->sid = sid; c.
sid = this->sid; d.
this->sid = this->sid; 46. Given a class named Book with data members title and author that does not declare any constructor functions, which of the following Book declarations has a syntax error?
a.
Book b1; b.
Book b1("Dune", "Frank Herbert"); c.
Both d.
Neither 47. Given a class named Book with data members title and author that declares only
the following constructor:
Book(string, string); which of the following Book declarations has a syntax error?
a.
Book b1; b.
Book b1("Dune", "Frank Herbert"); c.
Both d.
Neither 48. Given fin is properly connected to a file that contains the following characters
Alan Turing Enigma
what is output by the following code fragment?
string s1, s2; fin >> s1; getline(fin, s2); cout << s1 << ' ' << s2; a.
Alan Turing b.
Alan c.
Turing d.
Enigma e.
Alan Turing Enigma f.
Alan Enigma O
O
O
o
Alan
Turing
Name_______________________________________ Username_____________________________________ Page 11 of 13 49. Given fin is properly connected to a file that contains the following characters
Alan Turing Enigma
what is output by the following code fragment?
string s1, s2; fin >> s1; getline(fin, s2); cout << s1 << ' ' << s2; a.
Alan Turing b.
Alan c.
Turing d.
Enigma e.
Alan Turing Enigma f.
Alan Enigma 50. Which of the following is the correct declaration for a member function
of a class named Widget that overloads the + operator to return the sum of 2 Widgets?
a. const Widget operator+( const Widget &lhs, const Widget &rhs ) const; b. int operator+( const Widget &lhs, const Widget &rhs ) const; c. const Widget operator+( const Widget &rhs ) const; d. const Widget operator+( ) const; Use the following class definition for the next 3 questions. class GasTank { private: double amount; //amount of gas currently in tank double capacity; //amount of gas tank can hold public: GasTank(double capacity); //GasTank starts out empty double addGas(double howMuch); double useGas(double howMuch); bool isFull() const; bool isEmpty() const; double getGasLevel() const; double fillUp(); }; 51. Which of the following is the best choice for the definition of the GasTank constructor?
a.
GasTank(double capacity) : capacity(capacity) { } b.
GasTank(double capacity){ capacity = capacity; amount = 0; } c.
GasTank::GasTank(double) : capacity(capacity), amount(0) { } d.
GasTank::GasTank(double capacity) : amount(0), capacity(capacity) { } e.
GasTank::GasTank(double capacity){ amount = 0; capacity = this->capacity; } f.
GasTank::GasTank(double capacity){ amount = 0; capacity = capacity; } 52. Within which GasTank member function would the following statement cause a syntax
error?
amount = 0; a.
addGas b.
fillUp c.
isEmpty d.
useGas O
0
x
x
Page 12 of 13 53. Which of the following statements would cause a syntax
error if the statement is within the global (non-member) function f declared here: void f(const GasTank >);
? Hint: notice gt is passed in by const reference.
a.
double fillAmt = gt.fillUp(); b.
bool full = gt.isFull(); c.
double level = gt.getGasLevel(); d.
GasTank temp = gt; 54. Given this main function header: int main(int argc, char *argv[])
what will the value of argc
be if the program is executed with the following command line? ./a.out in.dat out.dat a.
22 b.
2 c.
3 d.
./a.out e.
out.dat f.
no way to know given the information provided 55. Given this code fragment:
ofstream outFS; outFS.open("outputFile.txt"); which of the following statements correctly outputs the string "The Force Awakens"
to the file named outputFile.txt? a.
outputFile.txt << "The Force Awakens"; b.
outFS.put("The Force Awakens"); c.
outFS << "The Force Awakens"; d.
ofstream << "The Force Awakens"; 56. Given a string variable named outputFile
that has been declared, which of the following statements has a syntax error
?
a.
ofstream fout(outputFile); b.
ofstream fout; c.
ofstream fout("out.txt"); d.
ofstream fout(outputFile.c_str()); 57. Placing the keyword const
after the parameter list of a member function means __________. a.
the value returned by the function will be constant b.
the function cannot change the value of the implicit parameter c.
the function cannot change the value of any explicit parameters d.
the function must be a mutator member function 58. When using a header file (.h) to store the class declaration and an implementation file (.cpp) to store the member function implementations, the file containing the main function should #include ____________________________. a.
the header file (.h) b.
the implementation file (.cpp) c.
both the header and the implementation files d.
None of the above.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Name_______________________________________ Username_____________________________________ Page 13 of 13 59. If the user types the following characters
12 a 42 apple
and then hits return, what is output the following code fragment?
int n1; char ch1; string str1; cin >> n1 >> ch1 >> str1; cout << n1 << ch1 << str1; a.
12 a b.
12 a 42 apple c.
12a42 d.
12a42 apple e.
12a f.
12 60. If the user types the following characters
12 17 42 apple
and then hits return, what is output the following code fragment?
int n1; char ch1; string str1; cin >> n1 >> ch1 >> str1; cout << n1 << ch1 << str1; a.
12 17 b.
12 17 42 apple c.
121742 d.
121742apple e.
1217 f.
12 61. If the user types the following characters
12 17 42 apple
and then hits return, what is output the following code fragment?
int n1; char ch1; string str1; cin >> n1; cin.get(ch1); getline(cin, str1); cout << n1 << ch1 << str1; a.
12 17 b.
12 17 42 apple c.
121742 d.
12 1742apple e.
121742apple f.
1217 12
A
42
12
1742
apple
Recommended textbooks for you

Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning

Fundamentals of Information Systems
Computer Science
ISBN:9781337097536
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning

EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT

Principles of Information Systems (MindTap Course...
Computer Science
ISBN:9781305971776
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning

Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Recommended textbooks for you
- Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningFundamentals of Information SystemsComputer ScienceISBN:9781337097536Author:Ralph Stair, George ReynoldsPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- Principles of Information Systems (MindTap Course...Computer ScienceISBN:9781305971776Author:Ralph Stair, George ReynoldsPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,

Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning

Fundamentals of Information Systems
Computer Science
ISBN:9781337097536
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning

EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT

Principles of Information Systems (MindTap Course...
Computer Science
ISBN:9781305971776
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning

Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,