Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Week-3_Assignment

Week-3_Assignment

Published by harshaphatak, 2020-11-04 06:12:51

Description: Week-3_Assignment-3_v3

Keywords: my_assignment

Search

Read the Text Version

Programming in C++: Assignment Week 3 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur – 721302 [email protected] August 9, 2019 Question 1 [MCQ, Marks 2] Consider the program below. #include <iostream> #include <cmath> using namespace std; int init_m1(int m1) { cout << \"init _m1 : \" << m1 << endl; return m1; } int init_m2(int m2) { cout << \"init _m2 : \" << m2 << endl; return m2; } int init_m3(int m3) { cout << \"init _m3 : \" << m3 << endl; return m3; } class MyClass { int ____________________; // LINE-1: declare the data members public: MyClass(int m1, int m2, int m3) : _m1(init_m1(m1)), _m2(init_m2(m2)), _m3(init_m3(m3)){} }; int main() { MyClass obj(10, 20, 30); return 0; } 1

Declare the data members at LINE-1 such that the output is as follows: init _m3 : 30 init _m2 : 20 init _m1 : 10 a) m3, m2, m1 b) m1, m2, m3 c) m2, m3, m1 d) m3, m2, m1 Answer: d) Explanation: The order of invocation to initialization-list function take place as per the order of the data members. 2

Question 2 [MCQ, Marks 2] Consider the program below. #include <iostream> using namespace std; class Circle { public: Circle() { cout << \"Calling the Constructor\"; } }; int main() { // LINE-1 Circle obj1; // LINE-2 Circle *obj2; return 0; } What will be the output / error? a) Calling the Constructor Calling the Constructor b) Calling the Constructor c) Error in LINE-1 d) Error in LINE-2 Answer: b) Explanation: Only one object obj1 is constructed here. obj2 is just a pointer, not an object. Hence the constructor will be called for obj1 only. 3

Question 3 [MSQ, Marks 2] In the below code which LINE/s will give error/s? #include <iostream> using namespace std; class accessed { private: int p; void getp() { cout << \"inside getp\"; } public: int h; void geth() { cout << \"inside geth\"; } }; int main() { // LINE-1 accessed obj; // LINE-2 obj.p = 2; // LINE-3 obj.h = 3; // LINE-4 obj.getp(); obj.geth(); return 0; } a) LINE-1 b) LINE-2 c) LINE-3 d) LINE-4 Answer: a), c) Explanation: All the class members declared under public accessible inside and outside of the class. The class members declared as private can be accessed only by the functions inside the class. LINE-1 gives error as p is private member. LINE-2 is fine as h is public member. LINE-3 gives error as getp() is private member. LINE-4 is fine as geth() is public member. 4

Question 4 [MCQ, Marks 2] Consider the program below. #include <iostream> using namespace std; class This_Test { private: int x; public: This_Test(int x = 0) { this->x = x; } void changeptr(This_Test *t) { this = t; } void printx() { cout << \"x = \" << x << endl; } }; int main() { This_Test obj(5); This_Test *ptr = new This_Test(10); obj.changeptr(ptr); obj.printx(); return 0; } What will be the output / error? a) x = 5 b) x = 10 c) Compiler Error: lvalue required as left operand of assignment d) Compiler Error: private x is inaccessible Answer: c) Explanation: In void changeptr(This Test *t) { this = t; } an assignment to this is being attempted. Since this is a constant pointer (This Test * const), hence it cannot be changed and the error occurs during compilation. 5

Question 5 [MCQ, Marks 2] Consider the program below. #include <iostream> using namespace std; class A { public: A() { std::cout << \"a\"; } ~A() { std::cout << \"A\"; } }; class B { public: B() { std::cout << \"b\"; } ~B() { std::cout << \"B\"; } }; class C { public: C() { std::cout << \"c\"; } ~C() { std::cout << \"C\"; } }; __________ // LINE-1 void foo() { __________ } // LINE-2 int main() { // LINE-3 __________ foo(); return 0; } Identify the code to be inserted in LINE-1, LINE-2 and LINE-3 so that the output will be abcCBA? a) LINE-1: A a; LINE-2: B b; LINE-3: C c; b) LINE-1: A a; LINE-2: C c; LINE-3: B b; c) LINE-1: C c; LINE-2: B b; LINE-3: A a; d) LINE-1: B b; LINE-2: A a; LINE-3: C c; 6

Answer: b) Explanation: The global object will be constructed before main() is called. Then, the main() will be called. main() invokes function foo(). So, in this order the constructor for the objects will be called and the corresponding destructors will be called in the reverse order. 7

Question 6 [MCQ, Marks 2] What is the output of the following program? #include <iostream> using namespace std; class Test_class { }; int main() { Test_class a, b; if (&a == &b) cout << \"Inside_If\" << \" \" << sizeof(a) << endl; else cout << \"Inside_Else\" << \" \" << sizeof(a) << endl; return 0; } a) Inside If 1 b) Inside Else 1 c) Inside If 0 d) Inside Else 0 Answer: b) Explanation: The objects a and b will be allocated different location of memory so addresses of a and b are not same. Hence, a) and c) are not correct. Size of an empty class is not zero. It is 1 byte usually. It is nonzero to ensure that the two different objects will have different addresses. 8

Question 7 [MCQ, Marks 2] Consider the program below. #include <iostream> using namespace std; class Test { public: Test(int i) { std::cout << \"Constructor execute : \" << i << std::endl; } ~Test() { std::cout << \"Destructor execute \" << std::endl; } }; void myfunc() { Test obj(1); } Test obj(2); int main() { Test obj(3); myfunc(); return 0; } What is the output? a) Constructor execute : 1 Constructor execute : 2 Destructor execute Constructor execute : 3 Destructor execute Destructor execute b) Constructor execute : 2 Constructor execute : 3 Destructor execute Constructor execute : 1 Destructor execute Destructor execute c) Constructor execute : 2 Constructor execute : 3 Constructor execute : 1 Destructor execute Destructor execute Destructor execute 9

d) Constructor execute : 1 Constructor execute : 2 Constructor execute : 3 Destructor execute Destructor execute Destructor execute Answer: c) Explanation: First the global object will be created as Test obj(2); Hence the output is Constructor execute : 2. This object is last to be destroyed. The object inside main() will be created next as Test obj(3); Hence the output is Constructor execute : 3. Finally, the object inside myfunc() will be created as Test obj(1); Hence the output is Constructor execute : 3. The destructors will be called in reverse order. 10

Question 8 [MCQ, Marks 2] Identify erroneous LINE/s in the program below? #include <iostream> using namespace std; class ABC { public: int x; ABC() { x = 20; } void function1() const { x = 30; // LINE-1 } void function2() const { cout << \"Inside function2 \" << x; } }; int main() { const ABC a; a.x = 10; // LINE-2 a.function2(); // LINE-3 return 0; } a) LINE-1, LINE-2, LINE-3 b) LINE-2, LINE-3 c) LINE-1, LINE-3 d) LINE-1, LINE-2 Answer: d) Explanation: void function1() const is a constant function. So it cannot change data member x inside its body as in LINE-1. Object a is a constant object (const ABC a), so its data members cannot be changed which is tried in LINE-2. 11

Question 9 How many times would the Copy Constructor run in the program below? [MCQ, Marks 2] #include <iostream> using namespace std; class Test { public: Test() { cout << \"Test Constructor running\\n\"; } Test(const Test& c2) { cout << \"Copy Constructor of Test is running\\n\"; } Test& func(Test &obj2) { Test &obj3 = obj2; return obj3; } }; int main() { Test obj1; obj1.func(obj1); return 0; } a) 0 b) 1 c) 2 d) 3 Answer: a) Explanation: Test obj1; invoke the default constructor. Function Test& func(Test &obj2) takes parameter by reference. Hence, the formal argument refers to the old object passed (the actual argument) to it. As no new object is created – it does not requires any (copy) constructor invocation. In Test &obj3 = obj2; line obj3 simply refers to the old object obj2. Hence, no copy constructor call is required. Finally, function Test& func(Test &obj2) returns and does not require copy. So the copy constructor is not invoked in this program. 12

Question 10 Consider the class below. [MCQ, Marks 2] class sample { // more code } What will be type of implicit this pointer for class sample? a) sample * const this b) sample const * const this c) sample * this d) const sample const * this Answer: a) Explanation: this pointer is a constant pointer of the same class type which holds the base address of the current object. Hence it is sample * const this. 13

Programming Questions Question 1 Consider the following program and fill the blanks (in LINE-1, LINE-2, and LINE-3) with appropriate definitions for constructor, destructor and area(). Please check the sample input and output. Marks: 3 #include<iostream> using namespace std; class rect { int *_w, *_h; public: rect(int, int); ~rect(); int area(); }; // LINE-1 Define the constructor rect::rect(int w, int h) : ____________________ { /*do not write anything*/ } // LINE-2: Write the destructor header ____________________ { delete _w, _h; } // LINE-3: Define header of area() ____________________ { return *_w * *_h; } int main() { int a, b; cin >> a >> b; rect r(a, b); cout << r.area(); return 0; } Public 1 Input: 10 20 Output: 200 Public 2 Input: 5 9 Output: 45 Private Input: 10 15 Output: 150 14

Answer: LINE-1: w(new int(w)), h(new int(h)) LINE-2: rect::∼rect() LINE-3: int rect::area() Explanation: As in the destructor memory allocated for * w and * h freed using delete operator, in con- structor the allocation of memory must be done dynamically using new operator. Hence we use the constructor with initialization list (at LINE-1) as: rect::rect(int w, int h) : _w(new int(w)), _h(new int(h)){} In LINE-2, we define the destructor outside of the class, so we need to use scope resolution operator as follows: rect::∼rect() { delete w, h; } Similarly, in LINE-3 we have defined the area() function outside of the class hence we need to use scope resolution operator as follows: int rect::area() { return *_w * *_h; } 15

Question 2 Consider the following program and complete the definition of function length() so that it matches the given sample input and output. Hints: The distance between two points (x1, y1) and (x2, y2) is computed by the formula ((x2 − x1)2 + (y2 − y1)2). Marks: 3 #include <iostream> #include <cmath> using namespace std; class point { public: int x, y; }; class line { public: point pt1, pt2; }; double length(line &obj) { // Complete the return statement return ____________________ // LINE-1 } int main() { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; line l = { { x1, y1 }, { x2, y2 } }; cout << length(l); return 0; } Public 1 Input: 10 20 40 60 Output: 50 Public 2 Input: 100 150 200 150 Output: 100 Private Input: 100 200 150 320 Output: 130 16

Answer: LINE-1: sqrt((obj.pt1.x - obj.pt2.x) * (obj.pt1.x - obj.pt2.x) + (obj.pt1.y - obj.pt2.y) * (obj.pt1.y - obj.pt2.y)) Explanation: As the length() is a global function with the line object as its argument, and as data members in classes point and line are public, the length can be calculated using the definition or its variants. double length(line &obj) { return sqrt((obj.pt1.x - obj.pt2.x) * (obj.pt1.x - obj.pt2.x) + (obj.pt1.y - obj.pt2.y) * (obj.pt1.y - obj.pt2.y)); } 17

Question 3 Consider the following program and define the constructor at LINE-1. Please check the sample input and output. Marks: 3 #include <iostream> #include <cmath> using namespace std; class MyClass { // LINE-1: define the constructor here const int _i; public: ____________________ void display() { cout << _i << endl; } }; int main(){ int a; cin >> a; MyClass obj(a); obj.display(); return 0; } Public 1 Input: 10 Output: 10 Public 2 Input: -5 Output: -5 Private Input: 6 Output: 6 Answer: LINE-1: MyClass(const int i): i(i) { } or LINE-1: MyClass(int i): i(i) { } Explanation: Since i is a const member, it must be initialized in the constructor using the initializer list. We cannot initialize const data member in C++ using assignment operator in constructor (that is, MyClass(int i) { i = i; } is wrong). The form is: LINE-1: MyClass(const int i) : i(i) or LINE-1: MyClass(int i) : i(i) 18

Question 4 Consider the following program and fill in the blanks (LINE-1 to 3) with appropriate keywords. Please check the sample input and output. Marks: 3 #include <iostream> #include <cmath> using namespace std; class MyClass { // LINE-1 int _m1; __________ int _m2; // LINE-2 // LINE-3 public: MyClass(int m1, int m2) : _m1(m1), _m2(m2) {} void set_m2(int m2) __________ { _m2 = m2; } int get_m2() __________ { return _m2; } }; int main() { int a, b, c; cin >> a >> b >> c; const MyClass obj(a, b); obj.set_m2(c); cout << obj.get_m2(); return 0; } Public 1 Input: 10 10 20 Output: 20 Public 2 Input: 32 34 56 Output: 56 Private Input: 23 34 55 Output: 55 Answer: LINE-1: mutable LINE-2: const LINE-3: const Explanation: A mutable data member m2 can be accessed as well as updated even in a const member function for const objects. 19


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook