C Interview Questios

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

**Difference between c and cpp?

c has no object but c++ has object c topdown approach c++ bottom up approach c cannot be overloading c++ we can do c is the subset of c++ in c data hiding is not possible and we have to write complete program at a time.whereas in c++ data hiding is possible and one can divide a huge program into certain small programs later combine it into one as per one's choice.moreover we hav header file different.eg:<stdio.h> in c supports printf & scanf statements while <iostearm.h> in c++ supports cout statements..... 1.What is the output of printf(%d) Prints 0. When we use %d the compiler internally uses it to access the argument in the stack (argument stack) .Ideally compiler determines the offset of the data variable depending on the format specification string.Now when we write printf(%d,a) then compiler firast accesses the top most element in the argument stack of the printf which is %d and dependin on the format string it calculated to offset to the actual data variable in the memory which is to be printed.Now when only %d will be present in the printf then compiler will calculte the correct offset (which will be the offset to access the integer varible) but as the actual data objet is to be printed is not present at that memory location so it will print what ever will be the contents of that memory location. 2.What will happen if I say delete this The destructor will be executed, but memory will not be freed (other than the work done by destructor). For example, if we have a class Test and inside we have a method Destroy that calls delete this, the destructor for Test will execute. If we have Test *var=new Test(), the pointer var will still be valid. 3.Difference between C structure and C++ structure. C structure does not support member functions to be declared in the structure whereas C++ structure can have member functions declared in them. 4.Diffrence between a assignment operator and a copy constructor In assignment operator ur equating an existing object to an already existing object but in copy constructor ur creating a new object and then equating the already existing one to the new one example: complex c1,c2; c1=c2;//this is assignment complex c3=c2;//copy constructor 5.What is the difference between overloading and overridding? Overloading - Two functions having same name and return type, but with different type and/or number of arguments. Overriding - When a function of base class is re-defined in the derived class. 6.Explain the need for Virtual Destructor. Destructors are declared as virtual because if do not declare it as virtual the base class destructor will be

called before the derived class destructor and that will lead to memory leak because derived classs objects will not get freed.Destructors are declared virtual so as to bind objects to the methods at runtime so that appropriate destructor is called. 7.Can we have Virtual Constructors? YES. 8.What are the different types of polymorphism? Ad hoc polymorphism (overloading) Parameterized polymorphism (Templates) Pure Polymorphism (Overriding) 9.What are Virtual Functions? How to implement virtual functions in C C++ virtual function is, A member function of a class Declared with virtual keyword Usually has a different functionality in the derived class A function call is resolved at run-time C language equivalent exists to a C++ program, then yes you can most certainly do polymorphism in C. You will have some extra work to get it done. 10.What are the different types of Storage classes? Storage classes include: auto, extern, register, static. The auto keyword places the specified variable into the stack area of memory. This is usually implicit in most variable declarations, e.g. int i; The extern keyword makes the specified variable access the variable of the same name from some other file. This is very useful for sharing variables in modular programs. The register keyword suggests to the compiler to place the particular variable in the fast register memory located directly on the CPU. Most compilers these days (like gcc) are so smart that suggesting registers could actually make your program slower. The static keyword is useful for extending the lifetime of a particular variable. If you declare a static variable inside a function, the variable remains even after the function call is long gone (the variable is placed in the alterable area of memory). The static keyword is overloaded. It is also used to declare variables to be private to a certain file only when declared with global variables. static can also be used with functions, making those functions visible only to the file itself. 11.What is Namespace? Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. The format of namespaces is: namespace identifier{ entities } 12.What are the types of STL containers?. Three types of containers are there 1. Adaptive containers like queue,stack,priority_queue

2. Associative containers like set,map,bitset,multimap,multiset 3. Sequence containers like vector ,dequeue,list 13.Difference between vector and array? Vector is srinkable ie it size(dynamic) is changed durring runtime .the size of array is fix (static )cant be change in runtime. 14.How to write a program such that it will delete itself after exectution? Destructor and virtual destructor 15.What are inline functions? Inline function is a function which is expanded at the calling block. it is not treated as a seperate function by the compiler,but it is upto to the compiler to decide whether an inline function will be expanded or not depending on the size of the function 16.What is strstream ? strstream is the class that specializes iostream to use a strstreambuf for input and output with arrays of characters in memory. You can create an strstream object by associating the object with a previously allocated array of characters. You can then write output to it, read input from it, and apply other operations to it just as you would to another type of stream. 17.Explain passing by value, passing by pointer and passing by reference Passing by value means that a copy of the object is made on the callees stack and altering the object means altering a local copy so the callers object is unchanged when the function returns. Passing by reference means that the address of the object is send (a reference holds an address but behaves like an object) so that the callee can directly alter the original object. Passing by pointer is a misconception. A pointer is a memory address. You can pass a pointer to a function, but the pointer is passed by value. You can distinguish between the two this way: if the parameter has a trailing & then it is passed by reference, otherwise by value. 18.Have you heard of mutable keyword? A mutable keyword is useful when we want to force a logical const data member to have its value modified. A logical const can happen when we declare a data member as non-const, but we have a const member function attempting to modify that data member. 19.What is a RTTI? It is run time type identification(RTTI).In an inheritance hierarchy we can find out the exact type of the objet of which it is member. It can be done by using: 1) dynamic id operator 2) typecast operator. 20.Is there something that I can do in C and not in C++? Defining member functions in structure 22.What is the difference between calloc and malloc? Calloc has two arguments eg:

a=(int *)calloc(n,sizeof(int)); malloc has only one argument eg: a=(int *)malloc(n*sizeof(int)); where a is an array and n is the size of the array. In calloc default value will be ZERO when the memory is allocated whereas in malloc default value will be GARBAGE values 23.What will happen if I allocate memory using new and free it using free or allocate sing calloc and free it using delete? Possibly Memory leak 24.Difference between printf and sprintf. sprintf writes data to the charecter array whereas printf writes data to the standard output device 25.What is map in STL? An associative container. 26.When shall I use Multiple Inheritance? When to inherit features from more than one class. 27.Explain working of printf. printf is defined as: char *printf(const char *control-string, ...); char *control-string defines the control string. the ellipse (...) defines printf has having any number of any type of parameters after the control string. #include <stdio.h> #include <stdlib.h> #include <stdarg.h> char *newfmt(const char *fmt, ...){ char *p; va_list ap; if ((p = malloc(128)) == NULL) return (NULL); va_start(ap, fmt); (void) vsnprintf(p, 128, fmt, ap); va_end(ap); return (p); } /* Plus a little routine to show how it is used. */ int main(int ac, char *av) {

int a = 42; char *str; str = newfmt("%d", a); if (!str) { fprintf(STDERR, "Out of memory!\n"); exit(5); } printf("%s\n", str); free(str); return 0; } 28.How to write Multithreaded applications using C++? C++ doesnt have the inbuilt thread functions. It can support through operating system calls. http://www.devarticles.com/c/a/Cplusplus/Multithreading-in-C/ 29.Write any small program that will compile in C but not in C++ In C if u have a const variable e.g. const int i = 2; u can use this variable in other module as follows extern const int i; C compiler will not complain. but for C++ compiler u must write extern const int i = 2; else error would be generated 30.What is Memory Alignment? Most CPUs require that objects and variables reside at particular offsets in the system's memory. For example, 32-bit processors require a 4-byte integer to reside at a memory address that is evenly divisible by 4. This requirement is called "memory alignment". Thus, a 4-byte int can be located at memory address 0x2000 or 0x2004, but not at 0x2001. On most Unix systems, an attempt to use misaligned data results in a bus error, which terminates the program altogether. On Intel processors, the use of misaligned data is supported but at a substantial performance penalty. Therefore, most compilers automatically align data variables according to their type and the particular processor being used. This is why the size that structs and classes occupy is often larger than the sum of their members' size: struct Employee { int ID; char state[3]; //CA, NY etc. + terminating null int salary; }; Apparently, Employee should occupy 11 bytes (4+3+4). However, most compilers add an unused padding byte after the field 'state' so that it aligns on a 4 byte boundary. Consequently, Employee occupies 12 bytes rather than 11. You can examine the actual size of an aggregate by using the expression sizeof(Employee). 31.Why pre increment operator is faster than post increment?

Increment and then assign ; assign and then increment. 32.What are the techniques you use for debugging? Now that we have learned the basics of Makefiles, we can now look into debugging our code in conjunction with Makefiles. As an introduction we will be using three debugging techniques: Non-interactive GNU gdb dbx 33.How to reduce a final size of executable? Size of the final execuatable can be reduced using dynamic linking for libraries. 34.Give 2 examples of a code optimization. Use constructor initialization lists Break big switch statements into nested switches

You might also like