Sample Final II
Sample Final II
Serial No:
CS103 Sample Final
Computer Programming Exam
Total Time: 3 Hours
Total Marks: 96
Course Instructor(s)
Gandalf, Frodo, Sam, Gollum, Merry and Fairy
Signature of Invigilator
I II III IV V VI Total
Total Marks 36 10 15 10 15 10 96
Marks Obtained
(b) (1 Mark) True/False: You must declare all private members of a class before the public members.
Solution: False
(c) (1 Mark) Assume that InventoryItem is the name of a class, and the class has a void member function named
setPrice which accepts a double argument. If book is an instance of the InventoryItem class, which of the
following statements properly uses the book object to call the setPrice member function?
InventoryItem::setPrice(1.49)
√
book.setPrice(1.49)
book::setPrice(1.49)
book:setPrice(1.49)
(d) (1 Mark) Declare an array of three InventoryItem objects.
Solution:
1 InventoryItem arr[3];
(e) (3 Marks) Write C++ statement(s) to dynamically allocate space for a 5-by-6 matrix of integers. Provide
necessary declarations.
Solution:
(f) (2 Marks) Write C++ statement(s) to free the dynamic memory allocated in the previous part.
Solution:
Page 2 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
(g) (10 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 #include<iostream>
2 #include<vector>
3 using namespace std;
4 int muffin(vector<int>::iterator i, vector<int>::iterator e, int t) {
5 if (i == e)
6 return 0;
7 return ((*i == t) + muffin(++i, e, t));
8 }
9 int main() {
10 int arr[] = { 45, 33, 45, 32, 31, 33, 45 };
11 vector<int> v;
12 for (int i = 0; i < 7; i++)
13 v.push_back(arr[i]);
14 for (unsigned int i = 0; i < v.size(); i++)
15 cout << muffin(v.begin(), v.end(), v[i]) << endl;
16 return 0;
17 }
Solution:
1 3
2 2
3 3
4 1
5 1
6 2
7 3
Page 3 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
(h) (3 Marks) Modify the code given below such that the following statement o.setX(10).setY(20) can
be executed without logical and syntax error:
1 class Horizon {
2 public:
3 void setX(int _x) {
4 x = _x;
5 }
6 void setY(int _y) {
7 y = _y;
8 }
9 private:
10 int x;
11 int y;
12 };
13 int main() {
14 Horizon o;
15 o.setX(10).setY(20);
16 }
Solution:
(i) (3 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 #include<iostream>
2 using namespace std;
3 class Maze {
4 private:
5 int i;
6 public:
7 Maze(int i) {
8 this->i = i;
9 cout << " C" << i << " \n";
10 }
11 ˜Maze() {
12 cout << " D" << i << " \n";
13 }
14 };
15 Maze a(1);
16 int build() {
17 Maze d(4);
18 static Maze e(5);
19 }
20 int main() {
21 Maze b(2);
22 static Maze c(3);
23 build();
24 Maze f(6);
25 return 0;
Page 4 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
26 }
Solution:
1 C1
2 C2
3 C3
4 C4
5 C5
6 D4
7 C6
8 D6
9 D2
10 D5 // D5 and D3 order can be reversed.
11 D3
12 D1
Page 5 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
(j) (5 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 #include<iostream>
2 using namespace std;
3 class Book {
4 public:
5 void info() {
6 cout << endl << ("This is a simple book ");
7 }
8 void info(Book &d) {
9 this->info();
10 d.info();
11 }
12 };
13
Page 6 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Solution:
Page 7 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
(k) (5 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 #include<iostream>
2 using namespace std;
3 class Book {
4 public:
5 virtual void info() {
6 cout << endl << ("This is a simple book ");
7 }
8 virtual void info(Book &d) {
9 this->info();
10 d.info();
11 }
12 };
13
Solution:
Page 8 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Page 9 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
INPUT: 5 INPUT: 4
OUTPUT: OUTPUT:
***** ****
**** ***
*** **
** *
*
Page 10 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Solution:
Page 11 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Page 12 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
(b) (8 Marks) Write a program that reads a text file specified by the user and reports how many times each word
occurred in the file. The output should be formatted in two columns, with the words in the first column and
the numbers in the second column aligned on their right side in a column. You may assume the input file
contains no punctuation marks. An example output is shown below for the file “in.txt”.
Input: In.txt
Gandalf is old
Gandalf is a wizard
Gandalf is a friend to hobbits
blah blah blah blah blah blah blah blah blah blah
Output
Enter file name: in.txt
Gandalf 3
is 3
old 1
a 2
wizard 1
friend 1
to 1
hobbits 1
blah 10
Page 13 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Solution:
1 #include<iostream>
2 #include<fstream>
3 #include<map>
4 using namespace std;
5
6 int main(){
7
22 return 0;
23 }
Page 14 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Page 15 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Page 16 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Page 17 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
(b) (9 Marks) Write the complete code for all the classes (except main) of your TCS system.
(c) (3 Marks) Given the classes of your system, now create a function that displays the address information
of sender and receiver and calculates the shipping costs for several different Packages. The function could
at most receive an array of pointers and size of the array as argument and should display the information
Page 18 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
polymorphically. It must keep track of total cost and should return the total cost of all the packages.
Page 19 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
25 private:
26 string sname;
27 int *marks;
28 int sid;
29 int nsubjects;
30 };
31 istream &operator >>(istream & in, Student & std) {
32 in >> std.sid;
33 in >> std.nsubjects;
34 in >> std.sname;
35 std.marks = new int[std.nsubjects];
36 for (int i = 0; i < std.nsubjects; ++i)
37 in >> std.marks[i];
38 return in;
39 }
40 ostream &operator <<(ostream &out, const Student &std) {
41 out << std.sname << " " << std.sid << " " << std.nsubjects << endl;
42 for (int i = 0; i < std.nsubjects; ++i)
43 out << std.marks[i] << " ";
44 return out;
45 }
46
47 int main() {
48 Student *std;
Page 20 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
49 int x;
50 cout << "Enter number of Students";
51 cin >> x;
52 std = new Student[x];
53 for (int i = 0; i < x; ++i)
54 cin >> std[i];
55 // Now writes the students information to a binary file...
56
Page 21 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam
Page 22 of 22