Sunday, August 16, 2009

c++ interview qa

1. is virtual inline function legal?
yes. but compiler most linkely omit the "inline" part since inline is a hint, not
a command. -- see also inline + static or inline + recursive func

class A {
int f() { return y;} <=== this is inline. }; class b { virtual g () { return y;} <===: virtua inline: no meaning. 2. private virtual function: legal. virtual function: interface and new inplementation class x { public: int process (&); // use virtual x1 bool isDOne(): // use virtual x2; private: virtual int x1(); virtual int x2(); }; 3. base class dtor virtual: ok. either public virtual OR private/protected and non-virtual ctor: NO: since partial obj. 4. prefer to make interface non virtual using template method. prefer to make virtual function priavate. only if derived classes need to involved the base class. imp of a virtual func, make virtual func protected. 5. A** a = new B; not legal. type checking 6. offsetof(type, mem) ((size_t)((char *) & ((type *)0)->mem -
(char *)(type *)0))

*(u32 *)((chae *)p + offset) = value; // lvalue like that
7. overload: not checked from 'return value'.
const int f() const; fine
static + inline func(); // ok but complier has deep level control
8. c++ pointer to reference is not allowed.
const and reference NOT in the initialization list. performance (temporary copy)
class array---default ctor.
assignment operator can be virtual? yes.
9. static member functiuon: must use static data.
can call non static member function if pass obj ref or pointer.
array [][]; size = sizeof(array)/(*array) or /(array[0])

. ?: :: not overload.
10. signed + unsigned ----> promoted to unsigned
11.
((int *)p)++; // ERR
p = (char *)((int *)p + 1); // OK
int **x;
int x[5][6];
int (*x)[6];
f(a[][4], ...);
f((*a)[4]);
f(*buf);
f(int **p);
5["abcdef"]; ok
a[3] = "abc"; ok
int c = --2; ok;
#define x(a, b) a##b #a
float 4.5 ==> 0.5 + 0.5 **2 +
double b; if (a == b) NO; use if (a-b < ESP)..
const volatile int *p; OK
volatile int p OK
11. signals: SIG_IGN, SIG_DFL, but sigKILL/SIGSTOP can not be ignored.
sigprocmask()
sigaction(&)

12. c++: how to create an array of objects on the stack ?


In C++, it is not possible to have an array on the stack with a size determined at runtime. Here you use std::vector to do that:

int N = 10;

std
::vector<Object> obj(N);
// non-default ctor: std::vector obj(N, Object(a1, a2));
// now they are all initialized and ready to be used

If the size is known at compile-time, you can just go ahead with a plain array:

int const N = 10;

Object obj[N]; // need default constrotor,
// non-default ctor: Object obj[N] =
// { Object(a1, a2), Object(a2, a3), ... (up to N times) };
// now they are

all initialized and ready to be used

13. static const int x = 5; // can be initialized in class

Speaking of objects with static storage, remember that you can initialize at least some types of static data members inside the class body and be rid of a separate definition of such members.

In the following class, the const static int member is declared but not initialized. The initializer appears in the definition, in a separate .cpp file:

//----first.h
class C
{
public:
static const int i;
};

//----first.cpp
#include "first.h"
const int C::i=5;

In standard C++ you can initialize static const members of integral types inside the class:

//----first.h
class C
{
public:
static const int i=5;
};

If you provide an in-class initializer, you shouldn't use a separate definition of this static member.

What happens if the header file is #included in several different translation units? Will the program have multiple copies of C::i? No, it won't. First, remember that this member is const, which means that the compiler can optimize it away, and use only the literal 5 instead. Secondly, linkers nowadays are clever enough to collapse multiple copies of the same entity (say a function template instantiation) into one copy so you can use this technique safely.

Notice however that you can use this initialization form only with integral type. Floating point data members, strings etc. cannot use this syntax



14.
Is there a way to create an Array of a class that doesn't have a default
ctor? NO

15. geekinterview
Invoke the virtual toString() function
To invoke the virtual toString() function defined in GeometricObject from a Circle object c, use :
A. ((GeometricObject*)c)->toString();
B. c.super.toString()
C. (GeometricObject*)c->toString();
D. c->GeometricObject::toString()

D.
16. what is friend function: can access private/protectd class member
17. what is virtualfunction: derived class can replace imp provided
by base class. run time binding. --oops/polymorphism
18. what is polymorphism: virtual func, overloading, inheritance
19. what is function overloading and operator overloading.
save name but different signature (not return value).
20. what is virtual base class? handle ambiguity by multiple iinheritance.
21. what is inline function? inserted code. instead of JUMP to code of function
inline--is just a request. Compiler will ignore the request
and compile the function as normal function.
inline+static: keep one copy
inline+recursive: deep level
inline + virtual: ignore inline.
22. what is pure virtual function.
Pure virtual function is the virtual functions which member functions does not have any definitions(implementation),just it equates(=) to 0. With Pure virtual function, the base class becomes "Abstract class". The abstract class does not instantiate
23. virtual ctor (x) and virtual dtor (ok)
24. what is dynamic binding?
While generating the binary executable a compiler have two options:

1. It can include the helper libraries (third party libraries for example) with the exe file in binary form which increases the size of the final exe but the exe file can run on its own without needing any more libraries to be included by the end user/client.

2. These libraries are included on run time by the end user as DLLs. Advantage is that the final size of the shipped exe file is small. On run time these DLLs are dynamically linked to the exe file

25. illegal cast
Which of the following is an illegal cast based on this code snippet?

struct B
{
operator int();
};

struct D: B
{
};
B b;
float const f = 0;
A. static_cast(&b);
B. static_cast(b);
C. static_cast(b);
D. static_cast(&f);
E. static_cast(&f);
A: d and E . Both trying to do const pointer.

26. Legal Function Naming
Which of the following function name is legal in C++?
A. $_foo()
B. !_foo()
C. 1_foo()
D. -_foo()
E. #_foo()

A is ok. $ used for c++ for name mangling

27. size of derived class.
class base
{
private :
int b;
};
class derived : public base
{
private :
int j;
};
void main()
{
cout<}
Output:
4
2
I think size of derived=sizeof(j)+sizeof(b).
Am I right or wrong?If I am wrong then please tell me how the size of derived class is calculated.If I am right then tell me
Why the sizeof derived class is 4 when it is not inheriting the private member b of class base?
You are right. It's 4 because it does inherit the member b. It just can't access it.


28. static and friend in a class.
29. method overloading (diff signature) and overiding (same signature).
derived->:base::func();

No comments:

Post a Comment