0% found this document useful (0 votes)
126 views84 pages

CSE 109 Programming Exam BUET 2021

Uploaded by

tafhimul25
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)
126 views84 pages

CSE 109 Programming Exam BUET 2021

Uploaded by

tafhimul25
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/ 84

Scanned by CamScanner

Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
L-1/T-1/EEE Date: 11/01/2021

BANGLADESH UNIVERSITY OF ENGINEERING AND TECHNOLOGY, DHAKA


L-1/T-1 B. Sc. Engineering Examinations 2019-2020

Sub: CSE 109 (Computer Programming)


Full Marks: 180 Section Marks: 90 Time: 2 Hours (Sections A + B)
USE SEPARATE SCRIPTS FOR EACH SECTION
The figures in the margin indicate full marks.
SECTION – A
There are FOUR questions in this section. Answer any THREE.

1. (a) Consider the following partial class definition Student. The class has two member variables:
stdid and stdname. We want to store each student's id (e.g., 201906012) and full name (e.g.,
Fahim Hasan) in the stdid and stdname variables, respectively. The maximum length of a
student's full name can be 50 characters.
class Student
{
char stdid[10];
char * stdname;
public:
Student(char * id, char * name); //constructor function
//Implement destructor and copy constructor functions
};

Implement the following member functions for the above class Student:
i. Implement the constructor given in the class. The constructor uses the id and name 8
parameters to initialize the stdid and stdname member variables. You should allocate
appropriate amount of dynamic memory for the stdname variable using the C++ new
operator.

ii. Implement a destructor function for the class Student. 6

iii. Implement a copy constructor for the class Student. 10

(b) What is the main benefit of using inline functions in C++? Why should you not make every 6
function inline in a C++ program?

2. (a) What is wrong with the following program? Add only appropriate codes to the Demo class 8
to correct the program. Do not change the main function.

#include<iostream>
using namespace std;

class Demo
{
int a;
public:
Demo(int arg){ a = arg; }
};
int main()
{
Demo * d = new Demo[5];
return 0;
}
(b) Define a macro AREA(x) that computes the area of a circle whose radius is specified by the 6
macro parameter x. Your macro should correctly compute the area when arithmetic expressions
are used as arguments to call the macro.
L-1/T-1/EEE Date: 11/01/2021

(c) What is the difference between text files and binary files? Implement the following C 4+12
function to concatenate two files: =16

void concat_files(char * in1, char * in2, char * out);

The function has three parameters that denote three distinct filename strings. The first two
strings in1 and in2 denote the two input filenames, and the third parameter out denotes the output
filename. Your job is to read the contents of two input files and produce their concatenated
output (contents of in1 followed by the contents of in2) in the output file. Note that you should
not use any temporary array as a buffer to store file contents. Assume that input files will always
be available and output file should be created as a new file.

3. (a) Briefly explain the three most important features of an object oriented programming 12
language using relevant example codes in C++.

(b) Write two important differences between a pointer and a reference variable in C++. What 4+5
are the implications of the following two expressions in C++ considering that p1 and p2 are =
object pointers and r1 and r2 are object references? 9

p2 = p1;
r2 = r1;

(c) For each of the three cases mentioned below, give example programs of function 9
overloading that will create ambiguity.
i) Ambiguity due to type conversion.
ii) Ambiguity due to default arguments.
iii) Ambiguity due to reference parameters.

4. (a) Consider the following two struct definitions in a C program. 12


struct Point
{
double * x; //points to the memory where x coordinate is stored
double * y; //points to the memory where y coordinate is stored
};
struct Line
{
struct Point p1; //first point on a line
struct Point p2; //second point on a line
};

The first struct Point will be used to store a point’s x and y coordinates using dynamic memory
allocation to the given member variables which are pointers. The second struct Line defines a
straight line using two points anywhere on the line. Note that Line defines a straight line of
infinite length and not a line segment which has endpoints.

You task is to implement the following C function named intersect.

int intersect(struct Line * l1, struct Line * l2);

The function receives two lines as input where appropriate memory addresses of two Line
variables will be passed as arguments to call the function. The function will return 1 if the two
straight lines given as parameters intersect; otherwise return 0.
L-1/T-1/EEE Date: 11/01/2021

(b) Consider the following partial implementation of a C++ program. The program uses
inheritance to create a class Point3D that is used to store and manipulate 3-dimensional points.

#include<iostream>
using namespace std;

class Point2D
{
double x, y;
public:
Point2D(){ cout << “Base” << endl; }
void set_x(double arg){ x = arg; }
void set_y(double arg){ y = arg; }
double get_x(){ return x; }
double get_y(){ return y; }
};

class Point3D : public Point2D


{
double z;
public:
Point3D(double x, double y, double z)
{
//write codes to initialize a Point3D object with parameters x, y, and z.
}
double dist(Point3D &rhs)
{
//write codes to calculate the distance between this point and rhs
}
};

int main(void)
{
Point3D p1(1.0,2.0,3.0);
Point3D p2(3.0,4.0,5.0);
double d = p1.dist(p2);
cout << d << endl;
return 0;
}

Now, answer the following questions: 5+8+5


(i) Implement the given constructor of the Point3D class. =
(ii) Implement the dist function so that the third line in the main function correctly computes 18
the Euclidean distance between the points p1 and p2. Do not add anything to the Point2D class
and main function.

(iii) Suppose we change the keyword “public” in the line “class Point3D : public Point2D” to
“private”. Do you need to make any changes to your implementation of the dist function? If yes,
mention the changes briefly.
L-1/T-1/EEE Date: 11/01/2021

BANGLADESH UNIVERSITY OF ENGINEERING AND TECHNOLOGY, DHAKA


L-1/T-1 B. Sc. Engineering Examinations 2019-2020

Sub: CSE 109 (Computer Programming)


Full Marks: 180 Section Marks: 90 Time: 2 Hours (Sections A + B)
USE SEPARATE SCRIPTS FOR EACH SECTION
The figures in the margin indicate full marks.
SECTION – B
There are FOUR questions in this section. Answer any THREE.
5. a) What is the difference between variable declaration and variable definition in C? Differentiate
between local and global variables with appropriate examples. (6+6)
b) What is the output of the following C code? Explain how? (6)
#include<stdio.h>
int main(){
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}
c) What is the difference between ‘a’ and "a"? (6)
d) What value will be assigned to the variable X for the expression X = a/b+c*d-c ; if a = 10, b = 20, c =
30, d = 40? Explain how? (6)

6. a) Write a program in C to find and display all the factors of an integer entered by a user. (10)
b) Write a code segment that will calculate the sum of n terms of the following series: (10)
1 - 1/22 + 1/32 - 1/42 + 1/52 + ... 1/n2
c) Write a C program to find the maximum and the minimum element in an array using recursion. (10)

7. a) Write a C program to interchange diagonals of a square matrix. (12)


b) Write short notes on: call by value and call by reference. (8)
c) What will be the output of the following program? Explain how? (10)

#include<stdio.h>
void fun(int*, int*);
int main()
{
int i=5, j=2;
fun(&i, &j);
printf("%d, %d", i, j);
return 0;
}
void fun(int *i, int *j)
{
*i = *i**i;
*j = *j**j;
}
L-1/T-1/EEE Date: 11/01/2021

8. a) Write a C program to read any string from user and remove last occurrence of a given character from
the string. (12)
b) What is Dynamic Memory Allocation (DMA)? Mention the advantages of it over static memory
allocation. (10)
c) If p is a pointer, what does p[-2] mean? When is this legal? (4+4)
L-lff -l/EEE Date: 28/05/2022
BANGLADESH UNIVERSITY OF ENGINEERING AND TECHNOLOGY, DHAKA
L-I!T-I B. Sc. Engineering Examinations 2020-2021

Sub: CSE 109 (Computer Programming)

Full Marks: 210 Time: 3 Hours


The figures in the margin indicate full marks.
USE SEPARATE SCRIPTS FOR EACH SECTION

SECTION-A
There are FOUR questions in this section. Answer any THREE.

1. (a) Write down the type of the function, the return type, and the type of the parameters

for the following function signatures: (9)


1. int* (*f1) (int a) ;
2. void* f3(int *a[]);
3. int f4 (int a, int (*m)(int x)) ;

(b) For a given decimal number n, the function print_base prints n as it is represented in

base b. The function has the following prototype: (13)


void print_base (int n, int b)
Write down the body of this function.
Note: n can be negative. b will be between 2 and 10.
For n = 66 and b = 6 the output will be 150.

(c) Write a function that deletes all the occurrences of a given integer from an array in-
place. For example: for the array [0, I ,2, I ,3], removing I turns in into [0,2,3], but

removing -I does not change it. (13)


The prototype of this function is given by:
int delete(int val, int* arr, int len)
Here,
al'r is the pointer to the array and len is its length;
val is the number to be removed.

This function returns the length of arr after the deletion operation.

2. (a) What is the scope of identifier? Describe different types of scopes with illustrative

examples. (9)

Contd P/2
=2=

CSE l09/EEE
Contd ... Q. NO.2
(b) Write down the output generated by the following code, if you put the last three digits

of your roll number as input. (13)

t:find~lde <11ld/j.h>
#include <stdio.h>
int main() {
int a = 23%15/2/2*2;
printf("%d\n". a);
int b = 15 » 4 / 2;
printf("%d\n". b);
int roll;
scanf("%d". &roll);
int c = 2. d = 2;
int e = roll % 4 > 1 ? c-- : d++;
printf("%d %d %d\n". c. d. e);
float f = 1/2;
printf("%f\n". f);
int g = 7.9/2;
printf("%d\n". g);
int h = 4.5/3+2+0.5;
printf("%d\n". h);
float i = -1.5;
printf("%f %f %f\n". round(i). ceil(i). floor(i»;
}

(c) Suppose, for a given integer d, we want to print all d digit numbers such that each
digit can only be in the set {1,2,3}. Use recursion to do this. (13)
The function prototype should be:
void print_rec(int d, int num, int r)
where, we want to print all d digit numbers having the digits in the set {I ,2,3}, num holds
an r digit number and if r= =d, then num gets printed. To print all such 2 digit numbers,
we call the function print Jec(2, 0, 0) from main and get the following result:
11,12,13,21,22,23,31,32,33

3. (a) What is a Binary Search Tree (BST)? Describe it with an example. (9)
(b) Mr. A wants to calculate the maximum difference between consecutive elements of an
array. For example: Consider the array [1,3,2,1]. Here I and 3 are consecutive and the
difference between them is 2. Similarly, 3 and 2 have difference of 1, and 2 and 1 have
difference of I. Hence, the maximum difference between consecutive elements of this
array is 2. If the array has less than two elements, then the default result is O. (13)
Mr. A has written the following function to do this.

Contd P/3
=3=

CSE 109/EEE
Coutd ... Q. No. 3(b)

int max_dist(int* arr, int len) {


int max_dist;
for (i = 0; i < len; i++) {
int dist = arr [i-l] - arr [i] ;
if (dist < max dist) {
max dist = dist;
}
}
return max_dist;
}
Unfortunately, this code does not work correctly. Find the mistakes and write down the
function after correcting them.
(c) Write a function that returns the highest number by absolute value in a 20 array of
integers. (13)
Some example cases are listed below:
input: [[1,2], [0, -3]]
Output: -3
Input [[0, 2], [-I, 0]
Output: 2
The prototype of this function should be:
int find_highest_abs(int** arr, int r, int c)
where,
arr is the pointer to the 20 array,
rand c are the number of rows and columns respectively.

4. (a) What is a Pointer? Show the differences between call by value and call by reference
with illustrative examples in C. (9)
(b) Write a function named calculate pade that returns the grade point for a given integer
mark using only switch-case statement. You are not allowed to use if else. Use the
following table for grade calculation. If the marks value is beyond possible range, return -I. (13)
Marks Grade Point
90-100 4.00
80-89 3.75
70-79 3.50
60-69 3.00
50-59 2.50
40-49 2.00
0-39 0.00
Contd P/4
=4=

CSE l09/EEE
Coutd ... Q. NO.4

(c) Mr. A has an array of integers. He wants to know ifhe can negate some numbers such
that the sum of the whole array is O. For example: the array [1,3,2,4] can be turned into
[1,-3,-2,4] by negating 2nd and 3rd elements so that the elements sums up to 0; but the
array [1,3,5,4] has no such transformation. Mr. A's knows that a recursive function to

solve this problem should have the following prototype. (13)


int sum_zero(int* arr, int len, int sum, int i)
where arr is the given array with length len; sum is the summation value found after
processing the first i elements of array arr.
Help Mr. A writing this function.

SECTION -B
There are FOUR questions in this section. Answer any THREE.

5. (a) The Dhaka Stock Exchange contains the stock information of 623 companies. A

sample data for 5 companies are given in Figure I: (25)


Trading code HIGH LOW Closing Trade Value Volume
Price (mn)
BEXIMCO 149.9 147.4 147.9 5,237 544.8920 3,658,844
JHRML 83.8 78.3 78.3 18,592 483.4690 5,986,437
GPHISPAT 57.8 54.6 54 4,916 420.2290 7,467,360
UNIQUEHRL 74.7 68.8 69.4 4,238 292.0110 4,069,519
NAHEEACP 71.6 67.8 71.3 2,264 221.8120 3,172,117
Figure I: Sample data for 5 companies

(i) Define a C structure named DSEInfo that contains the information of the companies of
Dhaka Stock Exchange as shown in Figure I. Write necessary code to store the
information for all 623 companies.
(ii) Write a function StockLoader that loads the information of the companies from a file
to your program.
(iii) What is the size of one instance of the DSElnfo structure? What would be the size of
DSElnfo if it was a union instead of a structure? (You can assume that the compiler won't
add additional space to the structure or union through padding or packing)
(iv) The DSEInfo structure actually holds the trading inforn1ation of the company for a
single day. We rank the companies based on how much (%) their current closing price
changed from the previous day. A sample ranking of 6 companies is shown in the figure
2. Based on the above information, write a function RankMaker to calculate the rank of
the Dhaka Stock Exchange for a single day.
Contd P/5
=5=

CSE l09/EEE
Contd ... Q. No. 5(a)

Sl. No. Company Code Change (%) Rank


1. REKITBENN +0.5 4
2. UNILEVERCL + 1.2 3
3. BEXIMCO -OJ 5
4. MARICO + 3.2 2
5. RENATA -3.5 6
6. TESLA + 903 I
Figure 2: Sample Ranking of 6 companies

(v) Suppose, you have DSElnfo of n days. Use the function RankMaker to create the
ranking for 2nd to the n'h day. Write a function 8toreToFiIe that create separate files to
store the ranking of each day. Your output must be stored in rank-order unlike figure 2.
You need to save the ranking information in the relevant file.
(b) Write a program to copy the content of one file to another file. Your program must
work for binary files. (10)

6. (a) Write a C code to reverse an array using pointer. An example is shown below: (8)
Original Array: 10,2,45, 3, 8,9, II
Reverse Array: 11,9, 8, 3, 45, 2, 10
(b) intarr[3][4][2] = {l,2,3,4,5,6, 7,8,9,10, II, 12, 13, 14, 15, 16, 17, 18,19,20,21,
22,23, 24}. The starting address of the first element of arr is 990. Represent the array
arr in 3-dimension. (2+5)
What is the value of the following
i. **(*arr) +1
ii. **arr
iii. *(*(*arr+2))
iv. **(*(arr+2))
v. *(*arr+1)+3
(c) Write C code to implement the following string functions. You can only use pointer
arithmetic to implement these functions. You are not allowed to use array indexing. (20)
(i) int mystrcmp(char 'sl, char 's2) - compares the string pointed by sl to the string
pointed by s2.
(ii) void mystrcpy(char 'sl, char 's2) - copies the string pointed by sl to the string
pointed by s2.
(iii) int mystrlen(char 's) - computes the length of the string pointed by s.
(iv) int strlstsearch( char 's I ,char c) - finds the position of the last occurrence of character
c in string s 1.
(v) char' strNcat(char 'sl, char 's2, int N) - concatenates string pointed by sl to the
string pointed by s2 in total of N times.
Contd P/6
=6=

CSE l09/EEE
7. (a) Write a C code to print the binary format of an integer using only bitwise operators. (5)
(b) Mention two applications of typedef in C with example. (3)
(c) Write down the following functions. You are not allowed to use logical operators,
arithmetic operators, relational operators, if-else, switch-case, loop, or any library
functions. You can only use bitwise operators to solve these problems. (5+5)
(i) int SwapAbit(int x, int y, int m) - Swap the mth bit ofx with that ofy, and return it.
(ii) int isNegative(int x) - Returns I if the number is negative, otherwise o.
(d) Mention three cases where the use of copy constructor will make a difference instead
of bit-by-bit copy of an object. What is the common form of a copy constructor for a
class named A? (3+2)
(e) Show multilevel and multiple inheritance with example. (6)
(f) Show two examples of ambiguity caused by function overloading. (3+3)

8. (a) Consider the following class: (25)


class Polar{
double r;
double theta;
public:
Polar () {};
}
Perform the following task:
(i) Implement the default constructor setting the value of r and theta to zero. Implement
an overloaded constructor Polar( double r, double theta) as appropriate.
(ii) Implement the copy constructor. Implement the public getter and setter functions.
(iii) Overload the = and the = = operator as standard practice.
(iv) Write a public function that takes another polar number and finds the dot product.
You can use the following definition for the dot product of two polar vectors.
(r!, 81). (r2, 82) = r! * r2 * cos(8! - 82)
(v) Overload the product (*) and division (f) operators as per standard definition.

(b) Show the difference between C and C++ in their ways of implementing the call-by-
reference parameter passing mechanism using appropriate examples. (6)

(c) What is the problem with the following code snippet? How can we solve this
problem? (4)
#include<iostream>
int main (){
int i, j;
cin » i » j i
i++i
++j;
cout « i « ., II « j « endl;
return 0;
}

,..... .(

.
,,,"

L-Iff-l/EEE Date: 29/04/2023


BANGLADESH UNIVERSITY Of ENGINEERING AND TECHNOLOGY, DHAKA
L-I/T-I B. Sc. Engineering Examinations 2021-2022

Sub: CSE 109 (Computer Programming)

Full Marks: 210 Time : 3 Hours


The figures in the margin indicate full marks.
USE SEP ARA TE SCRIPTS FOR EACH SECTION

SECTION -A
There are NINE questions in this section. Answer any SEVEN questions.

1. Write a C program that has the following features- (15)


(a) Take two numbers m, n as input from user.
(b) Dynamically allocate a 2D array of size m x n using pointer.

(c) Release the memory of that array.

2" Write down the output of the following two programs. And explain why these output

comes from these programs. (10+5=15)


[Without proper explanation you will get no mark.]
I.

---- ~---'--'~ •... _._- ~-.


#include <stdio.h>
int'mainO ,I
{
int a = 10,b = 12, c, d;

'printf("A: Xd\n", a);


1-

printf("8: %d\n", b);


printf("C: Xd\n", c);
printf("D: %d\n", d);
I
return 8;
)

ii.

#include <stdio.h> .'


i
I
int main () I
{ !

int x; I
x = 21 / 6 • 10 + 5 / 10 + 3;
printf("X: Xd", x);
return 0;
}

C,ontd ph

...
=2=

CSE l09/EEE

3. Identify the problem of the following code, give proper explanation and rewrite the

corrected version of the following code- (15)


"include <stdio.h>

int. takeArraylnput(int n)
{
int o[n];
for (tnt i g 0; i < nj i++) (
sconf("Xd", &0[1]);
}
return aj
}

int moin()
(
int OJ
printf("Enter the number: ");
scanf("Xd", &n);

tnt .a a takeArraylnput(n)j

. for (int i ~0; i < 0; 1++) .i


printf("Xd ", o[i]);
}

return 0j

4. Briefly explain the following terms. (5x3=15)


(a) Short Circuit Evaluation,
(b) Symbolic Constant
(c) Call by Reference

5. The Tower of Hanoi problem is a classic problem that consists of three pegs and a set of
disks of different sizes. At first all the disks are placed in first peg. You need to move all
of the disks from first peg to third peg using the help of second peg, while following

certain rules: (15)


(a) Only one disk can be moved at a time.
(b) A larger disk cannot be placed on top of a smaller disk.
(c) The disks can only be moved from the top of one peg to the top of another peg.
Write a C program that takes number of disks as input and print the moves such that after
performing these moves all the disks should moved from first peg to third peg.

Contd P/3
=3=
CSE l09/EEE

6. Write down the output of the following program with proper explanation. Assume that

the value ofp = 100. (3x5=15)


[Without proper explanation, you will get no mark.]
---~.~_._-----------------
#include <stdio.h>

int main()
. {
int 1, S = 5;
int 0p = (int 0) malloc(s ° sizeof(int»;

for (i = 0; i < s; i++) {


pli) = (i+l) ° 10; :J

) 1
printf("'p = %d\n", p); II value of p=100
printf("p[l) = %d\n", p[l]);
printf("'p + 3 = %d\n", .p' + 3);
printf("p[3) = %d\n", p[3);
printf("O(p + 2) = %d\n", O(p + 2»;
printf("p + 5 = %d\n", p + 2);

free(p);

return 0j
}
-----.,.....-- .•....------ -_.-
--- ---_._--~'

7. Write a C program that will cut a substring from a string. The program will take one
string (max_size = 100) as input, one index from which we need to start cutting and

length of the cut. Explain- for input "abcdefg", 2, 3 the resultant substring will be "cde".
The template code is given below. Please complete it. Your code should construct the
substring as well as store it in resul tSubStr string. [You will get no mark if you

just print the substring without constructing it inside resul tSubStr.] (15)

#include <stdio.h>

int main()
{
char inputStr[101); II input string
char resultSubStr(101); II result substring
int startldx; II start index
int length; II length of the substring

gets(inputStr);
scanf(" %d %d", &startIdx, &length);

II add your code here

printf("%s", resultSubStr);
return 0;
}
----------------------- '''-~-''' ,~

Contd P/4
=4=
,

CSE l09/EEE

8. Write a C program that tales an array as input (first take the array size) and check
whether it is sorted or not? If it is sorted in ascending order, then print "Sorted" and exit
the program, otherwise print "Not Sorted" then sort it and finally print the sorted array.

Sample input and output are given below- (15)


Input Output
5 Sorted
I 3 6 8 9
5 Not Sorted
16398 I 3 689

9. Implement the following grading system using switch statement in C programmmg


language. You can not use any if-else statement, and you do not need to validate the

input mark. (15)


Mark Range Grade
0-39 F
40-49 D
50-54 C
55-59 B-
60-64 B
65-69 B+
70-74 A-
75-79 A
80-100 A+

SECTION -B
There are FOUR questions in this section. Answer any THREE questions.

10. (a) (I) Define a structure "MyDate" in C to represent a date of a year. Use enumeration
for the month field of the date. You should use bit-field to use memory as efficiently as

possible. You can assume the maximum value of the year will be 2050. (7+8=15)
(II) Write a function which will take two "MyDate" type structures as parameters and will
return the number of days, months, and years between the two dates. You should define
another structure to return the difference in the specified format.
(b) Write a function in C to left circular shift or left rotate an integer by a specific
,
amount. The bits that get shifted out on the left get shifted back in on the right. The
function should take the integer and the amount by which the integer will be rotated as

parameters and return the resulting integer after the rotation. (13)
Contd : PIS
.'.
=5=
CSE l09/EEE
,

eooid ... Q. No. 10

(c) What will be the output of the following C program? (7)


Ifincluae <sta10.11>
typedef union{
int ij
char c;
double d;
} FOO;
int imain()
{
int i=10;
double d=S.6;
'char c='X'j
Foo f;
f.i=i;
f.d=d;
f.c=c;
printf("%ld\n",sizeof(f»;
if(f.i==i)
I printf("i is set to %d\n",i);
if(f.d==d)
I printf("d is set to %1f\n",d);
'if(f.c==c)
printf("c is set to %c",c);
return 8;
-}-~~--------------------~
II. (a) Suppose in your computer, you have the following text file "pollution. txt" in the
directory "C:\Users\Della\Documents". The contents of that file are given below. (20)
NOTHING is more important to life than breathing but increasing air pollution IS

becoming a dangerous concern over breathing freely. Toxic air is now one of the
biggest environmental threats for people who live in Dhaka city because we all already
know this city has been ranked as the most air polluted city on the earth.

Write a program in C to replace the word "important" with "crucial" of that text file. Your
program should work from any directory of that computer.
(b) Consider the following code in C++: (5+3+7=15)

. #include <iostream>
using namespace std;
class C{
~public: I

static. string x;
int y; i
static void printVars()
{
co~t«X«" "«y«endl;
}
}; i

string C:: x="something" ;


irit main() I
{ . .

.~.\.C01,02;
o1.y=S;
02::01; I

01. printVars ();


02.printVars();
02.y=10;
o2.x="something else";
01. printVars ();
02. printVars();
return.0; Contd P/6
_} I
=6=
CSE l09/EEE
Contd ... Q. No. tUb)

(I) What is wrong with the function "printVarsO"? Explain.


(II) Rewrite the function "printVarsO" so that it prints the variables x and y properly.
(III) After the fix, what will be the output of the program?

12. (a) Briefly explain the differences between the public, private and protected members of a

class. (5)
(b) Consider the following incomplete class and main function in C++: (3+5+3+9=20)

#include <iostream>
using names pace std;
class C{
private:
int *x;
public:
void Print(){
, (out « *x « endl;
}
,. C f(C o){
i
.O.x= le;
return OJ
}
};
int main()
{
C 01(5);
C 02=01;
.01.f(02).Print();
01. Print();
02. Print();
return e;
}

(I) Write a constructor for the class C that will take an integer as input and store the
value of that integer in x;
(lI) After you add the constructor mentioned in I, what will be the output of the
,
program?
(III) Add a.proper destructor for the class C to deallocate the memory ofx.
(IV) After you add the destructor in III, will the program run properly? If not, explain the
reason, and suggest a fix. Also, write the output of the program after the fix.

Contd P17
,

=7=
CSE l09/EEE
Conid ... Q. No. 12

(c) Consider the following incomplete class and main function in C++: (5+5=10)

.,_. - '~,. 0 --". •

#include <iostream> . --- -_.


using namespace std;
; class Person{
private:
int age;
public:
,Person(int age){
this->age=age;
}
void printAge(){
cout«age«endI;
}
}; .

:tnt main()
{
Person pl(lS);
Person p2=pl;
(p2++).printAge();
p2. printAge();
if(p2>pl)
cout«"p2 is oIder"«endI;
else if(pl>p2)
cout«"pl is oIder"«endI;
else
cout«"They are of equal age"«endI;
return ej
hl c

Overload the post increment (++) and the relational operator(s) of the Person class. The
post increment operator should do post increment of the age of that person and the
relational operator should compare two persons based on their ages. After you overload
the two operators, the output should be:
15
16
p2 is older '

13. (a) When and why should you write a virtual destructor? (7)
(b) What will be the output of the following code? (14)

Contd P/8
,

=8=
CSE l09/EEE
Contd ... Q. No. 13(b)

1-#incluae<ios~tFir
••e"aiiTm">--------~------------
I u?ing namespace"S:td; ,:' .
i .class A{ - '.
j public:
, A(){ coutee"Constrctor of class A"eeendl' }
V?id fl(){ coutee"A's fl"eeendl; } ,
vlrtual void f20{ coutcc"A' s f2"ccendl' }
~AO{ coutcc"Destructorof class A"ccendl; }
};
class B : public A{

,
I
public:
B(){ coutee"Constrctor of class B"eeendl' }
I virtual void flO{ coutcc"B's fl"CCendl"}
virtual void f20{ coutcc"B's f2"ccendl~ }
I ~B(){ coutee"Destructor of class B"eeendl' }
1
, ,
I }. , i
I
void fval(A a){
I
i a.flO;
a.f20;
j}
'void fref(A &a){
I
I
I
a.flO;
/ a. f20;
I}
I void fptr(A *a){
'. ,
,
ai~fl();,
I a->f20;
:!
i
I

.lInt mainO

I
I:{
A *al,*a2;
al= newA();
,I
a2= new BO; j
al->fl(); al->f2();
a2->fl(); a2->f2();
!
I
fval(*al); fref(*al); fptr(al); .
fval(*a2); fref(*a2); fptr(a2) ;
delete al; .delete a2;
r.eturn e;
i}
k •

(c) 1. Write an abstract class "Equilateral" which should have a protected member
variable "side" and a pure virtual function "getAreaO". The class should have one
and only constructor which takes a value as parameter and sets the member variable
"side" to that value. Note that, you are not allowed to write any other methods or
constructors for this class. (14)
Cootd P/9

=9=
CSE l09/EEE
Contd ... Q. No. B(e)

II. Write a class "Square" which publicly inherits the class "Equilateral". Override
necessary methods so that the following main function works properly and generate
the desired output. The formula to calculate the area of a square is (side x side).
III. Write a class "Triangle" which publicly inherits the class "Equilateral". Override
necessary methods so that the following main function works properly and generate
the desired output. The formula to calculate the area of an equilateral triangle is

(% x side x side).

#inc1ude' <iostream>
using namespace std;
II write the above-mentioned classes
int main()
{

Equilateral *el,*e2;
el= new Square(3);
e2= new Triangle(S);
cout«el->getArea()«endl;
cout«e2->getArea()«endl;
delete el;
delete e2;
return e;

Output:
9
18.75

You might also like