DHARMSINH DESAI UNIVERSITY, NADIAD
FACULTY OF TECHNOLOGY
B.TECH. SEMESTER II [CE/IT]
SUBJECT: (23ES201) NAME: PROGRAMMING FOR PROBLEM SOLVING II
Examination : Seat No :
Date : Day :
Time : to Max. Marks : 60
INSTRUCTIONS:
1. Answer each section in a separate answer book.
2. Figures to the right indicate maximum marks for that question.
3. The symbols used carry their usual meanings.
4. Assume suitable data, if required & mention them.
5. Draw neat sketches wherever necessary.
SECTION – I
Q.1 Do as directed. [10]
CO2 U (a) Explain this pointer with an example. [2]
CO2 U (b) How are overloaded functions distinguished from each other? [3]
CO5 E (c) What will be the output/error in the following code? [2]
#include <iostream> int *jp = &i;
using namespace std; int *&ipr = jp;
int main() { cout << *ipr;
int i = 7, j = 9; return 0;
int *ip = &i; }
CO5 E (d) What will be the output/error in the following code? [3]
#include <iostream> int main(){
using namespace std; cout << sum(10,20) << endl;
template<typename T1, typename cout << sum<int, int>(1);
T2> return 0;
T1 sum(T1 arg1, T2 arg2 = 5){ }
return arg1 + arg2;
}
Q.2 Attempt Any TWO from the following questions. [10]
CO5 E (a) What will be the output/error in the following code? [5]
#include <iostream> for(int i = 0; i < 6; i++) {
#include <vector> v1.push_back(6 + i);
using namespace std; cout << v1.size() << " ";
int main() { cout << v1.capacity() << endl;
vector<int> v1 = {1, 2, 3, 4, 5}; }
cout << v1.size() << " "; }
cout << v1.capacity() << endl;
CO5 E (b) What will be the output/error in the following code? [5]
#include <iostream> class C: public B
using namespace std; {
class A public:
{ void fun(){
public: cout << "C::fun() ";
void fun(){ }
cout << "A::fun() "; } };
}; int main()
class B: public A {
{ B *bp = new C;
public: bp->fun();
void fun(){ return 0;
cout << "B::fun() "; }
}
};
Page 1 of 3
CO5 E (c) Evaluate the following code and find out any error/warning, if any, correct it, [5]
and show the output:
#include <iostream> class B{
using namespace std; public:
class A { A x;
int data; };
public: int main() {
void f(int arg){ B obj;
data = arg; obj.f(20);
} cout << obj.g() << endl;
int g(){ }
return data;
}
};
Q.3 Attempt the following question. [10]
CO6 C (a) Create a class named ComplexNumber. It should have two private data
members to store the real and imaginary parts in a floating-point. It should
have a constructor that takes two floating-point numbers and initializes the
real and imaginary parts using them. It should overload the + operator to
add two ComplexNumber objects. It should overload * to multiply the
ComplexNumber object with a floating-point number. It will multiply both
real and imaginary parts by the floating-point number. e.g. (3.1 + 4.2i) * 2.0
should result in (6.2 + 8.4i). It should contain a print method that prints the
values of the complex number's real and imaginary parts separated by a
space. Also, write the main function for doing all the above-mentioned
operations.
OR
Q.3 Attempt the following question. [10]
CO6 C (a) Create a class named MyString with the following private data members:
char *str: a pointer to a character pointing to the first element of the
dynamically allocated memory for storing characters, int len: an integer
representing the length of the character array.
A default constructor, MyString(), which initializes an empty array. A
parameterized constructor, MyString(const char s[]), which initializes the
dynamic array with the provided character array s.
A Destructor for deallocating the dynamically allocated memory.
A public member function - void display(): for printing the character array.
Also, overload the + operator to concatenate two MyString objects and
overload the == operator to compare if two MyString objects are the same or
not.
Your program should also include a main() function to create several instances
of MyString objects and perform the following operations: Concatenate two
MyString objects s1 and s2 using the + operator and store the result in another
MyString object s3, Print the concatenated contents of s3, Check whether two
MyString objects s4 and s5 are equal or not using the == operator and print the
result.
SECTION – II
Q.4 Do as directed. [10]
CO3 A (a) Demonstrate the concept of References in C++ using a program. [2]
CO3 A (b) Apply the concept of a copy constructor to perform a copy operation between [3]
objects containing a dynamic array. Write a complete C++ program.
CO4 N (c) Differentiate a specific catch block and a catch-all block. [2]
CO4 N (d) Differentiate between early binding and late binding. [3]
Page 2 of 3
Q.5 Attempt Any TWO from the following questions. [10]
CO1 R (a) Describe all types of inheritance with their diagrams and specialties. [5]
CO1 R (b) Explain function templates with an example. [5]
CO1 R (c) Write the roles of all three access specifiers in inheritance with a diagram. [5]
Q.6 Attempt the following question. [10]
CO6 C (a) Create a class named Rectangle. Initialization of the Rectangle object should
be allowed only once. Define member functions that can move the whole
rectangle to the left, right, up, and down by given units.
Input Format
The first four lines contain two numbers each (separated by a space)
representing the coordinates of the four corners of the rectangle.
First line represents the top-left corner // 0 10
Second line represents the top-right corner// 20 10
Third line represents the bottom-left corner// 0 0
Fourth line represents the bottom-right corner// 20 0
Fifth line contains number n // 2
Next n lines contain commands to move the rectangle. // R 10
// U 10
If the first letter of the line is (L, R, U, or D), then the rectangle needs to move
in the respective direction (Left, Right, Up, or Down) by units specified in the
same line.
Output Format
Output should contain four lines representing the four corners of the final
rectangle
First line should represent the top-left corner // 10 20
Second line should represent the top-right corner// 30 20
Third line should represent the bottom-left corner// 10 10
Fourth line should represent the bottom-right corner// 30 10
OR
Q.6 Attempt the following question. [10]
CO6 C (a) Create a class named DynamicArray with the following private data
members: int *ptr: a pointer to an integer pointing to the first element of
dynamically allocated memory using malloc() instead of new for storing
integer numbers, int size: an integer indicating the number of currently stored
elements in the array, int capacity: an integer representing the maximum
number of elements that can be stored in the array.
A constructor for allocating the dynamic memory with the given size.
(Initially, size = capacity). If no size is specified while creating the object
instance, the array defaults to being empty (size = capacity = 0). A Destructor
for deallocating the dynamically allocated memory. A public member
function, void insert(int val): Inserts an element after the end of the current
size of the array. If the capacity is full, it automatically resizes itself using
realloc() (If capacity = 0, update it to capacity = 1, and if capacity is non-
zero, double the current capacity) to accommodate the new element. Also,
overload the subscript operator [] to access elements of the array by index.
Your program should also include a main() function to create two object
instances of DynamicArray named array1 of size 0 and array2 of size 10,
respectively, and perform the following operations: Insert the squares of
integers from 0 to 9 into array1, Insert the cubes of integers from 0 to 9 into
array2, and finally, print the elements of both array1 and array2 using a loop.
Bloom’s Taxonomy levels: R-Remembering, U- Understanding, A-Applying, N-Analyzing, E- Evaluating, C-Creating
Page 3 of 3