Assignment - 1 Solution
Assignment - 1 Solution
Total Marks : 25
Question 1
Consider the following program. [MCQ, Marks 1]
#include <iostream>
#include <string>
using namespace std;
int main() {
string message = "Good Morning World";
_________________; // LINE-1
cout << message;
return 0;
}
Fill in the blank at LINE-1 such that the output is Good Morning.
a) message.resize(12)
b) message.clear()
Answer: a)
Explanation:
message.resize(12) function resizes the string to the length of 12, leaving Good Morning.
message.clear() function erases the contents of the string, leaving it empty.
message.replace(0, 12, "Good Morning") function replaces the first 12 characters of the
string with "Good Morning", but since the first 12 characters are already “Good Morning”, it
does not change the string.
strcpy(message, "Good Morning") function gives a compilation error as strcpy can only be
applied to C-strings, not C++ strings.
Hence, the correct option is a).
1
Question 2
Consider the following code segment. [MSQ, Marks 1]
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {9, 4, 2, 8, 6, 3, 1};
sort(_______________________); // LINE-1
Identify the appropriate option(s) to fill in the blank at LINE-1, such that the output is:
2 4 9 8 6 3 1
Answer: a), b)
Explanation:
As the output suggests, the array arr needs to be sorted up to the 3rd element. Hence, the
sort function will take the first address of the array as the first argument and the address
of the fourth element (i.e., one past the third element) as the second argument. Hence, the
correct options are a) and b).
2
Question 3
Consider the following code segment. [MCQ, Marks 2]
#include <iostream>
#include <algorithm>
using namespace std;
int main () {
int values[] = {60, 25, 35, 15, 45};
sort (&values[1], &values[4]);
for (int i = 0; i < 5; i++)
cout << values[i] << " ";
return 0;
}
a) 60 15 25 35 45
b) 60 25 15 35 45
c) 60 25 35 45 15
d) 60 25 35 15 45
Answer: a)
Explanation:
Since the call is sort(&values[1], &values[4]), it considers 3 elements of the array values[]
from the second element for sorting. Thus, it prints 60 15 25 35 45.
3
Question 4
Consider the following code segment. [MCQ, Marks 2]
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numbers[6];
for(int i = 0; i < 6; i++)
*(numbers + i) = (i + 1) * 10;
rotate(numbers, numbers + 5, numbers + 6);
rotate(numbers, numbers + 2, numbers + 5);
for (int i = 0; i < 6; ++i)
cout << numbers[i] << " ";
return 0;
}
a) 10 20 30 40 50 60
b) 10 20 60 30 40 50
c) 60 30 40 50 10 20
d) 20 30 40 60 10 50
Answer: d)
Explanation:
The rotate(first, middle, last) function rotates the order of the elements in the range
[first, last), such that the element pointed to by middle becomes the new first element.
After the first rotation with rotate(numbers, numbers + 5, numbers + 6), the array be-
comes {60, 10, 20, 30, 40, 50}. After the second rotation with rotate(numbers, numbers +
2, numbers + 5), the array becomes {20, 30, 40, 60, 10, 50}. Hence, the output is 20 30 40
60 10 50.
4
Question 5
Consider the following code segment. [MCQ, Marks 2]
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int size = 4, c = 100;
vector<int> vi(size, 5);
for (int i = 1; i <= 3; i++)
vi.push_back(c + i);
vi.resize(12, 99);
vi.resize(10);
for (int i = 0; i < vi.size(); i++)
cout << vi[i] << " ";
return 0;
}
Answer: a)
Explanation:
Vectors are similar to dynamic arrays having the ability to resize themselves automatically
when an element is inserted or deleted, with their storage being handled automatically by the
container. The statements and the states of the vector are as follows:
• vector<int> vi(size, 5); creates a vector with initial values [‘5’, ‘5’, ‘5’, ‘5’],
• vi.push back(c + i); adds values [‘5’, ‘5’, ‘5’, ‘5’, ‘101’, ‘102’, ‘103’] (since 100 is the
value of ‘c’ and i varies from 1 to 3),
• vi.resize(12, 99); changes the vector to [‘5’, ‘5’, ‘5’, ‘5’, ‘101’, ‘102’, ‘103’, ‘99’, ‘99’,
‘99’, ‘99’, ‘99’],
• vi.resize(10); reduces the size to [‘5’, ‘5’, ‘5’, ‘5’, ‘101’, ‘102’, ‘103’, ‘99’, ‘99’, ‘99’].
5
Question 6
Consider the following code segment. [MCQ, Marks 2]
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Programming";
str.erase(3, 5);
str.insert(3, "AB");
str.insert(5, "XYZ");
cout << str;
return 0;
}
a) ProABXYZing
b) ProABgXYZramming
c) ProABXYZmming
d) ProXYZABgramming
Answer: a)
Explanation:
The initial string is “Programming”. The erase(3, 5) function call removes 5 characters
starting from index 3, resulting in “Proing”. The insert(3, ‘‘AB") function call inserts
”AB” at index 3, resulting in “ProABing”. Finally, the insert(5, "XYZ") function call
inserts “XYZ” at index 5, resulting in “ProABXYZing”. Therefore, the correct output is
“ProABXYZing”.
6
Question 7
Consider the following code segment. [MCQ, Marks 2]
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int array[] = { 10, 20, 30, 40, 50 };
for (int i = 0; i < 1; i++) {
int j = array[i];
replace(array, array + 5, j, *(_________________)); //LINE-1
}
for (int i = 0; i < 5; ++i)
cout << array[i] << " ";
return 0;
}
a) array + 4 - i
b) array + 5 - i
c) array + i - 4
d) array + i - 5
Answer: a)
Explanation:
The statement: replace(array, array + 5, j, *(array + 4 - i)); replaces the first el-
ement (10) with the last element (50), resulting in the array 50 20 30 40 50. Hence, LINE-1
will be filled as array + 4 - i .
7
Question 8
Consider the following code segment. [MCQ, Marks 2]
#include <iostream>
#include <cstring>
#include <stack>
int main(){
char str[12] = "ABCDEFGHIJK";
stack<char> stack1, stack2;
int i;
for(i = 0; i < strlen(str)/2; i++)
stack1.push(str[i]);
for(i=i-1; i < strlen(str); i++)
stack2.push(str[i]);
while (!stack1.empty()) {
stack2.push(stack1.top()); stack1.pop();
}
while (!stack2.empty()) {
cout << stack2.top(); stack2.pop();
}
return 0;
}
a) ABCDEKJIHGFE
b) ABCDEKJIHG
c) ABCDEJIHGF
d) ABCDEFGHIJK
Answer: a)
Explanation:
The stack stack1 stores {‘A’, ‘B’, ‘C’, ‘D’, ‘E’} and the stack stack2 stores {‘E’, ‘F’, ‘G’,
‘H’, ‘I’, ‘J’, ‘K’}. Then the elements of stack1 are also pushed into stack2 in the order {‘E’,
‘D’, ‘C’, ‘B’, ‘A’}. Thus, when we finally pop and print the elements from stack2, the output
would be ABCDEKJIHGFE.
8
Question 9
Consider the following code segment. [MCQ, Marks 2]
int x = 10;
const int *a = &x;
int * const b = &x;
int const *c = &x;
int const * const d = &x;
*a = 20; //STMT-1
*b = 20; //STMT-2
*c = 20; //STMT-3
*d = 20; //STMT-4
a) STMT-1
b) STMT-2
c) STMT-3
d) STMT-4
Answer: b)
Explanation:
In statement const int *a = &x;, for a the pointee is constant, hence *a cannot be modified.
So a) is incorrect.
In statement int const *c = &x;, again for c the pointee is constant, hence *c cannot be
modified. So c) is incorrect.
In statement int const * const d = &x;, for d the pointer and pointee both are constant,
hence *d cannot be modified. So d) is incorrect.
But in statement int * const b = &x;, for b the pointer is constant, but the pointee can be
modified. Hence, *b can be modified. So b) is the correct option.
9
Programming Questions
Question 1
Consider the program below.
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int main() {
char input[20];
char character;
cin >> input;
________________; //LINE-1
for(int i = 0; i < strlen(input); i++)
_________________; //LINE-2
for(int i = 0; i < strlen(input); i++) {
character = _________; //LINE-3
cout << character;
st.pop();
}
return 0;
}
Public 1
Input: coding
Output: gnidoc
Public 2
Input: reverse
Output: esrever
Private
Input: stack
Output: kcats
10
Answer:
LINE-1: stack<char> st;
LINE-2: st.push(input[i]);
LINE-3: st.top();
Explanation:
To reverse the input string using a stack, we need to declare a stack of characters at LINE-1 as
stack<char> st;. At LINE-2, we push each character of the input string into the stack using
st.push(input[i]);. At LINE-3, we retrieve and print the top element of the stack using
st.top();. We then pop the top element of the stack until the stack is empty, effectively
reversing the input string when printed.
11
Question 2
Consider the following program.
• Fill in the blank at LINE-2 and LINE-3 with the appropriate return statements.
The program must satisfy the sample input and output. Marks: 3
#include<iostream>
using namespace std;
bool IsLonger(string str1, string str2){
if(___________) //LINE-1
_________________; //LINE-2
else
_______________; //LINE-3
}
int main(){
string str1, str2;
cin >> str1 >> str2;
cout << str1 << ", " << str2 << " : " << IsLonger(str1, str2);
return 0;
}
Public 1
Input: apple banana
Output: apple, banana : 0
Public 2
Input: notebook note
Output: notebook, note : 1
Private
Input: open close
Output: open, close : 0
12
Answer:
LINE-1: str1.length() > str2.length()
LINE-2: return true
LINE-3: return false
Explanation:
At LINE-1 the condition must be if(str1.length() > str2.length()), then at LINE-2
it will be return true;, otherwise it will be return false; at LINE-3. This ensures the
function returns true if the first string is longer than the second, and false otherwise.
13
Question 3
Consider the program below.
• Fill in the blank at LINE-1 to include the appropriate header file to utilize the abs()
function.
• Fill in the blank at LINE-2 to compute the Manhattan distance between two points pt1
and pt2 as |pt1.y − pt2.y| + |pt1.x − pt2.x|.
#include <iostream>
__________________ //LINE-1
using namespace std;
struct Point{
int x, y;
};
int main() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Point pt1, pt2;
pt1.x = x1;
pt1.y = y1;
pt2.x = x2;
pt2.y = y2;
cout << calculate_distance(pt1, pt2);
return 0;
}
Public 1
Input: 2 5 3 8
Output: 4
Public 2
Input: 10 20 40 20
Output: 30
Private
Input: 20 40 60 10
Output: 70
14
Answer:
LINE-1: #include <cmath>
LINE-2: abs(pt1.x - pt2.x) + abs(pt1.y - pt2.y)
Explanation:
The C library math.h can be included in a C++ program as #include <cmath>.
At LINE-2, the formula to compute the Manhattan distance between two points can be imple-
mented as:
abs(pt1.x - pt2.x) + abs(pt1.y - pt2.y).
15