Microsoft Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
3
of 3 vote

mutable int num;

- Vikrant October 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks, it works
one more question
The routine 'myfunc' is designed to throw an exception of type 'myExcep'. However it is being called from the 'main' function assuming that an exception of type 'derivedExcep' will be caught. Correct the code so that the exception thrown by the 'myfunc' function is caught and handled by the catch block in the 'main' function. The routine 'my_handler' should be helpful to you.

#include <iostream>
#include <exception>
class myExcep {
public:
myExcep () {}
};
void myfunc() throw (myExcep) {
throw 0;
}
class derivedExcep : public myExcep {};

//changes allowed in below code only
void my_handler() throw (&amp;derivedExcep) {
std::cout<< "hit the handler" << std::endl;
throw derivedExcep();
}

int main() {
try {
myfunc();
}
catch (derivedExcep &amp; e) {
std::cout<<"caught exception gracefully"<< std::endl;
}
return 0;
}

- anuj.iiit2007 October 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you could add std::unexpected(my_handler) in main before calling myfunc.

- Anonymous October 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please explain whats wrong with the code.. is it the increment in the get_val() function ?

- bluesky October 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

'num' cannot be modified because it is being accessed through a const object

- R@M3$H.N October 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The keyword "mutable" is used to allow a particular data member of const object to be modified. It simply allows you to modify a variable in a 'const' method.
Most popularly used for Marking a boost::mutex member as mutable allowing const functions to lock it for thread-safety reasons

- R@M3$H.N October 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//some mistake was there in above code
#include <iostream>
#include <exception>
class myExcep {
public:
myExcep () {}
};
void myfunc() throw (myExcep) {
throw 0;
}
class derivedExcep : public myExcep {};

//changes allowed in below code only
void my_handler() throw (&derivedExcep) {
std::cout<< "hit the handler" << std::endl;
throw derivedExcep();
}

int main() {
try {
myfunc();
}
catch (derivedExcep & e) {
std::cout<<"caught exception gracefully"<< std::endl;
}
return 0;
}

- anuj.iiit2007 October 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dont make 'num' const in functions it works

- coder October 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i see the function declaration in the class C but where is the function being called?

- Naveen October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c++ does not allow to change the value of a variable in a const function. So change int num; to mutable int num; it should work

- venkat October 25, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More