CSCI 123 - Final Exam
CSCI 123 - Final Exam
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
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);
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
Str1 = str2;
Initializes str1 to str2's data
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:
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
#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;
}
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
#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
#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;
}
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;
}
int main()
{
cout << write_vertical(3): << endl;
write_vertical(3);
return 0;
}
void write_vertical(int n)
{
if (n < 10)
{
cout << n << end;
}
else
{
write_vertical();
cout << (n % 10) << endl;
}
}
int main()
{
vector<int> container;
for (int i = 1; i <= 4; i++)
container.push(i);
Free Responses
31. (25 points)
Given following array:
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
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.
(3 * 2 * 1)^3 = 216
Lastly, please use the final number and do another power of 3 and give me new number.
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