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

http://r4r.co.

in/cpp/

Q1. What is Polymorphism? Ans: Polymorphism is phenomena by which we can make make a method for reuse. Polymorphism is an object oriented term. Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate actions. In other words, polymorphism means taking more than one form. Polymorphism leads to two important aspects in Object Oriented terminology - First one is function Overloading and second is function Overriding. Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called. Overriding refers to the modifications made in the sub class to the inherited methods from the base class to change their behavior. Q2. What is Operator overloading? Ans: When an operator is overloaded, it takes on an additional meaning relative to a certain class. But it can still retain all of its old meanings. Examples: 1) The operators >> and << may be used for I/O operations because in the header, they are overloaded. 2) In a stack class it is possible to overload the + operator so that it appends the contents of one stack to the contents of another. But the + operator still retains its original meaning relative to other types of data. Q3. What are Templates ? Ans: C++ Templates allow u to generate families of functions or classes that can operate on a variety of different data types, freeing you from the need to create a separate function or class for each type. Using templates, u have the convenience of writing a single generic function or class definition, which the compiler automatically translates into a specific version of the function or class, for each of the different data types that your program actually uses. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q4. What is the difference between run time binding and compile time binding? Ans: Dynamic Binding:This is also known as "Late Binding".The address of the functions are determined at runtime rather than at compile time. Static Binding:This is also known as "Early Binding" .The address of the functions are determined at compile time rather than at run time. Q5. What is Difference Between C/C++ Ans: 1.In C passing value to a function is "Call by Value" whereas in C++ its "Call by Reference" 2.C does not have a class/object concept. C++ provides data abstraction, data encapsulation, Inheritance and Polymorphism. 3.C++ supports all C syntax. File extension is .c in C while .cpp in C++.(C++ compiler compiles the files with .c extension but C compiler can not!)

4.In C structures can not have contain functions declarations. In C++ structures are like classes, so declaring functions is legal and allowed. C++ can have inline/virtual functions for the classes. c++ is C with Classes hence C++ while in c the closest u can get to an User defined data type is struct and union. Q5. What will be the output of the following code? void main (){ int i = 0 , a[3] ; a[i] = i++; printf ("%d",a[i]) ; } The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value. Q6: Why doesn't the following code give the desired result? Ans: int x = 3000, y = 2000 ; long int z = x * y ; Ans:Here the multiplication is carried out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. However, to get the correct output, we should use an explicit cast to force long arithmetic as shown below: long int z = ( long int ) x * y ; Note that ( long int )( x * y ) would not give the desired effect. Q7: Why doesn't the following statement work? char str[ ] = "Hello" ; strcat ( str, '!' ) ; Ans: The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character. To make the above statement work writes the statement as shown below: strcat ( str, "!" ) ; Q8: How do I know how many elements an array can hold? Ans: main( ){ int i[32767] ; float f[16383] ; char s[65535] ; } Q9: How do I write code that reads data at memory location specified by segment and offset? Ans: Use peekb( ) function. This function returns byte(s) read from specific segment and offset locations in memory. The following program illustrates use of this function. In this program from VDU memory we have read characters and its attributes of the first row. The information stored in file is then further read and displayed using peek( ) function. #include <stdio.h> #include <dos.h>

main( ){ char far *scr = 0xB8000000 ; FILE *fp ; int offset ; char ch ; if ( ( fp = fopen ( "scr.dat", "wb" ) ) == NULL ) { printf ( "\nUnable to open file" ) ; exit( ) ; } for ( offset = 0 ; offset < 160 ; offset++ ) fprintf ( fp, "%c", peekb ( scr, offset ) ) ; fclose ( fp ) ; if ( ( fp = fopen ( "scr.dat", "rb" ) ) == NULL ) { printf ( "\nUnable to open file" ) ; exit( ) ; } for ( offset = 0 ; offset < 160 ; offset++ ) { fscanf ( fp, "%c", &ch ) ; printf ( "%c", ch ) ; } fclose ( fp ) ; } Q10: Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible ? Ans: There is nothing like Virtual Constructor. The Constructor cant be virtual as the constructor is a code which is responsible for creating a instance of a class and it cant be delegated to any other object by virtual keyword means. Q11: What about Virtual Destructor? Ans: Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object baller is balling to , proper destructor will be called. Q12: What is Pure Virtual Function? Why and when it is used ? Ans: The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error. This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class. Q13. What is problem with Runtime type identification? Ans: The run time type identification comes at a cost of performance penalty. Compiler maintains the class. Q14: How Virtual functions call up is maintained? Ans: Through Look up tables added by the compile to every class image. This also leads to performance penalty.

Q15: Can inline functions have a recursion? Ans: No. Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time.
Q16: How do you link a C++ program to C functions? Ans: By using the extern "C" linkage specification around the C function declarations. Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions. Q17. Explain the scope resolution operator? Ans: It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope. Q18: How many ways are there to initialize an int with a constant? 1. int f = 123; 2. int bar(123); Q19: What is your reaction to this line of code? Ans: delete this; It is not a good programming Practice. A good programmer will insist that you should absolutely never use the statement if the class is to be used by other programmers and instantiated as static, extern, or automatic objects. That much should be obvious. The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object did itself in. Subsequent dereferencing of the baller can and usually does lead to disaster. I think that the language rules should disallow the idiom, but that's another matter. Q20: What is the difference between a copy constructor and an overloaded assignment operator? Ans: A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class. Q21. When should you use multiple inheritance? Ans: There are three acceptable answers:- "Never," "Rarely," and "When the problem domain cannot be accurately modeled any other way." Consider an Asset class, Building class, Vehicle class, and CompanyCar class. All company cars are vehicles. Some company cars are assets because the organizations own them. Others might be leased. Not all assets are vehicles. Money accounts are assets. Real estate holdings are assets. Some real estate holdings are buildings. Not all buildings are assets. Ad infinitum. When you diagram these relationships, it becomes apparent that multiple inheritance is a likely and intuitive way to model this common problem domain. The applicant should understand, however, that multiple inheritance, like a chainsaw, is a useful tool that has its perils, needs respect, and is best avoided except when nothing else will do.

Q22: What is a virtual destructor? Ans: The simple answer is that a virtual destructor is one that is declared with the virtual attribute. The behavior of a virtual destructor is what is important. If you destroy an object through a baler or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be compile. Q23: Can a constructor throw a exception? How to handle the error when the constructor fails? Ans: The constructor never throws a error. Q24: What are the debugging methods you use when came across a problem? Ans: Debugging with tools like : GDB, DBG, Forte, Visual Studio. Analyzing the Core dump. Using tusc to trace the last system call before crash. Putting Debug statements in the program source code. Q25: How the compilers arranges the various sections in the executable image? Ans: The executable had following sections:Data Section (uninitialized data variable section, initialized data variable section ) Code Section Remember that all static variables are allocated in the initialized variable section. Q26: Explain the ISA and HASA class relationships. How would you implement each in a class design? Ans: A specialized class "is" a specialization of another class and, therefore, has the ISA relationship with the other class. This relationship is best implemented by embedding an object of the Salary class in the Employee class. Q27: When is a template a better solution than a base class? Ans: When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the generality) to the designer of the container or manager class. Q28: How do you know that your class needs a virtual destructor? Ans: If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a baller to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q29: What is the difference between new/delete and malloc/free? Ans: Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q30: What happens when a function throws an exception that was not specified by an exception specification for this function? Ans: Unexpected() is called, which, by default, will eventually trigger abort(). Q31: Can you think of a situation where your program would crash without reaching the breakball, which you set at the beginning of main()? Ans: C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes

the crash will occur before main() is entered. Q32: What issue do auto_ptr objects address? Ans: If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q33: Is there any problem with the following: char *a=NULL; char& p = *a;? The result is undefined. You should never do this. A reference must always refer to some object. Q34: Why do C++ compilers need name mangling? Ans: Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q35: Is there anything you can do in C++ that you cannot do in C? Ans: No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C

You might also like