ServiceNow Frequently Asked C++ Interview Questions Answers

When Should I Use Unitbuf Flag?

The unit buffering (unitbuf) flag should be turned on when we want to ensure that each character is output as soon as it is inserted into an output stream. The same can be done using unbuffered output but unit buffering provides a better performance than the unbuffered output.

What Are Manipulators?

Manipulators are the instructions to the output stream to modify the output in various ways. The manipulators provide a clean and easy way for formatted output in comparison to the formatting flags of the ios class. When manipulators are used, the formatting instructions are inserted directly into the stream. Manipulators are of two types, those that take an argument and those that don't.

Differentiate Between The Manipulator And Setf( ) Function?

The difference between the manipulator and setf( ) function are as follows

The setf( ) function is used to set the flags of the ios but manipulators directly insert the formatting instructions into the stream. We can create user-defined manipulators but setf( ) function uses data members of ios class only. The flags put on through the setf( ) function can be put off through unsetf( ) function. Such flexibility is not available with manipulators.

How To Get The Current Position Of The File Pointer?

We can get the current position of the file pointer by using the tellp( ) member function of ostream class or tellg( ) member function of istream class. These functions return (in bytes) positions of put pointer and get pointer respectively.

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.
ServiceNow Frequently Asked C++ Interview Questions Answers
ServiceNow Frequently Asked C++ Interview Questions Answers

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.

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.

2 Comments

Post a Comment

Previous Post Next Post