Question - Define private, protected and public access control.
Answer
Private
Private is the default access specifier for every declared data item in a class. Private data belongs to the same class in which it is created and can only be used by the other members of the same class.
Protected
When a data item is declared as protected it is only accessible by the derived class member.
Public
Public allows to use the declared data item used by anyone from anywhere in the program. Data items declared in public are open to all and can be accessed by anyone willing to use their values and functions they provide.
Question - What are the characteristics of Object Oriented programming language?
Answer
Some key features of the Object Oriented programming are:
- Emphasis on data rather than procedure
- Programs are divided into entities known as objects
- Data Structures are designed such that they characterize objects
- Functions that operate on data of an object are tied together in data structures
- Data is hidden and cannot be accessed by external functions
- Objects communicate with each other through functions
- New data and functions can be easily added whenever necessary
- Follows bottom up design in program design
Question - What are the basic Concepts used in the Object-Oriented Programming language?
Answer
Object
Class
Data Abstraction and Encapsulation
Polymorphism
Inheritance
Message passing
Dynamic binding
Question - What is Turbo C++?
Answer
Turbo C++ provides an environment called IDE (Integrated Development Environment). The editor is used to create the source file, compile it, link it and then execute it.
Question - How to control the number of chars read in a string by using scanf()?
Answer
By using the a format specifier
char buf[25];
scanf("%20s", buf);
Doing this limits the length of the characters that will be read by scanf() to 20 characters maximum although the buffer can hold 25 characters.
Question - How to detect an end of a file?
AnswerYou can either use the ifstream object ‘fin’ which returns 0 on an end of file or you can use eof() which is a member function of the ios class. It returns a non zero value on reaching the end of file.
Describe the virtual function and virtual function table.
A virtual function in C++ is
- A simple member function of a class which is declared with “virtual” keyword
- It usually performs different functionality in its derived classes.
- The resolving of the function call is done at run-time.
Virtual Table:
A virtual table is a mechanism to perform dynamic polymorphism i.e., run time binging. Virtual table is used to resolve the function calls at runtime. Every class that uses virtual functions is provided with its own virtual functions.
Every entry in the virtual table is a pointer that points to the derived function that is accessible by that class. A hidden pointer is added by a compiler to the base class which in turn calls *_vptr which is automatically set when an instance of the class is created and it points to the virtual table for that class.
What are recursive functions? What are the advantages and disadvantages of Recursive algorithms?
A recursive function is a function which calls itself.
The advantages of recursive functions are:
-Avoidance of unnecessary calling of functions.
-A substitute for iteration where the iterative solution is very complex. For example to reduce the code size for Tower of Honai application, a recursive function is bet suited.
- Extremely useful when applying the same solution
The disadvantages of Recursive functions:
-A recursive function is often confusing.
-The exit point must be explicitly coded.
-It is difficult to trace the logic of the function.
What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.
An internal iterator is implemented by the member functions of the class which has the iteration logic.
An external iterator is implemented by a separate class which can be attached to the object which has iteration logic.
The advantage of external iterator is that, many iterators can be made active simultaneously on the existing or same object.
Explain passing objects by reference, passing objects by value and passing objects by pointer.
Pass by value:
The callee function receives a set of values that are to be received by the parameters. All these copies of values have local scope, i.e., they can be accessed only by the callee function. The simplicity and guarantee of unchanging of values passed are the advantages of pass by value.
Pass by reference:
The callee function receives a set of references which are aliases to variables. If a change is made to the reference variable, the original value (passed by the caller function) will also be changed. All the references are handled by the pointers. Multiple values modification can be done by passing multiple variables.
Pass by pointer:
The callee function receives a pointer to the variable. The value of the pointer in the caller function can then be modified. The advantages of this process are that the changes are passed back to the caller function and multiple variables can be changed.
Explain the concepts of Throwing and catching exceptions with an example using C++.
C++ provides a mechanism to handle exceptions which occurs at runtime. C++ uses the keywords – throw, catch and try to handle exception mechanism. An entity called an exception class need to be created.
The application should have a separate section called catch block. The code in which an exception is predicted is authored in the try block.
The following code illustrates to handle the exception.
#include
class Excep {
public:
const char* error;
Excep(const char* arg) : error(arg) { }
};
class Test {
public:
int i;
// A function try block with a member initializer
Test() try : i(0) {
throw Excep("Exception thrown in A()");
}
catch (Excep& e) {
cout <<>
// A function try block
void f() try {
throw E("Exception thrown in f()");
}
catch (Excep& e) {
cout <<>
The following is the output of the above example:
Exception thrown in f()
Exception thrown in g()
Exception thrown in A()
What is an abstraction and why is it important?
Abstraction is to specify what to do (the necessary details) but not how to do (all details). For example, monitor information like size, screen type, power switches are specified for abstraction. But the inner parts and the functionality need not be known. In other words, making the details available for the user does matters.
For example, Employee class has a method calculateSalary() which receives the ‘empid’ as parameter and returns the salary of the employee concerned. The user who invokes the calculateSalary() method does not concerned about how the salary calculated. So, abstraction specifies to expose only the details which are concerned with the user.
Abstraction is important because, the code reusability is one of the benefits to the user. There is flexibility to implement the business logic for various objects which accesses the method. In the above example, salaries of employees of various cadres can use the neither same method with minor nor changes.
Previous Part2 next Part4