Wednesday 29 February 2012

C++/OOPS Programming language



Question - 1) What is a class?

A class is a group of objects, i.e. data members or member-functions that share common properties and relationships. It represents a group of similar objects.


Question - 2) What is an object?

An object is an identifiable entity with some characteristics and behavior. It represents an entity that can store data and its associated functions.


Question - 3) List the advantages of object oriented programming?

Object oriented programming has many advantages:
  • Re-usability of code.
  • Ease of comprehension.
  • Ease of fabrication and maintenance.
  • Easy redesign and extension.


Question - 4) What is meant by ‘call by value’?

In C++, structures can be passed to functions by two methods namely ‘by value’ and ‘by reference’. In ‘call by value’ method, the called function copies the passed structure on to its own work copy; the original structure then remains unaffected.


Question - 5) What is meant by ‘call by reference’?

In ‘call by reference’ method of calling functions, the called function declares a reference for the passed structure and refers to the original structure elements through its reference.


Question - 6) What is a reference variable ? What is its use ?

A reference variable is an alias name for a previously declared variable. The use of it is that is that the same data object can be referred to by two names and these names can be used interchangeably.


Question - 7) What are inline functions ? What is the advantage of using them ? How are they declared ?

An inline function is a function upon which the C++ compiler is asked to perform inline expansion, i.e. the compiler is requested to replace every function call with the complete body of the function, rather than generating the function code at each and every position of the function call.

The major advantage of inline functions is that it saves on the overhead of a function call. Since the function is not invoked, but its code is replaced in the program, it saves the compiler time and improves efficiency.

The format to declare an inline function is:

inline datatype function_name(parameters)
{
function content;
}


Question - 8) What are the defining traits of an object-oriented language ?

The defining traits of an object oriented programming language are the following:

Data abstraction.
Data encapsulation.
Inheritance.
Polymorphism.
Modularity.


Question - 9) What is the purpose of the scope resolution operator ?

In C++, the scope resolution operator (::) is used to qualify hidden file names so that
they can be used. Further, the scope resolution operator can also be used to qualify
class names or class member names.


Question - 10) What are the differences between type casting and automatic type conversion ?

Type casting or explicit type conversion is user-defined forced conversion. It is done with the help of casting operator ().

Automatic type conversion or implicit type conversion is a data type conversion performed by compiler on its own. It occurs whenever an expression has different types.
Question - 11) Why ‘char’ is often considered as an integer data type?

The memory implementation of char data type is in terms of the number code, which is an integer. Therefore, it is said to be another integer data type.


Question - 12) What is the difference between structure and class in C++?

A structure is a collection of data while the class is a collection of data and functions. Further, all members are public by default in a structure while all members are private by default in class. Class has greater data security than structure.


Question - 13) What is the purpose of ios:app?

The function of ios::app is to retain the previous contents of a file and to append to the end of the existing file contents, rather than truncating them.


Question -14) What is the purpose of ios::ate?

The purpose of ios::ate is to place file pointer at the end of the file, but you can write data anywhere in the file.


Question - 15) How is random access of data achieved in C++?

In C++, random access is achieved by manipulating seekg( ), seekp( ), tellg( ) and tellp( ) functions. The seekg() and tellg() functions allow you to set and examine the get_pointer, and the seekp() and tellp() functions perform these operations on the put_pointer.

Question - 16) Which header file is required for creating and manipulating data files in C++?

fstream.h


Question - 17) What is an access specifier?

An access specifier is a way to specify the scope and accessibility of members of a class. Access specifiers can be either private or protected or public.


Question - 18) How is memory allocated to a class and its objects?

When a class is defined, memory is allocated for its member functions and they are stored in the memory. When an object is created, separate memory space is allocated for its data members. All objects work with the one copy of member functions shared by all.


Question - 19) What is the difference between an object and a class ?

An object is an identifiable entity with some characteristics and behavior. It represents an entity that can store data and its associated functions.
A class is a group of objects that share common properties and relationships. It represents a group of similar objects.


Question - 20) Differentiate between data hiding and encapsulation ?

Data hiding is the property whereby the internal data structure of an object is hidden from the rest of the program.

Data encapsulation is the mechanism by which the data and associated functions are bound together within an object definition in such a way so that only relevant information is exposed and rest remains hidden. Thus, data encapsulation implements data hiding.

Question - 21) What is polymorphism? Explain with an example ?

Polymorphism is the property by which the same message can be sent to different objects of several different classes, and eah object can respond to it in a different way depending upon its class. In C++, polymorphism is implemented through overloading.

A function name having several definitions that are differentiable by the number of types of their arguments is known as function overloading. For example,

float area (float a)
{
return a * a;
}
float area (float a, float b)
{
return a * b;
}


Question - 22) What is public, protected and private ?

Public, protected and private are access labels in a class. A class provides three access labels namely private, protected and public.

A member declared as public is made available to the outside world. That is, it can be accessed by any function, any expression in the program but only by using an object of the same class type.

A member declared as private or protected remains hidden from outside world and it can only be accessed by the member functions and friend functions of a class.


Question - 23) What is the significance of the protected access specifier ?

The members having protected accessibility are hidden from outside world, like private members, but when inherited, they remain visible in the derived class unlike private members.


Question -24) Do 'derivation' and 'friendship' mean the same ?

No. ‘Friendship’ gets the access privilege for private and protected members of a class. However, ‘derivation’ from that class gets access privilege only for protected members of the class.


Question - 25) Enlist some advantages of object oriented programming ?

The major advantages of object oriented programming are:
  • Re-usability of code.
  • Ease of comprehension.
  • Ease of fabrication and maintenance.
  • Easy redesign and extension.

Question - 26) Is prototyping mandatory in C++? Why?

Yes, prototyping is mandatory in C++. C++ makes it mandatory to ensure proper invocation of functions and prompt error reporting.


Question -27) When should a function be made inline?

A function is made inline when:
  • The function is small.
  • The function is not returning any value and contains no return statement.
  • The function does not contain a loop or a switch or a goto.
  • The function does not contain static variables.
  • The function is not recursive.

Question - 28) How is a static variable different from a static function?

A static variable has a function scope but the global life time. A static function has a file scope instead of program scope.


Question - 29) What is meant by macro?

A macro is a #define function, whose every occurrence is the program is replaced prior to compilation as per its definition.


Question - 30) What do you mean by inline function?

An inline function is that which is not invoked at the time of function call rather its code is replaced in the program at the time of function call. Thus, inline functions save the overheads of a function call.

31) What is a constructor? Why would I ever use one?

A constructor is a member function with the same name as that of its class. Constructors are automatically invoked when an object of the same class is created. The constructor has the same name as that of the class.
A constructor is used often to initialize the objects of that class type with a legal initial value.


32) What are destructors really for? Why would I ever use them?

A destructor is a member function with the same name as that of its class but is preceded by a tilde (~). Destructors are really for the purpose of de-initializing an object before it goes out of scope.


33) Is a default constructor equivalent to a constructor having default arguments?

Yes, a default constructors is equivalent to a constructor having default arguments because when no initial values are provided for an object at the time of its declaration, the constructor having default argument values is invoked same way as the default constructor is invoked. Therefore, these two are considered equivalent.


34) Why is a destructor function required in a class?

During construction of an object by a constructor, resources are allocated for use. These resources must be de-allocated before the object is destroyed. A destructor achieves this function and performs all clean-up jobs.


35) What is a parameterized constructor?

A constructor that receives arguments or parameters is called a parameterized constructor.

36) What is a copy constructor?

A constructor that initializes an object using values of another object passed to it as parameter is called copy constructor. It creates the copy of the passed object. The format of declaration is:
classname (classname &)


37) What is a default constructor? What is its role?

A default constructor is the one that takes no arguments. It is automatically invoked when an object is created without providing any initial values. In case, the programmer has not defined a default constructor, the compiler automatically generates it.


38) What is meant by constructor overloading?

Constructor overloading refers to a class having multiple constructor definitions, each having a different signature.


39) What is operator overloading?

Operator overloading is a specific case of polymorphism in C++. In operator overloading, different operators have different implementations, depending on their arguments.


40) What operators can/cannot be overloaded?

Most can be overloaded. The only C++ operators that can't be are . and ?: (and sizeof, which is technically an operator). C++ adds a few of its own operators, most of which can be overloaded except :: and .*.

41) How is operator overloading implemented successfully in C++?

In C++, operator overloading follows the same procedure of functioning as function overloading. The implementation of operator overloading depends upon the situation where the operator is used. For example, the ‘-‘sign, when used with a number, like -20, is read by the compiler to be a negative number. At the same time, when the ‘-‘ sign is associated with two numbers, like 8-5, then the compiler identifies it to be the subtraction operation.


42) What is abstract class?

Abstract class is a class that defines an interface, but does not necessarily provide implementations for all its member functions. No objects of an abstract class exist, i.e, it cannot be instantiated.


43) What is concrete class?

A concrete class is a derived class of an abstract class that implements all the missing functionality. A concrete class can be instantiated. i.e, its objects can be created.


44) What is function overloading and operator overloading?

A function name having several definitions that are differentiable by the number or types of their arguments is known as function overloading.

Operator overloading is a specific case of polymorphism in C++. In operator overloading, different operators have different implementations, depending on their arguments.


45) What is a ‘friend’ function?

A friend function in C++ is a function which allows access to private or protected data in a class from outside. Friend functions are declared using the ‘friend’ keyword.

46) Do ‘friends’ violate encapsulation?

The friend lessens the value of encapsulation of separate classes in object oriented programming. Hence, too many external functions or friend functions are not advisable for use in object oriented programs.


47) What are some advantages/disadvantages of using friends?

The major advantage of the friend function is that it allows a great degree of freedom in the interface design option. This function is useful when a class needs to hide certain components from the users and use it elsewhere. Friend function efficiently keeps the data details private and secure from the outside-class area.

The major disadvantage of friend function is that it requires an extra line of code while using dynamic binding.


48) What do you mean by the friend function and friend class?

A friend function is a non-member function that is granted access to a class’s private and protected members.
A friend class is a class whose member functions can access another class’s private and protected members.


49) Why should I use `new' instead of trustworthy old malloc()?

It is necessary to use ‘new’ instead of the old ‘malloc’ to ensure efficiency of the program. Unlike malloc, new calls the specific constructor. Further, the value returned by new is type safe and the new operator can be overridden.


50) How do I allocate / unallocate an array of things?

An array of things is allocated or unallocated using the new and delete operators.

51) What if I forget the `[ ]' when `delete'ing array allocated via `new X[n]'?

Basically, it is the duty of the programmer, and not the compiler, to get the connection between the ‘new X[n]’ and the ‘[]’. If the programmer fails in this, the compiler will not generate a runtime error or compile-time error but will result in a heap corruption. Otherwise, the program will die.


52) What are free store operators?

Operators that facilitate dynamic memory allocation, i.e., allocation new and de-allocation delete operators are called free store operators.


53) What do you mean by free storage list or AVAIL list?

A list keeping account of available free memory is called an AVAIL list or free storage list.


54) What is meant by static memory allocation?

When the amount of memory to be allocated is known beforehand and the memory is allocated during compilation itself, it is referred to as static memory allocation.


55) What is dynamic memory allocation?

Allocation of memory at runtime is referred to as dynamic memory allocation.

56) Define free store ?

It is a pool of unallocated heap memory given to a program that is used by the program for dynamic allocation during execution.


57) What do you mean by a self referential structure?

A structure having a member element that refers to the structure itself is called a self-referential structure.


58) What is inheritance? What the types of inheritance?

Inheritance is the capability of one class to inherit properties from another class. The class from which properties are inherited is called a base class and the class inheriting properties is called the derived class.


59) What are the different types of inheritances?

Inheritances are of many types namely single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance and hybrid inheritance.


60) What is the difference between a virtual base class and a normal base class?

The major and only difference between a virtual base class and a normal one is when an object inherits the base more than once. If virtual base classes are used, then only one base class is present in the object. Otherwise, multiple copies will be found.
61) What is meant by a visibility mode?

The public, private or protected specifier that controls the visibility and availability of a member in a class is called a visibility mode.


62) What's the difference between `public:', `private:', and `protected:' visibility modes?

The differences between the `public:', `private:', and `protected:' visibility modes lie in the ways in which members and functions can be accessed within a class.

In the publicly derived class, the public and protected members of the base class become public and protected members of the derived class.

In the privately derived class, the public and protected members of the base class become private members of the derived class.

In the protectedly derived class, the public and protected members of the base class become protected members of the derived class.


63) When should you use inheritances?

Inheritance finds wide applications in C++ due to the following reasons.

  • Inheritance has the capability to express the inheritance relationships which makes it ensure the closeness with the real-world models.
  • Another reason is the idea of re-usability. Inheritance helps in addition of additional features to an existing class without modifying it.
  • Inheritance possesses a transitive nature.



64) How do you express `private inheritance'?

The private derivation means, the derived class can access the public and private members f the base class privately. Here, the inherited members can be accessed only through member functions of the derived class.

Private inheritance is expressed as :
class derived-class-name : visibility-mode base-class name
{
;
}


65) What are the access rules with `private' and `protected' inheritance?

In private derivation, though the derived class inherits all the public and protected members of the base class become private members of the derived class, the objects of the derived class cannot access them directly. Here, the public and protected members of the base class are directly accessible by the member functions and friends of the derived class as if they were the private members of the derived class.
66) What is meant by inheritance hierarchy ?

The relationship between a base and derived class is referred to as a derivation or inheritance hierarchy. The derivation from multiple base classes is referred to as a derivation or inheritance graph.


67) Differentiate between multiple and multilevel inheritances ?

Multiple inheritance is the inheritance hierarchy wherein one derived class inherits from multiple base classes.

Multilevel inheritance is the inheritance hierarchy wherein a subclass acts as a base class for other classes.


68) What is an ABC (`Abstract Base Class') ?

A class serving only as a base class for other classes and no objects of which are created is known as an abstract base class.


69) What is a `virtual destructor' ?

A virtual destructor is a destructor which is essential in C++ programming. The virtual destructor is important for a C++ base class to ensure that the destructor from the most called function is chosen.


70) What is a dangling pointer or wild pointers ?

Dangling pointers or wild pointers are pointers in computer programming, which do not point to a valid object of the appropriate type. They are special variations of memory safety violations.

71) What is abstraction?

Data abstraction refers to the act of representing essential features without including the background details or explanations. Data abstraction is an important characteristic of object oriented programming.


72) How is abstraction and encapsulation interrelated?

Encapsulation wraps up data and functions under a single unit and ensures that only essential features get represented without representing the background details which is nothing but abstraction. Therefore, encapsulation is a way of implementing abstraction.


73) What is an adaptor class or Wrapper class?

A wrapper class or an adaptor class refers to the method by which classes wrap their primitive values in a class and offers access to them through objects. A few of the primitive wrapper data types are:
Byte, short, int, long, float, double, char, Boolean.


74) What is a container class? What are the types of container classes?

Two classes such that the objects of a class are enclosed within the other class are called container classes and the property is called containership, nesting or composition.


75) What is polymorphism?

Polymorphism is the ability for a message or data to be processed in more than one form. Function overloading, operator overloading etc are all components of polymorphism.
76) What do you mean by a static data member and a static member function?

A static data member is a class data member common to all the objects of a class. A static member function is a member function that can access only the static data members.


77) What is modularity in C++?

Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules. In other words, modularity is the act of partitioning a program into individual components is called modularity.


78) What do you mean by data abstraction?

Data abstraction is the act of representing essential features without including the background details.


79) What is a pointer? How will you define a pointer to an integer and a pointer to a character?

A pointer is a variable which holds a memory address which is usually the location of another variable in memory.

A pointer, say ptr, to integer is declared as int *ptr; and a pointer, say cptr, is declared to a character as char *cptr.


80) How does a pointer variable differ from a simple variable?

A simple variable stores a data value whereas a pointer variable stores a memory address of another variable in the memory.
81) What is a nested structure ?

A nested structure is a structure having another structure as its member element. It finds a wide range of applications in C++ components like linked lists.


82) How are arrays and structures related to each other ?

Arrays bring together a group of items of the same data type whereas structures bring together a group of related data items of any data type.


83) How are structures and classes related to each other ?
                                             OR
What is the difference between structure and class?

Structure is actually a class in C++ declared with keyword struct. By default, all members are public in a structure, while on the other hand, all members are private by default in classes.


84) What is the difference between get and getline member functions ?

The get() and getline() functions are virtually identical to one another. But the difference between the two of them is that getline() reads and removes the newline character from the input stream if its is encountered, which is not done by the get() function.


85) Differentiate between ifstream class and ofstream class?

The ifstream class is an input stream class, which provides input operations for file. The ofstream class is an output stream class, which provides output operations for a file. Both ifstream and ofstream classes are defined under the header file iostream.h.
86) Name the streams generally used for file operations.

Ifstream, ofstream and fstream.


87) What do you mean by ‘this’ pointer?

The ‘this’ pointer is an already created object pointer that points to currently calling object.


88) What is a data structure?

A named group of data of different data types which can be processed as a single unit is referred to as a data structure.


89) List four major operations associated with linear data structures.

Four major operations performed on data structures are searching, sorting, traversing and inserting.


90) What are trees in C++?

Trees are non-linear data structures having a hierarchical relationship between various elements called nodes. Topmost node is called the root of the tree and the bottommost nodes are called leaves of the tree.

91) State conditions under which binary search is applicable.

For binary search,
  • The list must be sorted.
  • Lower bound and upper bound of the list must be known.



92) What is a linked list?

A linked list is a collection of data elements, called nodes pointing to the next nodes by means of pointers.


93) Differentiate between functions read() and write().

The read() function lest one read a unit of information from a file and the write() function lets one write a unit of information to a file.


94) What is a null pointer?

A pointer that does not point to any data object is called a null pointer, In C++, the null pointer can be represented by the constant 0.


95) What is a memory leak?

A situation in which there lie so many orphaned memory-blocks that are still allocated but no pointers are referencing to them, are referred to as a memory leak.

96) What is a stack?

The memory area that stores the return address of the functions, arguments passed to functions, and local variables of functions is called a stack.


97) What is meant by overflow in C++?

If one tries to insert a node into a linked list, when there is no memory available, it is called ‘Overflow’.


98) What is an infix expression?

An expression in which operators are placed in between the operands is called an infix expression.


99) What do you mean by static binding?

Static binding or early binding is the process of selection of a particular function for a particular call at the compile time.


100) What do you mean by a node?

A part of a linked list storing information of an element as well as having a pointer to the next node is called a node.

No comments:

Post a Comment