Programming in Modern C++ | Week 1

Session: JAN-APR 2024

Course Name: Programming in Modern C++

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 1 Answers


Quiz

Q1. Consider the following program.
What will be the output?

a) COMPUTER
b) RETUPMOC
c) UTERCOMP
d) COMPRETU

Answer: d) COMPRETU


Q2. Which of the following is NOT a container adapter?
a) stack
b) queue
c) deque
d) priority-queue

Answer: c) deque


Q3. Consider the following code segment.
Identify the appropriate option/s to fill in the blank LINE-1 such that output becomes found.

a) &data[0], &data[5], key
b) data, data+5, key
c) data, key, data+5
d) &datal0], &key, &data[5]

Answer: a) &data[0], &data[5], key
b) data, data+5, key


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 1 Answers


Q4. Consider the following code segment.
What will be the output?

a) 1 2 3 4 5
b) 1 2 4 5 3
c) 4 5 1 2 3
d) 2 3 4 5 1

Answer: b) 1 2 4 5 3


Q5. Consider the following code segment.
What will be the output?

a) 10 20 30 20 40 60
b) -1 -1 -1 10 20 30 20 40 60
c) -1 -1 -1 10 20 30 0 0 0 20 40 60
d) 10 20 30

Answer: a) 10 20 30 20 40 60


Q6. Consider the following code segment.
Fill in the blank at LINE-1 with appropriate option/s such that the output is: 40 10 10 20

a) remove (&iarr[0], &iarr[6], 50)
b) remove (&iarr([0], &iarr([5], 50)
c) remove(iarr, iarr + 6, 50)
d) remove(iarr, iarr + 6, iarr[5])

Answer: a) remove (&iarr[0], &iarr[6], 50)
c) remove(iarr, iarr + 6, 50)


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 1 Answers


Q7. Consider the following code segment.
What will be the output?

a) 3 2 1 2 3
b) 1 2 3 2 1
c) 5 4 3 4 5
d) 5 4 3 2 1

Answer: d) 5 4 3 2 1


Q8. Consider the following code segment.
Choose the appropriate option to fill in the blank at LINE-1, such that the output of the code would be: Modern C++.

a) stri += str2
b) strcat(stri, str2)
c) stri.append(str2)
d) stri.insert(str2)

Answer: a) stri += str2
c) stri.append(str2)


Q9. Consider the following code segment.
What will be the output?

a) 10 20 30 40 50
b) 10 30 40 50 20
c) 50 40 30 20 10
d) 50 40 30 10 20

Answer: b) 10 30 40 50 20


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 1 Answers


Programming

Question 1

Consider the program below.
•Fill in the blank at LINE-1 to include appropriate header file to utilize sqrt (.) function.
•Fill in the blank at LINE-2 to compute the length between two points p1 and p2 as V(p1.y – p2.y)^2 + (p1.x – p2.x)^2.
The program must satisfy the given test cases.

Solution:

#include <iostream>
#include <cmath>

using namespace std;
struct point{
    int x, y;
};

double get_len(point p1, point p2){

    return sqrt(pow(p1.y - p2.y, 2) + pow(p1.x - p2.x, 2));
}

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 1 Answers


Question 2

Consider the following program.
• Fill in the blank at LINE-1 with the appropriate header of max_str( ).
• Fill in the blank at LINE-2 with the appropriate statements such that the string array can be sorted in descending order.
The program must satisfy the sample input and output.

Solution:

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

bool max_str(const std::string &a, const std::string &b) {
    return a > b;
}

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 1 Answers


Question 3

Consider the following program.
• Fill in the blanks at LINE-1 to add each string to the stack.
• Fill in the blanks at LINE-2 to print the element at the top of the stack.
• Fill in the blanks at LINE-3 to remove the element at the top of the stack.
The program must satisfy the sample input and output.

Solution:

//Code

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 1 Answers

More Weeks of Programming in Modern C++: Click here

More Nptel Courses: Click here


Session: JULY-DEC 2023

Course Name: Programming in Modern C++

Course Link: Click Here

These are Nptel Programming in Modern C++ Assignment 1 Answers


Programming

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.

Solution:

stack<char> s; //LINE-1

    for(int i = 0; i < strlen(str); i+=2)
        s.push(str[i]); //LINE-2

    int len = s.size();

    for(int i = 0; i < len; i++) {

    	ch = s.top();  //LINE-3

These are Nptel Programming in Modern C++ Assignment 1 Answers


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.

Solution:

#include <iostream>
#include <cmath>

using namespace std;
struct point{
    int x, y;
};

double get_len(point p1, point p2){
    return sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
}

These are Nptel Programming in Modern C++ Assignment 1 Answers


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.

Solution:

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

bool max_str(string a, string b) {
    return a > b;
}

These are Nptel Programming in Modern C++ Assignment 1 Answers

More Weeks of Programming in Modern C++: Click here

More Nptel Courses: Click here


Session: JULY-DEC 2022

Course Name: Programming in Modern C++

Course Link: Click Here

These are Nptel Programming in Modern C++ Assignment 1 Answers


Quiz


Q1) Consider the following program
What will be the output/error?
a) 1
b) 0
c) -1
d) Compilation Error : function is not defined

Answer: a) 1


Q2) Consider the following code segment
Fill in the blank at LINE-1 so that the program will print “not found”?
a) &data[0], &data[5], key
b) data, data+5, key
c) &data[0], &data[4], key
d) data+1, data+4, key

Answer: c, d


Q3) Consider the following code segment
What will be the output?
a) 10 20 30 40 50
b) 10 30 40 50 20
c) 50 10 30 40 20
d) 50 10 20 30 40

Answer: c) 50 10 30 40 20


These are Nptel Programming in Modern C++ Assignment 1 Answers


Q4) Consider the following code segment
What will be the output?
a) 5 10 15 20 25
b) 5 10 15 25 20
c) 20 10 15 25 5
d) 25 5 10 15 20

Answer: b) 5 10 15 25 20


Q5) Consider the following code segment
What will be the output?
a) A A A B C Z Z Z
b) A A B B C Z Z Z
c) A A A B C Z Z
d) A A A B C Z Z Z Z

Answer: a) A A A B C Z Z Z


Q6) Consider the following code segment
Choose the appropriate option to fill in the blank at LINE-1, such that the output of the code would be: C++ Program.
a) si += s2
b) strcat (s1, s2)
c) s1.append(s2)
d) si.insert (s2)

Answer: a, c


These are Nptel Programming in Modern C++ Assignment 1 Answers


Q7) Consider the following code segment
Fill in the blank at LINE-1 such that the output is
5 2 3 4 5

a) data + 4 – i
b) data + 5 – i
c) data + i – 4
d) data + i – 5

Answer: a) data + 4 – i


Q8) Consider the following code segment
What will be the output?
a) 1234987654
b) 123498765
c) 1234897654
d) 123459876

Answer: a) 1234987654


Q9) Consider the following code segment
Which statement/statements is/are correct?
a) STMT-1
b) STMT-2
c) STMT-3
d) STMT-4

Answer: b) STMT-2


These are Nptel Programming in Modern C++ Assignment 1 Answers


Assignment Questions


Questions 1

Consider the program below.
• Fill in the blank at LINE-1 to declare a stack variable s.
• Fill in the blank at LINE-2 to push value into stack.
• Fill in the blank at LINE-3 with appropriate statement.
The program must satisfy the given test cases.

Solution:

//Code

These are Nptel Programming in Modern C++ Assignment 1 Answers


Question 2

Consider the following program.
• Fill in the blank at LINE-1 with the appropriate if statement,
• Fill in the blank at LINE-2 and LINE-3 with the appropriate return statements
The program must satisfy the sample input and output.

Solution:

//Code

These are Nptel Programming in Modern C++ Assignment 1 Answers


Question 3

Consider the program below.
• Fill in the blank at LINE-1 to include the appropriate header file to utilize abs(·) function.
• Fill in the blank at LINE-2 to compute the length between two points p1 and p2 as
|(p1.y − p2.y)| + |(p1.x − p2.x)|.
The program must satisfy the given test cases.

Solution:

//Code

These are Nptel Programming in Modern C++ Assignment 1 Answers

More Weeks of Programming in Modern C++: Click here

More Nptel Courses: Click here


These are Nptel Programming in Modern C++ Assignment 1 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.
Home
Account
Cart
Search