Jump to content

C++ Interview Questions and Answers


hackrishna

Recommended Posts

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.

Credits: Sid3u

Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...