[go: up one dir, main page]

0% found this document useful (0 votes)
16 views4 pages

Viva Questions

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

IMPORTANT NOTES FOR VIVA

INLINE FUNCTION in an expression is known as type


Functions save memory space because all promotion. It is done by the compiler.
the calls to the function cause the same Example : int a = 5; float b = 10.5;
code to be executed. The functions body float c = a + b;
need not be duplicated in memory. When In the above expression first integer ‘a’ will
the complier sees a function call, it be converted to float and then addition will
normally jumps to the function. At the end be carried out.
of the function, it normally jumps back to Type Casting : Conversion of one data
the statement following the call. type into another by the programmer is
While the sequence of events may save called type casting. It can be from lower to
memory space, it takes some extra time. upper or vice versa.
To save execution time in short Example : float x = 10.5;
functions, inline function is used. Each cout<<(int)x;
time there is a function call, the actual
code from the function is inserted Difference between #define and const
instead of a jump to the function. The #define: It is a preprocessor directive in
inline function is used only for shorter C++ for creating a Macro.
code. Example: #define sqr(i) i*i
inline int cube(int r) const: It is an Access Modifier in C++ that
{ assigns a constant (non modifiable) value
return r*r*r; to a variable. Any attempt in modifying the
} value assigned to such a variable is
Some important points to be noted reported as an error by the compiler.
• Function is made inline by putting a word Example: const float Pi = 3.14;
inline in the beginning.
• Inline function should be declared before Typedef: This keyword allows creating
main() function. synonyms or aliases for previously defined
• It does not have function prototype. data Types. The general form of typedef is
• Only shorter code is used in inline typedef old_name new_name;
function If longer code is made inline Example:
then compiler ignores the request and it typedef char STR [80];
will be executed as normal function. OR
Difference between Actual & formal typedef float REAL;
Parameters : The value of the actual
parameters in the calling function do not A preprocessor directive is an instruction
get affected when the arguments are to the complier itself. A part of compiler
passed using call by value method, since called preprocessor deals with these
actual and formal parameters have directives, before real compilation process.
different memory locations. # is used as preprocessor directive in C++.
The values of the formal parameters affect #include: The preprocessor directive
the values of actual parameters in the #include tells the complier to insert
calling function, when the arguments are another file into your source file. In effect,
passed using call by reference method. This #include directive is replaced by the
happens since the formal parameters are contents of the file indicated.
not allocated any memory, but they refer #define: It is a preprocessor directive to
to the memory locations of their define a macro in a C++ program. Macros
corresponding actual parameters provide a mechanism for token
replacement with or without a set of
Type Promotion : Automatic conversion of formal, function line parameters. For
a lower data type to the highest data type example :
#define PIE 3.1416
#define AVG(A,B,C) (A+B+C)/3 Basic concepts of OOP: Data Abstraction,
Data Hiding, Data Encapsulation,
Local variables are those variables which Inheritance and Polymorphism are the
are declared within a function or a basic concepts of OOP
compound statement and these variables
can only be used within that A class binds together data and its
function/scope. They cannot be accessed associated function under one unit thereby
from outside the function or a scope of it's enforcing encapsulation.
declaration. This means that we can have The private and protected member remain
variables with the same names in different hidden from outside world. Thus a class
functions/scope. Local variables are local to enforces data hiding
the function/scope in which they are The outside world is given only the
declared. essential information through public
Global variables are those variables members, thereby enforcing abstraction.
which are declared in the beginning of the
program. They are not declared within a private visibility mode: Members of a
function. So, these variables can be class declared under this visibility are
accessed by any function of the program. accessible only inside the class (in member
So, global variables are global to all the functions of the class). They can not be
functions of the program. accessed outside the class.

An array is a collection of variables of public visibility mode: Members of a


same data type. It is a derived data type. class declared under this visibility are
A structure is a collection of variables accessible inside the class (in member
which can be same or different types. It is functions of the class) as well as by the
a user-defined data type. Objects of that class (in any non member
A class is a collection of variables which function of the program, prototyped /
can be same or different types and defined after the class declaration).
functions. It is a user-defined data type.
Members in structure are public by Polymorphism: It is a method of using
default whereas members in class are the same operator or function (method) to
private by default. This is the only work using different sets of input. Function
difference between a structure and a overloading is one of the example of
class in C++. polymorphism, where more than one
function carrying same name behave
Object Oriented Programming differently with different set of parameters
*Emphasis on data passed to them.
*Follow bottom up approach in program void Display()
design { cout<<”Hello!”<<endl;
*Concept of Data hiding prevents }
accidental change in the data void Display(int N)
*Polymorphism, inheritance, Data { cout<<2*N+5<<endl;}
Encapsulation possible
Procedural Programming Abstract class: A class with no instances
*Emphasis on doing things (function) (no objects) is known as abstract class.
*Follow top-down approach in program Concrete class: A class having objects is
design known as concrete class.
*Due to presence of global variables, there Constructor : It is a member function of
are possibilities of accidental change in class with the following unique features.
data.
• It has same name as the name of the Inheritance is a process of creating new
class they belong to. classes (derived classes) from existing
• It has no return type. classes (base classes). The derived classes
• It is defined in public visibility mode. not only inherit capabilities of the base
• It is automatically called & executed class but also can add new features of own.
when an object is declared/created. The most important aspect of inheritance is
• Constructor can be overloaded. that it allows reusability of code.
Default Constructor-: A constructor that Base Class: A class from which another
accepts no parameters is known as default class inherits.
constructor. Derived Class: A class inheriting
Parameterized Constructor -: A properties from another class.
constructor that receives Types of Inheritance :
arguments/parameters is called
parameterized constructor.
Copy Constructor-: A constructor that
initializes data members of an object with
values of another object of the same class
(passed as parameter) is called copy
constructor. The argument of a copy
constructor is always an object of the same
class and it is passed by reference method
only. It creates the copy of the passed
object
Remember that a copy constructor may be
called
• When an object is defined and initialized
Virtual base class : Multipath inheritance
with another object. may lead to duplication of inherited
• When an object is passed by value. members from a grandparent base class.
• When a function returns an object. This may be avoided by making the
Why the argument to a copy common base class a virtual base class.
constructor is passed by reference? When a class is made a virtual base class,
When an object is passed by value, a copy of it C++ takes necessary care to see that only
is created. To create a copy of the object, copy one copy of that class is inherited.
constructor is called, thus it calls itself. Again
Base Derived class visibility
the called copy constructor requires another
copy of the object so again it is called. In fact it Class Public Private Protected
calls itself again and again until the compiler Visibility derivation derivation derivation
runs out of memory. So, in the copy
constructor, the argument must be passed by Private Not Not Not
reference, to avoid creation of infinite copies of
inherited inherited inherited
the passed object.

A destructor de-initializes an object and Protected Protected Private Protected


de-allocates all allocated resources.
Public Public Private Protected
· Name of the destructor is same as the
name of the class preceded by ~
· No return type required for destructor A text file store information in ASCII
function. characters. In text files, each line of text is
· Destructor functions are called terminated, with a special character known
automatically when the scope of the as EOL character.
object gets over A binary file store information in the same
· Destructor can not be overloaded format in which the information is held in
· Destructor function is defined in public.
memory. In binary file, there is no beforehead. This memory is allocated
delimiter for a line. during run time as and when required.
A stream is a sequence of byte.
ofstream: Stream class to write on files Memory leak is a situation in which there
ifstream: Stream class to read from files lie so many orphaned memory blocks that
fstream: Stream class to both read and are still allocated but no pointers are
write from/to files. referencing to them.

get() does not extract the delimeter The this pointer is an object pointer
newline character from input stream. On which points to the currently calling object,
the other hand getline() does extract the The this pointer is, by default, available to
delimeter newline character from the input each called member function.
stream so that the stream is empty after Full form of LIFO is LAST IN FIRST
getline() is over. OUT. An example of LIFO list is stack.
Stack is a linear data structure in which
The ios::out is the default mode of insertion and deletion of elements takes
ofstream. With the mode of the file does place only one end known as TOP. Stacks
not exist, it gets created but if the file are used
exists then its existing contents get • For converting postfix expression
deleted. The ios::app is output mode of into infix expression & for
ofstream. With the mode of the file does evaluation of postfix expressions
not exist, it gets created but if the file • In function calls, arguments are
exists then its existing contents are stored on stacks
retained and new information is appended
to it. Full form of FIFO is FIRST IN FIRST
OUT. An example of FIFO list is Queue.
Pointer is an address of a memory Queue is a linear data structure in which
location. A variable, which holds an address insertion and deletion of elements takes
of a memory location, is known as a Pointer place from two opposite ends rear and
variable (or Simply Pointer). For example front respectively.Queues are used
int *P; in Printers as a queue of printing jobs
Some arithmetic operators can be used in operating system as queue of processes
with pointers:Increment and decrement
operators ++, -- Two types of searches are there : Linear
Integers can be added to or subtracted search & Binary Search.
from pointers using the operators +, -, +=, Precondition for Binary search to be
and -= performed on a single dimensional array is
Each time a pointer is incremented by 1, it array should be sorted.
points to the memory location of the next Sorting : Arranging data either in
element of its base type. ascending or descending order is known as
If "p" is a character pointer then "p++" will sorting.
increment "p" by 1 byte. Types of sorting : Selection, Insertion &
If "p" were an integer pointer its value on Bubble
"p++" would be incremented by 2 bytes.

In the static memory allocation, the


amount of memory to be allocated is
predicted and preknown. This memory is
allocated during the compilation itself.
In the dynamic memory allocation, the
amount of memory allocated is not known

You might also like