Interview Questions | C/C++ Questions | Interview Questions



Get "C/C++ Questions" Category

Sort by:

What is function overloading and operator overloading?

Function overloading: C++ enables several functions of the same name to be defined, as long as
these functions have different sets of parameters (at least as far as their types are concerned). This
capability is called function overloading. When an overloaded function is called, the C++ compiler
selects the proper function by examining the number, types and order of the arguments in the call.
Function overloading is commonly used to create several functions of the same name that perform
similar tasks but on different data types.

Read more on What is function overloading and operator overloading?…

Write a program to implement user defined Macro instead of sizeof operator to find size of a data type

#include <stdio.h>
#define my_sizeof(X) (char*)((X*)10+1) – (char*)((X*) 10)
 
int main()
{
  int size;
     
  size =  my_sizeof(long);
 
  printf(“size=%d\n”,size);
 
  return 0;     
}

How do you write a program using c/c++ to configure maximum contiguous sum in an array?

Let us consider an array of integers, both positive and negative. we have to find out the maximum sum possible by adding ˜n consecutive integers in the array,

i.e. n <= [ARRAY_SIZE].

Read more on How do you write a program using c/c++ to configure maximum contiguous sum in an array?…

What is the difference between an object and a class?

Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.

  • A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don’t change.

Read more on What is the difference between an object and a class?…

Can bit fields be used on other data types than integer?

Ans: Bit fields can be specified only with integers (but when used on chars, it does not throw any warning or error!

Below are examples of structure with bit fields, highlighting different points.

Example-1:

struct bitFields {
int x : 16;
int y : 14;
int z : 2;
} bf1;

Read more on Can bit fields be used on other data types than integer?…

write a program to perform binary search using C

void main()
{
int a[10],i,n,m,c,l,u;
clrscr();
printf(“Enter the size of an array->”);
scanf(“%d”,&n);
printf(“\nEnter the elements of the array->”);
for(i=0;i”);
for(i=0;i”);
scanf(“%d”,&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf(“\nThe number is not in the list”);
else
printf(“\nThe number is found”);
getch();
}
int binary(int a[],int n,int m,int l,int u)
{
int mid,c=0;
if(l<=u)
  {
    mid=(l+u)/2;
    if(m==a[mid])
     {
        c=1;
    }
else
 if(m<a[mid])
 {
   return binary(a,n,m,l,mid-1);
  }
  else
   return binary(a,n,m,mid+1,u);
  }
  else
  return c;
 }

Read more on write a program to perform binary search using C…

write a program in C to find – a number is prime or not

void main()
{
  int num,i,count=0;
  clrscr();
  printf(“\nEnter a number:”);
  scanf(“%d”,&num);
  for(i=1;i<=num;i++)
  {
    if(num%i==0)
    count++;
  }
  if(count==2)
    printf(“%d is a prime number”,num);
  else
    printf(“%d is not a prime number”,num);
  getch();
  }

Read more on write a program in C to find – a number is prime or not…

write a program in C to reverse a number

void main()
{
  int num,out;
  clrscr();
  scanf(“%d”,&num);
  out=rev(num);
  printf(“%d”,out);
  getch();
}
int rev(int num)
{
  static sum,r;
  if(num)
  {
    r=num%10;
    sum=sum*10+r;
    rev(num/10);
  }
  else return 0;
  return sum;
}

Read more on write a program in C to reverse a number…

write a program to find out transpose of a matrix in C

void main()
{
int a[10][10],b[10][10],i,j,k=0,m,n;
clrscr();
printf(“\nEnter the row and column of matrix”);
scanf(“%d %d”,&m,&n);
printf(“\nEnter the First matrix->”);
for(i=0;i “);
for(i=0;i
 {
   printf(“\n”);
   for(j=0;j
   {
     printf(“%d\t”,b[i][j]);
   }
 }
getch();
}

Read more on write a program to find out transpose of a matrix in C…

What operations are allowed on an array ?

Please find the following points:
> Comparison of a string with pointer or base address is not allowed in C.
    We have to use string functions from string.h
 
   Following line is from K&R:
   There are no operations that manipulate an entire array or string although structures can be copied as a unit.
> sizeof operator will return number of bytes for a given variable/object/data type.
   Ex:
int main()
{
  int i,n;
  char str[]=”Hello”;
  int arr[]={1,2,3,4,5};
 

Read more on What operations are allowed on an array ?…

write a C program to convert decimal number to octal number

void main()
{
int i=0,j=0,rem=0,a[10],b[10];
long int num;
clrscr();
printf(“\nEnter a number :”);
scanf(“%ld”,&num);
while(num)
{
if(num<8) num=”num/8;” i=”j-1;i”>=0;i–)
 b[rem++]=a[i];
 printf(“\nOctal equivalent :”);
 for(j=0;j
 printf(“%d”,b[j]);
 getch();
}

Write a program to find out whether a number is armstrong number or not

What is an Armstrong number?

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Read more on Write a program to find out whether a number is armstrong number or not…

What is the difference between class and structure?

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.

Read more on What is the difference between class and structure?…

What is the use of friend function?

Friend functions
Sometimes a function is best shared among a number of different classes. Such functions can be declared either as member functions of one class or as global functions. In either case they can be set to be friends of other classes, by using a friend specifier in the class that is admitting them. Such functions can use all attributes of the class which names them as a friend, as if they were themselves members of that class.
A friend declaration is essentially a prototype for a member function, but instead of requiring an implementation with the name of that class attached by the double colon syntax, a global function or member function of another class provides the match.
class mylinkage
{
private:
mylinkage * prev;
mylinkage * next;

Read more on What is the use of friend function?…

What is the difference between realloc() and free()?

The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have been created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer.

Read more on What is the difference between realloc() and free()?…

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 dont 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.

Read more on What is virtual constructors/destructors?…

What is namespace?

Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say
it somehow, they serve to split the global scope in sub-scopes known as namespaces.
The form to use namespaces is:
namespace identifier { namespace-body }
Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace.

Read more on What is namespace?…

What is the difference between declaration and definition?

The declaration tells the compiler that at some later point we plan to present the definition of this declaration.

E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j>=0; j–) //function body
cout<<*;
cout<<endl;
}

Read more on What is the difference between declaration and definition?…

Can we assign characters or -ve values to enum?

Ans:
enum charEnum {MinuOne=-1, Zeroth, First, Second, NoThird=4, Fifth, Character=‘c’, LastButOne, Last};

printf(“Enum MinuOne: %d\n”, MinuOne);
printf(“Enum Second: %d\n”, Second);
printf(“Enum Fifth: %d\n”, Fifth);
printf(“Enum Character: %d[%c]\n”, Character, Character);
printf(“Enum LastButOne: %d\n”, LastButOne);
printf(“Enum Last: %d\n”, Last);

Read more on Can we assign characters or -ve values to enum?…

What happens if you access(read/write) an array beyond the boundary defined?

Ans: Its not legal. But just works. No guarantee of correct output. Below is one pattern of output(which is correct!).

int array[2];

array[0]=1;
array[1]=2;
array[2]=3;
array[3]=4;
printf(“array[2], array[3]: %d, %d\n”, array[2],array[3]);

Read more on What happens if you access(read/write) an array beyond the boundary defined?…