Facebook Latest C++ Interview Questions Answers

Facebook Most Frequently Asked Latest C++ Interview Questions Answers

What Do You Mean By Inline Function?

The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.

Write A Program That Ask For User Input From 5 To 9 Then Calculate The Average?

#include  "iostream.h" intmain()
{
intMAX = 4;
int total = 0;
int average; int  numb;
for  (int i=0; KMAX; i++) {
cout«
"Please  enter your input between 5 and 9: ";
cin  » numb;
while  (numb<5 || numb>9) {
cout« "Invalid input, please re-enter: ";
cin  » numb;
}
total = total + numb;
}
average  = total/MAX;
cout« "The average number is: "
« average « "n";
 return 0;
}

Write A Short Code Using C++ To Print Out All Odd Number From 1 To 100 Using A For Loop

for( unsigned int i = 1; i < = 100; i++ )
if( l & 0x00000001 )
cout«i«",";

What Is Public, Protected, Private In C++?

Public, protected and private are three access specifiers in C++.
Public data members and member functions are accessible outside the class.
Protected data members and member functions are only available to derived classes.
Private data members and member functions can't be accessed outside the class. However there is an exception can be using friend classes.
Facebook Most Frequently Asked Latest C++ Interview Questions Answers
Facebook Most Frequently Asked Latest C++ Interview Questions Answers

Write A Function That Swaps The Values Of Two Integers, Using Int* As The Argument Type?

void swap(int* a, int*b)
{
intt;
t=*a;
*a = *b;
*b = t;
}

What Is Virtual Constructors/destructors?

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object. There is a simple solution to this problem declare a virtual base-class destructor.

This makes all derived-class destructors virtual even though they don't have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.

What Is The Difference Between An Array And A List?

Array:  is collection of homogeneous elements.
List :is collection of heterogeneous elements.
 Array: memory allocated is static and continuous.
 List:  memory allocated is dynamic and Random.
Array:  User need not have to keep in track of next memory allocation.
List:  User has to keep in Track of next location where memory is allocated.

Does C++ Support Multilevel And Multiple Inheritance?

Yes.

What Is A Template In C++?

Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones

template <class indetifier> functiondeclaration; template <typename indetifier> functiondeclaration;

The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way.

Define A Constructor - What It Is And How It Might Be Called (2 Methods).

constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized.
Ways of calling constructor

Implicitly: automatically by complier when an object is created.
Calling the constructors explicitly is possible, but it makes the code unverifiable.

You Have Two Pairs: New() And Delete() And Another Pair : Alloc() And Free(). Explain Differences Between Eg. New() And Malloc()

"new and delete" are preprocessors while "malloc() and free()" are functions, [we dont use brackets will calling new or delete].
no need of allocate the memory while using "new" but in "malloc()" we have to use "sizeof()".
"new" will initlize the new memory to 0 but "malloc()" gives random value in the new alloted memory location [better to use calloc()]

Write A Program That Will Convert An Integer Pointer To An Integer And Vice-versa.?

The following program demonstrates this.

#include<stdio.h>
#include<iosream>
#include<conio.h>
void main( )
{
    int i = 65000 ;
    int *iptr = reinterpret_cast ( i ) ;
    cout << endl << iptr ;
    iptr++ ;
    cout << endl << iptr ;
    i = reinterpret_cast ( iptr ) ;
    cout << endl << i ;
    i++ ;
    cout << endl << i ;
}

What Is Meant By Const_cast?

The const_cast is used to convert a const to a non-const. This is shown in the following program

#include
void main( )
{
     const int a = 0  ;
     int *ptr = ( int * ) &a ; //one way
     ptr = const_cast_ ( &a ) ; //better way
}
Here, the address of the const variable a is assigned to the pointer to a non-const variable. The const_cast is also used when we want to change the data members of a class inside the const member functions. The following code snippet shows this

class sample
{
    private
    int data;
    public
      void func( ) const
      {
        (const_cast (this))->data = 70 ;
      }
};

What Is The Difference Between Class And Structure In C++?

Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private.

What Is Rtti In C++?

Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type of an object and to convert the type of a pointer or reference (that is, dynamic typing). The need came from practical experience with C++. RTTI replaces many homegrown versions with a solid, consistent approach.

What Is Encapsulation In C++?

Packaging an object's variables within its methods is called encapsulation.

What Is A C++ Object?

Object is a software bundle of variables and related methods. Objects have state and behavior.

Describe Private, Protected And Public - The Differences And Give Examples.

class Point2D{ int x; int y;
public int color;
protected bool pinned;
public Point2D():x(0),y(0){} //default(no argument) constructor
};
Point2D MyPoint;
You cannot directly access private data members when they are declared (implicitly) private

MyPoint.x = 5;          // Compiler will issue a compile ERROR
//Nor yoy can see them
int xdim = MyPoint.x;  // Compiler will issue a compile ERROR
On the other hand, you can assign and read the public data members

MyPoint.color = 255;      //no problem
int col = MyPoint.color; // no problem
With protected data members you can read them but not write them

bool isPinned = MyPoint.pinned; // no problem.

Stl Containers - What Are The Types Of Stl Containers?

There are 3 types of STL containers

Adaptive containers like queue, stack.
Associative containers like set, map.
Sequence containers like vector, deque.

Rtti - What Is Rtti In C++?

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

dynamic id operator.
typecast operator.

Assignment Operator - What Is The Diffrence Between A "assignment Operator" And A "copy Constructor"?

In assignment operator, you are assigning a value to an existing object. But in copy constructor, you are creating a new object and then assigning a value to that object.
For example

complex cl,c2;
cl=c2;           //this is assignment
complex c3=c2;  //copy constructor.

Post a Comment

Previous Post Next Post