Week 1 Solution

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Programming in Modern C++: Assignment Week 1

Total Marks : 20

Partha Pratim Das


Department of Computer Science and Engineering
Indian Institute of Technology
Kharagpur – 721302
partha.p.das@gmail.com

June 26, 2023

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

#include <iostream>
#include<cmath>

int main(){
int n = 4;
_______________________________; //LINE-1
return 0;
}

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

a) std::cout << std::pow(n,2) << " " << std::sqrt(n)

b) cout << pow(n,2) << " " << sqrt(n)

c) std::cout << std::pow(n) << " " << std::sqrt(n)

d) cout << pow(n) << " " << sqrt(n)

Answer: a)
Explanation:
The cout function is defined under std namespace. We need to include std namespace before
using any cout statement or we need to call the cout statement as std::cout. The pow
function takes second parameter as 2 for squaring the first number. Hence, the correct option
is a).

1
Question 2
Consider the code segment given below. [MSQ, Marks 2]

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

int main(){
_________________ ; //LINE-1
arr.resize(10);
for (int i=0;i<10;i++)
arr[i] = i+1;
return 0;
}

Fill in the blank at LINE-1 such that the program will run successfully.

a) vector<int> arr

b) vector<int> arr(3)

c) vector<int> arr[3]

d) int arr[3]

Answer: a), b)
Explanation:
The resize function is not applied to C-style array. Hence, option d is incorrect.
Vector declared with size within brackets is a fixed size variable and hence cannot be resized.
So, option c) is incorrect.
Vector can be resized when it is declared as option a and b.

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

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

int main(){
string s = "programming in modern C++";
cout << s.size(); //LINE-1
cout << strlen(s); //LINE-2
cout << s.length(); //LINE-3
cout << strlen(s.c_str()); //LINE-4
return 0;
}

Which line/s will give compilation error/s?

a) LINE-1

b) LINE-2

c) LINE-3

d) LINE-4

Answer: b)
Explanation:
The strlen function takes const char * type input to compute the length of the string. But,
LINE-2 is giving string type input to the strlen function which produces compilation error.
But LINE-4 converts the string type to c str type then that is passed to the strlen function.
This will not produce any compilation error. Function size and length works on string type
and correctly produces the length of the string.
Program intentionally made as MSQ

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

#include <iostream>
#include <algorithm>
using namespace std;
int main () {
int data[] = {50, 30, 40, 10, 20};
sort (&data[2], &data[5]);
for (int i = 0; i < 5; i++)
cout << data[i] << " ";
return 0;
}

What will be the output?

a) 10 20 30 40 50

b) 10 30 40 50 20

c) 50 30 10 20 40

d) 50 10 20 30 40

Answer: c)
Explanation:
Since the call is sort(&data[2], &data[5]), it considers 3 elements of the array data[] from
the third element for sorting. Thus, it prints 50 30 10 20 40.

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

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

int main(void) {
string str1 = "Welcome ";
string str2 = "students";
_______________________; //LINE-1
cout << str1;
return 0;
}

Choose the appropriate option to fill in the blank at LINE-1, such that the output of the code
would be: Welcome students.

a) str1 += str2

b) strcat(str1, str2)

c) str1.append(str2)

d) str1.insert(str2)

Answer: a), c)
Explanation:
In C++, operator+= and append(·) concatenate the strings. Please note that strcat(·) is a
C function, and required the inclusion of cstring. The function insert(·) is used to insert a
string in another string at a given position.

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

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

int main(){
char str[10] = "COMPUTER";
stack<char> s1, s2;
int i;
for(i = 0; i < strlen(str)/3; i++)
s1.push(str[i]);
for(; i < strlen(str); i++)
s2.push(str[i]);

while (!s1.empty()) {
s2.push(s1.top()); s1.pop();
}
while (!s2.empty()) {
cout << s2.top(); s2.pop();
}
return 0;
}

What will be the output?

a) COMPUTER

b) CORETUPM

c) UTERCOMP

d) COMPRETU

Answer: b)
Explanation:
The stack s1 stores {O, C}, and the stack s2 stores {R, E, T, U, P, M}. Then the elements of
s1 are also pushed into s2 as {C, O, R, E, T, U, P, M}. Thus, when we finally pop and print
the elements from s2, the output would be CORETUPM.

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

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

void modify(int *arr){


rotate(arr, arr + 3, arr + 5);
rotate(arr, arr + 2, arr + 4);
}

int main() {
int iarr[5];
for(int i = 0; i < 5; i++)
*(iarr + i) = i * 2;

modify(iarr);
for (int i = 0; i < 5; ++i)
cout << *(iarr + i) << " ";
return 0;
}

What will be the output?

a) 0 2 4 6 8

b) 0 2 6 8 4

c) 6 8 0 2 4

d) 2 4 6 8 0

Answer: b)
Explanation: rotate(first, middle, last) rotates the order of the elements in the range
[f irst, last], in such a way that the element pointed by middle becomes the new first element.
rotate(arr, arr + 3, arr + 5) makes the order as 6 8 0 2 4.
rotate(arr, arr + 2, arr + 4) makes the order as 0 2 6 8 4.

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

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int iarr[] = {10, 20, 50, 40, 10, 50};
rotate(&iarr[0], &iarr[2], &iarr[6]);
remove(&iarr[0], &iarr[6], 10); //LINE-1
for(int i = 0; i < 4; ++i)
cout << iarr[i] << " ";
return 0;
}

What will be the output?

a) 40 10 10 20

b) 50 40 50 20

c) 50 50 40 20

d) 40 10 40 20

Answer: b)
Explanation: After execution of rotate(&iarr[0], &iarr[2], &iarr[6]), the contents of
array becomes 50 40 10 50 10 20 .
The remove function removes all instances of 10 from the array. Hence, the resultant array
will be 50 40 50 20.

8
Question 9
Consider the code segment given below. [MCQ, Marks 2]

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

bool compare(char c1, char c2){


return tolower(c1) > tolower(c2); //LINE-1
}
int main(){
char arr1[20] = "C++ Program", arr2[20] = "C Program";
cout << lexicographical_compare(arr1, arr1+strlen(arr1), arr2, arr2+5,
compare);
return 0;
}

What will be the output?

a) 1

b) 0

c) -1

d) Compilation Error: function is not defined

Answer: a)
Explanation:
As per the LINE-1, it will return 1 if the first string is lexicographically larger than the second
string which is TRUE in our case. Hence the program will print 1.

9
Programming Questions

Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 with stack declaration,
• at LINE-2 to push values into stack,
• at LINE-3 with appropriate statement
such that it will satisfy the given test cases. Marks: 3
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int main() {
char str[20];
char ch;
cin >> str;
________________; //LINE-1
for(int i = 0; i < strlen(str); i+=2)
________________; //LINE-2
int len = s.size();
for(int i = 0; i < len; i++) {
ch = ____________; //LINE-3
cout << ch;
s.pop();
}
return 0;
}

Public 1
Input: computer
Output: eumc

Public 2
Input: programming
Output: gimrop

Private
Input: discussion
Output: osusd
Answer:
LINE-1: stack<char> s
LINE-2: s.push(str[i])
LINE-3: s.top()
Explanation:
As the stack has to work with char type. Hence at LINE-1, we need to declare it as
stack<char> s;. At LINE-2, the elements can be pushed at stack as s.push(str[i]);.
We need to print the top of the stack for which LINE-3 will be filled as s.top().

10
Question 2
Consider the following program. Fill in the blanks as per the instructions given below.

• at LINE-1 with appropriate header statement,

• at LINE-2 with appropriate statement to calculate euclidean distance

such that it will satisfy the given test cases. Marks: 3

#include <iostream>
___________________ //LINE-1
using namespace std;
struct point{
int x, y;
};

double get_len(point p1, point p2){


return _________________________________________; //LINE-2
}

int main() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
point p1, p2;
p1.x = x1;
p1.y = y1;
p2.x = x2;
p2.y = y2;
cout << round(get_len(p1, p2));
return 0;
}

Public 1
Input: 4 3 6 9
Output: 6

Public 2
Input: 2 5 3 9
Output: 4

Private
Input: 1 6 4 8
Output: 4

Answer:
LINE-1: #include <cmath>
LINE-2: sqrt(pow((p1.x - p2.x),2) + pow((p1.y - p2.y),2))
Explanation:
The C library math.h can be included in C++ program as
#include <cmath>

11
At LINE-2, the formula to compute the euclidean distance between two points can be imple-
mented as:
sqrt(pow((p1.x - p2.x),2) + pow((p1.y - p2.y),2))

12
Question 3
Consider the following program. Fill in the blanks as per the instructions given below:

• at LINE-1 with appropriate function header,

• at LINE-2 with appropriate return statement

such that it will satisfy the given test cases. Marks: 3

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

___________________________ { //LINE-1
return _________; //LINE-2
}

int main() {
std::string words[3], word;
for(int i = 0; i < 3; i++){
cin >> word;
words[i] = word;
}
sort(words, words + 3, max_str);
for (int i = 0; i < 3; i++)
cout << words[i] << " ";
return 0;
}

Public 1
Input: hello hi bye
Output: hi hello bye

Public 2
Input: soumen arup himadri
Output: soumen himadri arup

Private
Input: c c++ java
Output: java c++ c

Answer:
LINE-1: bool max str (string s1, string s2)
LINE-2: (s1 > s2)
Explanation:
At LINE-1, define function header as:
bool max str (string s1, string s2)
At LINE-2, define the return statement for sorting in descending order as:
return (s1 > s2);

13

You might also like