0% found this document useful (0 votes)
55 views13 pages

CS103 Computer Programming Mid-II: National University of Computer and Emerging Sciences

The document is a midterm exam for a computer programming course at the National University of Computer and Emerging Sciences in Islamabad, Pakistan. The exam contains 3 questions worth a total of 75 marks. The questions involve analyzing C++ code snippets, identifying errors, and predicting output. Students are instructed to show their work, explain errors, and allocate time properly according to the marks distribution for each question.

Uploaded by

20i0863 Maryam
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)
55 views13 pages

CS103 Computer Programming Mid-II: National University of Computer and Emerging Sciences

The document is a midterm exam for a computer programming course at the National University of Computer and Emerging Sciences in Islamabad, Pakistan. The exam contains 3 questions worth a total of 75 marks. The questions involve analyzing C++ code snippets, identifying errors, and predicting output. Students are instructed to show their work, explain errors, and allocate time properly according to the marks distribution for each question.

Uploaded by

20i0863 Maryam
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/ 13

National University of Computer and Emerging Sciences

School of Computing Spring 2017 Islamabad Campus

CS103 Serial No:


Mid-II
Computer Programming Total Time: 1 Hour
Tuesday, March 28, 2017 Total Marks: 75
Course Instructor(s)
Sibt ul Hussain, Aneequa Sundus and Atifa Sarwar
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 (13) different printed pages including this title
page. There are total of (3) 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 allocate your time properly according to the marks distribution.
9. Write proper explanation of the error (or bug) where required, without proper explanation no marks will be
awarded.

I II III Total
Total Marks 35 20 20 75
Marks Obtained

Vetted By: Vetter Signature:


CS103 - Computer Programming Spring 2017 Mid-II

Question I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (35 Marks)


Please write proper explanation of the bug or error where required, without proper explanation no marks will be
awarded.
(1) (5 Marks) What would be the output produced by executing the following C++ code? Identify and correct
errors, and write output, if any.
1 #include <iostream>
2 using namespace std;
3 class Number {
4 private:
5 int *n;
6 public:
7 Number() : n(new int) {
8 *n=5;
9 }
10 Number( int nn )
11 :n(new int)
12 {
13 *n=nn;
14 cout << *n<<" ";
15 }
16 Number(Number const& otherNum)
17 : n(otherNum.n)
18 {
19 cout << *n<<" ";
20 *n+=4;
21 }
22

23 void display() { cout << *n<<" "; }


24 void increase() { *n += 1; }
25 };
26 int main(){
27 Number a, b(1), c(b);
28 b.increase();
29 c.display();
30 b.display();
31 }

(2) (5 Marks) What would be the output produced by executing the following C++ code? Identify and correct
errors, if any.
1 #include<iostream>
2 using namespace std;
3 class A {
4 int len;
5 int *ptr;
6 public:

Page 2 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

7 A() {
8 len = 0;
9 ptr = NULL;
10 }
11 A(int l, int *arr) {
12 initialize(l);
13 for (int i = 0; i < l; i++)
14 ptr[i] = arr[i];
15 }
16 void initialize(int l) {
17 ptr = new int[l];
18 for (int i = 0; i < l; i++)
19 ptr[i] = 0;
20 }
21 void operator=(const A &a) {
22 len = a.len;
23 for (int i = 0; i < len; i++)
24 ptr[i] = a.ptr[i];
25 }
26 A operator+(const A a) {
27 A temp;
28 temp.initialize(len);
29 for (int i = 0; i < len; i++)
30 temp.ptr[i] = ptr[i] + a.ptr[i];
31 return temp;
32 }
33 int& operator()(int i) const {
34 if (i < len)
35 return ptr[i];
36 }
37 int GetLen() const {
38 return len;
39 }
40

41 ˜A() {
42 if (ptr != NULL)
43 delete[] ptr;
44 }
45 };
46

47 void operator <<(ostream &o, const A &a) {


48 for (int i = 0; i < a.GetLen(); i++)
49 o << a(i) << " ";
50 cout << endl;
51 }
52

53 int main() {
54 int arr1[] = { 1, 2, 3, 4, 5 };
55 int arr2[] = { 5, 4, 3, 2, 1 };
56 A v1(5, arr1), v2(5, arr2), v3;
57 cout << v1;
58 cout << (v1 + v2);
59 v3 = v2;

Page 3 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

60 cout << v3;


61 }

(3) (5 Marks) What would be the output produced by executing the following C++ code? Identify and correct
errors, if any.
1 #include<iostream>
2 using namespace std;
3 class Mystery {
4 int * p;
5 public:
6 Mystery(int value = 0) {
7 p = new int;
8 *p = value;
9 }
10 int getP() {
11 return *p;
12 }
13 bool setP(int x) {
14 *p = x;
15 }
16 Mystery & operator=(const Mystery& t) {
17 p = new int;
18 *p = *t.p;
19 }
20 Mystery add(const Mystery &toAdd) {
21 Mystery Res = toAdd;
22 *Res.p += *p + *toAdd.p;
23 return Res;
24 }
25 ˜Mystery() {
26 delete p;
27 }
28 };
29 int main() {
30 Mystery obj_one(3);
31 Mystery obj_two(6);
32 obj_one.add(obj_two);
33 cout << obj_two.getP();
34 return 0;
35 }

Page 4 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

(4) (5 Marks) What would be the output produced by executing the following C++ code? Identify and correct
errors, if any.
1 #include<iostream>
2 using namespace std;
3 class ShoppingCart {
4 private:
5 static int itemsCount;
6 string itemName;
7 public:
8 ShoppingCart() :
9 itemName("") {
10 }
11 ShoppingCart(string n) :
12 itemName(n) {
13 }
14 void setItemsCount(int count) {
15 this->itemsCount = count;
16 }
17 static int getItemsCount() {
18 return itemsCount;
19 }
20 };
21 int main() {
22 ShoppingCart Sc_one;
23 ShoppingCart Sc_two("1");
24 Sc_one.setItemsCount(5);
25 Sc_two.setItemsCount(10);
26 cout << ShoppingCart::getItemsCount();
27 return 0;
28 }

Page 5 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

(5) (5 Marks) What would be the output produced by executing the following C++ code? Identify and correct
errors, if any.
1 #include<iostream>
2 using namespace std;
3

4 class Point {
5 int x, y;
6 public:
7 Point(int a = 0, int b = 0) {
8 x = a;
9 y = b;
10 print();
11 }
12 void print() {
13 cout << " (" << x << "," << y << ") " << endl;
14 }
15 ˜Point() {
16 cout << "Point is going" << endl;
17 }
18 };
19 class Circle {
20 Point center;
21 float radius;
22 public:
23 Circle() :
24 center(0, 0) {
25 radius = 0;
26 cout << "The basic circle" << endl;
27 }
28 Circle(Point p) :
29 center(p) {
30 }
31 Circle(const Circle & c) :
32 center(c.center), radius(c.radius) {
33 cout << "The copied circle";
34 center.print();
35 }
36 ˜Circle() {
37 cout << "Circle is going" << endl;
38 }
39 };
40 int main() {
41 Point p1;
42 Circle c1(p1);

Page 6 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

43 Circle c3(c1), c4(Circle(c1));


44 return 0;
45 }

(6) (10 Marks) Identify and correct the errors in the following code so that the main() function could run (You
can add new functions if needed). You cannot change the main() function!
1 #include<iostream>
2 using namespace std;
3 class Student{
4 string name;
5 string rollno;
6 public:
7 Student(string n, string r){ name=n;rollno=r;}
8 };
9 class Class {
10 const int nstudents;
11 Student s[10];
12 public:
13 Class() {
14 nstudents=10;
15 }
16 Class(const Class c)
17 {
18 *this=c;
19 }
20 void operator<<(const ostream & out)
21 {
22 for(int i=0; i < nstudents;++i)
23 out<< s[i];
24 }
25 Student operator[](int & i)
26 {
27 if(i >=0 && i < nstudents)
28 return s[i];
29 }
30 };
31 Class operator=(const Class & c1, const Class & c2)
32 {
33 for(int i=0; i < nstudents;++i)
34 c1[i]=c2[i];
35 }
36 int main() {
37 Class c, c2;
38 c2=c;
39 c2<<cout;
40 c<<cout;

Page 7 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

41 return 0;
42 }

Page 8 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

Question II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (20 Marks)


Your goal is to write a program for creating a MovieStore.Your MovieStoreshould allow for storage of
many movies, where each movies should be represented by its name and a unique ID and ratings it has received
from users (assume there will be only ten ratings per movie). Your MovieStoreshould also store each user
information, where each user can have a name, and unique ID, and the ratings he has given to all movies (assumer
ten ratings from a user). In addition, to basic functionality (you have to identify the basic data members and
member functions) your MovieStore should allow facility of performing following operations:
1 int main(){
2 MovieStore s1, s2; // create two stores
3 s1.AddMovie("Hobbit", 4.5); // add a movie with average rating of 4.5
4 s1.AddUser("Alpha", 3.7); // add a user with average rating of 3.7
5 s1.AddMovieRatings("Hobbit", "Alpha") = 5; // Add the user "Alpha's"
,→ rating for movie hobbit this should add the user ratings to movie
,→ "Hobbit" as well as to users record as well
6 s1.AddMovieRatings(2, 3) = 1; // add the rating of 1 for movie id 2 form
,→ user-id 3;
7 cout << s1["Hobbit"]; // should print the movie hobbit ratings
8 cout << s1; // should display all the Movie store information
9 }

Page 9 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

Page 10 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

Question III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (20 Marks)


Your goal here is to create a system for managing menus at a restaurant. A user can build a menu from choosing
any of following four choices: Starter, Main Dish, Drinks, Desserts. Each choice can contain an item with its
name, number of servings, its price and whether price is charged per serving or not (a boolean). Your goal is to
identify and write the classes, their data members and functions. Your code must be able to be executed against
following sample code.
1 int main(){
2 Menu m1,m2;
3 m1 += "Starter";
4 m1 += "MDish"; //main dish
5 m1 += "Drinks";
6 m1 += "Desserts"
7 m1["Starter"] += Item("RussianSalad", 2, 300, true);
8 m1["Starter"] += Item("Raita", 1, 100, true);
9 m1["MDish"] += Item("Karahi", 7, 1200, false);
10 m1["Drinks"] += Item("7up", 6, 100, true);
11 m1 -= "Desserts";
12 cout<< m1; // should print the complete menu with each price item and
,→ total price.
13 m2+="Drinks";
14 m2["Drinks"] += Item("CocaCola", 3, 100, true);
15 cout<< m2;
16 }
(1) (5 Marks) Identify all the operators you need to overload, and write down their signatures.

Page 11 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

(2) (15 Marks) Now write all complete code with the required data members, member functions and operators

Page 12 of 13 Continue. . .
CS103 - Computer Programming Spring 2017 Mid-II

Page 13 of 13 End

You might also like