Google News
logo
CPP Interview Questions
There are two types of scope in C++, local and global. A variable has a local scope when it is declared inside a code block. The variable remains active only inside the block and is not accessible outside the code block. Conversely, a variable has a global scope when it is accessible by the entire program. A global variable needs to be declared at the top of a program before all the function definitions.
In the context of a C++ program, a constant is a type of expression that has a fixed value. Constants can be divided into integer, decimal, floating-point, character, or string, depending on the type of data. C++ also supports octal and hexadecimal constants.
No!. This is because even if we provide the default arguments to the parameters of the overloaded operator function we would end up using the binary operator incorrectly. This is explained in the following example :
sample operator + ( sample a, sample b = sample (2, 3.5f ) ){
 }
void main( )
{
   sample s1, s2, s3 ;
   s3 = s1 + ; // error
   }​
Vectors and Lists are defined under C++ Standard Template Library (STL). These data structures are basically sequential containers implemented using STLs.
 
* A Vector is a contiguous block of memory locations similar to Arrays in “C” programming while a List stores non-contiguous blocks and uses a double linked list.

* Inserting and Deleting elements in a List is easier compared to Vector as in the latter, shifting of elements is required and it is a tedious process.

* However sorting and searching are much faster in Vector compared to List and in former no traversal is required unlike List.

* Lists support no numeric index like Array while Vector supports the same.

      * Insertion and Deletion of elements in List has no iterator validation unlike Vector where the same is possible.
Upcasting is converting a derived class reference or pointer to a base class. In other words, upcasting allows us to treat a derived type as though it were its base type. It is always allowed for public inheritance, without an explicit type cast. This is a result of the is-a relationship between the base and derived classes. Upcasting allows us to treat a derived type as though it were its base type.
 
When a derived class object is passed by value as a base class object, the specific behaviors of a derived class object are sliced off. We're left with a base class object. In other words, if we upcast (Upcasting and Down Casting) to an object instead of a pointer or reference, the object is sliced. As a result, all that remains is the sub object that corresponds to the destination type of our cast and which is only a part of the actual derived class.
 
Converting a base-class pointer (reference) to a derived-class pointer (reference) is called downcasting. Downcasting is not allowed without an explicit typecast. The reason for this restriction is that the is-a relationship is not, in most of the cases, symmetric. A derived class could add new data members, and the class member functions that used these data members wouldn't apply to the base class.
In C++, RTTI (Run-time type information) is a mechanism that exposes information about an object’s data type at runtime and is available only for the classes which have at least one virtual function. It allows the type of an object to be determined during program execution
 
For example, dynamic_cast uses RTTI and following program fails with error “cannot dynamic_cast `b’ (of type `class B*’) to type `class D*’ (source type is not polymorphic) ” because there is no virtual function in the base class B.
 
// CPP program to illustrate 
// Run Time Type Identification 
#include<iostream>
using namespace std;
class B { };
class D: public B {};
  
int main()
{
    B *b = new D;
    D *d = dynamic_cast<D*>(b);
    if(d != NULL)
        cout<<"works";
    else
        cout<<"cannot cast B* to D*";
    getchar();
    return 0;
}
Adding a virtual function to the base class B makes it working.
// CPP program to illustrate 
// Run Time Type Identification 
#include<iostream>
using namespace std;
class B { virtual void fun() {} };
class D: public B { };
  
int main()
{
    B *b = new D;
    D *d = dynamic_cast<D*>(b);
    if(d != NULL)
        cout << "works";
    else
        cout << "cannot cast B* to D*";
    getchar();
    return 0;
}
Output :
works
In C++, equal to (==) and assignment operator (=) are two completely different operators.
 
* Equal to (==) is an equality relational operator that evaluates two expressions to see if they are equal and returns true if they are equal and false if they are not.
 
* The assignment operator (=) is used to assign a value to a variable. Hence, we can have a complex assignment operation inside the equality relational operator for evaluation.
C++ supports the following arithmetic operators :
 
+ addition
subtraction
* multiplication
/ division
% module

Let’s demonstrate the various arithmetic operators with the following piece of code.
 
Example :
#include <iostream>
using namespace std;

int main() {
    int a, b;
    a = 4;
    b = 5;

    // printing the sum of a and b
    cout << "a + b = " << (a + b) << endl;

    // printing the difference of a and b
    cout << "a - b = " << (a - b) << endl;

    // printing the product of a and b
    cout << "a * b = " << (a * b) << endl;

    // printing the division of a by b
    cout << "a / b = " << (a / b) << endl;

    // printing the modulo of a by b
    cout << "a % b = " << (a % b) << endl;

    return 0;
}
Output :
a + b = 9
a - b = -1
a * b = 20
a / b = 0
a % b = 4
Array uses the index to traverse each of its elements.
 
If A is an array then each of its elements is accessed as A[i]. Programmatically, all that is required for this to work is an iterative block with a loop variable i that serves as an index (counter) incrementing from 0 to A.length-1.
 
This is exactly what a loop does and this is the reason why we process arrays using for loops.
There are several methods by which one can allocate memory to 2D array dynamically one of which is as follows.
#include <iostream> 
int main() 
{ 
    int row = 2, col = 2; 
    int* a =  new int[row * col];
   
    int i, j, count = 0; 
    for (i = 0; i <  row; i++) 
      for (j = 0; j < col; j++) 
         *(a+ i*col + j) = count++; 
   
    for (i = 0; i <  row; i++) 
      for (j = 0; j < col; j++) 
         printf("%d ", *(a + i*col + j)); 
   
    delete[ ] a;
    return 0; 
} 

Output : 
0 1 2 3