You are on page 1of 5

Q1: What is type conversion?

Answer : When two operands of different data types are encountered in the same expression, the
variable of lower data type is automatically converted to the data types of variable with higher
data type, and then the expression is calculated.

Q2: What is type casting?


Answer : Type casting refers to the data type conversions specified by the programmer, as
opposed to the automatic type conversions. This can be done when the compiler does not do the
conversions automatically. Type casting can be done to higher or lower data type.

Q3: What are the different types of type casting in C++?


Answer : There are four types. static_cast, dynamic_cast, reinterpret_cast and const_cast.

Q4: What is the difference between call by value and call by reference in user defined
functions in C++?
Answer : The value of the actual parameters in the calling function do not get affected when the
arguments are passed using call by value method, since actual and formal parameters have
different memory locations. The values of the formal parameters affect the values of actual
parameters in the calling function, when the arguments are passed using call by reference
method. This happens since the formal parameters are not allocated any memory, but they refer
to the memory locations of their corresponding actual parameter. When the function needs to
return more than one value to the calling function, it may be achieved through call by reference.
Also during passing of objects to the functions, when the size of the object is large, passing by
reference reduces the time required for copying individual members to the formal argument.

Q5. What is the difference between C structures and C++ structures?


Answer: In C structures can contain only data members, whereas in C++ structures can have
both data members and member functions.

Q6: What is preprocessor directive?


Answer : A preprocessor directive is an instruction to the complier itself. A part of compiler
called preprocessor deals with these directives, before real compilation process. # is used as
preprocessor directive in C++.

Q7: What is a function?


Answer: A function is a block of code which executes the statements when we call it.
It consists of three entities:
1) The function name. This is simply a unique identifier.
2) The function parameters. This is a set of zero or more typed identifier.
3) The function return type. This specifies the type of value function returns.

Q8: Define scope and storage class (visibility and lifetime).


Answer : Scope / visibility of the variable determines which part of the program can access it.
Storage class determines how long the variable stays in existence. There are 3 types of scopes:
Local scope, File (Global) Scope, Class scope and there are two types of storage classes:
Automatic and Static.
Local scope means the variable is visible (accessible) only within the block in which it is
declared. File scope means the variable is visible (accessible) throughout the file. Class scope
means the variable is visible (accessible) only within the class in which it is declared.
Automatic storage class variables exist during the lifetime of the function in which they are
declared and static storage class variables exist for the lifetime of the entire program.

Q9: What are the different categories of variables? Discuss about them briefly.
Answer: Automatic or Local variables, External or Global variables, Static Local variables.
Automatic variables are not created until the function or block in which they are defined is
called and they are destroyed when the control return from that function or comes out that block.
They are visible (accessible) only within that function. Value is not initialized automatically.
Global variables are visible (accessible) to all the functions in that program. They are initialized
to 0 automatically. They exist for entire lifetime of the program.
Static local variables have visibility same as automatic variables but have lifetime same as
global variables. Initialization takes place only once at the first time the function is called.

Q10: What is the difference between local and global variables?


Answer : Local variables are those variables which are declared within a function or a
compound statement and these variables can only be used within that function/scope. They
cannot be accessed from outside the function or a scope of it's declaration. This means that we
can have variables with the same names in different functions/scope. Local variables are local to
the function/scope in which they are declared. All declarations within a block / function is by
default belongs to automatic storage class, if not specified explicitly.
Global variables are those variables which are declared in the beginning of the program. They
are not declared within a function. So, these variables can be accessed by any function of the
program. So, global variables are global to all the functions of the program. All variables
declared outside any function are by default belongs to static storage class.

Q11: What is the purpose of declaring a static variable in a function?


Answer : When a variable should retain its value between calls to the same function, it must be
declared as static. Static variables have visibility only within the function in which they are
declared. That is they cannot be accessed by other functions. But lifetime of static variables is
entire program. Hence they are initialized only once, when the function is called for the first time
and retains its value between function calls.

Q12: What are the major differences between Object Oriented Programming and
Procedural Programming?
Answer :
Object Oriented Programming
*Emphasis on data
*Follow bottom up approach in program design
*Concept of Data hiding prevents accidental change in the data
*Polymorphism, inheritance, Data Encapsulation possible

Procedural Programming
*Emphasis on doing things (function)
*Follow top-down approach in program design
*Due to presence of global variables, there are possibilities of accidental change in data

Q13:What are the basic concepts of OOPs?


Answer :Data Abstraction, Data Hiding, Data Encapsulation, Inheritance and Polymorphism are
the basic concepts of OOP.

Q14: What are the characteristics of OOPs? Describe about them.


Answer: Objects and classes, Data abstraction, Data Encapsulation, Data Hiding, Inheritance,
Reusability, Polymorphism and Overloading, Extensibility, Message Passing
Classes – Basic building blocks OOP and a single entity which has data and operations on data
together
Objects – The instances of a class which are used in real functionality – its variables and
operations
Data Abstraction – Specifying what to do but not how to do; a flexible feature for having an
overall view of an object’s functionality.
Encapsulation – Binding data and operations of data together in a single unit – A class adhere
this feature
Inheritance and class hierarchy – Reusability and extension of existing classes
Polymorphism – Multiple definitions for a single name - functions with same name with
different functionality; saves time in investing many function names - Operator and Function
overloading
Extensibility – Ability to create new data types – This is achieved by defining user defined
classes.
Message passing – Objects communicates through invoking methods and sending data to them.
This feature of sending and receiving information among objects through function parameters is
known as Message Passing.

Q15: What are the rules for naming variables (identifiers)?


Answer: Variables can use upper or lower case alphabets, digits 0 to 9, and underscore ( _ ), but
the first character should not be a digit. The maximum number of characters in an identifier is
247. They are case sensitive. Identifiers should not be a C++ keyword.

Q16: Describe about cout and cin.


Answer : The predefined object cout is an instance of ostream class and defined in iostream
header file. The cout object is said to be "connected to" the standard output device, which usually
is the display screen. cout is used to display output in the console. The insertion (<<) operator is
used along with cout to display the output.

Q17:What is class?
Answer : A class can be declared as a collection of data members along with members function
which allows association of data and functions into a single unit called encapsulation.

Q18: What is a constructor? What are its features?


Answer : It is a member function of class with the following unique features.
It has same name as the name of the class they belong to.
It has no return type.
It is defined in public visibility mode.
It is automatically called & executed when an object is declared / created.
Constructor can be overloaded.

Q19: What does a destructor do?


Answer : A destructor deallocates all allocated memory to that object. It has same name as the
name of the class they belong to and preceded with ~ (tilde) symbol. It has no return type and has
no arguments. It is defined in public visibility mode. It is automatically called & executed when
an object is destroyed (that is when the object’s life time is over). Destructor cannot be
overloaded.

Q20: Explain Inline function?


Answer: Inline function are those function whose function body is inserted in place of the
function call. They are defined by placing the keyword inline before the function name. Inline
functions should be defined before any calls are made to that function. The major advantage of
inline function is, it reduces the overhead of pushing the values of local variables into stack
before the function call and popping them back from stack after returning from the function.

Q21: What is polymorphism or overloading?


Answer : Function polymorphism or function overloading us a concept the allows multiple
functions to share the same name with different arguments type assigning one or more function
body to the same name. That is the ability of a function or operator to act in different ways on
different data types is called polymorphism. With overloading, which function has to be called
will be resolved at compile-time either by number of arguments or by data type of the arguments
or both.

Q22: Describe about Default Arguments in function.


Answer : In function declaration, default values to arguments can be provided and during
function call, if the function is called without values for those arguments the default values are
used. If default arguments are given in a function, they must be at the end of the argument list.
During function call, the actual arguments matched to the formal arguments on a one-to-one
mapping in the order they are given and for the remaining arguments if any will be assigned with
default values. Default arguments are used only in function calls where trailing arguments are
omitted — they must be the last argument(s). That is, once default value is used for an argument,
all subsequent arguments must have default value.

Q23: Name some functions defined in iomanip header file.


Answer: setiosflags(), resetiosflags(), setfill(), setw(), setprecision().

Q24. Describe about the static data member of a class.


Answer: If the data member is defined as static, then it means no matter how many objects of
the class are created, there is only one copy of the static member. It is declared by using the
static keyword. A static member is shared by all objects of the class. All static data is initialized
to zero when the first object is created, if no other initialization is present. We can't put it in the
class definition but it can be initialized outside the class by re-declaring the static variable, using
the scope resolution operator :: to identify which class it belongs to.

Eg. Class A
{ private:
static int a;
public:
…………;
…………;
};
int A::a = 0;
…….

Q25: Explain how const modifier behaves when it is attached to


i. Function Argument
ii. Member Function
iii. Data member
iv. Object
Answer:
i. Const Function Argument
When the argument is passed by reference to a function, by declaring the
argument as const, we can prevent the function from modifying the data members
of the object passed as argument.
ii. Const Member Function
When a member function is declared as const, it ensure that it will not modify the
values of its data members.
iii. Const Data Member
When a data member is declared as const, its value cannot be changed. It can be
initialized only in the constructor through initializer member list.
iv. Const Object
When an object is declared as const, its data members must be initialized through
parameterized constructor and cannot be changed throughout the program. Using
const objects we can call only const member functions of that class. Any member
function that is not explicitly declared as const cannot be invoked by a const
object.