An Introduction to Programming Through C++ Week 10 Answers

Q1. To create an array of integers which can be accessed as A[0],…,A[4] residing on the heap the code to be executed is:

a. int A[5]; 
b. int *A = new int[5]; 
c. int A = new int[5];
d. int new A[5];

Answer:- b. int *A = new int[5]; 

Q2. My program contains the line:

delete x;

It does not cause a compile time error. Which of the following is a possibledeclaration for x?

  • int x; 
  • double x; 
  • double *x; 
  • int x[10];

Answer: c

Q3. What is a memory leak? Pick the nearest description from those below.

  • The term refers to a pointer which does not point to a valid memory location. 
  • The term refers to a location in heap memory which has been allocated but to which your program has no pointer. 
  • The term refers to a memory location which is faulty. 
  • The term refers to a pointer which points to memory that has been deallocated.

Answer: b

Q4. What is a dangling pointer? Pick the nearest description from those below.

  • The term refers to a pointer which does not point to a valid memory location. 
  • The term refers to a location in heap memory which has been allocated but to which your program has no pointer. 
  • The term refers to a memory location which is faulty. 
  • The term refers to a pointer which points to memory that has been deallocated.

Answer: d

Q5. When we created the String class we redefined the = operator because

Answer: c

Q6. When I define the destructor for a class, under what circumstances should it delete the heap memory that a data member points to?

Answer:- b

Q7. What is the first number printed?

See also  An Introduction to Programming Through C++ Week 7 Answers

Answer: 10

Q8. What is the second number printed?

Answer: 9

Q9. What is the third number printed?

Answer: 8

Q10. Consider the following code fragment (assume appropriate header files have been loaded and using namespace commands issued)

Answer: xchgRate[“Yen”]=0.64

Q11. What should blank2 be?

Answer: xchgRate->first

Q12. What should blank3 be?

Answer: xchgRate->second

Q13. For which currency is the exchange rate printed first by the third statement?

Answer: a

Programming Assignment Answers:-

Q1. Write a program which reads in and executes very simple programs in a very simple programming language which is as follows. The language has 4 statements, with the following formats.

Code:-

#include<iostream>
using namespace std;

int main()
{
  string s;
  map<char,int>m;
  while(getline(cin,s))
  {
    if(s[0]=='d')
    {
      m[s[7]]=s[9]-'0';
    }
    else if(s[0]=='p')
    {
      for(auto x:m)
      {
        if(x.first==s[6])
        {
          cout<<x.second<<endl;
        }
      }
    }
    else if(s[0]=='a')
    {
      auto it1 = m.find(s[4]);
      auto it2 = m.find(s[9]);
      it2->second += it1->second;
    }
    else if(s[0]=='e')
    {
      return 0;
    }
  }
  return 0;
}

Q2. You are to write a function addM to add together two matrices, which are implemented as vectors of vectors. The matrices will only contain integers. If the matrices to be added are A,B and the sum is C, then we want C[i][j]=A[i][j]+B[i][j] for all i,j.

Code:-

vector<vector<int>> addM(const vector<vector<int>>& A, const vector<vector<int>>& B)
{
  vector<vector<int>> matrix(A);
  for(size_t i=0;i<matrix.size(); i++)
  {
      for(size_t j=0;j<matrix[0].size();j++)
      {
        matrix[i][j]+=B[i][j];
      }
  }
  return matrix;
}