100% found this document useful (1 vote)
354 views15 pages

CSCI 123 - Final Exam

This document contains details about a final exam for CSCI 123. It lists 20 multiple choice questions covering topics like classes, data structures, sorting algorithms, functions, iterators, and C++ basics. It provides the questions, possible answers for each question, and some include example code snippets. The exam is due to be completed and submitted by December 9th, 2016.

Uploaded by

BrianYoung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
354 views15 pages

CSCI 123 - Final Exam

This document contains details about a final exam for CSCI 123. It lists 20 multiple choice questions covering topics like classes, data structures, sorting algorithms, functions, iterators, and C++ basics. It provides the questions, possible answers for each question, and some include example code snippets. The exam is due to be completed and submitted by December 9th, 2016.

Uploaded by

BrianYoung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CSCI 123 Final Exam

Final Due Date: December 9th, 2016


Multiple Choices

1. (5 points)
A member variable of a class that cannot be directly accessed in the program except
within the definition of a member function is:
a. public
b. private
c. protected.
d. None of the above

2. (5 points)
A data structure that retrieves data in the reverse of the order in which the data is
stored is:
a. queue
b. stack
c. linked list
d. hash
e. tree

3. (5 points)
Please read the following passes and decide sorting algorithm

Initial array: {3, 5, 6, 10, 2, 1, 0, 4, 7, 8, 9}

First Pass: {0, 5, 6, 10, 2, 1, 3, 4, 7, 8, 9}

Second Pass: {0, 1, 6, 10, 2, 5, 3, 4, 7, 8, 9}

Third Pass: {0, 1, 2, 10, 6, 5, 3, 4, 7, 8, 9}

a. Bubble Sort
b. Selection Sort
c. Quick Sort
d. Insertion Sort
e. Heap Sort
4. (5 points)
How are parameters passed to functions?

a. Pass by Value
b. Pass by Pointer
c. Pass by Reference
d. a and c
e. a, b, and c

5. (5 points)
Polymorphism is the ability to associate multiple meanings to one function name by
means of late binding.
a. True
b. False

6. (5 points)
int i;
double score[ ] = {0, 1, 2, 3, 4, 5, 6};
for (int i = 0; i < sizeof(score) / 8; i++) {
cout << i << score[i];
}

Output: 00112233445566

a. true
b. false

7. (5 points)
In order to use .h file in cpp main program, we shall need to declare as following:
Given: time.h

a. #include time.h
b. #include <time>
c. #include <time.h>
d. #include time.h
e. #include time

8. (5 points)
An iterator is an object that can be used with a container to gain access to elements in
the container.

a. True
b. False
9. (5 points)
A byte is a memory location that can hold how many bits?

a. 4 bits
b. 6 bits
c. 8 bits
d. 10 bits

10. (5 points)
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;

int main()
{
vector<int> container;
for (int i = 1; i <= 5; i++)
container.push_back(i);

cout << "Here is what is in the container:\n";


vector<int>::iterator p;
for (p = container.begin(); p != container.end(); p++)
cout << *p << " ";
cout << endl;

cout << "Setting entries to 0:\n";


for (p = container.begin(); p != container.end(); p++)
*p = 0;

cout << "Container now contains:\n";


for (p = container.begin(); p != container.end(); p++)
cout << *p << " ";
cout << endl;
return 0;
}

Output:
Here is what is in the container:
1234
Setting entries to 0:
Container now contains:
0000
Output is true or false
a. True
b. False

Fill in the blank


11. (5 points)
Please write C++ code for following remarks in member functions of the standard class
string:

Str.find(str1,pos) Returns index of first occurrence of string str1


in str; the search starts at position pos.

Str1 = str2;
Initializes str1 to str2's data

Str.empty() Returns true if str is an empty string; false


otherwise.

Str.insert(pos, str2); Inserts str2 into str beginning at position pos.

Str.erase(pos, length); Removes substring of size length, starting at


position pos.

Str.length() Returns the length of str.

12. (5 points)
Please fill in the empty space
Function Example
toupper(Char_Exp) char c = toupper('a');
cout << cc;
Outputs: A
tolower(Char_Exp) char c = tolower('a');
cout << cc;
Outputs: A
isupper(Char_Exp) if (isupper(c)) {
cout << c << " is uppercase.";
} else {
cout << c << " is not uppercase.";
}
islower(Char_Exp) char c = 'a';
if (islower(c)) {
cout << c << " is lowercase.";
}
Outputs: a is lowercase.
isalpha(Char_Exp) if (isdigit('3')) {
cout << "It's a digit.";
} else {
cout << "It's not a digit.";
}
Outputs: It's a digit.

13. (5 points)
Stack is _______first____________ in / _______last______________ out.
Queue is _______first___________ in / ________first_____________ out.

14. (5 points)
The computer can be thought of as having five main components:

the input device(s), the output device(s), the processor (also called the CPU, for central
processing unit), the main memory, and the secondary memory.

15. (5 points)
An ______iterator______________ can be an object of some iterator class or something
simpler, such as an array index or a pointer.

16. (5 points)
A header file that has class within it is called as _____interface file___________________.

17. (5 points)
The main characteristics of OOP (Object Oriented Programming) are:
encapsulation,
inheritance,
and polymorphism.

18. (5 points)
___templates______________ allow you to define functions and classes that have
parameters for type names. This will allow you to design functions that can be used with
arguments of different types and to define classes that are much more general than
those you have seen.

19. (5 points)
Please draw arrows from p1 and p2 to their correct addresses based on the following
code:

int *p1, *p2; p1 42


p1 = new int;
*p1 = 42;
p2 = p1; p2 53
*p2 = 53;

20. (5 points)
Map Template Class
Member Function Meaning
m.insert(Element) Inserts Element in the map. Element is of type
pair<KeyType, T>. Returns a value of type
pair<iterator, bool>. If the insertion is
successful, the second part of the returned
pair is true and the iterator is located at the
inserted element.
m.erase(Target_Key) Removes the element with the key
Target_Key.
m.find(Target_Key) Returns an iterator located at the element with
key value Target_Key. Returns m.end() if
there is no such element.
m[Target_Key] Returns a reference to the object associated
with the key Target_Key. If the map does not
already contain such an object then a default
object of type T is inserted and returned.
m.size() Returns the number of pairs in the map
m.empty() Returns true if the map is empty; otherwise
returns false
m1 == m2 Returns true if the maps contains the same
pairs; otherwise returns false.
Printing Output
21. (10 points)
#include <iostream>
#include <string>
using namespace std;

int main()
{
string message = "Hello A, how are you?";
cout << message.find('A');
return 0;
}

Output: 6

22. (10 points)


Let n = 10;

#include <iostream>
using namespace std;

int fib(int n)
{
if (n == 0 || n == 1) return 1;
return fib(n-1) + fib(n-2);
}

int main()
{
int n;
cout << "Enter a non-negative integer: ";
cin >> n;
cout << "Fib(" << n << ") = " << fib(n) << "." << endl;
return 0;
}

Output: Fib(10) = 89.


23. (10 points)
#include <iostream>
using namespace std;

Int main(){
int x = 10;
int y = 40;
if (x >= 10) {
if (y < 40) {
y++;
}
} else {
y--;
}
cout << x << " and " << y << endl;
}

Output: 10 and 40

24. (10 points)

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

class Animal {
public:
int leg;
Animal();
void legs();
};

Animal::Animal(){
leg = 3;
}

void Animal::legs(){
leg = 2;
}

int main()
{
Animal animal;
animal.leg = 4;
animal.legs();
Animal();
cout << animal.leg << legs\n;
return 0;
}

Output: 2 legs

25. (10 points)


Please let donuts = 18 and milk = 6

#include <iostream>
using namespace std;

int main()
{
int donuts, milk;
double dpg;

try
{
cout << Enter number of donuts:\n;
cin >> donuts;
cout << Enter number of glasses of milk:\n;
cin >> milk;

if (milk <= 0)
throw donuts;

dpg = donuts/static_cast<double>(milk);
cout << donuts << donuts.\n
<< milk << glasses of milk.\n
<< You have << dpg
<< donuts for each glass of milk.\n;
}
catch(int e)
{
cout << e << donuts, and No Milk!\n
<< Go buy some milk.\n;
}
cout << End of program.\n;
return 0;
}

Output:
18 donuts.
6 glasses of milk.
You have 3 donuts for each glass of milk.
End of program.

Debugging
There are five errors on each question, please find the errors and fix them.
26. (10 points)
#include <ifstream>
using namespace std;
class DayOfYear {
private:
void output();
int month;
int day;
};

int main( )
{
DayOfYear today, birthday;
cout << "Enter today's date:\n";
cout << "Enter month as a number: ";
cin >> today.month;
cout << "Enter the day of the month: ";
cin >> today.day;
cout << "Enter your birthday:\n";
cout << "Enter month as a number: ";
cin << birthday.month;
cout << "Enter the day of the month: ";
cin >> birthday.day;
cout << "Today's date is ";
today.output( );
cout << "Your birthday is ";
birthday.output( );
if (today.month == birthday.month && today.day == birthday.day) {
cout << "Happy Birthday!\n";
cout << John\n;
} else
cout << "Happy Unbirthday!\n";
return;
}

void DayOfYear::output(int day)


{
cout << "month = " << month << ", day = " << day << endl;
}

27. (10 points)


#include <ifstream>
using namespace std;

int main(){
check_age();
return 0;
}

void check_age() {
int age = 0;
cout << "Input your age: " << endl; 1
cin >< age; 2
if (age => 65) { 3
cout << Retired << endl;
} else if (age > 18 & age < 65) { 4
cout << Please continue working, no retire yet << endl;
} else cout << You are too young! >> endl; 5
}
28. (10 points)
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
char str[23] = "I am studying CSCI 123";
string name = 'A';
char str1= str.find("123");
str.erase(str, 3);
str.insert(name, str1);
cout << str;
return 0;
}

29. (10 points)


#include <ifstream>
using namespace std;

void write_vertical(int n);

int main()
{
cout << write_vertical(3): << endl;
write_vertical(3);

cout << write_vertical(12): << endl;


write_vertical();

cout << write_vertical(123): >> endl;


write_vertical(123);

return 0;
}

void write_vertical(int n)
{
if (n < 10)
{
cout << n << end;
}
else
{
write_vertical();
cout << (n % 10) << endl;
}
}

30. (10 points)


#include <iostream>
using std::cout;
using std::endl;
using std::vector;

int main()
{
vector<int> container;
for (int i = 1; i <= 4; i++)
container.push(i);

cout << Here is what is in the container:\n;


vector<>::iterator p;
for (p = container.begin(); p != container.end(); p++)
cout << *p << ;
cout << endl;

cout << Setting entries to 0:\n;


for (p = container.end(); p != container.end(); p++)
*p = 0;

cout << Container now contains:\n;


for (p = container.begin(); p != container.begin(); p++)
cout << *p << ;
cout << endl;
return 0;
}

Free Responses
31. (25 points)
Given following array:

double score[ ] = {70,88,86,79,83,46.5,75,78.5,97.5,91.5,


85.5,86,87.5,91.5,85.5,86,87.5,91.5,82,76.5,
87,79,100,90,82,86,54.5,65.5,89,71};

1. Please print out the average of these 30 exam scores.


2. Let the highest score is 100, please also print out the amount of tests which receives
A, B, C, and F.

32. (25 points)

Given input.txt contains:


5
10
15
20
25

Calculation step:
(5 + 10) / 2, (10 + 15) / 2, .

Once you get four numbers, please add them all together and print out to output.txt as
following:

7.5
12.5
17.5
22.5
Total = 60

33. (25 points)


John goes to Albertson today to exchange some coins to dollar. He is a picky person, so
he wants the machine to display what he input every single time to make sure the coin
he input is correct coin.
a. Please help the machine to write a program to display type of coins every single
input
Coins List:
Half-dollar: 0.50
Quarter: 0.25
Dime: 0.10
Nickel: 0.05
Cent: 0.01
b. John was so fascinated with this machine, he decided to crash this machine by
input in a gold one-dollar coin. Please help the program to check whether this is valid or
invalid coin.
c. Most importantly, John does not want to rerun the program again for every
single input. Please avoid that.

Sample Output:
Please input your coins: 0.50
You input half-dollar coin.
Please input your coins: 0.05
You input a nickel coin.
Please input your coins: 1.00
Invalid coin, cannot read this coin.

34. (25 points)


Please write recursion functions that help the user calculate the n factorial value and
power n value of factorial n number.

For instance, if user input 3, he will receive

(3 * 2 * 1)^3 = 216

Lastly, please use the final number and do another power of 3 and give me new number.

For instance: 216^3= 10077696

Sample Output:
Please input n number: 3
Factorial of 3 = 6
Power of 3 of factorial 3 = 216
Power of 3 of new number = 10077696

You might also like