C++ Programming Questions and Answers – Resource Management

This section on advanced C++ interview questions focuses on “Resource Management”. One shall practice these advanced C++ questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our advanced C++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of advanced C++ interview questions on “Resource Management” along with answers, explanations and/or solutions:

1. What can go wrong in resource management on c++?
a) Leakage
b) Exhaustion
c) Dangling
d) Exception
View Answer

Answer: d
Explanation: If there is any mishap in memory or resource management means, the problems that are mentioned above can happen.

2. When do we call that resource is leaked?
a) Arise of compile time error
b) It cannot be accessed by any standard mean
c) Arise of runtime error
d) It can be accessed by any standard mean
View Answer

Answer: b
Explanation: Resource is said to be leaked when it cannot be accessed by any means of standard mean.

3. What kind of error can arise when there is a problem with memory?
a) Segmentation fault
b) Produce an error
c) Both Segmentation fault & Produce an error
d) runtime error
View Answer

Answer: a
Explanation: segmentation fault error can arise when there is a problem with memory.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <new>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         int i, n;
  7.         int * p;
  8.         i = 2;
  9.         p= new (nothrow) int[i];
  10.         if (p == 0)
  11.             cout << "Error: memory could not be allocated";
  12.         else
  13. 	{
  14.             for (n=0; n<i; n++)
  15.             {
  16.                 p[n] = 5;
  17.             }
  18.             for (n = 0; n < i; n++)
  19.                 cout << p[n];
  20.             delete[] p;
  21.          }
  22.          return 0;
  23.     }

a) 5
b) 55
c) 555
d) Error: memory could not be allocated
View Answer

Answer: b
Explanation: As we had given i value as 2, It will print the 5 for two times.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ res.cpp
$ a.out
55

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main(void)
  4.     {
  5.         const char *one = "Test";
  6.         cout << one << endl;
  7.         const char *two = one;
  8.         cout << two << endl;
  9.         return 0;
  10.     }

a) Test
b) TestTest
c) Te
d) TestTe
View Answer

Answer: b
Explanation: We are copying the values from one variable to other, So it is printing is TestTest
Output:

advertisement
$ g++ res1.cpp
$ a.out
TestTest

6. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int funcstatic(int)
  4.     {
  5.         int sum = 0;
  6.         sum = sum + 10;
  7.         return sum;
  8.     }
  9.     int main(void)
  10.     {
  11.         int r = 5, s;
  12.         s = funcstatic(r);
  13.         cout << s << endl;
  14.         return 0;
  15.     }

a) 10
b) 15
c) error
d) 20
View Answer

Answer: a
Explanation: Even Though we passed the value, we didn’t caught to manipulate it, So it is printing as 10.
Output:

$ g++ res2.cpp
$ a.out
10

7. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include<string.h>
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         try
  7.         {
  8.             char *p;
  9.             strcpy(p, "How r u");
  10.         }
  11.         catch(const exception& er)
  12.         {
  13.         }
  14.     }

a) How r u
b) segmentation fault
c) error
d) runtime error
View Answer

Answer: b
Explanation: As we are using a pointer value to copy a string, So it will be producing a runtime error.
Output:

$ g++ res3.cpp
$ a.out
segmentation fault

8. What is meant by garbage collection?
a) The form of manual memory management
b) The form of automatic memory management
c) Used to replace the variables
d) Used to delete the variables
View Answer

Answer: b
Explanation: The garbage collection attempts to reclaim memory occupied by objects that are no longer in use by the program.

9. What are the operators available in C++ for dynamic allocation and de-allocation of memories?
a) new
b) delete
c) compare
d) both new & delete
View Answer

Answer: d
Explanation: new and delete operators are mainly used to allocate and deallocate during runtime.

10. Which is used to solve the memory management problem in c++?
a) smart pointers
b) arrays
c) stack
d) queue
View Answer

Answer: a
Explanation: In C++, smart pointers are used to manage memory issues like deallocate memory after use, checking bounds, etc.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.