February 14, 2019

Srikaanth

Nate Frequently Asked C++ Interview Questions Answers

What Are Put And Get Pointers?

These are the long integers associated with the streams. The value present in the put pointer specifies the byte number in the file from where next write would take place in the file. The get pointer specifies the byte number in the file from where the next reading should take place.

What Does The Nocreate And Noreplace Flag Ensure When They Are Used For Opening A File?

nocreate and noreplace are file-opening modes. A bit in the ios class defines these modes. The flag nocreate ensures that the file must exist before opening it. On the other hand the flag noreplace ensures that while opening a file for output it does not get overwritten with new one unless ate or app is set. When the app flag is set then whatever we write gets appended to the existing file. When ate flag is set we can start reading or writing at the end of existing file.

What Is The Limitation Of Cin While Taking Input For Character Array?

To understand this consider following statements,

       char str[5] ;
cin >> str ;
While entering the value for str if we enter more than 5 characters then there is no provision in cin to check the array bounds. If the array overflows, it may be dangerous. This can be avoided by using get( ) function. For example, consider following statement,cin.get ( str, 5 ) ; On executing this statement if we enter more than 5 characters, then get( ) takes only first five characters and ignores rest of the characters. Some more variations of get( ) are available, such as shown below

get ( ch ) - Extracts one character only get ( str, n ) - Extracts up to n characters into str get ( str, DELIM ) - Extracts characters into array str until specified delimiter (such as '\n'). Leaves delimiting character in stream.

get ( str, n, DELIM ) - Extracts characters into array str until n characters or DELIM character, leaving delimiting character in stream.

Mention The Purpose Of Istream Class?

The istream class performs activities specific to input. It is derived from the iosclass. The most commonly used member function of this class is the overloaded >> operator which canextract values of all basic types. We can extract even a string using this operator.
Nate Frequently Asked C++ Interview Questions Answers
Nate Frequently Asked C++ Interview Questions Answers

Would The Following Code Work?

 
#include<iosteram.>
Void Main( )
 {
 Ostream O ;
 O << "dream. Then Make It Happen!" ;
}

No This is because we cannot create an object of the iostream class since its constructor and copy constructorare declared private.

Assert( ) Macro..?

We can use a macro called assert( ) to test for conditions that should not occur in a code. This macro expands to an if statement. If test evaluates to 0, assert prints an error message and calls abort to abort the program.

#include
#include
void main( )
{
   int i ;
   cout << "\nEnter an integer: " ;
   cin >> i ;
   assert ( i >= 0 ) ;
   cout << i << endl ;
}

Why Is That Unsafe To Deal Locate The Memory Using Free( ) If It Has Been Allocated Using New?

This can be explained with the following example

#include
class sample
{
  int *p ;
  public
     sample( )
     {
       p = new int ;
     }

     ~sample( )
     {
        delete p ;
     }
} ;

void main( )
{
  sample *s1 = new sample ;
  free ( s1 ) ;
  sample *s2 = ( sample * ) malloc ( sizeof ( sample ) ) ;
  delete s2 ;
}
The new operator allocates memory and calls the constructor. In the constructor we have allocated memory on heap, which is pointed to by p. If we release the object using the free( ) function the object would die but the memory allocated in the constructor would leak. This is because free( ) being a C library function does not call the destructor where we have deal located the memory.
As against this, if we allocate memory by calling malloc( ) the constructor would not get called. Hence p holds a garbage address. Now if the memory is deal located using delete, the destructor would get called where we have tried to release the memory pointed to by p. Since p contains garbage this may result in a runtime error.

Can We Distribute Function Templates And Class Templates In Object Libraries?

No! We can compile a function template or a class template into object code (.obj file). The code that contains a call to the function template or the code that creates an object from a class template can get compiled. This is because the compiler merely checks whether the call matches the declaration (in case of function template) and whether the object definition matches class declaration (in case of class template). Since the function template and the class template definitions are not found, the compiler leaves it to the linker to restore this. However, during linking, linker doesn't find the matching definitions for the function call or a matching definition for object creation. In short the expanded versions of templates are not found in the object library. Hence the linker reports error.

Differentiate Between An Inspector And A Mutator ?

An inspector is a member function that returns information about an object's state (information stored in object's data members) without changing the object's state. A mutator is a member function that changes the state of an object. In the class Stack given below we have defined a mutator and an inspector.

class Stack
{
   public
   int pop( ) ;
   int getcount( ) ;
}
In the above example, the function pop( ) removes top element of stack thereby changing the state of an object. So, the function pop( ) is a mutator. The function getcount( ) is an inspector because it simply counts the number of elements in the stack without changing the stack.

Namespaces

The C++ language provides a single global namespace. This can cause problems with global name clashes. For instance, consider these two C++ header files: // file1.h float f ( float, int ) ; class sample { ... } ; // file2.h class sample { ... } ; With these definitions, it is impossible to use both header files in a single program; the sample classes will clash.A namespace is a declarative region that attaches an additional identifier to any names declared inside it. The additional identifier thus avoids the possibility that a name will conflict with names declared elsewhere in the program. It is possible to use the same name in separate namespaces without conflict even if the names appear in the same translation unit. As long as they appear in separate namespaces, each name will be unique because of the addition of the namespace identifier. For example

// file1.h
namespace file1
{
   float f ( float, int ) ;
   class sample { ... } ;
}
// file2.h
namespace file2
{
    class sample { ... } ;
}
Now the class names will not clash because they become file1::sample and file2::sample, respectively.

Declare A Static Function As Virtual?no. The Virtual Function Mechanism Is Used On The Specific Object That Determines Which Virtual Function To Call. Since The Static Functions Are Not Any Way Related To Objects, They Cannot Be Declared As Virtual.

No. The virtual function mechanism is used on the specific object that determines which virtual function to call. Since the static functions are not any way related to objects, they cannot be declared as virtual.

Can User-defined Object Be Declared As Static Data Member Of Another Class?

Yes. The following code shows how to initialize a user-defined object.

#include
class test
{
  int i ;
  public
    test ( int ii = 0 )
    {
      i = ii ;
    }
} ;
class sample
{
  static test s ;
} ;
test sample::s ( 26 ) ;
Here we have initialized the object s by calling the one-argument constructor. We can use the same convention to initialize the object by calling multiple-argument constructor.

What Is A Forward Referencing And When Should It Be Used?

Consider the following program

class test
{
   public
     friend void fun ( sample, test ) ;
} ;

class sample
{
   public
     friend void fun ( sample, test ) ;
} ;

void fun ( sample s, test t )
{
   // code
}

void main( )
{
   sample s ;
   test t ;
   fun ( s, t ) ;
}
This program would not compile. It gives an error that sample is undeclared identifier in the statement friend void fun ( sample, test ) ; of the class test. This is so because the class sample is defined below the class test and we are using it before its definition. To overcome this error we need to give forward reference of the class sample before the definition of class test. The following statement is the forward reference of class sample. Forward referencing is generally required when we make a class or a function as a friend.

What Is Virtual Multiple Inheritance?

A class b is defined having member variable i. Suppose two classes d1 and d2 are derived from class b and a class multiple is derived from both d1 and d2. If variable i is accessed from a member function of multiple then it gives error as 'member is ambiguous'. To avoid this error derive classes d1 and d2 with modifier virtual as shown in the following program.

#include
class b
{
  public
    int i ;
  public
    fun( )
    {
      i = 0 ;
    }
} ;
class d1 : virtual public b
{
  public
    fun( )
    {
      i = 1 ;
    }
} ;
class d2 : virtual public b
{
  public
    fun( )
    {
      i = 2 ;
    }
} ;
class multiple : public d1, public d2
{
  public
    fun( )
    {
      i = 10 ;
    }
} ;
void main( )
{
   multiple d ;
   d.fun( ) ;
   cout << d.i ;
}

Can We Use This Pointer In A Class Specific, Operator-overloading Function For New Operator?

No! The this pointer is never passed to the overloaded operator new() member function because this function gets called before the object is created. Hence there is no question of the this pointer getting passed to operator new( ).

How To Allocate Memory Dynamically For A Reference?

No! It is not possible to allocate memory dynamically for a reference. This is because, when we create a reference, it gets tied with some variable of its type. Now, if we try to allocate memory dynamically for a reference, it is not possible to mention that to which variable the reference would get tied.

Write Code To Make An Object Work Like A 2-d Array?: Take a look at the following program.

#include
class emp
{
  public
    int a[3][3] ;
    emp( )
    {
      int c = 1 ;
      for ( int i = 0 ; i <= 2 ; i++ )
      {
         for ( int j = 0 ; j <= 2 ; j++ )
         {
           a[i][j] = c ;
           c++ ;
         }
       }
     }
     int* operator[] ( int i )
     {
       return a[i] ;
     }
} ;
void main( )
{
  emp e ;
  cout << e[0][1] ;
}
The class emp has an overloaded operator [ ] function. It takes one argument an integer representing an array index and returns an int pointer. The statement cout << e[0][1] ; would get converted into a call to the overloaded [ ] function as e.operator[ ] ( 0 ). 0 would get collected in i. The function would return a[i] that represents the base address of the zeroeth row. Next the statement would get expanded as base address of zeroeth row[1] that can be further expanded as *( base address + 1 ). This gives us a value in zeroth row and first column.

Subscribe to get more Posts :