Download as pdf
Download as pdf
You are on page 1of 12
Exercises Many of these exercises are taken from past exams of ECE 244 Programming Fundamentals, courses at University of Toronto, The solutions are provided in the answer boxes. Headings inthis page classify the exercises into ferent categories: (Easy), [Intermediate], anc [challenging]. | suggest you start by easy exercises and work your way up to the challenging ‘Question 11 in Fall 2018 Final Exam [Easy] Consider the following class definitions and implementations, as well asa nain function that uses them All the code appears in one file and it compiles with no errors. ‘include ctostrean> using manespace std class firstone { private: ‘int x ‘char faests pubic ‘Firstoned)s instone(const char* 5); » lass secondone : public fiestone ( private: int yi har" seconds public Ssecondone (const char* 5); Seconcone(int ¥)3 » faestone:férstone() ( First» new char[1}s test = "8; cout << "Constructor 1 of irstone done” << endl; d ‘Hestone::Firstone(const char* 5) { x= 8 nt size = stelen(s)s First = new char[size + 215 strepy(first, 5); cout ce "Constructor 2 of FirstOne done” << endl; d secondone::secondone(const char 5) ¢ ye nt size = strien(s)s second = new char[size + 1)5 Strepy(second, 5); cout << “Constructor 1 of secondOne done” << endl y secondone:ssecondone(int v) : firstone("x") ¢ yo vs Second = new char(i}s ‘second = "e's cout << "Constructor 2 of secondone done” << endly > nt maint) ¢ ‘FirstOne 3 finstone ("6"); secondOne €(°N")3 seconadne (8); > Indicate what each statement in the above main function prints, using the table below. Ifno ‘output is produced, write “NONE” Statement Output ‘FirstOne as ‘farazone 276"); secondone © ("1 secondone (8); © Answer > ‘Question 14 in Fall 2019 Final Exam [Easy] Consider the following class definitions and implementations of classes, shape and circle. They are followed by a main function that utilizes these classes. include Using manespace std class shape { protected fine shapelDs pubic: it getz0() { return shapelos } void set7D(int k) { shaper - ij > virtual void drax() = 8 virtual void print) % class circle : public shape { protected ‘oat radius; pubtte Float gethadivs() ( retuen radius; } void setRadius(float r) { radius = r5 ) virtual void draw() ¢ Y/ code 0 drew ? % lass Rectangle : public shape { protected ‘flast Length; ‘Float width; pubic: ‘Float getLength() { return Length; } void setLength(float x) ¢ length =x: ) Float getwidtn() { return width; ) vod sethidth(flea x) { width = x) virtual void draw() { YW Cote to drow virtual void prént() 1 Coe 0 print ) » ant nain() ¢ U/ Statements 1» the main function return 93 y Indicate which ofthe following statements that appear inthe main funetion compile with no ‘errors or produce a compile time error. Indicate algo the reason in the table below. Does the code snippet compile? If yes, Why? shape si Rectangle resetr0(9) fectangle 4; autengtn = 3.05 © Answer ‘Question 12 in Fall 2018 Final Exam [Intermediate] Consider the following base and derived classes. They compile with no errors. ‘Winchude ctostrean> ‘rinelude using manespace std; class Dessert ( protected int price = &: pubic Dessert() { cout << “we have dessert\n"s “bessert() ( cout << "no nore decsert\n"} ) void prine() { cout class candy : public Dessert ( protected ‘nt wedght_sn_grans; public ‘Gandy(int grans, int per_gran) { ‘welght_in_grans = grans; price > per gran: cout ce "we have cangy\n"s » “candy() { cout << “no sore candy\n"s void prant() { cout Virtual int cost) { return weight in_grans © prices » class Cookies : public Dessert ¢ protected ft nun_dozens; pubic: Cookiestint dozens, int per_dozen) { run_dozens = dozens; prize = per_dozens cout < "he have cookses\n"s > “cooktes() { cout << “no more cookies\n"s } void prant() ( cout ce "Cookies: * ¢¢ mumdovens << "dozens, st " << price Ke" cents per Sovem\n' ? Virtual int cost() { return num_dozens * price; } % class Teeceeam + public Dessert { protected ‘string flavor = "vanilla"; int scoops; pubic: Teecrean(string fly, int seps, int per_scoop) ¢ flavor = Flys scoops = 56963 price = per_scoop; cout ce "he have" <¢ flavor ¢¢ " ce Coeam\n"s “reecrean() { cout << “no more ice crean\n"; ota print) { cout 2) dnt maing) ¢ Dessert” pe; Teecrean” pit pe ~ new Cookdes(2, 258); Pi = new Teecrean("rist”, 1, 250)3 delete pei delete pi: return 0; oes the program compile with no errors? IFit does compile with no errors, what is the ‘output produced by the execution? © Answer > Bint main() ‘sundae ps; Teacrean™ pi; ph new Teecrean("rint’, 1, 250)3 ps = pis aerete ps; © Answer > 4. ine maint) ¢ Dessert” ps = new Icecrean("vanilla", 2, 350)s out ce pe->cost() ce endl return 0; y © Answer > ‘Question 15 in Fall 2019 Final Exam [Easy] Consider the following class definitions, class Basee { private: int 3) void help()s protected int bs pubic int & int @) vod print); » lass Derivede + public Base ( private: int *) void rohelp()s pubic int 5 int 2) void noprint()s » The following declaration appears in the noteip member function of the class Derived. Ddertvede derived: Indicate whether the following statements is correct code (ie, compiles with no errors), or incorrect code (Le, generates a compile time error. Assume the statements in the rows of the table also appear in the nohelp member function of the class Derived Statement Correct/Incorrect devived.a = 8 Gerived.b = 105 erived.x = 425 serived.n = 4; © Answer > ‘Question 16 in Fall 2019 Final Exam [Intermediate] Consider the following base and derived classes. They compile with no errors include ctostream> include using manespace sta; class circuitelenent ( protected int code: pubic: ‘ircuitelenent() { cout << “eicust elenent\n"; } CCireuitelenent(int ¢) { code = cf cout © Answer > 2 ant maint) ¢ ‘careultelenent™ prs pr = new PowerResistor(200, 250); delete pr ) © Answer > 3.) de maing) ¢ ‘copacitor® ¢; c= new circuttelenent(5); aetete c; return © > © Answer > 4. sot main) ¢ Powerfesistor” prj Resistort rs = new Resiston(450); pre aetete prs © Answer > 5.) dnt maing) ¢ PowerResistor (250, 1000); CErevitélenent €; return © ) © Answer > 6.) Ant maint) ¢ ‘cancuitélenent* @ = new Powercapacitor(s0@, 2080); esprintOr return 0 > © Answer > 7.) Ane maint) ¢ ‘Capacitor’ ¢ = new Powercapacitor(308, 100); cout ce c->getPower() << endl; return 0 y © Answer > 8) Ant main) ¢ PowerResistor pe(28, 108); Resistost = Spry cout << r-spowertesistance() << endl; ) © Answer > ‘Question 5 in Fall 2022 Final Exam (Intermediate) ‘The University of Toronto wants to manage students from different departments using a C++ program, Students from all departments will share the following base class: class student ( protected string mane; int 4a; public: student(const string nane_, int 4d_) : mane(nane.), £4(4¢.) ( cout cc "stugent” ce endl w virtual votd print_server() = @3 virtual void print departrent() = string get_nane() { return nates } int get_ie() { return td; } y Derived from student class, the programmers then write another class for engineering students, All engineering students have access to the ECF server: lass eng student + public student { protected int ecf_ics pubic: feng student (const string nane_, int id_, int ecid_) student(nane_, 16), ecf_id(ect 14) { cout cc "eng student” << endl b Virtual votd print server() { cout ce mane % In addition, programmers write a new derived class for Computer Science (CS) students, Unlike ‘engineering students, they use Markus servers instead of ECF servers: lass cs_student + public student ( protected ‘int markus_Ads pubic: ‘s_student (const string nane_, int id_, int markus id) studentnane_, 1d_), markus_ia(narkus 1) { cout ce "es student” << endl; > Virtual votd print _server() { cout ce mame <é * logs into markus.” 2. Now, an array named student_2ist is declared and initialized as follows. Write the output (generated by std::cout. stucent® student lst[4] = ( ‘new cs_student{Leo", 8, 18), new ece_student("Si21"s 1 11, 0), fnew mie_student("Ellic", 2, 12), new ece_student(Holey", 3, 13,2) B © Answer 3. After the student_1is initialized in question 2, the following for loops will be executed, ‘What wil be the output generated by these two loops? cout << "Department: " <¢ endl for (int 1" 0; 4 < 45 44) ¢ student_List[1]->print_departrent(); > cout cc “Server: ce endly for (int i= 8; 4.04; 444) Student_List[1]-sprint_serven(); > © Answer > By Salma Emara eta © Copyright 2028,

You might also like