Object Oriented Programming Interview Questions - Part - 1

1. Define Object Oriented Programming?
Object Oriented Programming is an approach that provides a way of modularizing programs by creating partitioned memory areas for both data and functions that can be used as templates for creating copies of such modules on demand.

2. List some features of Object Oriented Programming?
i. Emphasis is on data rather than procedures
ii. Programs that are divided in to what are known as objects
iii. Functions that operate on the data of an object are tied together in the data structure
iv. Follows bottom-up approach in program design

3. What are the basic concepts of OOP’s?
i. Objects
ii. Classes
iii. Data abstraction
iv. Inheritance
v. Polymorphism
vi. Dynamic binding
vii. Message passing

4. Define Objects?
An entity that can store data and send and receive messages. Objects are basic run-time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. Each object has the data and code to manipulate the data and theses objects interact with each other.It is an instance of a class.

5. Define classes?
The entire set of data members and member functions of an object can be made a user-defined data type with the help of a class. Once a class has been defined, we can create any number of objects belonging to the classes. Classes are user-defined data types and behave like built-in types of the programming language. A group of objects that share common properties and relationships. It is a new data type that contains member variables and member functions () that operate on the variables. A class is defined with the keyword class.

6. Define data encapsulation?
Wrapping up of data and function within the structure .The wrapping up of data and functions into a single unit (Called class) is known as encapsulation.

7. What is data hiding?
The insulation of data from direct access by the program is called as data hiding or information binding. The data is not accessible to the outside world and only those functions, which are wrapped in the class, can access it.

7. What is inheritance?
It is the process by which objects of one class acquire the properties of the objects of another class.

8. Define operator overloading?
The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.

9. Define dynamic binding late binding?
Binding refers to the linking of a procedure to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure is not known until the time of the call at run-time.

11) What are data members and member functions?
Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight, and cost and uses functions to operate on these attributes.
The attributes are sometimes called as data members because they hold information. The functions that operate on these data are called as methods or member functions.
Eg: int a,b; // a,b are data members
void getdata ( ) ; // member function

10. Give some advantages of OOP’s?
i. Through inheritance, we can eliminate redundant code and extend the use of existing
ii. Classes
iii. Easy to partition the work.
iv. Object oriented programming can be easily upgraded from small to large systems.
v. Software complexity can easily be managed

11. What are the object-oriented languages used?
Object based programming languages
Object oriented programming languages.

12. Give some applications of OOP’s?
i. Real-time systems
ii. Simulation and modeling
iii. Object-oriented data bases
iv. CIM/CAD systems

13. Give the structure of a C++ program? / Program elements in C++

Include Header files
Class declaration
Class Member function definition
Main function program







14. Write a program to display the following using single cout statement?
Name=xxx
Age=23
Pgm:
#include <iostream.h>
Main()
{
Cout<<”name=xxx”<<”\n age=23”<<\n;
}

15. What is a comment?
Comments start with a double slash symbol and terminate at the end of the line. A comment may start any where in the line, and whatever follows till the end of the line is ignored.
Ex:
#include <iostream.h> //this is single line comment.
man()  /* ………………….. Multiline comment */
{
Cout<<”name=xxx”<<”\n age=23”<<\n;
}

16. List some tokens?
The smallest individual units in a program are known as tokens. C++ has the following tokens,
o Keyword
o Identifiers
o Constants
o Strings
o Operator

17.  What are keywords?
They are explicitly reserved identifiers and cannot be used as names fro the program variables or other user defined program elements.
Eg: go to, If, struct , else ,union etc.

18.  Rules for naming the identifiers in C++.

_ Only alphabetic characters, digits and underscore are permitted.
_ The name cannot start with a digit.
_ The upper case and lower case letters are distinct.
_ A declared keyword cannot be used as a variable name.

19.  What are the operators available in C++?
All operators in C are also used in C++. In addition to insertion operator << and extraction operator >> the other new operators in C++ are,
1. :: Scope resolution operator
2. : : * Pointer-to-member operator
3. ->* Pointer-to-member operator
4. .* Pointer-to-member operator
5. delete Memory release operator
6. endl Line feed operator
7. new Memory allocation operator
8. setw Field width operator

20. What is a scope resolution operator?
Scope resolution operator is used to uncover the hidden variables. It also allows access to global version of variables.
Eg:
#include<iostream.h>
int m=10; // global variable m
void main ( )
{
int m=20; // local variable m
cout<<”m=”<<m<<”\n”;
cout<<”: : m=”<<: : m<<”\n”;
}
output:
20
10 (: : m access global m)
Scope resolution operator is used to define the function outside the class.
Syntax:
Return type <class name> : : <function name>
Eg:
Void x : : getdata()

21. What are free store operators (or) Memory management operators?
New and Delete operators are called as free store operators since they allocate the memory dynamically. New operator can be used to create objects of any data type.
Pointer-variable = new data type;
Initialization of the memory using new operator can be done. This can be done as,
Pointer-variable = new data-type(value)
Delete operator is used to release the memory space for reuse. The general form of
its use is
Delete pointer-variable;

22. What are manipulators?
Setw, endl are known as manipulators.
Manipulators are operators that are used to format the display. The endl manipulator when used in an output statement causes a linefeed to be inserted and its effect is similar to that of the newline character”\n”.
Eg:
Cout<<setw(5)<<sum<<endl;

23. What do you mean by enumerated datatype?
An enumerated datatype is another user-defined datatype, which provides a way for attaching names to numbers, thereby increasing comprehensibility of the code. The syntax of an enum statement is similar to that of the struct statement.
Eg:
enum shape{ circle, square, triangle}
enum color{ red, blue, green, yellow}

24. What are symbolic constants?
There are two ways for creating symbolic constants in C++:
_ Using the qualifier constant.
_ Defining a set of integer constants using enum keyword.
The program in any way cannot modify the value declared as constant in c++.
Eg:
Const int size =10;
Char name [size];

25.  What do you mean by dynamic initialization of variables?
C++ permits initialization of the variables at run-time. This is referred to as dynamic initialization of variables. In C++, a variable can be initialized at run-time using expressions at the place of declaration as,
……..
…......
int n =strlen(string);
……..
float area=3.14*rad*rad;
Thus declaration and initialization is done simultaneously at the place where the variable
is used for the first time.

26.  What are reference variable?
A reference variable provides an alias (alternative name) for a previously defined variable. For example, if make the variable sum a reference to the variable total, then sum and total can be used interchangeably to represent that variable.
Syntax:
Data-type &reference-name = variable-name
Eg:
float total = 100;
float sum = total;

27.  What is member-dereferencing operator?
C++ permits to access the class members through pointers. It provides three pointer-to-member operators for this purpose,
: :* To declare a pointer to a member of a class.
* To access a member using object name and a pointer to the member
->* to access a member using a pointer to the object and a pointer to that member.

28. What is function prototype? function declaration
The function prototype describes function interface to the compiler by giving details such as number, type of arguments and type of return values Function prototype is a declaration statement in the calling program and is of the following
Type function_name (argument list);
Eg float volume int x,float y);

29. What is an inline function?
An inline function is a function that is expanded in line when it is invoked. That is compiler replaces the function call with the corresponding function code. The inline functions are defined as
Inline function-header
{
function body
}

30. Write some situations where inline expansion may not work
_ for functions returning values, if loop, a switch, or a goto exists
_ for functions not returning values, if a return statement exists
_ if function contain static variables
_ if inline functions are recursive

31. What is a default argument?
Default arguments assign a default value to the parameter, which does not have matching argument in the function call. An argument value that is specified in a function declaration and is used if the corresponding actual argument is omitted when the function is called. Default values are specified when the function is declared.
Eg : float amount(float principle,int period,float rate=0.15)
Function call is
Value=amount (5000,7);
Here it takes principle=5000& period=7
And default value for rate=0.15
Value=amount (5000,7,0.34)
Passes an explicit value 0f 0.34 to rate. We must add default value from right to left.

32.  What are constant arguments?
The keyword is const. The qualifier const tells the compiler that the function should not modify the argument. The compiler will generate an error when this condition is violated. This type of declaration is significant only when we pass arguments by reference or pointers
eg: int strlen(const char *p);

33. Define structures and classes?
Structures: A hierarchical set of names that refer to an aggregate of data of data items may have different attributes
Class: A group of objects that share common properties and relationships .A class is defined with a keyword class.

34. Define arrays?
A collection of data elements arranged to be indexed in one or more dimensions. The arrays are stored in contiguous memory.

35. What are the two forms of if statement? Give the syntax also?
The two forms of if statement is
i. Simple if statement
ii. If…else statement
Syntax:
Form1: if (expression is true)
{
action1
}
action2;
Form2: if (expression is true)
{
action1;
}
else
{
action2
}
action3;

36. What is an expression?
An expression is a combination of operators, constants, and variables arranged as per rules of the language. It may include function calls, which return values. Expressions are sequences of operators and operands that are used for one or more of these purposes:
· Computing a value from the operands.
· Designating objects or functions.
Types:
· Constant expressions
· Integral expressions
· Float expressions
· Pointer Expressions
· Relational Expressions
· Logical Expressions
· Bitwise Expressions

37. Define pointers?
A data type that holds the address of a location in memory.  It’s a variable that holds the address of another variable, function or data.
void main()
int firstvalue, secondvalue;
int * mypointer;
  mypointer = &firstvalue;
Use the two operators Dereference operator (*) and Reference operator (&)

39. What is a call by reference?
Functions call mechanism that passes arguments to a function by passing the addresses of the arguments.

40. Define call by value?
A function calls mechanism that passes arguments to a function by passing a copy of the value of the arguments.

41. Define macro?
A short piece of text, or text template that can be expanded in to a longer text.

42. Explain return by reference with an example.
A function can also return a reference. Consider the following function
int & max( int &x , int &y)
{ if(x>y)
return x;
else
return y;
}
Since the return type of max ( ) is int & the function returns reference to x or y (and not the values). Then a function call such as max ( a , b) will yield a reference to either a or b depending on their values.
The statement
max ( a , b) = -1;
is legal and assigns –1 to a if it is larger, otherwise –1 to b.

43. Explain function overloading?
Function names can be overloaded in C++. We can assign the same name to two or more distinct functions.
(eg): int mul (int a, int b);
float mul (float x, flaot y);
The function name mul () is overloaded, meaning that it has more than one implementation. The execution of the correct implementation is decided by the compiler by matching the type of the arguments in the function call with the types in the function prototypes.

44. Write about the different data types.
Data type denotes the type of the data. It is used to allocate the space for the variable in memory.

44. Write about the STL.
STLàStandard template library
STL (Standard Template Library) is a generic collection of class templates and algorithms that allow programmers to easily implement standard data structures like queues, lists, and stacks.
The C++ STL provides two kinds of containers:
· Sequence Containers
o C++ Vectors
o C++ Lists
o C++ Double-Ended Queues
· Associative Containers
o C++ Maps
o C++ Multimaps
o C++ Sets
o C++ Multisets
Iterators are used to access elements of a sequence, and can be used in a similar manner to pointers.

44. Write about the assertions.
An assertion is a statement that can be used to test our assumptions about certain parts of the program. It is a Boolean expression, and by placing an assertion, we expect it to return true. Placing assertions in the code increases the robustness of the code by letting the programmer catch the logic errors in the early stages of the development.
If the assertion returns true, it confirms that the assumptions about the behavior of the program is right. Otherwise, the system throws an error.
Pre-, Post-conditions Assertions are generally useful for testing the pre- and post-conditions of a routine.
A pre-condition specifies what must be true when a routine is invoked. ie., it is a statement by the programmer that a given routine should not be entered unless its pre-condition is guaranteed.
A post-condition specifies what must be true after a routine completes successfully.
Example:
If the pre-condition for a function requires the age of a person to be > 25 years, we can place an assertion like the following as the very first line of the function:
assert (age > 25);
If any value <= 25 is passed as input to the function, the assertion statement halts the execution of the program and triggers an error Assertion Failed.
45. Write about Storage classes.
A storage class defines where the value of the variable is stored. Storage class defined for a variable determines the accessibility that is what portion of the program could have access to the variable and also the longevity of the variable that is how long the variable would exist.
There are four types of storage class: automatic, register, external and static.
Auto
Automatic variable, also called as local variable and it has scope only within the function block where it is defined.
External
External variable are defined outside any function and memory is set aside for this type of variable once it is declared and remained until the end of the program. These variables are also called global variables.
Static
the static automatic variables, as with local variables, are accessible only within the function in which it is defined. Static automatic variables exist until the program ends in the same manner as external variables. In order to maintain value between function calls, the static variable takes its presence.

46. What are the types of operators?
1. Arithmetic operators (+, -, *, /, % )
2. Assignment operators (=)
3. Increase and decrease (++, --)
4. Relational and equality operators (==,! =, >, <, >=, <= )
5. Logical operators (!, &&, || )
6. Conditional operator ( ? )
7. Bitwise Operators ( &, |, ^, ~, <<, >> )
8. Comma operator ( , )

47. What is the scope of the variable?
A variable can be declared either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.

Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration. The scope of local variables is limited to the block enclosed in braces ({}) where they are declared. For example, if they are declared at the beginning of the body of a function (like in function main) their scope is between its declaration point and the end of that function.

48. What is namespace?
Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.
The format of namespaces is:
Namespace identifier
{
entities
}
Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace.
 For example:



namespace myNamespace
{
  int a, b;
}
In this case, the variables a and b are normal variables declared within a namespace called myNamespace. In order to access these variables from outside the myNamespace namespace we have to use the scope resolution operator ::.

49. What is a void pointer?
The void type of pointer is a special type of pointer. In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereference properties).
This allows void pointers to point to any data type, from an integer value or a float to a string of characters. To cast the address in the void pointer to some other pointers type that point to a concrete data type before dereferencing it. Pointers to pointers
void *data;

50. What is a pointer-to-pointer?
Point to data (or even to other pointers). In order to do that, we only need to add an asterisk (*) for each level of reference in their declarations:





char a;
char * b;
char ** c;
a = 'z';
b = &a;
c = &b;
This, supposing the randomly chosen memory locations for each variable of 7230, 8092 and 10502, could be represented as:

1.  Compare and contrast C with C++.
S.No C Language C++ Language

1 It is a structured programming language It is an object oriented programming Language
2 C does not use the objects in the program C++ is mainly based on the objects.

3 C does not support the data hiding and encapsulation C++ supports the data hiding and
 and encapsulation

2. Compare Procedure oriented and object oriented languages
S.No Procedure oriented object oriented
1 Emphasis is on doing things (algorithms). Emphasis is on data rather than on procedure.
2 Larger programs are divided into smaller programs known as functions. Programs are divided into objects
3 Data move openly around the system from function to function. Data is hidden and cannot be accessed by external functions
4 Employs top-down approach in program design Follows bottom-up approach in program design.

What is the difference between inline function and function?
In normal function, every time it is called, it takes extra time in executing series of instructions for tasks such as jumping to the function, saving registers, pushing arguments into the stack, and returning to the calling function.
An inline function is a function that is expanded in line when it is invoked. That the compiler replaces the functions call with the corresponding function code.
What is extraction and insertion operators?
The input operator “>>” is called Extraction operator.
The output operator”<<” is called Insertion operator.
Define Procedure-oriented programming
The program is viewed as a sequence of things to be done such as reading, calculating and printing.
What is object oriented programming
OOP allows decomposition of problem into number of entities called objects and then builds data and function around these objects.
List out the basic concepts of object oriented programming
1. Data abstraction and encapsulation
2. Inheritance
3. Polymorphism
4. Dynamic binding
5. Message passing
List out the benefits of OOP
1. Eliminate redundant code
2. Extend the use of existing
3. Classes
4. Build secure programs
5. software complexity can be easily managed

Declaring all the functions as inline functions will occupy more memory. –Justify;
The inline functions are the functions that are expanded in line wherever they are invoked. Since when the inline functions are invoked, they make a copy of the function definition where it is invoked. This is the second copy (duplicated copy) of the function that occupies more memory. So having more number of inline functions will take much memory.