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
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
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
Explanation: segmentation fault error can arise when there is a problem with memory.
4. What will be the output of the following C++ code?
#include <iostream>
#include <new>
using namespace std;
int main ()
{
int i, n;
int * p;
i = 2;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
p[n] = 5;
}
for (n = 0; n < i; n++)
cout << p[n];
delete[] p;
}
return 0;
}
a) 5
b) 55
c) 555
d) Error: memory could not be allocated
View Answer
Explanation: As we had given i value as 2, It will print the 5 for two times.
Output:
$ g++ res.cpp $ a.out 55
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main(void)
{
const char *one = "Test";
cout << one << endl;
const char *two = one;
cout << two << endl;
return 0;
}
a) Test
b) TestTest
c) Te
d) TestTe
View Answer
Explanation: We are copying the values from one variable to other, So it is printing is TestTest
Output:
$ g++ res1.cpp $ a.out TestTest
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int funcstatic(int)
{
int sum = 0;
sum = sum + 10;
return sum;
}
int main(void)
{
int r = 5, s;
s = funcstatic(r);
cout << s << endl;
return 0;
}
a) 10
b) 15
c) error
d) 20
View Answer
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?
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
try
{
char *p;
strcpy(p, "How r u");
}
catch(const exception& er)
{
}
}
a) How r u
b) segmentation fault
c) error
d) runtime error
View Answer
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
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
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
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.
- Practice Programming MCQs
- Check Computer Science Books
- Apply for C++ Internship
- Practice Computer Science MCQs
- Apply for Computer Science Internship