0% found this document useful (0 votes)
137 views22 pages

Basic Interview Questions On OOPS

OOPs is an abbreviation for Object Oriented Programming System where programs are made up of objects. Some widely used OOP languages include Python, Java, C++, C#, and Ruby. The main features of OOPs are inheritance, encapsulation, polymorphism, and data abstraction. OOPs is popular because it models real-world entities and allows for code reusability. Classes are blueprints for objects that consume memory at runtime.

Uploaded by

Thirumal Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views22 pages

Basic Interview Questions On OOPS

OOPs is an abbreviation for Object Oriented Programming System where programs are made up of objects. Some widely used OOP languages include Python, Java, C++, C#, and Ruby. The main features of OOPs are inheritance, encapsulation, polymorphism, and data abstraction. OOPs is popular because it models real-world entities and allows for code reusability. Classes are blueprints for objects that consume memory at runtime.

Uploaded by

Thirumal Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

OOPs

1) What is OOPS?

OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection
of objects. 

2) Name any seven widely used OOP languages.

There are various OOP languages but the most widely used are:

o Python
o Java
o Go
o Dart
o C++
o C#
o Ruby

3) What is the purpose of using OOPs concepts?

The aim of OOP is to implement real-world entities like inheritance, hiding, polymorphism in programming. The
main purpose of OOP is to bind together the data and the functions that operate on them so that no other part of
the code can access this data except that function.

4) What are the four main features of OOPs?

The OOP has the following four features:

o Inheritance
o Encapsulation
o Polymorphism
o Data Abstraction

5) Why OOP is so popular?

OOPs, programming paradigm is considered as a better style of programming. Not only it helps in writing a
complex piece of code easily, but it also allows users to handle and maintain them easily as well. Not only that,
the main pillar of OOPs - Data Abstraction, Encapsulation, Inheritance, and Polymorphism, makes it easy for
programmers to solve complex scenarios. As a result of these, OOPs is so popular.

6) What are the advantages and disadvantages of OOP?

Advantages of OOP

o It follows a bottom-up approach.


o It models the real word well.
o It allows us the reusability of code.
o Avoids unnecessary data exposure to the user by using the abstraction.
o OOP forces the designers to have a long and extensive design phase that results in better design and fewer
flaws.
o Decompose a complex problem into smaller chunks.
o Programmer are able to reach their goals faster.
o Minimizes the complexity.
o Easy redesign and extension of code that does not affect the other functionality.

Disadvantages of OOP

o Proper planning is required.


o Program design is tricky.
o Programmer should be well skilled.
o Classes tend to be overly generalized.

7) What are the limitations of OOPs?


o Requires intensive testing processes.
o Solving problems takes more time as compared to Procedure Oriented Programming.
o The size of the programs created using this approach may become larger than the programs written using
the procedure-oriented programming approach.
o Software developed using this approach requires a substantial amount of pre-work and planning.
o OOP code is difficult to understand if you do not have the corresponding class documentation.
o In certain scenarios, these programs can consume a large amount of memory.
o Not suitable for small problems.
o Takes more time to solve problems.

 What is the difference between OOP and SOP?

Object-Oriented Programming Structural Programming

Object-Oriented Programming is a type of


Provides logical structure to a program where programs
programming which is based on objects rather than just
are divided functions
functions and procedures

Bottom-up approach Top-down approach

Provides data hiding Does not provide data hiding

Can solve problems of any complexity Can solve moderate problems

Code can be reused thereby reducing redundancy Does not support code reusability

9) What do you understand by pure object-oriented language? Why Java is not a pure object-oriented
programming language?
The programming language is called pure object-oriented language that treats everything inside the program as
an object. The primitive types are not supported by the pure OOPs language. There are some other features that
must satisfy by a pure object-oriented language:

o Encapsulation
o Inheritance
o Polymorphism
o Abstraction
o All predefined types are objects
o All user-defined types are objects
o All operations performed on objects must be only through methods exposed to the objects.

Java is not a pure object-oriented programming language because pre-defined data types in Java are not
treated as objects. Hence, it is not an object-oriented language.

10) What do you understand by class and object? Also, give example.

Class: A class is a blueprint or template of an object. It is a user-defined data type. Inside a class, we define
variables, constants, member functions, and other functionality. It does not consume memory at run time. Note
that classes are not considered as a data structure. It is a logical entity. It is the best example of data binding.

Object: An object is a real-world entity that has attributes, behavior, and properties. It is referred to as an
instance of the class. It contains member functions, variables that we have defined in the class. It occupies space
in the memory. Different objects have different states or attributes, and behaviors.

11) What are the differences between class and object?

Class Object

It is a logical entity. It is a real-world entity.

It is conceptual. It is real.

It binds data and methods together into a single It is just like a variable of a class.
unit.

It does not occupy space in the memory. It occupies space in the memory.

It is a data type that represents the blueprint of It is an instance of the class.


an object.

It is declared once. Multiple objects can be declared as and when


required.

It uses the keyword class when declared. It uses the new keyword to create an object.

A class can exist without any object. Objects cannot exist without a class

12) What are the key differences between class and structure?

Class Structure
Class is a group of common objects that shares The structure is a collection of different
common properties. data types.

It deals with data members and member functions. It deals with data members only.

It supports inheritance. It does not support inheritance.

Member variables cannot be initialized directly. Member variables can be initialized


directly.

It is of type reference. It is of a type value.

It's members are private by default. It's members are public by default.

The keyword class defines a class. The keyword struct defines a structure.

An instance of a class is an object. An instance of a structure is a structure


variable.

Useful while dealing with the complex data Useful while dealing with the small data
structure. structure.

8. Can you call the base class method without creating an instance?

Yes, you can call the base class without instantiating it if:

 It is a static method
 The base class is inherited by some other subclass

 28) What are the access modifiers?

Access modifiers determine the scope of the method or variables that can be accessed from other various objects
or classes. There are five types of access modifiers, and they are as follows:

 Private
 Protected
 Public
 Friend
 Protected Friend

Accessibility from Accessibility from Accessibility from


Name
own class derived class world
Public Yes Yes Yes
Private Yes No No 14) What are the
Protected Yes Yes No manipulators in
OOP and how it
works?

Manipulators are helping functions. It is used to manipulate or modify the input or output stream. The
modification is possible by using the insertion (<<) and extraction (>>) operators

There are two types of manipulators with arguments or without arguments.

The example of manipulators that do not have arguments is endl, ws, flush, etc. Manipulators with arguments
are setw(val), setfill(c), setbase(val), setiosflags(flag). 
36. What is a constructor?

A constructor is a special type of method that has the same name as the class and is used to initialize objects of
that class.

Rules for constructor are:

 Constructor Name should be the same as a class name.


 A constructor must have no return type.

37. What is a destructor?

A destructor is a method that is automatically invoked when an object is destroyed. 

38. Types of constructors

 Default constructor
 Parameterized constructor
 Copy constructor
 Static constructor
 Private constructor

 Default Constructor- A constructor with no parameters is known as a default constructor. 

 Parameterized Constructor- In C++, a constructor with parameters is known as a parameterized

constructor. This is the preferred method to initialize member data.

 Copy Constructor- The copy constructor in C++ is used to copy data of one object to another.
 40. What is the use of ‘finalize’?
Finalize as an object method used to free up unmanaged resources and cleanup before Garbage
Collection(GC). It performs memory management tasks.

 41. What is Garbage Collection(GC)?


GC is an implementation of automatic memory management. The Garbage collector frees up space
occupied by objects that are no longer in existence.

10. What is inheritance?

Inheritance is a feature of OOPs which allows classes inherit common properties from other classes. This
property helps you get rid of redundant code thereby reducing the overall size of the code.

11. What are the different types of inheritance?

 Single inheritance
 Multiple inheritance
 Multilevel inheritance
 Hierarchical inheritance
 Hybrid inheritance

12. What is the difference between multiple and multilevel inheritance?

Multiple Inheritance Multilevel Inheritance


Multilevel inheritance means a class inherits from
Multiple inheritance comes into picture when a class
another class which itself is a subclass of some other
inherits more than one base class
base class
Example: A class describing a sports car will inherit
Example: A class defining a child inherits from two
from a base class Car which inturn inherits another
base classes Mother and Father
class Vehicle
13. What is hybrid inheritance?

Hybrid inheritance is a combination of multiple and multi-level inheritance.

14. What is hierarchical inheritance?

Hierarchical inheritance refers to inheritance where one base class has more than one subclasses. For example,
the vehicle class can have ‘car’, ‘bike’, etc as its subclasses.

15. What are the limitations of inheritance?

 Increases the time and effort required to execute a program as it requires jumping back and forth between
different classes
 The parent class and the child class get tightly coupled
 Any modifications to the program would require changes both in the parent as well as the child class
 Needs careful implementation else would lead to incorrect results

16. What is a superclass?


 A superclass or base class is a class that acts as a parent to some other class or classes. For example, the
Vehicle class is a superclass of class Car.

17. What is a subclass?


 A class that inherits from another class is called the subclass. For example, the class Car is a subclass or a
derived of Vehicle class.

What is Polymorphism?
 Polymorphism is nothing but assigning behavior or value in a subclass to something that was already
declared in the main class. Simply, polymorphism takes more than one form.

What is static polymorphism?

Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time. An example of
compile-time polymorphism is method overloading.

20. What is dynamic polymorphism?

Runtime polymorphism or dynamic polymorphism (dynamic binding) is a type of polymorphism which is


resolved during runtime. An example of runtime polymorphism is method overriding.

 What is method overloading?

If a class has multiple methods having same name but different in parameters, it is known as Method
Overloading.

 What is method overriding

If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding

23. What is operator overloading?

Operator overloading refers to implementing operators using user-defined types based on the arguments passed
along with it.
24. Differentiate between overloading and overriding.

Overloading Overriding
Child class redefining methods present in the
Two or more methods having the same name
base class with the same parameters/
but different parameters or signature
signature
Resolved during compile-time Resolved during runtime
25. What is encapsulation?

Encapsulation refers to binding the data and the code that works on that together in a single unit. For example, a
class. Encapsulation also allows data-hiding as the data specified in one class is hidden from other classes.

28. What is data abstraction?

Data abstraction is a very important feature of OOPs that allows displaying only the important information and
hiding the implementation details.

29. How to achieve data abstraction?

Data abstraction can be achieved through:

 Abstract class
 Abstract method

30. What is an abstract class?

An abstract class is a class that consists of abstract methods. These methods are basically declared but not
defined. If these methods are to be used in some subclass, they need to be exclusively defined in the subclass.

31. Can you create an instance of an abstract class?

No. Instances of an abstract class cannot be created because it does not have a complete implementation.
However, instances of subclass inheriting the abstract class can be created

33. Differentiate between data abstraction and encapsulation. 

Data abstraction Encapsulation


Solves the problem at the implementation
Solves the problem at the design level
level
Allows showing important aspects while Binds code and data together into a single
hiding implementation details unit and hides it from the world

34. What are virtual functions?

Virtual functions are functions that are present in the parent class and are overridden by the subclass. These
functions are used to achieve runtime polymorphism.

35. What are pure virtual functions?

Pure virtual functions or abstract functions are functions that are only declared in the base class. This means that
they do not contain any definition in the base class and need to be redefined in the subclass.

20) What is the super keyword?


The super keyword is used to invoke the overridden method, which overrides one of its superclass methods. This
keyword allows to access overridden methods and also to access hidden members of the superclass

22) What is an interface?

An interface is a collection of an abstract method. If the class implements an interface, it thereby inherits all the
abstract methods of an interface

friend Function

A friend function can access the private and protected data of a class. We declare a friend function using


the friend keyword inside the body of the class.

Virtual Functions: A virtual function is a member function which is declared within a base class and is re-

defined(Overriden) by a derived class.

46. What is exception handling?

Exception handling in Object-Oriented Programming is a very important concept that is used to manage errors.
An exception handler allows errors to be thrown and caught and implements a centralized mechanism to resolve
them.

47. What is the difference between an error and an exception?

Error Exception
Errors are problems that should not be encountered by
Conditions that an application might try to catch
applications
48. What is a try/ catch block?

A try/ catch block is used to handle exceptions. The try block defines a set of statements that may lead to an
error. The catch block basically catches the exception.

49. What is a finally block?

A finally block consists of code that is used to execute important code such as closing a connection, etc. This
block executes when the try block exits. It also makes sure that finally block executes even in case some
unexpected exception is encountered.

19) What are the different types of arguments?

A parameter is a variable used during the declaration of the function or subroutine, and arguments are passed to
the function body, and it should match with the parameter defined. There are two types of Arguments.

 Call by Value – Value passed will get modified only inside the function, and it returns the same value
whatever it is passed into the function.
 Call by Reference – Value passed will get modified in both inside and outside the functions and it returns
the same or different value.

38) What are all the operators that cannot be overloaded?

Following are the operators that cannot be overloaded -.

1. Scope Resolution (::)


2. Member Selection (.)
3. Member selection through a pointer to function (.*)

What is ‘this’ pointer?


THIS pointer refers to the current object of a class. THIS keyword is used as a pointer which
differentiates between the current object with the global object. It refers to the current object.

Tokens

A token is the smallest element of a program that is meaningful to the compiler. Tokens can be classified as
follows:

1. Keywords

2. Identifiers

3. Constants

4. Strings

5. Special Symbols

6. Operators

What are a base class, subclass, and superclass?

The base class is the most generalized class, and it is said to be a root class.

A Subclass is a class that inherits from one or more base classes.

The superclass is the parent class from which another class inherits.

What is static and dynamic Binding?

Binding is nothing but the association of a name with the class. Static Binding is a binding in which name
can be associated with the class during compilation time, and it is also called as early Binding.

Dynamic Binding is a binding in which name can be associated with the class during execution time, and
it is also called as Late Binding.

What is an Inline function?

An inline function is a technique used by the compilers and instructs to insert complete body of the
function wherever that function is used in the program source code.

11) What is the difference between call by value and call by reference in C?

Call by value Call by reference

Description When a copy of the value is passed to When a copy of the value is passed to
the function, then the original value is the function, then the original value is
not modified. modified.

Memory Actual arguments and formal Actual arguments and formal


location arguments are created in separate arguments are created in the same
memory locations. memory location.

Safety In this case, actual arguments remain In this case, actual arguments are not
safe as they cannot be modified. reliable, as they are modified.

Arguments The copies of the actual arguments are The addresses of actual arguments are
passed to the formal arguments. passed to their respective formal
arguments.

What is the difference between malloc() and calloc()?

calloc() malloc()

Description The malloc() function allocates a The calloc() function allocates


single block of requested memory. multiple blocks of requested memory.

Initialization It initializes the content of the It does not initialize the content of
memory to zero. memory, so it carries the garbage
value.

Number of It consists of two arguments. It consists of only one argument.


arguments

Return value It returns a pointer pointing to the It returns a pointer pointing to the
allocated memory. allocated memory.

Why is C known as a mother language?


It introduces new core concepts like arrays, functions, file handling which are used in these languages.

C++ Data Types

3) What is the difference between C and C++?

C C++
C language was developed by Dennis C++ language was developed by Bjarne
Ritchie. Stroustrup.

C is a structured programming language. C++ supports both structural and object-


oriented programming language.

C is a subset of C++. C++ is a superset of C.

In C language, data and functions are the In the C++ language, both data and functions
free entities. are encapsulated together in the form of a
project.

C does not support the data hiding. C++ supports data hiding. Therefore, the data
Therefore, the data can be used by the cannot be accessed by the outside world.
outside world.

C supports neither function nor operator C++ supports both function and operator
overloading. overloading.

In C, the function cannot be implemented In the C++, the function can be implemented
inside the structures. inside the structures.

Reference variables are not supported in C C++ supports the reference variables.
language.

C language does not support the virtual and C++ supports both virtual and friend functions.
friend functions.

In C, scanf() and printf() are mainly used C++ mainly uses stream cin and cout to
for input/output. perform input and output operations.

Sr.No. Header Files & Description

1 stdio.h
Input/Output functions

2 conio.h
Console Input/Output functions

3 stdlib.h
General utility functions

4 math.h
Mathematics functions

5 string.h
String functions

6 ctype.h
Sr.No. Header Files & Description

Character handling functions

Why is C still so popular?One of the very strong reasons why C programming language is so popular and used
so widely is the flexibility of its use for memory management. Programmers have opportunities to control how,
when, and where to allocate and deallocate memory. Memory is allocated statically, automatically, or
dynamically in C programming with the help of malloc and calloc functions.

 Bitwise Operators

Operator Description

& Bitwise AND Operator

| Bitwise OR Operator

^ Bitwise XOR Operator

~ Bitwise Complement Operator

<< Bitwise Shift Left Operator

>> Bitwise Shift Right Operator

AND Operator

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

//Bitwise AND Operation of 12 and 25

00001100

& 00011001

00001000 = 8 (In decimal)

OR Operator

12 = 00001100 (In Binary) 25 = 00011001 (In Binary)


Bitwise OR Operation of 12 and 25

00001100

00011001

00011101 = 29 (In decimal)

 Bitwise XOR Operator

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25

00001100

^ 00011001

_________

00010101 = 21 (In decimal)

Bitwise Complement Operator

35 = 00100011 (In Binary)

// Using bitwise complement operator

~ 00100011

__________

11011100

2's Complement

36 = 00100100 (In Binary)

1's Complement = 11011011

2's Complement :
11011011

+ 1

_________

11011100

Shift Operators

There are two shift operators in C++ programming:

 Right shift operator >>


 Left shift operator <<
Right Shift Operator

 The right shift operator shifts all bits towards the right by a certain number of specified bits. It is
denoted by >>.
 When we shift any number to the right, the least significant bits are discarded, while the most
significant bits are replaced by zeroes.

 One bit Right Shif

Left Shift Operator

 The left shift operator shifts all bits towards the left by a certain number of specified bits. It is denoted
by <<.

 One bit Left Shift


1) What is C language?

C is a mid-level and procedural programming language. The Procedural programming language is also known
as the structured programming language is a technique in which large programs are broken down into smaller
modules, and each module uses structured code. This technique minimizes error and misinterpretation.

2) Why is C known as a mother language?

C is known as a mother language because most of the compilers and JVMs are written in C language. Most of
the languages which are developed after C language has borrowed heavily from it like C++, Python, Rust,
javascript, etc. It introduces new core concepts like arrays, functions, file handling which are used in these
languages.

3) Why is C called a mid-level programming language?

C is called a mid-level programming language because it binds the low level and high -level programming
language.

6) What are the features of the C language?

The main features of C language are given below:

o Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into
parts
o Portable: C is highly portable means that once the program is written can be run on any machine with
little or no modifications.
o Mid Level: C is a mid-level programming language as it combines the low- level language with the
features of the high-level language.
o Structured: C is a structured language as the C program is broken into parts.
o Fast Speed: C language is very fast as it uses a powerful set of data types and operators.
o Memory Management: C provides an inbuilt memory function that saves the memory and improves the
efficiency of our program.
o Extensible: C is an extensible language as it can adopt new features in the future.

7) What is the use of printf() and scanf() functions?

printf(): The printf() function is used to print the integer, character, float and string values on to the screen.

Following are the format specifier:

o %d: It is a format specifier used to print an integer value.


o %s: It is a format specifier used to print a string.
o %c: It is a format specifier used to display a character value.
o %f: It is a format specifier used to display a floating point value.

scanf(): The scanf() function is used to take input from the user.

8) What is the difference between the local variable and global variable in C?
Following are the differences between a local variable and global variable:

Basis for Local variable Global variable


comparison

Declaration A variable which is declared inside A variable which is declared


function or block is known as a local outside function or block is known
variable. as a global variable.

Scope The scope of a variable is available The scope of a variable is available


within a function in which they are throughout the program.
declared.

Access Variables can be accessed only by Any statement in the entire


those statements inside a function in program can access variables.
which they are declared.

Life Life of a variable is created when the Life of a variable exists until the
function block is entered and program is executing.
destroyed on its exit.

Storage Variables are stored in a stack unless The compiler decides the storage
specified. location of a variable.

10) What is the use of the function in C?

Uses of C function are:

o C functions are used to avoid the rewriting the same code again and again in our program.
o C functions can be called any number of times from any place of our program.
o When a program is divided into functions, then any part of our program can easily be tracked.
o C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes
the C program more understandable.

11) What is the difference between call by value and call by reference in C?

Following are the differences between a call by value and call by reference are:

Call by value Call by reference

Description When a copy of the value is passed to When a copy of the value is passed to
the function, then the original value is the function, then the original value is
not modified. modified.

Memory Actual arguments and formal arguments Actual arguments and formal
location are created in separate memory arguments are created in the same
locations. memory location.

Safety In this case, actual arguments remain In this case, actual arguments are not
safe as they cannot be modified. reliable, as they are modified.
Arguments The copies of the actual arguments are The addresses of actual arguments are
passed to the formal arguments. passed to their respective formal
arguments.

12) What is recursion in C?

When a function calls itself, and this process is known as recursion. The function that calls itself is known as a
recursive function.

13) What is an array in C?

An Array is a group of similar types of elements. It has a contiguous memory location. It makes the code
optimized, easy to traverse and easy to sort. The size and type of arrays cannot be changed after its declaration.

Arrays are of two types:

o One-dimensional array: One-dimensional array is an array that stores the elements one after the
another.
o Multidimensional array: Multidimensional array is an array that contains more than one array.

14) What is a pointer in C?

o A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the
performance fast. Whenever a variable is declared inside a program, then the system allocates some
memory to a variable. The memory contains some address number. The variables that hold this address
number is known as the pointer variable.

15) What is the usage of the pointer in C?

o Accessing array elements: Pointers are used in traversing through an array of integers and strings. The
string is an array of characters which is terminated by a null character '\0'.
o Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during the
execution of a program.
o Call by Reference: The pointers are used to pass a reference of a variable to other function.
o Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data
structures like tree, graph, linked list, etc.

16) What is a NULL pointer in C?

A pointer that doesn't refer to any address of value but NULL is known as a NULL pointer. When we assign a '0'
value to a pointer of any type, then it becomes a Null pointer.

17) What is a far pointer in C?

A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer. A far
pointer is a 32-bit pointer that obtains information outside the memory in a given section.
18) What is dangling pointer in C?

o If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory
occupied by the first pointer while the first pointer still points to that memory location, the first pointer
will be known as a dangling pointer. This problem is known as a dangling pointer problem.
o Dangling pointer arises when an object is deleted without modifying the value of the pointer. The
pointer points to the deallocated memory.

Let's see this through an example.

1. #include<stdio.h>  
2. void main()  
3. {  
4.         int *ptr = malloc(constant value); //allocating a memory space.  
5.         free(ptr); //ptr becomes a dangling pointer.  
6. }  

In the above example, initially memory is allocated to the pointer variable ptr, and then the memory is
deallocated from the pointer variable. Now, pointer variable, i.e., ptr becomes a dangling pointer.

How to overcome the problem of a dangling pointer

The problem of a dangling pointer can be overcome by assigning a NULL value to the dangling pointer. Let's
understand this through an example:

1. #include<stdio.h>  
2.       void main()  
3.       {  
4.               int *ptr = malloc(constant value); //allocating a memory space.  
5.               free(ptr); //ptr becomes a dangling pointer.  
6.               ptr=NULL; //Now, ptr is no longer a dangling pointer.  
7.       }  

In the above example, after deallocating the memory from a pointer variable, ptr is assigned to a NULL value.
This means that ptr does not point to any memory location. Therefore, it is no longer a dangling pointer.

19) What is pointer to pointer in C?

In case of a pointer to pointer concept, one pointer refers to the address of another pointer. The pointer to
pointer is a chain of pointers. Generally, the pointer contains the address of a variable. The pointer to pointer
contains the address of a first pointer. Let's understand this concept through an example:

1. #include <stdio.h>  
2.  int main()  
3. {  
4.     int a=10;  
5.     int *ptr,**pptr; // *ptr is a pointer and **pptr is a double pointer.  
6.     ptr=&a;  
7.     pptr=&ptr;  
8.     printf("value of a is:%d",a);  
9.     printf("\n");  
10.     printf("value of *ptr is : %d",*ptr);  
11.     printf("\n");  
12.     printf("value of **pptr is : %d",**pptr);  
13.     return 0;  
14. }  

In the above example, pptr is a double pointer pointing to the address of the ptr variable and ptr points to the
address of 'a' variable.

20) What is static memory allocation?

o In case of static memory allocation, memory is allocated at compile time, and memory can't be increased
while executing the program. It is used in the array.
o The lifetime of a variable in static memory is the lifetime of a program.
o The static memory is allocated using static keyword.
o The static memory is implemented using stacks or heap.
o The pointer is required to access the variable present in the static memory.
o The static memory is faster than dynamic memory.
o In static memory, more memory space is required to store the variable.

1. For example:  
2. int a[10];  

The above example creates an array of integer type, and the size of an array is fixed, i.e., 10.

21) What is dynamic memory allocation?

o In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased
while executing the program. It is used in the linked list.
o The malloc() or calloc() function is required to allocate the memory at the runtime.
o An allocation or deallocation of memory is done at the execution time of a program.
o No dynamic pointers are required to access the memory.
o The dynamic memory is implemented using data segments.
o Less memory space is required to store the variable.

1. For example  
2. int *p= malloc(sizeof(int)*10);  

The above example allocates the memory at runtime.


22) What functions are used for dynamic memory allocation in C language?

1. malloc()
o The malloc() function is used to allocate the memory during the execution of the program.
o It does not initialize the memory but carries the garbage value.
o It returns a null pointer if it could not be able to allocate the requested space.

Syntax

1. ptr = (cast-type*) malloc(byte-size) // allocating the memory using malloc() function.  

2. calloc()

1. The calloc() is same as malloc() function, but the difference only is that it initializes the memory
with zero value.

Syntax

1. ptr = (cast-type*)calloc(n, element-size);// allocating the memory using calloc() function.  

2. realloc()

1. The realloc() function is used to reallocate the memory to the new size.
2. If sufficient space is not available in the memory, then the new block is allocated to accommodate
the existing data.

Syntax

1. ptr = realloc(ptr, newsize); // updating the memory size using realloc() function.  

In the above syntax, ptr is allocated to a new size.

2. free():The free() function releases the memory allocated by either calloc() or malloc() function.

Syntax

1. free(ptr); // memory is released using free() function.  

The above syntax releases the memory from a pointer variable ptr.

More details.

23) What is the difference between malloc() and calloc()?

calloc() malloc()

Description The malloc() function allocates a The calloc() function allocates


single block of requested memory. multiple blocks of requested memory.
Initialization It initializes the content of the It does not initialize the content of
memory to zero. memory, so it carries the garbage
value.

Number of It consists of two arguments. It consists of only one argument.


arguments

Return value It returns a pointer pointing to the It returns a pointer pointing to the
allocated memory. allocated memory.

24) What is the structure?

o The structure is a user-defined data type that allows storing multiple types of data in a single unit. It
occupies the sum of the memory of all members.
o The structure members can be accessed only through structure variables.
o Structure variables accessing the same structure but the memory allocated for each variable will be
different.

25) What is a union?

o The union is a user-defined data type that allows storing multiple types of data in a single unit. However,
it doesn't occupy the sum of the memory of all members. It holds the memory of the largest member
only.
o In union, we can access only one variable at a time as it allocates one common space for all the members
of a union.

29) What is a token?

The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in
a program. C has the following tokens:

1. Identifiers: Identifiers refer to the name of the variables.


2. Keywords: Keywords are the predefined words that are explained by the compiler.
3. Constants: Constants are the fixed values that cannot be changed during the execution of a program.
4. Operators: An operator is a symbol that performs the particular operation.
5. Special characters: All the characters except alphabets and digits are treated as special characters.

30) What is command line argument?

6. The argument passed to the main() function while executing the program is known as command line
argument. For example:

36) What is the maximum length of an identifier?

It is 32 characters ideally but implementation specific.

37) What is typecasting?


The typecasting is a process of converting one data type into another is known as typecasting. If we want to
store the floating type value to an int type, then we will convert the data type into another data type explicitly.

Syntax

(type_name) expression;  

39) Can we access the array using a pointer in C language?

Yes, by holding the base address of array into a pointer, we can access the array using a pointer.

40) What is an infinite loop?

A loop running continuously for an indefinite number of times is called the infinite loop.

Infinite For Loop:

1. for(;;){  
2. //code to be executed  
3. }  

Infinite While Loop:

1. while(1){  
2. //code to be executed  
3. }  

Infinite Do-While Loop:

1. do{  
2. //code to be executed  
3. }while(1);  

You might also like