C Language Interview Questions and Answers for Freshers

What is C language?

The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is one of the most widely used programming languages. C is prized for its efficiency, and is the most popular programming language for writing system software, though it is also used for writing applications.

Can you Mention some Application of C/C++?

Google-Some of the Google applications are also written in C++, including Google file system and Google Chromium.
Symbian OS- is also developed using C++. This is one of the most widespread OS’s for cellular phones.
Apple – OS X -Few parts of apple OS X are written in C++ programming language. Also few application for iPod are written in C++.
Microsoft-Most of the big applications like Windows 95, 98, Me, 200 and XP are also written in C++. Also Microsoft Office, Internet Explorer and Visual Studio are written in Visual C++.

What are the different storage classes in C ?

There are four types of storage classes in C. They are extern, register, auto and static

What is an object?

Object is a software bundle of variables and related methods. Objects have state and behavior

What is a class?

Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class.

What is a modifier?

A modifier, also called a modifying function is a member function that changes the value of at least one data member. In other words, an operation that modifies the state of an object. Modifiers are also known as ‘mutators’.

Define namespace.

It is a feature in C++ to minimize name collisions in the global name space. This namespace keyword assigns a distinct name to a library that allows other libraries to use the same identifier names without creating any name collisions. Furthermore, the compiler uses the namespace signature for differentiating the definitions.

What is data structure?

A data structure is a way of organizing data that considers not only the items stored, but also their relationship to each other. Advance knowledge about the relationship between data items allows designing of efficient algorithms for the manipulation of data.

Can you list out the areas in which data structures are applied extensively?

Compiler Design,
Operating System,
Database Management System,
Statistical analysis package,
Numerical Analysis,
Artificial Intelligence,

What is the difference between C and C++?

  • C follows the procedural programming paradigm
  • C++ is a multi-paradigm language (procedural as well as object oriented)
  • In C language focus on procedure and steps.
  • C++ focuses on the data rather than the process
  • In C data hiding and data security is not possible.
  • In C++Data hiding and data security is present.
  • C uses Top-Down approach
  • C++ uses Bottom-Up approach
  • C is a function driven programming language
  • C++ is a object driven programming language
  • C does not support overloading concept
  • C++ supports overloading concepts like operator overloading and function overloading
  • C does not support namespaces concept
  • C++ supports Namespaces concept.
  • C not support exception handling
  • C++ supports Exception Handling
  • C is structured programming language
  • C++ is object oriented programming language.
  • C does not support inheritance, reusability, polymorphism, data abstraction
  • C++ supports inheritance, reusability, polymorphism, data abstraction.
  • C language only support Early binding
  • C++ supports both Early and Late binding
  • C uses standard input, output functions like scanf and printf.
  • C++ uses input function cin and output function is cout.

What is the advantage of OOP?

simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear;

modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system;

modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods;

extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones;

maintainability: objects can be maintained separately, making locating and fixing problems easier;

re-usability: objects can be reused in different programs.

How do you do dynamic memory allocation in C applications?

In C, malloc, calloc and realloc are used to allocate memory dynamically. In C++, new(), is usually used to allocate objects.

List advantages and disadvantages of dynamic memory allocation vs. static memory allocation.?


Memory is allocated on an as-needed basis. This helps remove the inefficiencies inherent to static memory allocation (when the amount of memory needed is not known at compile time and one has to make a guess).


Dynamic memory allocation is slower than static memory allocation. This is because dynamic memory allocation happens in the heap area.
Dynamic memory needs to be carefully deleted after use. They are created in non-contiguous area of memory segment.
Dynamic memory allocation causes contention between threads, so it degrades performance when it happens in a thread.
Memory fragmentation.

What is Java?

Java is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak (named after the oak trees outside Gosling’s office), was intended to replace C++, although the feature set better resembles that of Objective C. Java should not be confused with JavaScript, which shares only the name and a similar C-like syntax. Sun Microsystems currently maintains and updates Java regularly.

Can you have virtual functions in Java?

Yes, all functions in Java are virtual by default. This is actually a pseudo trick question because the word “virtual” is not part of the naming convention in Java (as it is in C++, C-sharp and VB.NET),

so this would be a foreign concept for someone who has only coded in Java. Virtual functions or virtual methods are functions or methods that will be redefined in derived classes.

What is a transient variable in Java?

A transient variable is a variable that may not be serialized. If you don’t want some field to be serialized, you can mark that field transient or static.

What is thread?

A thread is an independent path of execution in a system.

What is multi-threading?

Multi-threading means various threads that run in a system.

What is a dangling pointer?

pointers that do not point to a valid object of the appropriate type. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, as silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults (*NIX) or general protection faults (Windows).

What are the advantages and disadvantages of B-star trees over Binary trees.?

The major difference between B-tree and binary tres is that B-tree is a external data structure and binary tree is a main memory data structure. The computational complexity of binary tree is counted by the number of comparison operations at each node, while the computational complexity of B-tree is determined by the disk I/O, that is, the number of node that will be loaded from disk to main memory. The comparision of the different values in one node is not counted.

What will be printed as the result of the operation below:

int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
Answer : 5794

What will be printed as the result of the operation below:

int x=5;
printf(“%d,%d,%dn”,x,x< <2,x>>2);}

Answer: 5,20,1

What will be printed as the result of the operation below:

#define swap(a,b) a=a+b;b=a-b;a=a-b;void main()
int x=5, y=10;
swap (x,y);
printf(“%d %dn”,x,y);
printf(“%d %dn”,x,y);

int swap2(int a, int b)
int temp;
return 0;
Answer: 10, 5
10, 5

what you know about c++?

Released in 1985, C++ is an object-oriented programming language created by Bjarne Stroustrup. C++ maintains almost all aspects of the C language, while simplifying memory management and adding several features – including a new datatype known as a class (you will learn more about these later) – to allow object-oriented programming. C++ maintains the features of C which allowed for low-level memory access but also gives the programmer new tools to simplify memory management.

C++ used for:
C++ is a powerful general-purpose programming language. It can be used to create small programs or large applications. It can be used to make CGI scripts or console-only DOS programs. C++ allows you to create programs to do almost anything you need to do. The creator of C++, Bjarne Stroustrup, has put together a partial list of applications written in C++.

What is an object?

Object is a software bundle of variables and related methods. Objects have state and behavior.

What do you mean by inheritance?

Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.

What is polymorphism?

Polymorphism is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.

What is a scope resolution operator?

A scope resolution operator (::), can be used to define the member functions of a class outside the class.

Anything wrong with this code?

T *p = new T[10];
delete p;
Everything is correct, Only the first element of the array will be deleted”, The entire array will be deleted, but only the first element destructor will be called.

What is Boyce Codd Normal form?

A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a-> , where a and b is a subset of R, at least one of the following holds:
* a- > b is a trivial functional dependency (b is a subset of a)
* a is a superkey for schema R

What is virtual class and friend class?

Friend classes are used when two or more classes are designed to work together and need access to each other’s implementation in ways that the rest of the world shouldn’t be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.

How do you find out if a linked-list has an end? (i.e. the list is not a cycle)

You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower.
If that is the case, then you will know the linked-list is a cycle.

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.

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.

Operator overloading allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn’t add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).

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

What are the advantages of inheritance?

It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.

How do you write a function that can reverse a linked-list?

void reverselist(void)
head->next = 0;
tail->next = head;
node* pre = head;
node* cur = head->next;
node* curnext = cur->next;
head->next = 0;
cur-> next = head;

for(; curnext!=0; )
cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
curnext->next = cur;

What do you mean by inline function?

The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application’s performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.

Write a program that ask for user input from 5 to 9 then calculate the average

#include “iostream.h”
int main() {
int MAX = 4;
int total = 0;
int average;
int numb;
for (int i=0; icout << “Please enter your input between 5 and 9: “;
cin >> numb;
while ( numb<5>9) {
cout << “Invalid input, please re-enter: “;
cin >> numb;
total = total + numb;
average = total/MAX;
cout << “The average number is: ” <<>return 0;

What is public, protected, private?

Public, protected and private are three access specifiers in C++.
Public data members and member functions are accessible outside the class.
Protected data members and member functions are only available to derived classes.
Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.

What is scope & storage allocation of global and extern variables? Explain with an example

Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project.

The scope of the extern variables is Global.

Global variables: are variables which are declared above the main( ) function. These variables are accessible throughout the program. They can be accessed by all the functions in the program. Their default value is zero.

What is scope & storage allocation of static, local and register variables? Explain with an example.

Register variables: belong to the register storage class and are stored in the CPU registers. The scope of the register variables is local to the block in which the variables are defined. The variables which are used for more number of times in a program are declared as register variables for faster access

Example: loop counter variables.
register int y=6;
Static variables: Memory is allocated at the beginning of the program execution and it is reallocated only after the program terminates. The scope of the static variables is local to the block in which the variables are defined.

void decrement(){
static int a=5;
printf(“Value of a:%d\n”, a);
int main(){
return 0;
Local variables: are variables which are declared within any function or a block. They can be accessed only by function or block in which they are declared. Their default value is a garbage value.

What are the advantages of using unions?

Union is a collection of data items of different data types.
It can hold data of only one member at a time though it has members of different data types.

If a union has two members of different data types, they are allocated the same memory. The memory allocated is equal to maximum size of the members. The data is interpreted in bytes depending on which member is being accessed.


union pen {
char name;
float point;

Here name and point are union members. Out of these two variables, ‘point’ is larger variable which is of float data type and it would need 4 bytes of memory. Therefore 4 bytes space is allocated for both the variables. Both the variables have the same memory location. They are accessed according to their type.

Union is efficient when members of it are not required to be accessed at the same time.

Tell how to check whether a linked list is circular.

Create two pointers, each set to the start of the list. Update each as follows:
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print (\”circular\n\”);

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it’s either 1 or 2 jumps until they meet.

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 don’t 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.

Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.

What are the advantages of inheritance?

It permits code reusability.
• Reusability saves time in program development.
• It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.

Does c++ support multilevel and multiple inheritance?


What is the difference between an ARRAY and a LIST?

Array is collection of homogeneous elements.
List is collection of heterogeneous elements.

For Array memory allocated is static and continuous.
For List memory allocated is dynamic and Random.

Array: User need not have to keep in track of next memory allocation.
List: User has to keep in Track of next location where memory is allocated.

Array uses direct access of stored members, list uses sequencial access for members.

What is a template?

Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones:

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.
Class: Class is a successor of Structure. By default all the members inside the class are private.

What is encapsulation?

Packaging an object’s variables within its methods is called encapsulation.

What is a COPY CONSTRUCTOR and when is it called?

A copy constructor is a method that accepts an object of the same class and copies it’s data members to the object on the left part of assignment:

class Point2D{
int x; int y;
public int color;
protected bool pinned;
public Point2D() : x(0) , y(0) {} //default (no argument) constructor
public Point2D( const Point2D & ) ;
Point2D::Point2D( const Point2D & p )
this->x = p.x;
this->y = p.y;
this->color = p.color;
this->pinned = p.pinned;
Point2D MyPoint;
MyPoint.color = 345;
Point2D AnotherPoint = Point2D( MyPoint ); // now AnotherPoint has color = 345

Program: To calculate the factorial value using recursion.

Program: To calculate the factorial value using recursion.

int fact(int n);
int main() {
int x, i;
printf(“Enter a value for x: \n”);
scanf(“%d”, &x);
i = fact(x);
printf(“\nFactorial of %d is %d”, x, i);
return 0;

int fact(int n) {
/* n=0 indicates a terminating condition */
if (n <= 0) {
return (1);
} else {
/* function calling itself */
return (n * fact(n – 1));
/*n*fact(n-1) is a recursive expression */

Enter a value for x:4

Factorial of 4 is 24

swap 2 numbers without using third variable?

void main()
int a,b;
printf(“enter number1: ie a”);
printf(“enter number2:ie b “);
printf(value of a and b before swapping is a=%d,b=%d”a,b);
printf(value of a and b after swapping is a=%d,b=%d”a,b);

Write a C++ Program to check whether a number is prime number or not?

void main()
int n,i,flag=1;
cout<<“Enter any number:”;

cout<<“\n”<<n<<” is=”” a=”” prime=”” number”;<br=””> else
cout<<“\n”<<n<<” is=”” not=”” a=”” prime=”” number”;<br=””> getch();

Write a program in c to replace any vowel in a string with z?

// Replace all vowels in str with ‘z’
void replaceWithZ(char* str) {
int i = 0;
while(str[i] != ‘z’) {
if(isVowel(str[i])) {
str[i] = ‘z’;
// Returns 1 if ch is a vowel, 0 otherwise
int isVowel(const char ch) {
switch(ch) {
case ‘a’:case ‘A’:
case ‘e’:case ‘E’:
case ‘i’:case ‘I’:
case ‘o’:case ‘O’:
case ‘u’:case ‘U’:
return 1;
return 0;
// Sample call
int main() {
char str[] = “HELLO”;
printf(“%s\n”, str);
printf(“%s\n”, str);
return 0;

You May Also Like :