Professional Documents
Culture Documents
CS103 Computer Programming Sessional II: National University of Computer and Emerging Sciences
CS103 Computer Programming Sessional II: National University of Computer and Emerging Sciences
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
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
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
Page 6 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II
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
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
Page 11 of 12 Continue. . .
CS103 - Computer Programming Spring 2016 Sessional II
Page 12 of 12 End