C++ Interview Questions
0of 0 votesWhat is Volatile keyword ? What happens when u use it ? (Expected Ans was compiler puts it into register and hence no optimization )
0of 0 votesDesign OO model for given movie name and zip code to find nearest theaters showing that movie.
0of 0 votesWhat is the main reason for using pass by reference over pass by pointer
0of 0 voteshow to initialize the reference member varibale in a class?
0of 0 votesWhy is this not allowed in C++ ? Explain problems with this approach
Derived *d = new Base()
0of 0 votesIf I create an object of class Foo, and send its address and sizeof to memset and set it all to 0, will it work? I answered yes, it ll set it to 0.
then he asked, if everything sets to 0, will u still be able to call functions? i answered yes, but if the function uses any data, it might not work, as data would have been corrupted.
he asked, will any function not work? yes, if there are virtual functions, call to virtual functions will fail, as pointer to vtable will be corrupted.
0of 0 votesimplement a function to_dollar(int a) that takes in an int, say '10200', and prints $10,200.
0of 0 votesClass Design:
Using char*,
design an CString implementation.
List the functions that are supported,
Implementation of "operator=".
0of 0 votesStatic members,
What are they?
Their reason of existence?
Challenges they have? (Link initilization)......
0of 0 voteswhy copy constructor must pass by reference
0of 0 votesclass Person{ public: Person(const char* szName); const char* GetName() const; /*put a function here*/ private: char *m_szName; }; int main() { Person person("John"); std::cout << Person; return 0; }Referring to the sample code above, which one of the following member
functions do you add at the comment to support std::cout << person
statement?
A. std::string operator() { return GetName(); }
B. std::string ToString() { return GetName(); }
C. const char* Convert() const { return GetName(); };
D. char* operator char*() const { return GetName(); };
E. operator const char*() const { return GetName(); };
0of 0 votesWhy does your class have a pure virtual function?
A. To ensure that this function is overridden in derived classes
that are to be instantiated
B. To allow for templated classes to be used with friend functions
C. To maximize the memory efficiency provided that execution speed
is not at a premium
D. To maximize code reuse
E. To maximize the execution speed of the function provided that
memory is not at a premium
0of 0 votesWhats the size of a derived object ? How would you calculate it ? Does it include size of base class size?
0of 0 votesCan you do this : Derived *d = new Base()
Where base has a virtual func foo() that is also implemented in derieved.
What are the implications ? (Note: its opposite of what one does for polymorphism..)
0of 0 votesHow do you perform error handling in destructors ? (throws Exceptions?)
0of 0 votesIf a base or derieved class does not have any members that are allocated on heap, would you still use a virtual destructor ? Why ?
0of 0 votesVirtual Functions and its implementation
0of 0 votesHi all, I had MS interview today on campus. He asked me to implement a function readone().and also gave me an input file with lot of lines. This function readone() should read the file(just one line from the file) whenever it is called.for example, if readone() is called first time - it ll read first line, if readone() is called second time, it will read second line. Though the qn seems simple, I baffled there!:(
0of 0 votes1. Explain OOP,classes,inheritance and polumorphism.
2. Difference between C,C++.
0of 0 votesWhat is the difference between a C++ array and an STL array type
0of 0 voteschar a[] = " ";
Q. What will be sizeof(a)
0of 0 votesclass Foo {
public:
void virtual abc() throw (int , double, long) {
};
};
class DFoo : public Foo {
public :
void abc() throw (double , int , long ) {}
};
// Will it compile
int main(){
DFoo d;
return 0;
}
0of 0 votesGo through all the C++ Qs put on careercup , in online test most of these Qs repeat .
// Online Test
class Someclass {
public:
int x;
public :
Someclass(int xx) : x(xx) { }
Someclass(const Someclass& a) { x = a.x ; x++;}
Someclass& operator =(const Someclass& a1) { x = a1.x ; x--;}
};
int main( )
{
/*Someclass a(4);
Someclass b = a;
}
Q. What will be output
0of 0 votes//Online Test
extern void print(int *ia, int sz);
void print(int *ia, int sz);
Q. Will it compile
0of 0 votes//Online Test
struct A{
int i , j;
A(int ii , int jj) :i(ii),j(ii){}
A(const A&a){
cout << a.i << a.j;
}
void operator = (const A& a){
cout <<a.i << a.j
}
};
Q. A a(1,2);
A b(2,3);
A z = (a = b);
what will be output
0of 0 votes//Online Test
template <class T>
struct sum {
static void foo(T op1 , T op2){
cout << op1 <<op2;
}
};
Q. sum::foo(1,3); will it compile , what will it take to compile
0of 0 votes//Online Test
class Base {
public :
virtual void method () = 0;
private :
int n;
};
void Base::method() { n = 1;}
class D1 : Base {};
class D2 : public D1{
int i;
void method() {i = 2;}
};
Q.Does it compile , what will it have to add to compile
0of 0 votes//online Test
CLASS A{
PUBLIC :
INT &I;
INT J;
A(){
INT J;
I = J;
}
};
//Where to init reference , in member init list or ctor
0of 0 votes//From online Test
class String{
public:
explicit String(char ch , int n = 1){}
String(const char *p){}
private :
void operator=(const char*){}
};
Q.Will it compile
0of 0 votesWhat is the difference between initializing values using overloaded new and the constructor???
