Easy way to IT Job

C and C++ Interview Questions and Answers
Share on your Social Media

C and C++ Interview Questions and Answers

Published On: May 23, 2022

C Programming Language is a fundamental skill based on a procedural programming approach. It was developed by Dennis Ritchie for writing operating systems.

As it has structural-based, machine-dependent, and high-level abstraction, it will the perfect beginning for freshers to learn to code.

C++, on the other hand, is a powerful object-oriented based programming language developed by Bjarne Stroustrup for high-speed application development.

Both programming languages are in-demand in the industries for operating system development and application development. Here are the popular and frequently asked C and C++ Interview Questions and Answers to help you ace the technical rounds.

C and C++ Freshers Interview Questions and Answers

1. Explain C along with its features

C is the pioneering programming language that has both characteristics of assembly-level (low-level) and higher-level languages.

It is also known as middle-level language and it is used to write an operating system and menu-driven consumer billing applications. Following are the features of C Programming.

  • Simple and Efficient
  • Portable and Machine-Dependent
  • Middle-level programming characteristics
  • Structured programming language
  • Function-rich libraries
  • Dynamic memory management system
  • Super-fast and extensible
  • Pointers can be used in C.

2. Define Token

The token is the individual element of a program and the types of the token are as follows


3. Define a built-in function in C

Built-in functions are library functions that provide by the system to make developer’s life easy by helping them to do common things using pre-defined tasks.

For instance, If we want to print output into the terminal, we can use printf() in C. The most popular built-in functions in C are scanf(), printf(), strcpy, strlwr, strcmp, strlen, strcat, and so on.


4. Define Preprocessor

A Preprocessor is a software application that processes a source file before the compilation. It includes header files, conditional compilation, line control, and macro expansion.


5. Define recursion in C

Recursion is the thing that happens when a function in C calls a copy of itself. In simple terms, when a function calls itself is known as recursion and the function is known as a recursive function.

Syntax

void do_recursion()

… .. … 

do_recursion();

… .. …]

int main() 

{

… .. …

do_recursion();

… .. …

}


6. Differentiate global int and static int declaration with example

The scope is only the difference between static and global int. The global_temp is a global variable that is known to everything inside the program and it will be visible when we add “extern int global_temp;” in the source file.

#include <stdio.h> 

int my_global_var = 0; 

int 

main(void)

printf(“%dn”, my_global_var); 

return 0; 

}

A static variable has a local scope that doesn’t allocate the variable in the stack segment of the memory. It will have less than global scope and it resides in the .bss segment of the combined binary.

#include <stdio.h>

int 

myfunc(int val)

static int my_static_var = 0; 

my_static_var += val; 

return my_static_var; 

int 

main(void)

int myval; 

myval = myfunc(1); 

printf(“first call %dn”, myval); 

myval = myfunc(10);

printf(“second call %dn”, myval); 

return 0; 

}


7. Define Pointer in C

A pointer is a variable that stores the address of another variable. If the value of the variable is stored in a normal variable, then the address of a variable is stored in a pointer variable.


8. Define typecasting in C

Typecasting is the process that converts a variable from one data type to another and if we want to store the large type value to an int type, then we can convert the data type into another data type explicitly.

Syntax

int x;

for(x = 12; x<=54; x++)

{

printf(“%c”, (char) x); /*Explicit casting from int to char*/

}


9. Explain the various types of decision control statements

The statements written in a program will be implemented from top to bottom one by one and the control statements will be used to execute or convert the control from one part of the program to another according to the condition.

  • If – else statement

Normal if-else statement

Else-if statement

Nested if-else statement

  • Switch statement

10. Explain r-value and I-value

  • The r-value represents a data value stored in a memory at a given address and it is an expression that can’t have a value assigned to it. It exists on the right side of an assignment operator (=).
  • The I-value represents a memory location used to denote an object and it will be found on the left or right side of an assignment operator. It is used popularly by an identifier.

11. Differentiate struct and union in C

A struct is a group of complicated data structures stored in a memory block where each member on the block that gets separate memory location to make them accessible at once.

Whereas an union, all the member variables are stored at the same location on the memory as an output where assigning value to a member that change the value of all members of a group.

/* struct & union*/

struct bar {

int a; // we can use a & b both simultaneously

char b;

} bar;

union foo {

int a; // we can’t use both a and b simultaneously

char b;

} foo;

/* using struc and union variables*/

struct bar y;

y.a = 3; // OK to use

y.b = ‘c’; // OK to use

union foo x;

x.a = 3; // OK

x.b = ‘c’; // NOl this affects the value of x.a!


12. Explain memory leak with a way to avoid it

Variable takes space of the RAM once we assign it depending on the size of the data type. If the programmer uses memory on the heap and forgets to delete it, then the memories will be occupied which leads to no memory left for the new variable and it is known as a memory leak.

int main()

{

char * ptr = malloc (sizeof (int));

/* processing line*/

/*forget to free up the allocated memory*/

return 0; }

We can trace all the memory allocations to avoid memory leaks where we want to destroy that memory.

The other way is to avoid memory leak by using a smart pointer in C that links to the GNU compiler.


13. Explain near pointer and far pointer in C

A near pointer is used to hold the address that has a maximum size of 16 bits. An address with a large size can’t store using the near pointer. We can only access 64kb of data at drawbacks, it is no longer will be used for large memory allocation.

A far pointer is a pointer of size 32 bits but the use of the current segment to access information stored outside the computer’s memory. It is used to allocate the sector register to store the data address in the current segment.


14. Explain dangling pointers

The dangling pointer points to a memory that has been freed already and the storage will be no longer allocated. When we try to access it, that might lead to a segmentation fault. To end up this, we can use a dangling pointer.

#include<stdio.h>

#include<string.h>

char *func()

{

char str[10];

strcpy(str,”Hello!”);

return(str);

}

Here, the local variable will return an address that can have scope by the time control that was returned to the calling function.

*c = malloc (sizeof(int));

free(c);

*c = 3;


15. Explain typedef

The typedef is a keyword in C used to define synonyms for an existing type in the C programming language. It can be used to simplify the existing type declaration syntax that provides a specific descriptive name to a type.

typedef <existing-type> <new-type-identifiers>

It provides a name to the existing complex type definition and we can simply create an alias for any type. Typedef is used to shorten the code of the C programming language.


16. Write a program to remove duplicates in an array

#include <stdio.h>

int main()

{

int n, a[100], b[100], calc = 0, i, j,count;

printf(“Enter no. of elements in array.n”);

scanf(“%d”, &n);

printf(“Enter %d integers”, n);

for (i = 0; i < n; i++)

scanf(“%d”, &a[i]);

for (i = 0; i<n; i++)

{

for (j = 0; j<calc; j++)

{

if(a[i] == b[j])

break;

}

if (j== calc)

{

b[count] = a[i];

calc++;

}

}

printf(“Array obtained after removing duplicate elements”);

for (i = 0; i<calc; i++)

{

printf(“%dn”, b[i]);

}

return 0;

}


17. Write a program for checking whether the given number is palindrome or not without a recursive method

#include <stdio.h>

#include <conio.h>

int reverse(int num);

int isPalindrome(int num);

int main()

{

int num;

printf(“Enter a number: “);

scanf(“%d”, &num);

if(isPalindrome(num) == 1)

{

printf(“the given number is a palindrome”);

}

else

{

printf(“the given number is not a palindrome number”);

}

return 0;

}

int isPalindrome(int num)

{

if(num == reverse(num))

{

return 1;

}

return 0;

}

int reverse(int num)

{

int rem;

static int sum=0;

if(num!=0){

rem=num%10;

sum=sum*10+rem;

reverse(num/10);

}

else

return sum;

return sum;

}

Output

Enter a number: 1331

The given number is a palindrome


18. Write a program to find n’th Fibonacci Number

Fibonacci number sequence is segmented by the fact that each number after the first two is the sum of the two preceding ones. Example, 0, 1, 1, 2, 3, 3, 8, 13, 21, 34, 55, 89, .. etc. Now, we have to find F{n} = F{n-1} + F{n-2} with base values F(0) = 0 and <code> F(1) = 1.

// Function to find the nth Fibonacci number

int fib(int n)

{

if (n <= 1) {

return n;

}

return fib(n – 1) + fib(n – 2);

}

int main()

{

int n = 8;

printf(“nth Fibonacci number is %d”, fib(8));

return 0;

}


19. Write a simple program to find whether the given number is Even or Odd without using arithmetic or relational operator

#include<stdio.h>

int main()

{

int x;

printf(“Enter a number: “);

scanf(“%d”, &x);

(x&1)?printf(“Odd”):printf(“Even”);

return 0;

}


20. Write a program in C to add two numbers without using the addition operator

Without using the addition operator, we can add two numbers in C program using the following methods.

#include<stdio.h>

#include<stdlib.h>

int main()

{

int x, y;

printf(“Enter two number: “);

scanf(“%d %d”,&x,&y);

// method 1

printf(“%dn”, x-(-y));

// method 2

printf(“%dn”, -(-x-y));

// method 3

printf(“%dn”, abs(-x-y));

// method 4

printf(“%d”, x-(~y)-1);

return 0;

}


C++ Advanced Interview Questions and Answers

21. What are the real-world applications that can be developed using C++?

C++ is the powerful programming language used to develop the following kinds of applications


22. What are the various data types used in C++?

There are 4 major data types used in C++ and they are

  • Primitive data type is a basic datatype like char, short, int, long, float, bool, or double.
  • Derived data type like an array, pointer, and so on.
  • Enumeration data type like Enum
  • User-defined data types like structure or class

23. Explain class and object in C++

A class is a user-defined data type that has data members and functions. Data variables are known as data members and functions are used to perform operations on the variables.

An object is an instance created for a class as the class is a user-defined data type for an object to be called a variable of the data type.

Example

class A

{

private:

int data;

public:

void fun()

{

}

};


24. Differentiate struct and class in C++

The structure has the members or variables that can be public by default but the members of the class are private by default. The default access specifier for a base class or struct is public when deriving a struct from a class but the default access specifiers will be private when deriving a class.


25. Explain operator overloading with an example

Operator overloading is an important thing in C++ for performing the operations on user-defined data types. We can modify the default meaning to the operators like +,-, *, or / using the operator overloading process.

Example

class complex{

private:

 float r, i;

public:

 complex(float r, float i){

this->r=r;

this->i=i;

 }

 complex(){}

 void displaydata(){

cout<<”real part = “<<r<<endl;

cout<<”imaginary part = “<<i<<endl;

 }

 complex operator+(complex c){

return complex(r+c.r, i+c.i);

 }

};

int main(){

complex a(2,3);

complex b(3,4);

complex c=a+b;

c.displaydata();

return 0;

}


26. What is a constructor in C++?

The constructor is a member used to execute automatically when an object is created. It has the same name as the class name where the members are located. The compiler will know the member function is a constructor and there will be a return type used for constructors.

Example

class A

{

private:

int val;

public:

A(int x)

{ //one argument constructor

val=x;

}

A()

{ //zero argument constructor

}

}

int main()

{

A a(3);

return 0;

}


27. Explain polymorphism with its types and examples

Polymorphism is a thing that has many forms with various behavior for different situations. It occurs when we use multiple classes for inheritance purposes. There are two types of polymorphism and they are a compile-time polymorphism and a runtime polymorphism.

Compile-time polymorphism

In this, we will know which method to be called when the compile-time and the call will be resolved by the compiler. It offers fast execution as it is known as the compile time. It will be achieved by function overloading and operator overloading.

Example

int add(int a, int b){

return a+b;

}

int add(int a, int b, int c){

return a+b+c;

}

int main(){

cout<<add(2,3)<<endl;

cout<<add(2,3,4)<<endl;

return 0;

}

Runtime Polymorphism

In this method, we will know which method to be called during the runtime and the call will not be resolved by the compiler. It offers slow execution as it is known at the runtime only. It will be achieved by virtual functions and pointers.

Example

class A

{

public:

virtual void fun()

{

cout<<“base “;

}

};

class B: public A

{

public:

void fun()

{

cout<<“derived “;

}

};

int main()

{

A *a=new B;

a->fun();

return 0;

}


28. What are the access specifiers in C++?

C++ programming language has the following access specifiers

  • Public: All the data members and member functions can be accessed outside the class
  • Protected: All the data members and member functions are accessible inside the class and the derived class.
  • Private: No data members and member functions can be called outside the class

29. Explain Destructors in C++

When a constructor is called automatically when an object has been created, and must be destroyed by an object automatically using a destructor. It has the same but is preceded by a tilde.

Example

class A{

 private:

int val;

 public:

A(int x){

val=x;

}

A(){

}

~A(){ //destructor

}

}

int main(){

 A a(3);

 return 0;

}


30. How to call a virtual function from a constructor

We can call a virtual function from a constructor but the behavior is a little varied. Once the virtual function is called, it resolves a virtual call at runtime. It is the number function of the present class that gets called and the virtual machine will not work within the constructor.

Example

class base

{

private:

int value;

public:

base(int x){

value=x;

}

virtual void fun()

{

}

}

class derived

{

private:

int a;

public:

derived(int x, int y):base(x){

base *b;

b=this;

b->fun(); //calls derived::fun()

}

void fun(){

cout<<”fun inside derived class”<<endl;

}

}


Conclusion

There are many interesting things to learn in C and C++ programming languages. They are the must skill for every IT aspirant to understand the fundamental of programming.

Learn the best C and C++ Training in Chennai at Softlogic as we provide updated C and C++ Interview Questions and Answers to help clear the technical rounds easily.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.