Programming in Modern C++ | NPTEL 2023 | Week 2 Assignment Solutions

This set of MCQ(multiple choice questions) focuses on the Programming in Modern C++ NPTEL Week 2 Assignment Solutions.

Programming in C++ is so fundamental that all companies dealing with systems as well as application development (including web, IoT, embedded systems) have a need for the same. These include – Microsoft, Samsung, Xerox, Yahoo, Oracle, Google, IBM, TCS, Infosys, Amazon, Flipkart, etc. This course would help industry developers to be up-to-date with the advances in C++ so that they can remain at the state-of-the-art.

Course layout (Answers link)

Answers COMING SOON! Kindly Wait!

Week 0: Assignment answers
Week 1: Programming in C++ is Fun
Week 2: C++ as Better C.
Week 3: OOP in C++.
Week 4: OOP in C++. 
Week 5: Inheritance.
Week 6: Polymorphism. 
Week 7: Type Casting.
Week 8: Exceptions and Templates.
Week 9: Streams and STL
Week 10: Modern C++
Week 11: Lambda and Concurrency.
Week 12: Move, Rvalue and STL Containers.

NOTE: You can check your answer immediately by clicking show answer button. This set ofProgramming in Modern C++ NPTEL Week 2 Assignment Solution” contains 09 questions.

Now, start attempting the quiz.

Programming in Modern C++ NPTEL Week 2 Assignment Solutions

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)

Programming in Modern C++ NPTEL week 2 Assignment SolutionsProgramming in Modern C++ NPTEL week 2 Assignment Solutions

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

a) 12
b) 25
c) 9
d) 16

Answer: a) 12

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)

Programming in Modern C++ NPTEL week 2 Assignment Solutions

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

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

Programming in Modern C++ NPTEL week 2 Assignment Solutions

Q6. 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

Q7. 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)

Programming in Modern C++ NPTEL week 2 Assignment Solutions

Q8. 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)’

Q9. 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)

Programming in Modern C++ NPTEL week 2 Assignment Solutions

Previous – Programming in Modern C++ NPTEL Week 2 Assignment Solutions

Q1. Consider the following program.

#include<iostream>
using namespace std;

int main() {
    const int n;           // LINE-1
    n = 10;                // LINE-2
    
    cout << n;

    return 0;
}

What will be the output/error?

a) 0
b) 10
c) Compilation Error at LINE-1: uninitialized const ‘n’
d) Compilation Error at LINE-2: assignment of read only variable ‘n’

Answer: c), d)

Programming in Modern C++ NPTEL week 2 Assignment Solutions

Q2. Consider the following code segment.

#include<iostream>
using namespace std;

int main() {
   int n = 5;
   const int *p = &n;

   *p = 10;        // LINE-1
   cout << n;

   return 0;
}

What will be the output/error?

a) 5
b) 10
c) 0
d) Compilation Error at LINE-1: assignment of read only location ‘*p’

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

Q3. Consider the following code segment.

#include<iostream>
using namespace std;

#define CUBE(x) x * x * x

int main() {
    int a = 3;
    int b = CUBE(a+1);
    
    cout << b;

    return 0;
}

What will be the output/error?

a) 10
b) 64
c) 9
d) Compilation Error: incorrect parameter to macro ‘CUBE’

Answer: a) 10

Programming in Modern C++ NPTEL week 2 Assignment Solutions

Q4. Consider the following code segment.

#include<iostream>
using namespace std;

int main() {
    int a = 3, b = 5;
    _ _ _ _ int &c = ++a + ++b;    // LINE-1
    cout << c;

    return 0;
}

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

a) static
b) volatile
c) const
d) inline

Answer: c) const

Q5. Consider the following code segment.

#include<iostream>
using namespace std;

void increment(int x1, _ _ _ _, _ _ _ _) {    // LINE-1
       x2 = ++ x1;
       *x3 = x1++;
}

int main() {
    int a = 3, b, c;
    increment(a, b, &c);

    cout << a << " " << b << " " << c;

    return 0;
}

Choose the correct option for parameter list at LINE-1 such that the option is 3 4 4.

a) int &x2, int *x3
b) int *x2, int &x3
c) int *x2, int *x3
d) int &x2, int &x3

Answer: a) int &x2, int *x3

Programming in Modern C++ NPTEL Week 2 Assignment Solutions

Q6. Consider the following code segment.

#include<iostream>
using namespace std;

double fun(int a, double b) {
    double c = a + b;
    return c;
}

double fun(double a, int b) {
    double c = a - b;
    return c;
}

int main() {
    cout << fun(2,3);    // LINE-1

    return 0;
}

What will be the output/error?

a) 5.0
b) -1.0
c) 1.0
d) Compilation Error at LINE-1: Call of overloaded ‘fun(int, int)’ is ambiguous

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

Q7. Consider the following code segment.

#include<iostream>
using namespace std;

int add(int a) { return a; }
int add(int a, int b=0) { return a+b; }
int add(int a, int b, int c=0) { return a+b+c; }
double add(double a, double b, double c=0.0) { return a+b+c; }

int main() {

    add(1);              // LINE-1
    add(1, 2);           // LINE-2
    add(1, 2, 3);        // LINE-3
    add(1.0, 2.0, 3.0);  // LINE-4

    return 0;
}

Which line/s will give compilation error?

a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4

Answer: a), b)

Programming in Modern C++ NPTEL Week 2 Assignment Solutions

Q8. Consider the following code segment.

#include<iostream>
using namespace std;

enum E { Monday, Tuesday=3, Wednesday, Thursday, Friday, Saturday, Sunday };

E operator+(const E &a, const E &b) {
    unsigned int ea = a, eb = b;
    unsigned int c = (ea + eb) % 7;
    return (E)c;
}

int main() {
    E a = Wednesday, b = Friday;
    E day = a + b;

    cout << day;

    return 0;
}

What will be the output/error?

a) 3
b) 6
c) 1
d) Compilation Error: invalid value in enum

Answer: a) 3

Programming in Modern C++ NPTEL Week 2 Assignment Solutions

Q9. Consider the following code segment.

#include<iostream>
#include<stdlib.h>
using namespace std;

int main() {
    int *p = _ _ _ _ _ _ _;   // LINE-1

    cout << *p;

    delete p;

    return 0;
}

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

a) (int*)malloc(10*sizeof(int))
b) new int
c) new int(10)
d) new int[10]

Answer: c) new int(10)

Programming in Modern C++ NPTEL week 2 Assignment Solutions

<< Prev- Programming in Modern C++ Week 1 Assignment Solutions

>> Next- Programming in Modern C++ Week 3 Assignment Solutions


DISCLAIMER: Use these answers only for the reference purpose. Quizermania doesn't claim these answers to be 100% correct. So, make sure you submit your assignments on the basis of your knowledge.

NPTEL answers: Principles of Management

NPTEL answers: Problem solving through programming in C

Programming in Java NPTEL week 1 quiz answers

NPTEL – Python for Data Science assignment solutions

Nptel – Deep Learning assignment solutions

The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Don’t forget to provide the appropriate URL along with error description. So that we can easily correct it.

Thanks in advance.

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.