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.
C++ interview questions on “Resource Management” along with answers, explanations and/or solutions:
Q 1. What can go wrong in resource management on c++?
A. Leakage
B. Exhaustion
C. Dangling
D. Exception
Show Answer
D. ExceptionExplanation
If there is any mishap in memory or resource management means, the problems that are mentioned above can happen.
Q 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
Show Answer
B. It cannot be accessed by any standard meanExplanation
Resource is said to be leaked when it cannot be accessed by any means of standard mean.
Q 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
Show Answer
A. Segmentation faultExplanation
segmentation fault error can arise when there is a problem with memory.Q 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
Show Answer
B. 55Explanation
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
Q 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
Show Answer
B. TestTestExplanation
We are copying the values from one variable to other, So it is printing is TestTest Output: $ g++ res1.cpp $ a.out TestTest
Q 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. 20
D. error
Show Answer
A. 10Explanation
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
Q 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
Show Answer
B. segmentation faultExplanation
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
Q 8. What is meant by garbage collection?
A. Used to replace the variables
B. Used to delete the variables
C. The form of manual memory management
D. The form of automatic memory management
Show Answer
D. The form of automatic memory managementExplanation
The garbage collection attempts to reclaim memory occupied by objects that are no longer in use by the program.
Q 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
Show Answer
D. both new & deleteExplanation
new and delete operators are mainly used to allocate and deallocate during runtime.
Q 10. Which is used to solve the memory management problem in c++?
A. stack
B. arrays
C. smart pointers
D. queue
Leave a Reply