Programming in Modern C++ | Week 2
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 2 Answers
Quiz
Q1. Consider the following program.
What will be the output?
a)15
b)14
c)62
d)Compilation error:call of overload “add(int,int)”is ambiguous
Answer: b)14
Q2. Consider the following program.
What will be the output (Consider right to left execution of the cout statement)?
a) 7 4
b) 8 4
c) 4 4
d) 7 6
Answer: a) 7 4
Q3. Consider the following code segment.
What will be the output?
a) 6 6 2 2
b) 6 8 4 2
с) 6 6 4 4
d) 8 6 4 4
Answer: с) 6 6 4 4
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 2 Answers
Q4. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 1, 300.
a) int x3, int x4
b) int &x3, int* x4
c) int x3, int& x4
d) int x3, int* x4
Answer: d) int x3, int* x4
Q5. Consider the following code segment.
What will be the output?
a) 0
b) 10
c) Compilation error at LINE-1: ‘x’ declared as reference but not initialized
d) Compilation error at LINE-2: assignment of read only reference x
Answer: c), d)
c) Compilation error at LINE-1: ‘x’ declared as reference but not initialized
d) Compilation error at LINE-2: assignment of read only reference x
Q6. Consider the following code segment.
Fill in the blank at LINE-1 with appropriate option/s such that the output is: 11
a) const
b) volatile
c) static
d) inline
Answer: a) const
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 2 Answers
Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print a.
a) (char*)malloc (10*sizeof (char))
b) new char(‘a’)
c) new char (97)
d) new char [97]
Answer: b), c)
b) new char(‘a’)
c) new char (97)
Q8. Consider the following code segment.
What will be the output(s) /error(s)?
a) 97
b) 120
c) Error at LINE-1: cannot convert int* to const char* in initialization
d) Error at LINE-2: assignment of read-only location *p
Answer: c), d)
c) Error at LINE-1: cannot convert int* to const char* in initialization
d) Error at LINE-2: assignment of read-only location *p
Q9. Consider the following function prototypes of overloaded function func().
Which functions will be invoked for the call func (2.1, 3.7f)?
а) 2, 3, 5
b) 2, 3
c) 2
d) 1, 2
Answer: b) 2, 3
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 2 Answers
Programming
Question 1
Consider the program below.
• Fill in the blank at LINE-1 with an appropriate statement.
The program must satisfy the given test cases.
Solution:
#include <iostream>
using namespace std;
#define THRICE(X) ((X) * 3)
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 2 Answers
Question 2
Consider the following program.
• Fill in the blank at LINE-1 with appropriate formal arguments list.
• Fill in the blank at LINE-2 with the appropriate return statement.
The program must satisfy the sample input and output.
Solution:
#include <iostream>
using namespace std;
int sqr(int num) {
return num * num;
}
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 2 Answers
Question 3
Consider the following program.
• Fill in the blanks at LINE-1 with appropriate function header
The program must satisfy the sample input and output.
Solution:
#include <iostream>
#include <string>
using namespace std;
void print(int a, int b) {
int r = b + 10 * a;
cout << r;
}
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 2 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 2 Answers
Programming
Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 with function header
such that it will satisfy the given test cases.
Solution:
#include <iostream>
using namespace std;
void print(int a, int b=0){
int r = b + a;
cout << r;
}
These are Nptel Programming in Modern C++ Assignment 2 Answers
Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
• at LINE-1 with appropriate function parameter,
• at LINE-2 with appropriate statement
such that it will satisfy the given test cases.
Solution:
#include <iostream>
using namespace std;
int Fun(int& x) {
x = x * x;
return x;
}
These are Nptel Programming in Modern C++ Assignment 2 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>
using namespace std;
struct point {
int x, y;
};
point operator+(point &pt1, point &pt2){
pt1.x += pt2.x;
pt1.y += pt2.y;
return pt1;
}
These are Nptel Programming in Modern C++ Assignment 2 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 2 Answers
Quiz
Q1. Consider the following program.
What will be the output/error(s)?
a) y
b) z
c) Compilation Error : default argument missing for “char add (char, char, char)”
d) Compilation Error : call of overload “add (char, char)” is ambiguous
Answer: c, d
Q2. Consider the following code segment.
What will be the output?
a) 12
b) 25
c) 9
d) 16
Answer: a) 12
These are Nptel Programming in Modern C++ Assignment 2 Answers
Q3. Consider the following code segment.
Which line/s will give you an error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: b, d
Q4. Consider the following code segment.
What will be the output/error?
a) 36
b) 30
c) 25
d) Compilation Error : invalid initialization of non-const reference
Answer: d) Compilation Error : invalid initialization of non-const reference
These are Nptel Programming in Modern C++ Assignment 2 Answers
Q5. Consider the following code segment.
What will be the output?
a) 6 6 2 2
b) 6 6 7 7
c) 1 1 2 2
d) 1 1 7 7
Answer: a) 6 6 2 2
Q6. Consider the following code segment.
Choose the appropriate option to fill in the blanks at LINE-1, such that the output of the code would be : 300 20000.
a) int n3, int* n4
b) int& n3, int n4
c) int n3, int* n4
d) int& n3, int& n4
Answer: b) int& n3, int n4
These are Nptel Programming in Modern C++ Assignment 2 Answers
Q7. Consider the following code segment.
What will be the output/error?
a) < garbage value >
b) 5
c) Compilation Error at LINE-1 : uninitialized const ‘ptr’
d) Compilation Error at LINE-2 : assignment of read-only variable ‘ptr’
Answer: c, d
Q8. Consider the following code segment.
What will be the output/error?
a) 5
b) 10
c) 5
10
d) Compilation error at LINE-2 : ambiguating new declaration of ‘int fun (int)’
Answer: d) Compilation error at LINE-2 : ambiguating new declaration of ‘int fun (int)’
These are Nptel Programming in Modern C++ Assignment 2 Answers
Q9. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will print 5 + i3
a) complex operator+(complex &c1, complex &c2)
b) complex operator+(const complex &c1, const complex &c2)
c) operator+(complex &c1, complex &c2)
d) complex +(complex &c1, complex &c2)
Answer: a) complex operator+(complex &c1, complex &c2)
These are Nptel Programming in Modern C++ Assignment 2 Answers
Assignment Questions
Question 1
Consider the program below.
• Fill in the blank at LINE-1 to complete the function header.
The program must satisfy the given test cases.
Solution:
#include <iostream>
#include <string>
using namespace std;
void print(string a, string b="Any")
{ // LINE-1
These are Nptel Programming in Modern C++ Assignment 2 Answers
Question 2
Consider the following program.
• Fill in the blank at LINE-1 with the appropriate parameter,
• Fill in the blank at LINE-2 with the appropriate return statement
The program must satisfy the sample input and output.
Solution:
#include <iostream>
using namespace std;
int Fun(int a){
// LINE-1
return a*a;
// LINE-2
}
These are Nptel Programming in Modern C++ Assignment 2 Answers
Question 3
Consider the program below.
• Fill in the blank at LINE-1 to complete the function header
• Fill in the blank at LINE-2 to complete the return statement
The program must satisfy the given test cases.
Solution:
point operator+( point& pt, int& t) { //LINE-1
pt.x += t;
pt.y -= t;
return pt; //LINE-2
}
These are Nptel Programming in Modern C++ Assignment 1 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/
The content uploaded on this website is for reference purposes only. Please do it yourself first. |