Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

National University of Computer and Emerging Sciences

School of Computing Spring 2016 Islamabad Campus

CS103 Serial No:


Sessional II
Computer Programming Total Time: 1 Hour
Monday, March 28, 2016 Total Marks: 90
Course Instructor(s)
Sibt ul Hussain, Usman Farrokh and Hassan Mustafa
Signature of Invigilator

Student Name Roll No Section Signature

DO NOT OPEN THE QUESTION BOOK OR START UNTIL INSTRUCTED.


Instructions:
1. Answer all the questions on this paper. Read the question carefully, understand the question, and then answer it.
2. No additional sheet will be provided for rough work. Utilize the empty spaces for rough work.

3. After asked to commence the exam, please verify that you have (12) different printed pages including this title
page. There are total of (3) questions.
4. Use of calculator is strictly prohibited.
5. Use permanent ink pens only. Any part done using soft pencil will not be marked and cannot be claimed for
rechecking.
6. Use proper indentation while writing code and make sure that your code is legible. Failing to do so can cost
you marks. You can use comments or a little description to explain your code.
7. Please allocate your time properly according to the marks distribution.

I II III Total
Total Marks 46 30 14 90
Marks Obtained

Vetted By: Vetter Signature:


CS103 - Computer Programming Spring 2016 Sessional II

Question I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (46 Marks)


Please write proper explanation of the error (or bug) where required, without proper explanation no marks will be
awarded.

Read the statement before each question carefully.


(a) (12 Marks) What will be the output of following code. Explain the error or bug if there is any.
1 #include <iostream>
2 using namespace std;
3 class Point{
4 int x, y;
5 public:
6 Point(int a=0, int b=0){
7 x=a;
8 y=b;
9 print();
10 }
11 void print(){
12 cout<<" ("<<x<<","<<y<<") " <<endl;
13 }
14 ˜Point(){
15 cout<<"Point is going"<<endl;
16 }
17 };
18 class Circle
19 {
20 Point center;
21 float radius;
22 public:
23 Circle():center(0,0){
24 radius=0;
25 cout<<"The basic circle"<<endl;
26 }
27 Circle(Point p):center(p){
28

29 }
30 Circle(const Circle & c):center(c.center), radius(c.radius){
31 cout<<"The copied circle";
32 center.print();
33 }
34 ˜Circle(){
35 cout<<"Circle is going"<<endl;
36 }
37 };
38

39 int main() {
40 Point p1;
41 Circle c1;
42 static Circle c2(p1);
43 Circle c3(c2);
44 return 0;
45 }

Page 2 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

(b) (6 Marks) What will be the output of following code. Explain the error or bug if there is any.
1 #include <iostream>
2 using namespace std;
3 class Dummy
4 {
5 float z;
6 int x, y;
7 public:
8 Dummy(int x=0, int y=1):x(x+2),y(y+3){
9 z = x + y + 1;
10 }
11 void print(){
12 cout<< " X= "<< x
13 <<" Y = "<< y
14 <<" Z = "<< z;
15 }
16 };
17

18 int main() {
19 Dummy d(10);
20 d.print();
21 return 0;
22 }

Page 3 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

(c) (8 Marks) What will be the output of following code. Explain the error or bug if there is any.
1 #include <string>
2 #include <iostream>
3 using namespace std;
4 class Volume {
5 int volume;
6 public:
7 Volume(int l, int h, int w){
8 volume = l*h*w;
9 }
10 Volume(int v){
11 volume=v;
12 }
13 int getVolume() const {
14 return volume;
15 }
16 Volume operator ++(){
17 int t = volume++;
18 Volume temp(t);
19 return *this;
20 }
21 Volume operator ++(int){
22 Volume temp(0);
23 ++*this;
24 return temp;
25 }
26 };
27 ostream& operator<<(ostream& out, const Volume& v){
28 out<<"The volume is "<<v.getVolume()<<endl;
29 return out;
30 }
31 int main(){
32 Volume v(2,3,4);
33 Volume v2(4,4,4);
34 v2=++v;
35 v2=v++;
36 cout<<v2;
37 cout<<v;
38 v2=++v;
39 cout<<v2;
40 cout<<v;
41 return 0;
42 }

Page 4 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

(d) (12 Marks) Identify and correct the errors in the following code so that the main() function could run.
You cannot change the main() function!
1 #include <string>
2 #include <iostream>
3 using namespace std;
4 class Employee{
5 string name;
6 const int nClients;
7 string *Clients;
8 Employee();
9 public:
10 Employee(char E[], int n){
11 name = E;
12 nClients = n;
13 Clients = new string[nClients];
14 }
15 string & operator[](int i){
16 return Clients[i];
17 }
18 int getnClients(){
19 return nClients;
20 }
21 ˜Employee(){
22 delete [] Clients;
23 }
24 };
25

26

27 void operator>>(istream in, Employee E){


28 cout<<"Enter the names of "<<E.getnClients()<<" clients:"<<endl;
29 for(int i = 0; i < E.getnClients() ;i++)
30 in >> E[i];
31 }
32

33 void display(Employee E){


34 for(int i = 0; i < E.getnClients() ;i++)
35 cout<<E[i]<<endl;
36 }
37

38 int main(){
39 Employee E1("Abassi", 2);
40 cin>>E1;
41 display(E1);
42 return 0;
43 }

Page 5 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

(e) (8 Marks) Identify and correct the errors in the following code so that the main() function could run.
You cannot change the main() function!
1 #include <iostream>
2 using namespace std;
3 class Complex
4 {
5 float real, imag;
6 public:
7 void setR(float x){
8 real = x;
9 }
10 void setI(float x){
11 imag = x;
12 }
13 float getR(){
14 return real;
15 }
16 float getI(){
17 return imag;
18 }
19 void display(){
20 cout<<real<<" + i"<<imag;
21 }
22 };
23

24 ostream & operator<<(ostream &o, const Complex &C){


25 C.display();
26 }
27 int main() {
28 Complex C;
29 cout<<C.setR(3).setI(3); //should print 3 + i3
30 return 0;
31 }

Page 6 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

Question II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (30 Marks)


Consider the following figure:

We need a map (or dictionary) to store data about medical practitioners. A map consists of multiple [key, values]
entries, where extra space is dynamically allocated when needed. Typically multiple values may be mapped
against one key. For simplicity, we assume that maximum six (06) values can be stored against one key (as shown
in figure). Here key is the specialty of medical practitioner, and values are names of medical practitioners.
You are required to design and implement map class (and any other required classes) in C++, where following
code should run without any syntax/logical errors:
1 void main()
2 {
3 CMap map, map2, map3; // assume CMap is the name of map class
4

5 // operator[] is used to get "entry" against given "key" from map and
6 // can be used in following context with operator= to create/refresh an entry
7 map["ENT"] = "Dr. Sibt Ul Hussain";
8 map["Radiology"] = "Dr. Sana Mir";
9 map2["Cardiology"] = "Dr. Umer Farooq";
10

11 // operator+= with an "entry" is used to add new "value" against given "key".
12 // If entry with given "key" does not exist then new entry is created in map
13 map["ENT"] += "Dr. Ehtesham Zahoor";
14 map["ENT"] += "Dr. Shahid Afridi";
15 map2["Cardiology"] += "Dr. Husnain Abdullah";
16 map2["Cardiology"] += "Dr. Omer Beg";
17 map2["Cardiology"] += "Dr. Mark Waugh";
18 map2["Cardiology"] += "Dr. Saeed Anwar";

Page 7 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

19 map2["Cardiology"] += "Dr. David Warner";


20

21 // following statement creates new entry in map, and then add values to it
22 map2["Dermatology"] += "Dr. Bismah Maroof";
23 map2["Dermatology"] += "Dr. Sidra Ameen";
24 map2["Dermatology"] += "Dr. Wahab Riaz";
25 map2["Dermatology"] += "Dr. Zahid Alam";
26

27 // operator+ is also used to add two maps and return a new map.
28 // operator= is used to assign one map to other (deep copy).
29 map3 = map + map2; // map3 now looks like the map in figure above!
30

31 // operator-= is used to remove value from the map against key = ENT.
32 // If value does not exist, then nothing changes in the map. If a "key"
33 // has no remaining value against it, then the "key" must also be removed.
34 map["ENT"] -= "Dr. Ehtesham Zahoor";
35 }

Page 8 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

Page 9 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

Page 10 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

Question III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (14 Marks)


Your goal in this question is to design a mobile application management system in other words an ”App Store”.
In the application store you can store information about different type of applications such as system and enter-
tainment applications. For all the types of applications you will store application id (integer), programmer id
(integer), and date of publishing. System applications include number of threads they can use and number of
hardware devices they can access. Entertainment applications have predefined storage limit and already defined
features.
Apart from the applications’ information, App Store also stores user information, i.e. his name and email. Fur-
thermore, it also records for each user what applications he is currently using.
Now given this description, identify all the classes, their data members and their relationships (composition,
aggregation, inheritance) for the complete system. I.e. implement the identified classes without their member
functions.

Page 11 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II

Page 12 of 12 End

You might also like