Answers of Final - COM253 - Fall 2023
Answers of Final - COM253 - Fall 2023
Faculty Engineering
Department Mechatronics Systems Engineering
Module Code COM253
Module Title Engineering Computer Programming
Semester Fall 2023
Time Allowed 3 Hours
Total Mark 40
No. of Pages 6+ cover
Material provided ------------
Equipment permitted - Scientific calculator
- PC; disconnected from internet.
- VC++ Compiler (Visual Studio)
- Printer
Additional Instructions - All Answers must be in English otherwise it will not be considered.
- Any code developed on the PC should be either re-written in the
answer sheet or printed and delivered as a hardcopy attached to it.
- Attach page _4_ to your answer sheet
No books, paper or electronic devices are permitted to be brought into the examination room
other than those specified above.
Faculty of Engineering
Model Answers of Final Exam
Then write down main() function that uses the above mentioned functions to produce the following pattern:
X
X X
X X X
X X X X
X X X
X X
X
Answer:
#include <iostream>
using namespace std;
void Triangle(int n,char c='X')
{
for (int i = 0; i < n; i++)
{
cout << ' ';
for (int j = 1; j < 2*n; j++)
{
if (j < (n - i))
cout << ' ';
else if (j <= (n + i))
cout << c;
else
break;
}
cout << "\n";
}
}
void Inverted_Triangle(int n, char c = 'X')
{
for (int i = 0; i < n; i++)
{
for (int j = 1; j < 2 * n; j++)
{
//cout << "j=" << j;
if (j <= i)
cout << ' ';
else if (j < (2*n - i))
cout << c;
else
break; Run
}
cout << "\n";
}
}
int main()
{
Triangle(3);
Inverted_Triangle(4);
return 0;
}
Page 2 of 7
1.2 Write a C++ program using functions that: ......................................................................................................................... (5 Marks)
1- Define a structure named Student that has the following data fields:
- Name
- ID
- GPA
- Grades (of 5 courses)
2- For number of three students, find the average grade for each student and display it as a formatted table.
3- Find and print the student’s data which has the maximum average grade.
Answer:
#include <iostream>
using namespace std;
struct Student
{
long ID;
string Name;
float GPA;
float Grades[5];
};
Page 3 of 7
Question 2 : .................................................................................................................(10 Marks)
Write a C++ program to calculate the following series e given number of terms (N) and the
factor x using functions as the following (Remark: DO NOT use math library):
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝑵
𝒆(𝒙, 𝑵) = + + + + …+
𝟎! 𝟏! 𝟐! 𝟑! 𝑵!
#include <iostream>
using namespace std;
long Fact(int x)
{
if (x <= 0)
return 1;
else
return x * Fact(x - 1);
}
Page 4 of 7
Question 3 : ...............................................................................................................(10 Marks)
For the following code (Page 4):
3.1 In the shadow box, write down your code that permits the whole code to run properly as per
OOP principles.................................................................................................................. (2 Marks)
3.2 After completing the code mark the following (by circling around it/them) then write its
description in your answer sheet :................................................................................... (6 Marks)
1) Abstract class
2) Constructor(s)
3) Inheritance statement(s) and its tree
4) Polymorphism
5) Virtual function(s)
6) Encapsulation
3.3 Trace the code to get its output using memory map ...................................................... (2 Marks)
Answer:
3.1
Shape(int i = 0) { id = i; }
Shape
Rectangle Circle
4) array of pointer to base class points to derived class objects (Shape* shapes[])
5) getArea()
6) Data + functions : private and public members of classes Shape, Rectangle and Circle
3.3
Run:
Page 5 of 7
Attach to your answer sheet.
#include <iostream>
#include <iomanip>
#include <cmath>
class Shape
{ 1) Abstract Class
int id;
public:
int getID()
{ return id;}
string getType()
{ return (id == 1) ? "Rectangle" : "Circle"; }
};
class Rectangle : public Shape //3- Inheritance
{
private: //6-Encapsulation
double length;
double width;
public:
Rectangle(double l, double w) : Shape(1)
//2-Constructor
{ length = l; width = w; }
virtual double getArea()
{return length * width;}
};
class Circle : public Shape // 3-Inheritance
{
private:
double radius;
public:
Circle(double r) : Shape(2)
//2-Constructor
{radius=r;}
virtual double getArea()
{return Pi * radius * radius;}
};
int main()
{
Shape* shapes[2];
//4-Polymorphism
shapes[0] = new Rectangle(4, 5);
shapes[1] = new Circle(4);
for (int i = 0; i < 2; ++i)
{
cout <<"Shape:\tID:" << setw(3) << shapes[i]->getID()
<<",\tType:" << setw(10) << shapes[i]->getType()
<<",\tArea:" << setw(5) << shapes[i]->getArea()
<< endl;
}
delete shapes[0];
delete shapes[1];
return 0;
}
Page 6 of 7
Question 4 : .................................................................................................................(10 Marks)
Fill in the blanks with the appropriate word, term, or symbol:
1. _________ is a named block of code that performs a specific task and can be reused.
5. _________ is a class that cannot be instantiated but can be a base for other classes.
6. _________ is a process of combining data and functions into a single unit called an object.
7. _________ is a repetition control structure that executes a block of code as a certain condition is still true.
8. _________ is an operator that returns true if two operands are equal and false otherwise.
9. _________ is an operator that returns the reminder of the division of two integer operands.
10. _________ is an operator that increments the value of its operand by one.
11. _________ is an operator that assigns the value of its right operand to its left operand.
12. _________ is an operator that accesses the members of an object, of a class or structure, using a pointer to that object.
13. _________ is an operator that releases the memory allocated dynamically by the new operator.
14. __________ allows functions having the same name, but with different parameters, to be defined in the same scope.
15. _________ permits a pointer-of-superclass pointer that points to objects of different subclasses to invokes their functions
dynamically depending on their own definition.
17. ____________ involves a function calling itself multiple times with different arguments.
18. Functions that don't return a value are declared with the return type ____________.
19. ____________ loop is used when a set of statements needs to be executed certain number of times, repeatedly.
20. ____________ statement is used for decision-making and allows the program to execute different code blocks based on
the value of an integer variable.
Answer:
1- function 11- =
2- constructor 12- ->
3- inheritance 13- delete
4- virtual 14- overloading
5- abstract 15- Polymorphism
6- Encapsulation 16- Instantiation
7- while/conditional 17- Recursion
8- == 18- Void
9- % 19- for/counted
10- ++ 20- Switch-case
Page 7 of 7