0% found this document useful (0 votes)
11 views22 pages

Sample Final II

This document contains a sample final exam for a computer programming course. The exam consists of multiple choice and coding questions testing concepts like classes, inheritance, dynamic memory allocation, and more. The questions provide code snippets to analyze, modify, or write additional code to test understanding of programming fundamentals.

Uploaded by

i233045
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

Sample Final II

This document contains a sample final exam for a computer programming course. The exam consists of multiple choice and coding questions testing concepts like classes, inheritance, dynamic memory allocation, and more. The questions provide code snippets to analyze, modify, or write additional code to test understanding of programming fundamentals.

Uploaded by

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

National University of Computer and Emerging Sciences

School of Computing Hobbitesses at Shire For Saving the World

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

Student Name Roll No Section Signature

DO NOT OPEN THE QUESTION BOOK OR START UNTIL INSTRUCTED.


Instructions:
1. Attempt on question paper. Attempt all of them. Read the question carefully, understand the question, and then
attempt it.
2. No additional sheet will be provided for rough work. Use the back of the last page for rough work.
3. If you need more space write on the back side of the paper and clearly mark question and part number etc.
4. After asked to commence the exam, please verify that you have (22) different printed pages including this title
page. There are total of (6) questions.
5. Use of calculator is strictly prohibited.
6. Use permanent ink pens only. Any part done using soft pencil will not be marked and cannot be claimed for
rechecking.
7. Use proper indentation while writing code and make sure that your code is legible. Failing to do so can cost
you marks.
8. Please read the question thoroughly and use your time properly, an uneven distribution of time can lead to
incomplete answers.

I II III IV V VI Total
Total Marks 36 10 15 10 15 10 96
Marks Obtained

Vetted By: Vetter Signature:


CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam

Question I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (36 Marks)


(a) (2 Marks) What is the output of the following program segment? Identify errors (if any).
1 int num1 = 392;
2 int& num2 = num1;
3 num2 = 444;
4 num1 = num1 + num2;
5 int *pointer1 = &num1;
6 cout<< *pointer1<<endl;

(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:

1 int **matrix = new int *[5];


2 for(int i=0; i<5; i++ )
3 matrix[i] = new int[6];

(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

1 for(int i=0; i<5; i++ )


2 delete[] matrix[i];
3 delete[] matrix;

(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:

1 Horizon & setX(int _x) {/*Alias is necessary*/


2 x = _x;
3 return *this;
4 }

(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

14 class FunBook: public Book {


15 public:
16 void info() {
17 cout << endl << ("This is a FunBook ");
18 }
19 };
20 class StoryBook {
21 public:
22 void info() {
23 cout << endl << ("This is a StoryBook ");
24 }
25 };
26

27 class NovelBook: public StoryBook {


28 public:
29 void info() {
30 StoryBook::info();
31 cout << endl << ("This is a NovelBook");
32 }
33 };
34 int main() {
35 Book *b = new Book;
36 FunBook *fb = new FunBook;
37 b->info(*fb);
38 StoryBook *sb = new NovelBook;
39 sb->info();
40 }

Page 6 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam

Solution:

1 This is a simple book


2 This is a simple book
3 This is a StoryBook

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

14 class FunBook: public Book {


15 public:
16 virtual void info() {
17 cout<<endl<< ("This is a FunBook ");
18 }
19 };
20 class StoryBook {
21 public:
22 virtual void info() {
23 cout<<endl<< ("This is a StoryBook");
24 }
25 };
26 class NovelBook: public StoryBook {
27 public:
28 virtual void info() {
29 StoryBook::info();
30 cout<<endl<< ("This is a NovelBook");
31 }
32 };
33 int main() {
34 Book *b = new Book;
35 FunBook *fb = new FunBook;
36 b->info(*fb);
37 StoryBook *sb = new NovelBook;
38 sb->info();
39 }

Solution:

Page 8 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam

1 This is a simple book


2 This is a FunBook
3 This is a StoryBook
4 This is a NovelBook

Page 9 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam

Question II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (10 Marks)


(a) (10 Marks) Write a recursive function pattern that only receives a positive integer (non-zero) as argument
and prints a pattern on screen. Look at the following examples to make a sense of what is required.

INPUT: 5 INPUT: 4
OUTPUT: OUTPUT:
***** ****
**** ***
*** **
** *
*

Page 10 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam

Solution:

1 void pattern(int n){


2 if( n > 0 ){
3 for( int i=0; i<n; i++ )
4 cout << "*";
5 cout << endl;
6 pattern(n-1);
7 for( int i=0; i<n; i++ )
8 cout << "*";
9 cout << endl;
10 }
11 }

Page 11 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam

Question III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (15 Marks)


(a) (7 Marks) Write a program that reads a file containing integers and uses map to display the distinct (unique)
numbers in the file, named input.txt, with their frequencies.
For example, you can consider input.txt contains the following: 23 24 12 4 54 12 3 54 23
The output should be: 3:1 4:1 12:2 23:2 24:1 54:2

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

8 map<int, int> numcount;


9 ifstream ifile("input.txt");
10 int num;
11 ifile >> num;
12 while(!ifile.eof()){
13 numcount[num]++;
14 ifile >> num;
15 }
16

17 for( map<int,int>::iterator i=numcount.begin();


18 i!=numcount.end();
19 ++i)
20 cout << (*i).first << ":" << (*i).second << " ";
21

22 return 0;
23 }

Page 14 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam

Question IV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (10 Marks)


(a) (10 Marks) Create a class RationalNumber(fractions) with the following capabilities:
1. Create a constructor that two positive integers as numerator and denominator of a fraction.
2. Overload the addition, subtraction, multiplication and division operators for this class.
3. Overload the relational and equality operators for this class.

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

Question V . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (15 Marks)


Package-delivery companies, such as TCS, offer a number of different shipping options, each with specific costs
associated. Your goal is to build a system for managing the package-delivery services. Your TCS company provide
two types of options TwoDayPackage and OvernightPackage. Each package has following associated
information of its sender and recipient, name, address, city, province and postal code. In addition each package
has its associated weight(in grams) and cost per gram, this information is used to calculate the shipping cost of
the package. Draw a Simley on front page to get three bonus marks.
In TwoDayPackage service there is additional flat fee that the shipping company charges for two-day-delivery
service. Thus the total shipping cost of TwoDayPackage should be calculated by adding the flat fee to the
weight-based cost discussed above. In OvernightPackage your company charges an additional fee per gram
for overnight-delivery service. Thus in order to calculate shipping cost it adds the additional fee per gram to the
standard cost per gram before calculating the shipping cost.
(a) (3 Marks) Build a hierarchical diagram of your system by identifying the main classes and the relationships
among them.

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

Question VI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (10 Marks)


Given the following class and main function, write the code for storing and reading the given objects from a
binary file. [Note that string class c str() function returns a pointer to an array that contains a null-terminated
sequence of characters.]
1 #include<iostream>
2 #include<vector>
3 #include<string>
4 using namespace std;
5 class Student {
6 friend istream &operator >>(istream &, Student &);
7 friend ostream &operator <<(ostream &, const Student &);
8 public:
9 Student() {
10 sid = -1;
11 nsubjects = -1;
12 marks = NULL;
13 }
14 Student(string sname, int sid, int nsubjects) {
15 this->sname = sname;
16 this->sid = sid;
17 this->nsubjects = nsubjects;
18 marks = new int[nsubjects];
19 }
20 ˜Student() {
21 if (marks)
22 delete[] marks;
23 }
24

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

57 // Now read the students information from that binary file


58 // and display
59 }

Page 21 of 22
CS103 - Computer Programming Hobbitesses at Shire Sample Final Exam

Page 22 of 22

You might also like