0% found this document useful (0 votes)
9 views6 pages

CT1 CT2 CT3 CT4 Merged

The document contains a series of programming exercises and questions for a CSE 107 course, focusing on C++ and Java programming concepts. It includes tasks such as correcting code, writing functions, and explaining concepts like function overloading and the use of STL algorithms. Additionally, it addresses issues in Java code and requires students to demonstrate knowledge of interfaces and abstract classes.

Uploaded by

Tahmid Khan
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)
9 views6 pages

CT1 CT2 CT3 CT4 Merged

The document contains a series of programming exercises and questions for a CSE 107 course, focusing on C++ and Java programming concepts. It includes tasks such as correcting code, writing functions, and explaining concepts like function overloading and the use of STL algorithms. Additionally, it addresses issues in Java code and requires students to demonstrate knowledge of interfaces and abstract classes.

Uploaded by

Tahmid Khan
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/ 6

CSE 107 Jan 2024 CT1

Time: 25 minutes
September 18, 2024

1. A Programmer wanted to make show function friend of Box function and wrote the following program.
Only write the incorrect lines and their corrected form in the blank box. [6]
1 # include < iostream >
2 using namespace std ;
3 class Box
4 {
5 int capacity ;
6 public :
7 Box ( int cap ) { capacity = cap ;}
8 int getcapacity () { return capacity ;}
9 friend void show () ;
10 };
11 void Box :: show ( Box b )
12 {
13 cout < < " Capacity : " << getcapacity () ;
14 }
15 int main ()
16 {
17 Box b (10) ;
18 b . show () ;
19 return 0;
20 }

2. Write the outputs of the following program: [3]


1 # include < iostream >
2 using namespace std ;
16 inline void modifyValue ( MyClass obj ) {
3 class MyClass
17 obj . getX () = 100;
4 {
18 }
5 private :
19 int main () {
6 int x ;
20 MyClass obj1 ;
7 public :
21 obj1 . setX (50) ;
8 MyClass () {
22 int & ref = onj1 . getX () ;
9 x = 0;
23 ref = 75;
10 cout < < " Constructor " << endl ;
24 modifyValue ( obj1 ) ;
11 }
25 cout < < obj1 . getX () << endl ;
12 ~ MyClass () { cout < < " Destructor " << endl ;}
26 return 0;
13 void setX ( int val ) { x = val ;}
27 }
14 int & getX () { return x ;}
15 };

3. Write one C++ statement to implement each of the following actions. [8]
Using new operator, allocate memory for an integer variable and assign 10 to it.
Using new operator, allocate memory for an array of ten integers.

Using new operator, allocate memory for an object of class A, where no constructor is defined inside
class A.
Using delete operator, delete the entire integer array pointed by a.
4. Why is di!erentiating in return type alone not enough for function overloading? [3]

1
CSE 107 Jan 2024 CT2
Time: 25 minutes
Novenber 6, 2024

1. Write a generic function named getMax to find the max of the two arguments such that the output
of the follwing C++ program is 7 5.8 Trump [4]
1 # include < iostream >
2 int main () {
3 std :: cout << getMax (5 , 6) << " " << getMax (2.4 , 5.8) << " " << getMax ( std ::
string ( " Kamala " ) , std :: string ( " Trump " ) ) << " \ n " ;
4 return 0;
5 }
6

2. Give two examples of ”Container associative class”. Describe the di!erences between map and
unordered map. Write the opearators that are needed to be redefined for any iterator. [5]
3. Given two lists of equal sizes, Write a program that replaces the negative elements of the firsdt list
by the corresponding elements of the second list. You need to use a custom function object named
”ReplaceNegative” and STL algorithm transform in your code. For your convenienve the main
function is given below. Expected output of the program is: 5 200 7 400 500 10. Your task is to
write the functor ”ReplaceNegative”. Also list the header files that must be included for correctly
compiling the program. [5]
1 int main () {
2 std :: list < int > list1 = {5 , -3 , 7 , -8 , -2 , 10};
3 std :: list < int > list2 = {100 , 200 , 300 , 400 , 500 , 600};
4 std :: transform ( list1 . begin () , list1 . end () , list2 . begin () , list1 . begin () ,
ReplaceNegative < int >() ) ;
5 for ( int value : list1 ) std :: cout << value << " " ;
6 return 0;
7 }
8

4. The interim government has decided to rename institutions, structures and anything that include
the name of the ex-prime minister and her family members. In that psirit, write a program that
given a list of names, erases the text ”Sheikh Hasina” from all those names and print the updated
names in sorted (ascending) order. You are not allowed to use any loop. use STL algorithms
for each and sort and appropriate functions from string class for this purpose. As a starter code,
consider the main function below. Its expected output is:

Bangladesh University of Engineering and Technology


Gangachara Bridge
National Institute of Burn and Plastic Surgery
National Youth Center

Complete the main functions and the other necessary codes to get the desired output [6]
1 int main () {
2 std :: vector < std :: string > strings = {
3 " Sheikh Hasina National Institute of Burn and Plastic Surgery " ,
4 " Sheikh Hasina National Youth Center " ,
5 " Gangachara Sheikh Hasina Beidge " ,
6 " Bangladesh University of Engineering and Technology "
7 };
8 // Add code here and other functions if necessary
9 for ( auto & str : strings ) std :: cout < < str << " \ n " ;
10 }
11

1
CSE 107 Jan 2024 CT2
Time: 25 minutes
Novenber 20, 2024

1. Identify 5 problems in the following java code. Describe the line number, problem description and
necessary solutions for each problem so that the methods in the main method runs perfectly. [14]
1 class AB {
2 AB ( int a , int b ) {
3 this . a = a ;
4 this . b = b ;
5 }
6 AB ( int x ) {
7 this . a = this . b = x ;
8 }
9 abstract void f1 () ;
10 void f2 () {
11 System . out . println ( " In f2 " ) ;
12 }
13 }
14
15 class C extends AB {
16 int c ;
17 @override
18 void f2 ( int c ) {
19 System . out . println ( " In f2 of C " ) ;
20 }
21 }
22
23 public class Main {
24 public static void main ( String [] args ) {
25 C c1 = new C (10 , 20) ;
26 C c2 = new C () ;
27 AB x = c1 ;
28 C c3 = x ;
29 }
30 }
31

2. Show 3 applications of the keyword final with short example for each application. [6]

1
CSE 107 Jan 2024 CT4
Time: 20 minutes
December 4, 2024

1. Consider the following code segment. Write the minimum code in class c2 for successful compilation
without declaring it as abstract. [10]
1 interface i1 {
2 default void f1 () {
3
4 }
5 void f2 () ;
6 static void f3 () {
7
8 }
9 }
10 interface i2 {
11 void f4 () ;
12 private void f5 () {
13
14 }
15 }
16 abstract class c1 implements i1 {
17 abstract void f6 () ;
18 final void f7 () {
19
20 }
21 }
22 class c2 extends c1 implements i2 {
23 // your code
24 }
25

2. Supposed there are 5 methods defined as void m1(), void m2(), void m3(), void m4() and
void m5(). There are also 2 interfaces defined as x1 and x2 and one abstract class named as
A. There is a class named MyClass that needs to be forced to implement all the abovementioned
methods. You must maintain the following constraints:
(a) Each interface can define at most 2 functions.
(b) The abstract class can only implement one interface and define at most one abstract function.
(c) The class MyClass can only extend a class but cannot implement any interface.
Write the complete Java code to achieve the above scenario. [6]

You might also like