October 13, 2018

Srikaanth

MuleSoft Most Frequently Asked C++ Interview Questions Answers

How The Keyword Struct Is Different From The Keyword Class In C++?

In C++, a class is similar to a struct with the exception that, by default, all the members of a class are private; while the members of a struct are public. Encapsulation is not supported by structures but supported by classes.

How New/delete Differs From Malloc()/free?

The memory uses the malloc() operator, which in turn uses the free operator, to remove the unnecessary programs. On the other hand, the program builds the array by using the new operator, fills it with random numbers, displays each of the elements in the array, and deletes the array by using the delete operator. The new and delete operators should be used in C++ because they are type safe. The malloc operator is used when there is a need of forcing a type on an object because a void pointer is returned by it. Moreover, that object cannot be assigned to other types.

How The Delete Operator Differs From The Delete[]operator?

When the new[] operator is used to allocate memory dynamically, the delete[]operator is used to free the memory. The new[] operator is used to allocate memory to an array of values, which starts with the index 0.

How A New Operator Differs From The Operator New?

The new operator creates a class's new instance. On the other hand, overloading of a new operator is done globally with the help of the operator new. The new operator allocates memory for the item and assigns the address of that memory to the pointer by using the name of an item with a pointer of a data type, structure, or array. For example, consider the following code snippet

Double * pi = new double;
In the preceding code snippet, the new operator returns a pointer to the double variable, because it allocates the space for a double value.

Explain The Term Memory Alignment?

The primary meaning of the term alignment is to maintain the appropriate positioning of different components in the memory with respect to each other. In C++, there is a requirement of setting of various objects and variables in a particular way in the system's memory.Therefore, many data variables are aligned automatically by the compiler according to their processor and type.

Can A New Be Used In Place Of Old Mallocq? If Yes, Why?

The new operator should be used in place of old malloc because the new operator ensures the calling of an appropriate destructor at the time of execution and also it is more type-safe than mallocQ.

Is It Possible To Use A New For The Reallocation Of Pointers ?

The reallocation of pointers cannot be done by using new. It can be done by using the reallocQ operator.
MuleSoft Most Frequently Asked C++ Interview Questions Answers
MuleSoft Most Frequently Asked C++ Interview Questions Answers

On Throwing An Exception By The Animal Constructor In P = New Animalq, Can Memory Leak Occur?

Memory of an animal class cannot leak by throwing an exception in p = new Animal(). In case of occurrence of an exception during the Animal constructor of p = new Animal(),there is a surety of the automatic releasing of the allocated memory back to the heap.

What Would Happen On Forgetting [], While Deallocating An Array Through New?

If you forget to use [] while deallocating an array through new, it throws a run time or compile time exception and results in the corruption of the heap.Therefore, it is the responsibility of the programmer to establish the connection between T[n] and delete[]p correctly.

What Is Meant By Forward Referencing And When Should It Be Used?

Forward referencing is generally required when we make a class or a function as a friend.Consider 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 ) ;
}
On compiling this program it gives error on the following statement of test class. It gives an error that sample is undeclared identifier. friend void fun ( sample, 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.

class sample;

Write My Own Zero-argument Manipulator That Should Work Same As Hex?

This is shown in following program.

#include
ostream& myhex ( ostream &o )
{
  o.setf ( ios::hex) ;
  return o ;
}
void main( )
{
  cout << endl << myhex << 2000 ;
}
   
We All Know That A Const Variable Needs To Be Initialized At The Time Of Declaration. Then How Come The Program Given Below Runs Properly Even When We Have Not Initialized P?

#include<iostream>
Void Main( )
{
      Const Char *p ;
      P = "a Const Pointer" ;
      Cout << P ;
}

The output of the above program is 'A const pointer'. This is because in this program p is declared as 'const char*' which means that value stored at p will be constant and not p and so the program works properly.

Write About The Retrieval Of N Number Of Objects During The Process Of Delete[]p?

The n number of objects can be retrieved with the help of the pointer p (it stores the n number of objects) at the run time.

This can be achieved by the application of two techniques, which are

An array which can be associated with p in the form of key and a value such as n.
Over-allocation of the array can be done. The preceding techniques are used by the compilers of commercial grade.

Is It Possible For A Member Function To Use Delete This?

It is possible for a member function to use delete this but on certain conditions which are as follows

 Ensure that the allocation of this object is done through new[] operator.
Ensure that the invocation of a member function done on this (current) object will be the last member function.
Ensure that calling of other member functions and data members should not be done after the line of code which includes delete this.
Examination or comparison of this pointer with other pointers and NULL, printing or casting it, must be avoided after using the delete this.
The preceding points are applied in the cases where the this pointer belongs to the base class in the absence of the virtual destructor.

Discuss The Effects Occur, After An Exception Thrown By A Member Function Is Unspecified By An Exception Specification?

When a function throws an exception, not given in the specification, the exception is passed to a system function named unexpected. The unexpected function calls the latest function named as an argument in a call to the set_unexpected function, which returns its current settings. A function with no exception specification, by default, calls the terminate function, which finally calls the abort (function to terminate the program).

How Can I Be Thrown Polymorphically?

The following code can be used to throw i polymorphically

classExceptionParent { );
classE,cceptionChild : public ExceptionParent ( );
void f(ExceptionParent8 e)
/1
throw e;
void go
ExceptionChild e;
try(
fCc);
catch (ExceptionChild& e) (
//...code to handle ExceptlonChild...
catch C...) (
//...code to handle other exceptions...
)
)
In the preceding code, you can enter the catch (...) clause in the absence of throwing i polymorphically. The throw e in the function f () throws the same type of an object as an expression of static type.

What Is The Role Of Copy Constructor In Copying Of Thrown Objects?

A copy constructor with a public access specifier must be applied to the objects which are thrown.With the help of a compiler, the code is generated through which copying of thrown objects can be done. The thrown objects must have a copy constructor with the public access specifier.

How Can An Improvement In The Quality Of Software Be Done By Try/catch/throw?

Error-handling is necessity while developing applications to account for unexpected situations, such as insufficient memory, resource allocation errors, inability to find/open files, division by zero, an arithmetic or array overflow, and the exhaustion of free heap space, which occur at runtime.

Programmers have various styles of dealing with such exceptions, such as try/catch /throw methods, which cause diversity in coding practice. This diversity increases with the use of user-defined classes, as each class brings with it potential class-specific errors. These methods provide a standard facility to deal with runtime exceptions. Moreover, try/catch/throw result in the code with fewer errors. It is low in cost in relation to development. With error-handling, your program can detect unexpected events and recover from them.

What Is A Dangling Pointer?

When the location of the deallocated memory is pointed by the pointer even after the deletion or allocation of objects is done, without the modification in the value of the pointer, then this type of pointer is called a dangling pointer.

https://mytecbooks.blogspot.com/2018/10/mulesoft-most-frequently-asked-c_12.html
Subscribe to get more Posts :