Programming in Modern C++ Week 2 Assignment Answers

Programming in Modern C Nptel Week 2 Assignment Answer and solution Swayam Platform image

Are you looking for the NPTEL Programming in Modern C++ Assignment 2 Answers 2025? You’ve come to the right place! This resource provides comprehensive solutions to all the questions from the Week 2 assignment, helping you navigate through the complexities of modern C++ programming.

image for Programming in Modern C++ Week 2 Assignment Answers
Programming in Modern C++Nptel Week 2 Assignment Answers

Programming in Modern C++ Week 2 Assignment Answers (Jan-Apr 2026)


Que1.
Assume that letter_ → [A-Za-z_] and digit → [0-9]. What is the correct regular expression to define a valid identifier in C programming language?

a) letter_(letter_| digit)*
b) (letter| digit)*
c) letter_(letter| digit)*
d) letter(letter | digit)*


View Answer


Que2.
Consider the alphabet Σ = {a, b}. Which of the following is a correct regular expression that represents even number of a?

a) (babab*)* + (bb*)
b) (aba) + (bb*)
c) (babab*)*
d) (ababa*)* + (bb*)

View Answer


Que3.
What is the regular expression over Σ = {a, b} for the language that includes the strings containing ab as substring?

a) (a+b)ab(a+b)
b) (a+b)ab(a+b)
c) (ab)ab(ab)
d) ab(a+b)*

View Answer


Que4.
Consider the following Deterministic Finite Automata (DFA). It represents:

a) A DFA that accepts all the strings that end with ba
b) A DFA that accepts all the strings that end with a
c) A DFA that accepts all the strings that end with b
d) An NFA that accepts all the strings that end with ab

View Answer


Que5.
Which of the following statements about ε-moves (epsilon transitions) in finite automata is correct?

a) An ε-move consumes one input symbol
b) ε-moves are allowed only in DFA
c) ε-moves allow the automaton to change states without consuming any input symbol
d) An automaton with ε-moves always accepts all strings

View Answer


Que6.
Assume that the token set is {+, −, *, ++, –, **, >, <=, >=}. How many tokens can be generated for the input
>=>++><=">>--?

a) 10
b) 11
c) 12
d) 14

View Answer


Que7.
What is the regular expression accepted by the following DFA?

a) 0(10)*
b) (01)*0
c) Both (A) and (B)
d) Neither (A) nor (B)

View Answer


Que8.
Consider the grammar G with productions
S → aSb and S → ab, where S is the non-terminal and a and b are terminal symbols.
G represents which language?

a) L(G) = {aⁿbⁿ : n ≥ 1}
b) L(G) = {aᵐbⁿ : m, n ≥ 1}
c) L(G) = {aⁿ : n ≥ 1}
d) L(G) = {aᵐbⁿ : n ≥ 1, m < n}

View Answer


Que9.
Consider the following C program segment:

void main()
{
 int 1x, y;
 x = 10;
 y = 5;
 if x > y
   y = y + x;
}

This program suffers from:

a) One syntax error
b) One lexical error
c) Both (A) and (B)
d) Neither (A) nor (B)

View Answer


Que10.
How many states will the DFA equivalent to an NFA with n states have in the worst case?

a) n
b) 2n
c) 2ⁿ
d) n²

View Answer


(July-Dec 2025)


Question 1. Consider the following program.

#include <iostream>
using namespace std;
int main(){
    const int n = 20; // LINE-1
    n = 30;           // LINE-2
    cout << n;
    return 0;
}

What will be the output/error?
a) 20
b) 30
c) Compilation Error at LINE-1: const must be initialized
d) Compilation Error at LINE-2: assignment of read-only variable ’n’

View Answer


Question 2. Consider the following program.

#include <iostream>
using namespace std;
int main(){
    int n = 10;
    int * const p = &n;
    int m = 20;
    p = &m; // LINE-1
    cout << *p;
    return 0;
}

What will be the output/error?
a) 10
b) 20
c) Compilation Error at LINE-1: assignment of read-only variable ’p’
d) Segmentation fault

View Answer


Question 3. Consider the following code segment and find the output.

#include <iostream>
#define SQUARE(x) (x + x)
using namespace std;
int main(){
    int a = 4;
    int b = SQUARE(a + 1);
    cout << b;
    return 0;
}

What will be the output/error?
a) 10
b) 5
c) 13
d) 20

View Answer


Question 4. Consider the following code segment.

#include <iostream>
using namespace std;
int main(){
    ____ int z = x + y; // LINE-1
    cout << z;
    return 0;
}

Fill in the blank at LINE-1 such that the code compiles.
a) const
b) static
c) inline
d) register

View Answer


Question 5. Consider the following code segment.

#include <iostream>
using namespace std;
void modify(int x1, _______, _______){ //Line-1
    x2 = x1 * 2;
    *x3 = x1 + 5;
}
int main(){
    int a = 2, b, c;
    modify(a, b, &c);
    cout << a <<" "<< b <<" "<< c;
    return 0;
}

Choose the correct option for parameter list at LINE-1 such that the output is 2 4 7.
a) int &x2, int *x3
b) int x2, int *x3
c) int &x2, int &x3
d) int *x2, int &x3

View Answer


Question 6. Consider the following code segment.

#include <iostream>
using namespace std;
int max(int a, int b){
    return (a > b) ? a : b;
}
float max(float a, float b){
    return (a > b) ? a : b;
}
int main(){
    cout << max(3, 4.5); // LINE-1
    return 0;
}

What will be the output/error?
a) 4.5
b) 3
c) Compilation Error at LINE-1: call to ’max’ is ambiguous
d) 4

View Answer


Question 7. Consider the following code segment.

#include <iostream>
using namespace std;
void fun(int a, int b = 0){ cout << a + b << endl; }
void fun(double a, double b = 1.0){ cout << a * b << endl; }
int main(){
    fun(2);    // LINE-1
    fun(2.0);  // LINE-2
    return 0;
}

Which line/s will give compilation error?
a) LINE-1
b) LINE-2
c) Both LINE-1 and LINE-2
d) No error

View Answer


Question 8. Consider the following code segment.

#include <iostream>
using namespace std;
enum class Color { Red = 1, Green, Blue };
int main(){
    Color c = Color::Red;
    cout << static_cast<int>(c + 1); // LINE-1
    return 0;
}

What will be the output/error?
a) 2
b) Compilation Error at LINE-1: invalid operands to binary +
c) 1
d) 3

View Answer


Question 9. Consider the following code segment.

#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
    int *p = new int[5]; // LINE-1
    p[0] = 42;
    cout << *p;
    delete[] p;
    return 0;
}

What will be the output?
a) 0
b) 42
c) Compilation Error
d) Segmentation fault

View Answer


(Jan-Apr 2025)

Course Link: Click Here


1) Consider the following function prototypes of function add().

image 20

Which of the following sets consists of all valid prototypes of function add()?

image 21

View Answer


2) Consider the following function prototypes of function add().

image 22

Which of the above pairs of the prototypes is ambiguous and illegal as per the rule of function overloading?

image 23

View Answer


3) Consider the following code segment.

What will be the output/error?

#include <iostream>
using namespace std;

int add(int n1) { return n1; }
int add(int n1, int n2) { return n1 + n2; }
double add(double d1) { return d1; }
int add(int n1, int n2, double n3) { return n1 + n2 + n3; }

int main() {
    int r = add(10, 20);
    cout << r << endl;
    return 0;
}

A) 30
B) 130
C) 300
D) Compilation Error: call of overloaded add(int, int) is ambiguous

View Answer


4) Consider the following code segment.

What will be the output?

#include <iostream>
using namespace std;

#define DOUBLE(x) (2 * x)
inline int TRIPLE(int x) { return 3 * x; }

int main() {
    cout << DOUBLE(10 + 10) << endl;
    cout << TRIPLE(10 + 10) << endl;
    return 0;
}

A) 40 60
B) 30 60
C) 30 40
D) 20 30

View Answer


5) Consider the following code segment.

Choose the correct option to fill in the blank at LINE—I such that the output is 11 11 20 20.

#include <iostream>
using namespace std;

int& incr(int i) {
    return ++i;
}

int main() {
    int x = 10, y = 20;
    int& z = incr(x);
    cout << x << " " << z;
    incr(x);
    cout << x << " " << z;
    return 0;
}

A) int incr(int i)
B) int incr(int& i)
C) int& incr(const int& i)
D) int& incr(int& i)

View Answer


6) Consider the following code segment.

What will be the output?

using namespace std;

#define CL 10 + 1
const int C2 = 10;

int main() {
    int nl, n2;
    nl = CL * 100 * C2;
    n2 = C2 * 100 * CL;
    cout << nl << " " << n2;
    return 0;
}

A) 1110 11001
B) 1011 1011
C) 12100 12100
D) 11001 1110

View Answer


7) Consider the following code segment.

What will be the output?

#include <iostream>
using namespace std;

int main() {
    int i = 21;
    int* ptr = &i;
    cout << *ptr << " " << i;
    return 0;
}

A) 21 32
B) 32 32
C) 21 44
D) 44 44

View Answer


8) Consider the following code segment.

image 24

Identify the correct statement(s) to fill in the blank at LINE—I such that the output is X.

A) const char* cp
B) char* const cp
C) char const* cp
D) const char* const cp

View Answer


9) Consider the following statement in C.

image 25

Among the given options below, identify the equivalent statement/s (perform the same task).

A) int* new int(5);
B) int* new int[5];
C) int* new int t operator new(sizeof(int) * 5);
D) int* (int*) operator new(sizeof(int))[5];

View Answer


(July-Dec 2024)


Q1.Consider the following code segment.

What will be the output/error?
a) 5
b) 10
c) 0
d) Compilation Error at LINE-1: assignment of read only variable ‘p’

Answer: d) Compilation Error at LINE-1: assignment of read only variable ‘p’


Q2.Consider the following code segment.

What will be the output?
a) 3 4
b) 89
c) 84
d) 3 9

Answer: b) 89


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

These are Programming in Modern C++ Week 2 Assignment 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
e) LINE-5

Answer: a) LINE-1
b) LINE-2


Q4.Consider the following code segment.

What will be the output/error?
a) 10 21
b) 10 20
c) Compilation Error: attempt to increment a constant reference
d) Compilation Error: invalid initialization of non-const reference

Answer: c) Compilation Error: attempt to increment a constant reference


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

These are Programming in Modern C++ Week 2 Assignment Answers


Q5. Consider the following code segment

What will be the output?
a) 6 6 4 4
b) 6 6 8 8
c) 3 3 4 4
d) 3 3 8 8

Answer: a) 6 6 4 4


Q6. Consider the following code segment.

Fill in the blank at LINE-1 such that the output is 20.
a) (int)malloc(20sizeof(int))
b) new int
c) new int (20)
d) new int [20]

Answer: c) new int (20)


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

These are Programming in Modern C++ Week 2 Assignment Answers


Q7. Consider the following code segment.

What will be the output/error?
a) 0
b) 5
c) 2
d) Compilation Error: invalid value in enum

Answer: c) 2


Q8. Consider the following code segment.

What will be the output/error?
a) 9.0
b) -1.0
c) -1
d) Compilation Error at LINE-1: Call of overloaded ‘calculate (int, int)’ is ambiguous

Answer: d) Compilation Error at LINE-1: Call of overloaded ‘calculate (int, int)’ is ambiguous


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

These are Programming in Modern C++ Week 2 Assignment Answers


Q9. Consider the following code segment.

Fill in the blank at LINE-1 with the correct function header.
a) vector operator* (vector &v1, vector &v2)
b) vector operator(vector v1, vector v2) c) int operator (vector v1, vector v2)
d) void operator*(vector v1, vector v2)

Answer: a) vector operator* (vector &v1, vector &v2)
b) vector operator(vector v1, vector v2) c) int operator (vector v1, vector v2)


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

These are Programming in Modern C++Nptel Week 2 Assignment Answer


Programming Questions

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

Solution:

Update Soon

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

These are Programming in Modern C++ Week 2 Assignment Answers


W1_Programming-Qs.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:

Update Soon

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

These are Programming in Modern C++ Week 2 Assignment Answers


W1_Programming-Qs.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.ypt2.y+pt1.x pt2.r
The program must satisfy the given test cases.

Solution:

Update Soon

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

These are Programming in Modern C++Nptel Week 2 Assignment Answer

All Solutions of Programming in Modern C++: Click Here

For answers to additional Nptel courses, please refer to this link: NPTEL Assignment Answers


(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


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


(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


(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


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