0% found this document useful (0 votes)
2 views

Assignment - 2 Solution

The document contains an assignment for a Programming in Modern C++ course, detailing multiple-choice questions and programming tasks related to C++ concepts such as pointers, references, operator overloading, and function overloading. Each question includes code segments, possible answers, and explanations for the correct answers. The assignment is structured to assess students' understanding of C++ programming principles.

Uploaded by

akhila890234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment - 2 Solution

The document contains an assignment for a Programming in Modern C++ course, detailing multiple-choice questions and programming tasks related to C++ concepts such as pointers, references, operator overloading, and function overloading. Each question includes code segments, possible answers, and explanations for the correct answers. The assignment is structured to assess students' understanding of C++ programming principles.

Uploaded by

akhila890234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Programming in Modern C++: Assignment Week 2

Total Marks : 25

Partha Pratim Das


Department of Computer Science and Engineering
Indian Institute of Technology Kharagpur, Kharagpur – 721302
[email protected]

July 15, 2024

Question 1
Consider the following code segment. [MCQ, Marks 2]

#include <iostream>
using namespace std;

int main(){
int n = 5;
int *const p = &n;
int m = 10;
p = &m; //LINE-1
cout << *p;
return 0;
}

What will be the output/error?

a) 5

b) 10

c) 0

d) Compilation Error at LINE-1: assignment of read only variable ‘p’

Answer: d)
Explanation:
The pointer variable p is declared as a constant pointer to an integer, which means the address
stored in p cannot be changed. At LINE-1, we attempt to change the address stored in the
constant pointer p, leading to a compilation error.

1
Question 2
Consider the following code segment. [MCQ, Mark 2]

#include <iostream>
using namespace std;

void update(int &x){


x += 5;
}

int main(){
int a = 3;
int b = 4;
update(a);
update(b);
cout << a << " " << b;
return 0;
}

What will be the output?

a) 3 4

b) 8 9

c) 8 4

d) 3 9

Answer: b)
Explanation:
The function update(int &x) takes an integer reference as a parameter and increments the
value of the referenced variable by 5. When update(a) is called, the value of a becomes 8
(since 3 + 5 = 8). Similarly, when update(b)

2
Question 3
Consider the following code segment. [MSQ, Marks 2]

#include <iostream>
using namespace std;

void update(const int &x){


x = 10; // LINE-1
}

int main(){
const int a = 5;
int b = 15;
const int *p = &a;
*p = b; // LINE-2
p = &b; // LINE-3
update(a); // LINE-4
update(b); // LINE-5
return 0;
}

Which line/s will give you an error?

a) LINE-1

b) LINE-2

c) LINE-3

d) LINE-4

e) LINE-5

Answer: a), b)
Explanation:
The function parameter const int &x is a reference to a constant integer, so attempting to
modify it at LINE-1 results in a compilation error. The pointer p is a pointer to a constant
integer, so attempting to modify the value at the location it points to at LINE-2 results in a
compilation error. At LINE-3, the pointer p is reassigned to point to another address, which is
allowed. At LINE-4, the constant integer a is passed to the function, but since no modification
is attempted in the function body, it doesn’t cause an error here. At LINE-5, the non-const
integer b is passed to the function, which also doesn’t cause an error since it’s a valid operation.

3
Question 4
Consider the following code segment. [MCQ, Marks 2]

#include <iostream>
using namespace std;

void increment(const int &x) {


x++; // LINE-1
}

int main() {
const int a = 10;
int b = 20;
increment(a); // LINE-2
increment(b); // LINE-3
cout << a << " " << b;
return 0;
}

What will be the output/error?

a) 10 21

b) 10 20

c) Compilation Error: attempt to increment a constant reference

d) Compilation Error: invalid initialization of non-const reference

Answer: c)
Explanation:
The function parameter const int &x is a reference to a constant integer, so attempting to
modify it at LINE-1 results in a compilation error. Hence, the code will not compile.

4
Question 5
Consider the following code segment. [MCQ, Marks 2]

#include <iostream>
using namespace std;
int& modify(int& a) { //LINE-1
return a = a * 2;
}
int main() {
int p = 3, q = 4;
int& r = modify(p);
cout << p << " " << r << " ";
modify(p) = q;
cout << p << " " << r;
return 0;
}

What will be the output?

a) 6 6 4 4

b) 6 6 8 8

c) 3 3 4 4

d) 3 3 8 8

Answer: a)
Explanation:
The modification of the formal parameter a is reflected on the actual variable p because it
is passed as a reference. The function modifies the value of p to p * 2. So, the first cout
statement will print 6 6. The statement int& r = modify(p); modifies p and returns it by
reference. The statement modify(p) = q; then modifies the value of p and r to the value of
q. Hence, the output is 6 6 4 4.

5
Question 6
Consider the following code segment. [MCQ, Marks 2]

#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
int *ptr = ___________________; //LINE-1
cout << *ptr;
free(ptr);
return 0;
}

Fill in the blank at LINE-1 such that the output is 20.

a) (int)malloc(20sizeof(int))

b) new int

c) new int(20)

d) new int[20]

Answer: c)
Explanation:
The pointer variable ptr should be assigned with integer type memory and should be initialized
with the value 20 to get the desired output. This can be done by new int(20). Hence, the
correct option is c).

6
Question 7
Consider the following code segment. [MCQ, Marks 2]

#include <iostream>
using namespace std;

enum Days {Sunday, Monday=2, Tuesday, Wednesday, Thursday, Friday, Saturday};

Days operator+(const Days &a, const Days &b){


unsigned int ea = a, eb = b;
unsigned int c = (ea + eb) % 7;
return (Days)c;
}

int main(){
Days x = Tuesday, y = Friday;
Days result = x + y;
cout << result;
return 0;
}

What will be the output/error?

a) 0

b) 5

c) 2

d) Compilation Error: invalid value in enum

Answer: c)
Explanation:
The corresponding integer values for Tuesday and Friday are 3 and 5, respectively. Hence,
the addition operator overload will be calculated as (3 + 5) % 7 = 8 % 7 = 1, which maps
to the second element in the enum, which is 2 (Monday). Therefore, the correct option is c).

7
Question 8
Consider the following code segment. [MCQ, Marks 2]

#include <iostream>
using namespace std;

float calculate(int x, float y){


return x + y;
}
float calculate(float x, int y){
return x - y;
}

int main(){
cout << calculate(4, 5); //LINE-1
return 0;
}

What will be the output/error?

a) 9.0

b) -1.0

c) -1

d) Compilation Error at LINE-1: Call of overloaded ‘calculate(int, int)’ is ambiguous

Answer: d)
Explanation:
The call to the function calculate(.) is ambiguous because it matches both the function
prototypes calculate(int, float) and calculate(float, int). Hence, the correct option
is d). Note that the int data type is implicitly converted to the float data type by the compiler,
which leads to the ambiguity.

8
Question 9
Consider the following code segment. [MSQ, Marks 2]

#include<iostream>
using namespace std;
struct vector{
int x, y;
void show(){ cout << "(" << x << ", " << y << ")"; }
};
______________________________________{ //LINE-1
vector temp;
temp.x = v1.x * v2.x;
temp.y = v1.y * v2.y;
return temp;
}
int main(){
struct vector v1={2,3},v2={4,5};
struct vector t = v1 * v2;
t.show();
return 0;
}

Fill in the blank at LINE-1 with the correct function header.

a) vector operator*(vector &v1, vector &v2)

b) vector operator*(vector v1, vector v2)

c) int operator*(vector v1, vector v2)

d) void operator*(vector v1, vector v2)

Answer: a), b)
Explanation: We need to overload the multiplication operator for the structure vector. Both
options a and b are correct, as they take two vector objects as parameters and return a new
vector object that is the result of multiplying the two input vectors. Option a passes the
parameters by reference, while option b passes them by value. Both approaches are valid.

9
Programming Questions

Question 1
Consider the program below.

• Fill in the blank at LINE-1 and LINE-2 with appropriate statements

such that it satisfies the given test cases. Marks: 3

#include <iostream>
using namespace std;

float compute(__________ x){ //LINE-1


return (____); //LINE-2
}

int main(){
float y;
cin >> y;
cout << compute(y * 2);
return 0;
}

Public 1
Input: 3.5
Output: 6

Public 2
Input: 2.0
Output: 3

Private 3
Input: 5.5
Output: 10

Answer:
LINE-1: const float&
LINE-2: x - 1
Explanation:
Since we are passing an expression as an argument, the header for the function compute() will
be: float compute(const float& x).
Since x is a constant, it cannot be changed by decrement operator. But we need to decrement
the argument by 1 to get the result. Hence, LINE-2 will be filled as return (x - 1).

10
Question 2
Consider the following program that is intended to reverse a given string and check if it is a
palindrome.

• Fill in the blanks at LINE-1 and LINE-2 to complete the function definitions

such that it satisfies the given test cases. Marks: 3

#include <iostream>
#include <string>
using namespace std;

string reverseString(const string& str) {


______________ ;// LINE-1
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}

bool isPalindrome(const string& str) {


______________ ;// LINE-2
return str == reversedStr;
}

int main(){
string input;
cin >> input;
string reversedInput = reverseString(input);
cout << "Reversed: " << reversedInput << endl;
if (isPalindrome(input)) {
cout << "The string is a palindrome." << endl;
} else {
cout << "The string is not a palindrome." << endl;
}
return 0;
}

Public 1
Input: level
Output:
Reversed: level
The string is a palindrome.

Public 2
Input: hello
Output:
Reversed: olleh
The string is not a palindrome.

11
Private
Input: radar
Output:
Reversed: radar
The string is a palindrome.

Answer
LINE-1: string reversed = "";
LINE-2: string reversedStr = reverseString(str);

Explanation:
In LINE-1, to reverse the string correctly, we initialize an empty string reversed and append
characters from the input string in reverse order.
In LINE-2, for the palindrome check, we call the reverseString function and compare the
original string with the reversed string. If they are equal, the original string is a palindrome.

12
Question 3
Consider the following program.
• Fill in the blank at LINE-1 to complete the operator function header for the addition
operator +.

• Fill in the blank at LINE-2 with a proper statement


such that the program must satisfy the sample input and output. Marks: 3

#include <iostream>
using namespace std;

struct Vector{
int x;
int y;
};

_____________________________(const Vector& v1, const Vector& v2){ //LINE-1


Vector v;
v.x = v1.x + v2.x;
v.y = v1.y + v2.y;
return v;
}

Vector wrapper(const Vector v1, const Vector v2){


Vector v = _________; //LINE-2
return v;
}

int main(){
int a, b, c, d;
cin >> a >> b >> c >> d;
Vector v1 = {a, b}, v2 = {c, d};
Vector result = wrapper(v1, v2);
cout << result.x << " " << result.y;
return 0;
}

Public 1
Input: 1 2 3 4
Output:
4 6

Public 2
Input: 5 5 10 10
Output: 15 15

Private
Input: -1 -2 -3 -4
Output: -4 -6

13
Answer:
LINE-1: Vector operator+
LINE-2: v1 + v2
Explanation:
The operator function calculates vector addition. So, this addition operator needs to be over-
loaded for the Vector structure. Hence, LINE-1 should be filled as
Vector operator+(const Vector& v1, const Vector& v2)
To call the operator function, LINE-2 should be filled as
v = v1 + v2.

14

You might also like