CSE 109 Programming Exam BUET 2021
CSE 109 Programming Exam BUET 2021
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
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.
(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
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.
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.
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; }
};
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;
}
(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
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)
#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
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
(b) For a given decimal number n, the function print_base prints n as it is represented in
(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
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
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)
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
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
(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)
(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)
(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;
}
•
,..... .(
.
,,,"
SECTION -A
There are NINE questions in this section. Answer any SEVEN questions.
2" Write down the output of the following two programs. And explain why these output
ii.
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
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
return 0j
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
Contd P/3
=3=
CSE l09/EEE
6. Write down the output of the following program with proper explanation. Assume that
int main()
. {
int 1, S = 5;
int 0p = (int 0) malloc(s ° sizeof(int»;
) 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);
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.
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
,
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
.~.\.C01,02;
o1.y=S;
02::01; I
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)
: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