0% found this document useful (0 votes)
6 views13 pages

Week 01 Assignment Solution

The document contains a programming assignment for a Modern C++ course, featuring multiple-choice questions (MCQs) and programming tasks related to string manipulation, array operations, and algorithm usage. Each question includes code snippets requiring completion or analysis, with correct answers and explanations provided. The assignment is designed to assess students' understanding of C++ concepts and their ability to apply them in practical scenarios.

Uploaded by

Harini Perumal
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)
6 views13 pages

Week 01 Assignment Solution

The document contains a programming assignment for a Modern C++ course, featuring multiple-choice questions (MCQs) and programming tasks related to string manipulation, array operations, and algorithm usage. Each question includes code snippets requiring completion or analysis, with correct answers and explanations provided. The assignment is designed to assess students' understanding of C++ concepts and their ability to apply them in practical scenarios.

Uploaded by

Harini Perumal
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

Programming in Modern C++: Assignment Week 1

Total Marks : 25

Partha Pratim Das


Department of Computer Science and Engineering
Indian Institute of Technology Kharagpur, Kharagpur – 721302
[Link]@[Link]

July 16, 2025

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

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

int main(){
string greet = "Welcome Back";
____________; // LINE-1
cout << greet;
return 0;
}

Choose the correct option to fill in the blank at LINE-1 so that the output is Welcome.

a) [Link](7)

b) [Link] to fit()

c) [Link]("Welcome", 7, 0)

d) strncpy(greet, "Welcome", 7)

Answer: a)
Explanation: resize(7) shortens the string to ”Welcome”. Other functions are not
suitable for std::string modification or give errors.

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

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

bool descending(int i, int j){


return (i > j);
}

int main(){
int arr[] = {8, 3, 5, 9, 6, 7};
sort(______________); // LINE-1
for(int i=0; i<6; i++)
cout << arr[i] << " ";
return 0;
}

Choose the correct option to fill in the blank at LINE-1 so that the output is 8 5 3 9 6
7.

a) arr, arr+3, descending

b) &arr[0], &arr[0]+4, descending

c) arr+1, arr+4, descending

d) &arr, &arr+3, descending

Answer: a)
Explanation: Sorting only a portion of the array, first 3 or positions 1–3.

2
Question 3
Consider the following code and find the correct output. [MCQ, Marks 2]

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
vector<int> nums = {10, 20, 30, 40, 50, 60, 70};
rotate([Link](), [Link]()+2, [Link]()+4);
for(auto i : nums)
cout << i << " ";
return 0;
}

a) 30 10 20 40 50 60 70

b) 30 40 10 20 50 60 70

c) 40 10 20 30 50 60 70

d) 10 30 20 40 50 60 70

Answer: b)
Explanation: rotate rearranges elements so that the 3rd becomes the first.

3
Question 4
Consider the following code and find the correct output or error. [MCQ, Marks 2]

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

int main(){
int arr[5] = {6, 2, 5, 4, 1};
stack<int> s;
for(int i=0; i<5; i++){
while(![Link]() && [Link]() >= arr[i])
[Link]();
if([Link]())
cout << -1 << " ";
else
cout << [Link]() << " ";
[Link](arr[i]);
}
return 0;
}

a) -1 6 2 2 1

b) -1 -1 6 2 2

c) -1 -1 2 2 -1

d) Compilation Error

Answer: c)
Explanation: Finds previous smaller element using monotonic stack.

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

#include<iostream>
using namespace std;

int sumRows(__________) {
// returns the sum of first row
}
int main(){
int mat[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
cout << sumRows(mat);
return 0;
}

Choose the correct option to fill in the blank at LINE-1 so that the program does not
generate any compilation error.

a) int mat[][]

b) int mat[][3]

c) int mat[3][]

d) int mat[3][3]

Answer: b), d)
Explanation: All but the first dimension must be provided for 2D arrays in function
parameters.

5
Question 6
Consider the following code and choose the correct output. [MCQ, Marks 2]

#include<iostream>
using namespace std;

int main(){
bool x = false, y = true, z = true;
cout << (x && y || z);
return 0;
}

a) 0

b) 1

c) false

d) true

Answer: b)
Explanation: false && true || true evaluates to true = 1

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

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

int main() {
string word1 = "Good ";
string word2 = "Morning";
____________ // LINE-1
cout << message;
return 0;
}

Fill in the blank at LINE-1 so that the program generates output as Good Morning.

a) string message = word1 + word2;

b) string message = strcat(word1, word2);

c) string message = [Link](word2);

d) string message = [Link](word2);

Answer: a), d)
Explanation: Use + or append() to concatenate strings.

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

#include <iostream>
#include <algorithm>

int main() {
int data[] = {5, 10, 15, 20, 25};
int key = 20;
if (__________________) // LINE-1
std::cout << "Found";
else
std::cout << "Not Found";
return 0;
}

Fill in the blank at LINE-1 so that the program generates output as Found.

a) binary search(data, data+3, key)

b) std::binary search(data, data+5, key)

c) std::binary search(data, data+3, key)

d) binary search(data, data+5, key)

Answer: b)
Explanation: Use full range (data+5) with qualified namespace.

8
Question 9
Consider the following code and choose the correct output. [MCQ, Marks 2]

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

int main() {
int data[] = {2, 3, 3, 4, 5, 5, 6};
replace(data, data+7, 3, 5);
for (int i = 0; i < 4; ++i)
cout << data[i] << " ";
return 0;
}

a) 2 4 6 5

b) 2 4 6 3

c) 2 5 5 4

d) 2 5 6 6

Answer: a)
Explanation: After replacement of 3 with 5, the effective sequence becomes 2 5 5 4.

9
Programming Questions
Question 1
Fill in the blanks at LINE-1 and LINE-2 to implement a function that compares two strings
s1 and s2. The function should return: -1 if s1 is shorter, 0 if both strings have equal length,
1 if s1 is longer. [Marks 3]
#include <iostream>
#include <string>
using namespace std;

int lenCompare(string s1, string s2){


if (__________) // LINE-1
return -1;
else if (__________) // LINE-2
return 0;
else
return 1;
}

int main(){
string s1, s2;
cin >> s1 >> s2;
int res = lenCompare(s1, s2);
if(res == 0)
cout << "Equal length";
else if(res == 1)
cout << s1 << " is longer";
else
cout << s2 << " is longer";
return 0;
}

Public 1
Input: hello worldwide
Output: worldwide is longer

Public 2
Input: sunshine darkness
Output: Equal length

Private
Input: C++ Language
Output: Language is longer

Answer:
• LINE-1: [Link]() < [Link]()
• LINE-2: [Link]() == [Link]()

10
Question 2
You are given a function to sort characters of a string in descending alphabetical order using
a custom comparator. Fill in the blanks to complete the compareChars function and make it
work with sort(). [Marks 2]

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

______ compareChars(char a, char b) { // LINE-1


return (_________); // LINE-2
}

int main(){
string str;
cin >> str;
sort([Link](), [Link](), compareChars);
cout << str;
return 0;
}

Public 1
Input: delta
Output: tleda

Public 2
Input: apple
Output: pplea

Private
Input: okay
Output: yoka

Answer:
• LINE-1: bool
OR
LINE-1: int

• LINE-2: a > b

11
Question 3
Complete the functions to: Replace all instances of value x with y in the array. Then reverse
the array. [Marks 2]

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

void replaceValue(int arr[], int n, int x, int y){


____________; // LINE-1
}

void reverseArray(int arr[], int n){


____________; // LINE-2
}

int main(){
int arr[] = {1, 4, 3, 4, 2, 4};
int x, y;
cin >> x >> y;
int n = sizeof(arr)/sizeof(arr[0]);

replaceValue(arr, n, x, y);
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;

reverseArray(arr, n);
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
}

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

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

Private
Input: 1 9
Output:
9 4 3 4 2 4
4 2 4 3 4 9

12
Answer:
• LINE-1: replace(arr, arr + n, x, y);

• LINE-2: reverse(arr, arr + n);

13

You might also like