Programming in Modern C++ | NPTEL | Week 9 Solutions

This set of MCQ(multiple choice questions) focuses on the Programming in Modern C++ Week 9 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++ Week 9 Solutions” contains 09 questions.

Now, start attempting the quiz.

Programming in Modern C++ Week 9 Solutions

Q1. Consider the following program.

#include<cstdio>
int main() {
    int i = 65;
    std::printf(______________, i, i, i, i);               //LINE-1
    return 0;
}

Identify the correct option to fill in the blank at LINE-1 such that the output becomes 65 41 101 A.

a) “%d %o %x %c”
b) “%d %x %o %c”
c) “%d %0x %o %s”
d) “%i %0x %o %s”

Answer: b) “%d %x %o %c”

Programming in Modern C++ Week 9 Solutions

Q2. Consider the code segment given below.

#include <iostream>
#include<iomanip>

int main() {
      std::cout << std::setprecision(6) << std::setfill('0');
      std::cout << std::setw(8) << (double)10/3;
      return 0;
}

What will be the output?

a) 3.333333
b) 03.33333
c) 000003.33333333
d) 03.333333

Answer: b) 03.33333

Q3. Consider fp as a file pointer to a existing file, and the file is open in readonly mode. Match the appropriate descriptions about the fseek function calls.

a) 1 – A, 2 – D, 3 – C, 4 – B
b) 1 – A, 2 – C, 3 – D, 4 – B
c) 1 – B, 2 – A, 3 – C, 4 – D
d) 1 – B, 2 – C, 3 – A, 4 – D

Answer: a) 1 – A, 2 – D, 3 – C, 4 – B

Programming in Modern C++ Week 9 Solutions

Q4. Consider the following code segment.
Choose the correct option regarding the program.

a) It prints the number of blank spaces in file myfile.txt.
b) It prints the number of words in file myfile.txt.
c) It prints the number of lines in file myfile.txt.
d) It prints the number of characters in file myfile.txt.

Answer: b) It prints the number of words in file myfile.txt.

Programming in Modern C++ Week 9 Solutions

Q5. Consider the code given below to print all the lines from in.txt.
Identify the appropriate option to fill in the blank at LINE-1 such that it checks if the file does not exist.

a) infile.is_open()
b) !infile.is_open()
c) !infile.open()
d) fopen(infile) == NULL

Answer: b) !infile.is_open()

Programming in Modern C++ Week 9 Solutions

Q6. Consider the code segment given below.
Which of the following statement can be used to fill in the blank at LINE-1 that the output becomes 10000.

a) li.begin(), li.end()
b) li.begin(), li.end(), cp
c) li.begin(), li.end(), 0.0, cp
d) li.begin(), li.end(), 0.0

Answer: c) li.begin(), li.end(), 0.0, cp

Q7. Consider the following code segment.
Identify the appropriate call to copy function to fill in the blank at LINE-1 such that it prints angle as output.

a) copy(ca, ca + len – 3, lc.begin())
b) copy(ca + 3, ca + len, lc.begin())
c) copy(&ca[2], &ca[len – 1], lc.begin())
d) copy(&ca[3], &ca[len], lc.begin())

Answer: b), d)

Programming in Modern C++ Week 9 Solutions

Q8. Consider the following code segment which computes inner product of the elements of list and vector.
Identify the appropriate call/calls to inner_product function to fill in the blank at LINE-1 such that it prints 122 as output.

a) inner_product(li.begin(), li.end(), vi.begin(), 0, multi(), add())
b) inner_product(li.begin(), li.end(), vi.begin(), 0, add(), multi())
c) inner_product(vi.begin(), vi.end(), li.begin(), 0.0, add(), multi())
d) inner_product(vi.begin(), vi.end(), li.begin(), 0.0, multi(), add())

Answer: b), c)

Programming in Modern C++ Week 9 Solutions

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

Answer: a)

Programming in Modern C++ Week 9 Solutions

Programming in Modern C++ Week 9 Solutions

Q1. Consider the code segment given below.

#include<cstdio>
using namespace std;

int main() {
  int i = 66;
  printf("%d, %x %o, %c", i, i, i, i);
  return 0;
}

Which line/s will give error?

a) 66, 66, 66, 66
b) 66, 66, 66, B
c) 66, 42, 102, B
d) 66, 066, 0x66, B

Answer: c) 66, 42, 102, B

Programming in Modern C++ Week 9 SolutionsProgramming in Modern C++ Week 9 Solutions

Q2. Consider the code segment given below.

#include<cstdio>
using namespace std;

int main() {
  FILE *ifp, *ofp;
  int c;
  if((ifp = fopen("in.txt", "r")) == NULL)
     return 1;
  if((ofp = fopen("out.txt", "w")) == NULL)
     return 2;
  while(_ _ _ _ _ _ _)  // LINE-1
     fputc(c, ofp);
  fclose(ifp);
  fclose(ofp);
  return 0;
}

Identify the appropriate option to fill in the blank at LINE-1 such that the program copies the entire content of file a.txt into file b,txt.

a) c = fgetc(ifp) != EOF
b) (c = fgetc(ifp)) != EOF
c) c = (fgetc(ifp) != NULL)
d) (c = fgetc(ifp))

Answer: b) (c = fgetc(ifp)) != EOF

Q3. Match the following
Identify the appropriate option.

a) A-IV, B-II, C-I, D-II
b) A-III, B-IV, C-I, D-II
c) A-III, B-II, C-I, D-IV
d) A-IV, B-II, C-I, D-III

Answer: b) A-III, B-IV, C-I, D-II

Programming in Modern C++ Week 9 Solutions

Q4. Consider the code segment given below.

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

int main() {
  double d = (doube)10/7;
  _ _ _ _ _ _ _ _ // LINE-1
  return 0;
}

Identify the appropriate option(s) to fill in the blank at LINE-1 such that the program prints 1.43

a) cout << setprecision(3) << d;
b) cout << setprecision(2);
cout << d;
c) cout.precision(3);
cout << d;
d) cout.precision(3) << d;

Answer: a), c)

Q5. Consider the code given below to print all the lines from in.txt.

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

int main() {
  ifstream myfile("in.txt");
  string line;
  if (_ _ _  _ _ _ ) {    // LINE-1
      cout << "Unable to open file";
  }
  else {
      while(getline(myfile, line))
          cout << line << endl;
      myfile.close();
    }
    return 0;
}

Identify the appropriate option to fill in the blank at LINE-1 such that it checks if the file does not exist.

a) myfile.is_open()
b) !myfile.is_open()
c) !myfile.open()
d) fopen(myfile) == NULL

Answer: b) !myfile.is_open()

Programming in Modern C++ Week 9 Solutions

Q6. Consider the code segment given below.

#include<iostream>
using namespace std;

template<class Itr, class T>
void max(Itr first, Itr last, T& mval) {
  int pos = 0;
  mval = *first++;
  while(first != last) {
     if(*first > mval)
        mval = *first;
     ++first;
  }
}

int main(){
  int iArr[] = {30, 20, 60, 10, 40, 50};
  double mVal = 0.0;
  _ _ _ _ _ _ _ _ _ _;   // LINE-1
  cout << mVal;
  return 0;
}

Identify the appropriate option(s) to fill in the blank at LINE-1 such that the program finds out the maximum element of the array iArr and the output is 60

a) mVal = max(iArr, iArr + sizeof(iArr) / sizeof(*iArr))
b) max(iArr, iArr + sizeof(iArr) / sizeof(*iArr), mVal)
c) max(iArr, &iArr[sizeof(iArr) / sizeof(*iArr)], mVal)
d) max(iArr, iArr + sizeof(*iArr) / sizeof(iArr), mVal)

Answer: b), c)

Q7. Consider the code segment below.

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

template<class Itr, class T>
Itr search(Itr first, Itr last, T key) {
   while(first != last && *first != key) ++first;
   return first;
}

int main(){
  int iArr[] = {30, 20, 60, 10, 40, 50};
  vector<int> iVec(iArr, iArr + sizeof(iArr) / sizeof(*iArr));
  int key = 30;
  _ _ _ _ _  _ _ _ _ _ _;   // LINE-1
  if(it == iVec.end())
    cout << "key not found";
  else
    cout << "key found";
  return 0;
}

Identify the appropriate option(s) to fill in the blank at LINE-1 such that the output is key found

a) int* it = search(iVec, iVec + sizeof(iArr) / sizeof(*iArr), key)
b) vector<int>::iterator it = search(iVec, key, iVec.end())
c) vector<int>::iterator it = search(iVec.begin(), iVec.end(), key)
d) vector<int>::iterator it = search(iVec.begin(), key, iVec.end())

Answer: c)

Programming in Modern C++ Week 9 Solutions

Q8. Consider the code segment below.

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

class Student{
  int roll;
  string name;
  public:
     Student(int _roll, string _name) : roll(_roll), name(_name) {}
     int get_roll()P return roll; }
     string get_name(){ return name; }
};

struct Comparator{
   bool operator()(Student s1, Student s2){
      return s1.get_name() > s2.get_name();
   }
};
int main() {
  Student s[] = { Student(30, "Lohit"), Student(10, "Pranit"), Student(20, "Ronit"), Student(40, "Viraj") };
  vector<Student> st(s, s + sizeof(s) / sizeof(*s));
  sort(st.begin(), st.end(), Comparator());
  for(vector<Student>::iterator it = st.begin(); it < st.end(); it++)
     cout << it->get_roll() << " : " << it->get_name() << endl;
  return 0;
}

What will be the output?

a) 10 : Pranit
    20 : Ronit
    30 : Lohit
    40 : Viraj
b) 40 : Viraj
    30 : Lohit
    20 : Ronit
    10 : Pranit
c) 30 : Lohit
    10 : Pranit
    20 : Ronit
    40 : Viraj
d) 40 : Viraj
    20 : Ronit
    10 : Pranit
    30 : Lohit

Answer: d)

Programming in Modern C++ Week 9 Solutions

Q9. Consider the code segment given below.

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

double compute(vector<int>& vec) {
  double result = accumulate(vec.begin(), vec.end(), 10.0, multiplies<int>());
  return result;
}
int main() {
  int arr[] = {4, 5, 6, 7};
  vector<int> vec(arr, arr + sizeof(arr) / sizeof(*arr));
  cout << compute(vec) << endl;
  return 0;
}

Answer: d)

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

>> Next- Programming in Modern C++ Week 10 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.

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.