• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

CPlusPlus Interview Questions Answers

Question - 11 : - Will the following program execute? void main() { void *vptr = (void *) malloc(sizeof(void)); vptr++; }

Answer - 11 : - 1. It will throw an error, as arithmetic operations cannot be performed on void pointers. 2. It will not build as sizeof cannot be applied to void* ( error “Unknown size” ) 3. How can it execute if it won’t even compile? It needs to be int main, not void main. Also, cannot increment a void *. 4. According to gcc compiler it won’t show any error, simply it executes. but in general we can’t do arthematic operation on void, and gives size of void as 1 5. The program compiles in GNU C while giving a warning for “void main”. The program runs without a crash. sizeof(void) is “1? hence when vptr++, the address is incremented by 1. 6. Regarding arguments about GCC, be aware that this is a C++ question, not C. So gcc will compile and execute, g++ cannot. g++ complains that the return type cannot be void and the argument of sizeof() cannot be void. It also reports that ISO C++ forbids incrementing a pointer of type ‘void*’. 7. in C++ voidp.c: In function `int main()’: voidp.c:4: error: invalid application of `sizeof’ to a void type voidp.c:4: error: `malloc’ undeclared (first use this function) voidp.c:4: error: (Each undeclared identifier is reported only once for each function it appears in.) voidp.c:6: error: ISO C++ forbids incrementing a pointer of type `void*’ But in c, it work without problems

Question - 12 : - What is the difference between realloc() and free()?

Answer - 12 : - The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer.

Question - 13 : - What are the advantages of inheritance?

Answer - 13 : - It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.

Question - 14 : - Explain the ISA and HASA class relationships. How would you implement each in a class design?

Answer - 14 : - A specialized class "is" a specialization of another class and, therefore, has the ISA relationship with the other class. An Employee ISA Person. This relationship is best implemented with inheritance. Employee is derived from Person. A class may have an instance of another class. For example, an employee "has" a salary, therefore the Employee class has the HASA relationship with the Salary class. This relationship is best implemented by embedding an object of the Salary class in the Employee class.

Question - 15 : - What is namespace?

Answer - 15 : - Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces. The form to use namespaces is: namespace identifier { namespace-body } Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example: namespace general { int a, b; } In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would have to put: general::a general::b The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error.

Question - 16 : - What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.

Answer - 16 : - An internal iterator is implemented with member functions of the class that has items to step through. .An external iterator is implemented as a separate class that can be "attach" to the object that has items to step through. .An external iterator has the advantage that many difference iterators can be active simultaneously on the same object.

Question - 17 : - Can I free() pointers allocated with new? Can I delete pointers        allocated with malloc()?

Answer - 17 : - No! It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program.  But it is illegal, immoral, and despicable to call free() with a pointer allocated via new, or to call delete on a pointer allocated via malloc(). Beware! I occasionally get e-mail from people telling me that it works OK for them on machine X and compiler Y.  That does not make it right! Sometimes people say, "But I'm just working with an array of char." Nonetheless do not mix malloc() and delete on the same pointer, or new and free() on the same pointer! If you allocated via p = new char[n], you must use delete[] p; you must not use free(p).  Or if you allocated via p = malloc(n), you must use free(p); you must not use delete[] p or delete p! Mixing these up could cause a catastrophic failure at runtime if the code was ported to a new machine, a new compiler, or even a new version of the same compiler. You have been warned.

Question - 18 : - What is the difference between a Java application and a Java applet?

Answer - 18 : - The difference between a Java application and a Java applet is that a Java application is a program that can be executed using the Java interpeter, and a JAVA applet can be transfered to different networks and executed by using a web browser (transferable to the WWW).

Question - 19 : - RTTI - What is RTTI?

Answer - 19 : - 1. RTTI stands for "Run Time Type Identification". 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 2.   RTTI is defined as follows: Run Time Type Information, a facility that allows an object to be queried at runtime to determine its type. One of the fundamental principles of object technology is polymorphism, which is the ability of an object to dynamically change at runtime.

Question - 20 : - What is a node class?

Answer - 20 : - A node class is a class that, * relies on the base class for services and implementation, * provides a wider interface to the users than its base class, * relies primarily on virtual functions in its public interface * depends on all its direct and indirect base class * can be understood only in the context of the base class * can be used as base for further derivation * can be used to create objects. A node class is a class that has added new services or functionality beyond the services inherited from its base class.

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners