0% found this document useful (0 votes)
38 views8 pages

External Solution

The document outlines an examination paper for a B.Tech course on Programming for Problem Solving II at Dharmsinh Desai University. It includes instructions, various programming questions covering topics such as pointers, function overloading, class design, operator overloading, and dynamic memory management. The exam consists of multiple sections, requiring students to write code and analyze outputs for given C++ programming scenarios.
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)
38 views8 pages

External Solution

The document outlines an examination paper for a B.Tech course on Programming for Problem Solving II at Dharmsinh Desai University. It includes instructions, various programming questions covering topics such as pointers, function overloading, class design, operator overloading, and dynamic memory management. The exam consists of multiple sections, requiring students to write code and analyze outputs for given C++ programming scenarios.
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/ 8

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; }
Output:

7
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; }
Output:

30
6

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;
Output:

55
6 10
7 10
8 10
9 10
10 10
11 20
Page 1 of 8
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() "; }
}
};
Output:

B::fun()
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;
}
};
main.cpp:18:9: error: ‘class B’ has no member named ‘f’
18 | obj.f(20);
| ^
main.cpp:19:17: error: ‘class B’ has no member named ‘g’
19 | cout << obj.g() << endl;

Corrected Code:
#include <iostream> class B: public A{
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;
}
};
Output:

20
Page 2 of 8
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.
#include <iostream> ComplexNumber operator*(float
using namespace std; scalar){
class ComplexNumber{ return ComplexNumber(real *
float real; scalar, imag * scalar);
float imag; }
public: void print(){
ComplexNumber(float r = 0.0, cout << real << " " << imag <<
float i = 0.0){ endl;
real = r; }
imag = i; };
} int main(){
ComplexNumber ComplexNumber c1(3.1, 4.2);
operator+(ComplexNumber& ComplexNumber c2(1.2, 2.3);
other){ ComplexNumber sum = c1 + c2;
return ComplexNumber(real + sum.print();
other.real, imag + other.imag); ComplexNumber product = c1 *
} 2.0;
cout << "Product with scalar: ";
product.print();
return 0;
}
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.

Page 3 of 8
#include <iostream> bool operator==(MyString&
#include <cstring> other){
using namespace std; return strcmp(str, other.str) ==
class MyString{ 0;
char* str; }
int len; void display(){
public: cout << str << endl;
MyString(){ }
len = 0; };
str = new char[1]; int main() {
str[0] = '\0'; MyString s1("Hello, ");
} MyString s2("World!");
MyString(const char s[]){ MyString s3 = s1 + s2;
len = strlen(s); s3.display();
str = new char[len + 1]; MyString s4("Test");
strcpy(str, s); MyString s5("Test");
} if (s4 == s5)
MyString(const MyString& cout << "s4 and s5 are equal."
other){ << endl;
len = other.len; else
str = new char[len + 1]; cout << "s4 and s5 are not
strcpy(str, other.str); equal." << endl;
} return 0;
~MyString(){ }
delete[] str;
}
MyString operator+(MyString&
other){
int newLen = len + other.len;
char* temp = new char[newLen
+ 1];
strcpy(temp, str);
strcat(temp, other.str);
MyString result(temp);
delete[] temp;
return 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]

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]

Page 4 of 8
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

Page 5 of 8
#include <iostream> void display(){
using namespace std; cout << x1 << " " << y1 <<
class Rectangle{ endl;
int x1, y1; cout << x2 << " " << y2 <<
int x2, y2; endl;
int x3, y3; cout << x3 << " " << y3 <<
int x4, y4; endl;
int initialized = 0; cout << x4 << " " << y4 <<
public: endl;
void initialize(int a1, int b1, int }
a2, int b2, };
int a3, int b3, int a4, int int main() {
b4){ Rectangle rect;
if (initialized == 0){ int a1, b1, a2, b2, a3, b3, a4, b4;
x1 = a1; y1 = b1; cin >> a1 >> b1;
x2 = a2; y2 = b2; cin >> a2 >> b2;
x3 = a3; y3 = b3; cin >> a3 >> b3;
x4 = a4; y4 = b4; cin >> a4 >> b4;
initialized = 1; rect.initialize(a1, b1, a2, b2, a3,
} b3, a4, b4);
} int n;
void moveLeft(int units){ cin >> n;
x1 -= units; for (int i = 0; i < n; ++i) {
x2 -= units; char direction;
x3 -= units; int units;
x4 -= units; cin >> direction >> units;
} switch(direction){
void moveRight(int units){ case 'L':
x1 += units; rect.moveLeft(units);
x2 += units; break;
x3 += units; case 'R':
x4 += units; rect.moveRight(units);
} break;
void moveUp(int units){ case 'U':
y1 += units; rect.moveUp(units);
y2 += units; break;
y3 += units; case 'D':
y4 += units; rect.moveDown(units);
} break;
void moveDown(int units){ default:
y1 -= units; break;
y2 -= units; }
y3 -= units; }
y4 -= units; rect.display();
} return 0;
}

Page 6 of 8
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.

Page 7 of 8
#include <iostream> void insert(int val) {
#include <cstdlib> if (size == capacity) {
using namespace std; if (capacity == 0)
class DynamicArray{ capacity = 1;
int* ptr; else
int size; capacity *= 2;
int capacity; int* temp = (int*)realloc(ptr,
public: capacity * sizeof(int));
DynamicArray() : ptr(nullptr), if (temp == nullptr) {
size(0), capacity(0){} cerr << "Memory
DynamicArray(int initialSize) : reallocation failed!" << endl;
size(initialSize), free(ptr);
capacity(initialSize){ exit(EXIT_FAILURE);
if (initialSize > 0){ }
ptr = (int*)malloc(capacity * ptr = temp;
sizeof(int)); }
if (ptr == nullptr){ ptr[size] = val;
cerr << "Memory size++;
allocation failed!" << endl; }
exit(EXIT_FAILURE); int operator[](int index) {
} if (index < 0 || index >= size) {
} cerr << "Index out of
else bounds!" << endl;
ptr = nullptr; exit(EXIT_FAILURE);
} }
~DynamicArray() { return ptr[index];
if (ptr != nullptr) { }
free(ptr); int getSize(){
ptr = nullptr; return size;
} }
} };
int main() {
DynamicArray array1;
DynamicArray array2(10);
for (int i = 0; i < 10; ++i)
array1.insert(i * i);
for (int i = 0; i < 10; ++i)
array2.insert(i * i * i);
for (int i = 0; i < array1.getSize();
++i)
cout << array1[i] << " ";
cout << endl;
for (int i = 0; i < array2.getSize();
++i)
cout << array2[i] << " ";
return 0;
}

Bloom’s Taxonomy levels: R-Remembering, U- Understanding, A-Applying, N-Analyzing, E- Evaluating, C-Creating

Page 8 of 8

You might also like