Monday 21 May 2012

C++ Two Mark Questions



1. State the characteristics of procedure oriented programming.
• Emphasis is on algorithm.
• Large programs are divided into smaller programs called functions.
• Functions share global data.
• Data move openly around the system from function to function.
• Functions transform data from one form to another.
• Employs top-down approach in program design.
2. What are the features of Object Oriented Programming?
• Emphasis is on data rather than procedure.
• Programs are divided into objects.
• Data structures are designed such that they characterize the objects.
• Functions that operate on the data of an object are tied together.
• Data is hidden and cannot be accessed by external functions.
• Objects may communicate with each other through functions.
• New data and functions can easily be added whenever necessary.
• Follows bottom-up approach.
3. Distinguish between Procedure Oriented Programming and Object Oriented Programming.
Procedure Oriented Programming
Object Oriented Programming
• Emphasis is on algorithm.
Emphasis is on data rather than procedure
• Large programs are divided into smaller programs called functions.
Programs are divided into objects.
Functions share global data.
Functions that operate on the data of an object are tied together.
• Data move openly around the system from function to function.
Data is hidden and cannot be accessed by external functions.
Employs top-down approach in program
design.
• Follows bottom-up approach.

 4. Define Object Oriented Programming (OOP).
Object Oriented Programming is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on
demand.
5. List out the basic concepts of Object Oriented Programming.
• Objects
• Classes
• Data Abstraction and Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
6. Define Objects.
Objects are the basic run time entities in an object oriented system. They are instance of a class. They may represent a person, a place etc that a program has to handle. They may also represent user-defined data. They contain both data and code.
7. Define Class.
Class is a collection of objects of similar data types. Class is a user-defined data type. The entire set of data and code of an object can be made a user defined type through a class.
8. Define Encapsulation and Data Hiding.
The wrapping up of data and functions into a single unit is known as data encapsulation. Here the data is not accessible to the outside world. The insulation of data from direct access by the program is called data hiding or information hiding.
9. Define Data Abstraction.
Abstraction refers to the act of representing the essential features without including the background details or explanations.
10. Define data members and member functions.
The attributes in the objects are known as data members because they hold the information. The functions that operate on these data are known as methods or member functions.

11. State Inheritance.
Inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification and provides the idea of reusability. The class which is inherited is known as the base or super class and class which is newly derived is known as the derived or sub class.
12. State Polymorphism.
Polymorphism is an important concept of OOPs. Polymorphism means one name, multiple forms. It is the ability of a function or operator to take more than one form at different instances.
13. List and define the two types of Polymorphism.
• Operator Overloading – The process of making an operator to exhibit different behaviors at different instances.
• Function Overloading – Using a single function name to perform different types of tasks. The same function name can be used to handle different number and different types of arguments.
14. State Dynamic Binding.
Binding refers to the linking of procedure call to the code to be executed in response to the call. Dynamic Binding or Late Binding means that the code associated with a given procedure call is known only at the run-time.
15. Define Message Passing.
Objects communicate between each other by sending and receiving information known as messages. A message to an object is a request for execution of a procedure. Message passing involves specifying the name of the object, the name of the function and the information to be sent.
16. List out some of the benefits of OOP.
• Eliminate redundant code
• Saves development time and leads to higher productivity
• Helps to build secure programs
• Easy to partition work
• Small programs can be easily upgraded to large programs
• Software complexity can easily be managed
17. Define Object Based Programming language.
Object Based Programming is the style of programming that primarily supports encapsulation and object identity. Languages that support programming with objects are known as Object Based Programming languages.
They do not support inheritance and dynamic binding.
18. List out the applications of OOP.
• Real time systems
• Simulation and modeling
• Object oriented databases
• Hypertext, Hypermedia and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems
19. Define C++.
C++ is an object oriented programming language developed by Bjarne Stroustrup. It is a super set of C. Initially it was known as “C with Classes”. It is a versatile language for handling large programs.
20. What are the input and output operators used in C++?
The identifier cin is used for input operation. The input operator used is >>, which is known as the extraction or get from operator. The syntax is,
cin >> n1;
The identifier cout is used for output operation. The input operator used is <<, which is known as the insertion or put to operator. The syntax is,
cout << “C++ is better than C”;
21. What is the return type of main ()?
The return type of main () is integer i.e. main () returns an integer type value to the operating system. Therefore, every main () should end with a return (0) statement. It’s general format is,
int main ()
{
…………
return 0;
}
22. List out the four basic sections in a typical C++ program.
• Include files
• Class declaration
• Member functions definition
• Main function program
23. Define token. What are the tokens used in C++?
The smallest individual units in a program are known as tokens. The various tokens in C++ are keywords, identifiers, constants, strings and operators.
24. Define identifier. What are the rules to be followed for identifiers?
Identifiers refer to the names of variables, functions, arrays, classes etc created by the programmer. The rules are as follows:
• Only alphabetic characters, digits and underscores are permitted
• The name cannot start with a digit
• Uppercase and lowercase letters are distinct
• A declared keyword cannot be used as a variable name
25. State the use of void in C++.
The two normal uses of void are
• To specify the return type of the function when it is not returning a value
• To indicate an empty argument list to a function
26. Define an enumeration data type.
An enumerated data type is a user defined data type that provides a way for attaching names to numbers thereby increasing comprehensibility of the code. The enum keyword is used which automatically enumerates a list of words by assigning them values 0, 1, 2…
E.g. enum shape {circle, square, triangle};
27. Define constant pointer and pointer to a constant.
The constant pointer concept does not allow us to modify the value initialized to the pointer.
e.g. char * const ptr = “GOOD”;
The pointer to a constant concept doesn’t allow us to modify the address of the pointer.
E.g. int const * ptr = &n;
28. What are the two ways of creating symbolic constants?
• Using the qualifier const
• Defining a set of integer constants using enum keyword
29. Define reference variable. Give its syntax.
A reference variable provides an alias or alternate name for a previously defined variable. It must be initialized at the time of declaration. Its syntax is given by, data-type & reference-name =  variable-name;
30. List out the new operators introduced in C++.
• :: Scope resolution operator
• ::* Pointer to member declarator
• ->* Pointer to member operator
• .* Pointer to member operator
• delete Memory release operator
• endl Line feed operator
• new Memory allocation operator
• setw Field width operator
31. What is the use of scope resolution operator?
A variable declared in an inner block cannot be accessed outside the block.
To resolve this problem the scope resolution operator is used. It can be used to
uncover a hidden variable. This operator allows access to the global version of
the variable. It takes the form,
:: variable-name
32. List out the memory differencing operator.
• ::* To declare a pointer to the member of the class
• ->* To access a member using object name and a pointer to that member
• .* To access a member using a pointer to the object and a pointer to that member
33. Define the 2 memory management operators.
• new Memory allocation operator
The new operator can be used to create objects of any data-type. It allocates sufficient memory to hold a data object of type data-type and returns the address of the object.
Its general form is,
Pointer variable=new data-type;
• delete Memory release operator When a data object is no longer needed it is destroyed to release the memory space for reuse.
The general form is,
delete pointer variable;
34. List out the advantages of new operator over malloc ().
• It automatically computes the size of the data object.
• It automatically returns the correct pointer type.
• It is possible to initialize the objects while creating the memory space.
• It can be overloaded.
35. Define manipulators. What are the manipulators used in C++?
Manipulators are operators that are used to format the data display. The manipulators used in C++ are
• endl – causes a linefeed to be inserted
• setw – provides a common field width for all the numbers and forces them to be printed right justified
36. What are the three types of special assignment expressions?
• Chained assignment e.g., x = y = 10;
• Embedded assignment e.g., x = (y = 50) + 10;
• Compound assignment e.g., x + = 10;
37. Define implicit conversion.
Whenever data types are mixed in a expression, C++ performs the conversions automatically. This process is known as implicit or automatic conversion.
e.g., m = 5 + 2.75;
38. Define integral widening conversion.
Whenever a char or short int appears in an expression, it is converted to an int. This is called integral widening conversion.
39. What are the control structures used in C++?
• Sequence structure (straight line)
• Selection structure (branching)
_ if – else (two way branch)
_ switch (multiple branch)
• Loop structure (iteration or repetition)
_ do – while (exit controlled)
_ while (entry controlled)
_ for (entry controlled)
40. Define Function Prototyping.
The function prototype describes the function interface to the compiler by giving details such as the number and type of arguments and type of return values. It is the declaration of a function in a program. It is in the following form,
type function – name (argument – list);
where argument – list -> types and names of arguments to be passed to the function
41. What is call by reference?
When we pass arguments by reference, the formal arguments in the called function become the aliases to the actual arguments in the calling function. Here the function works on the original data rather than its copy.
e.g., void swap (int &a, int &b)
{
int t = a;
a = b;
b = t;
}
42. What are inline functions?
An inline function is a function that is expanded in line when it is invoked. Here, the compiler replaces the function call with the corresponding function code. The inline function is defined as,
inline function-header
{
function body
}
43. List out the conditions where inline expansion doesn’t work.
• For functions returning values, if a loop, a switch, or a goto exists
• For functions not returning values, if a return statement exists
• If functions contain static variables
• If inline functions are recursive
44. Why do we use default arguments?
The function assigns a default value to the parameter which does not have
a matching argument in the function call. They are useful in situations where
some arguments always have the same value.
e.g., float amt (float P, float n, float r = 0.15);
45. State the advantages of default arguments.
The advantages of default arguments are,
• We can use default arguments to add new parameters to the existing function.
• Default arguments can be used to combine similar functions into one.
46. Define function overloading.
A single function name can be used to perform different types of tasks. The same function name can be used to handle different number and different types of arguments. This is known as function overloading or function polymorphism.
47. List out the limitations of function overloading.
We should not overload unrelated functions and should reserve function overloading for functions that perform closely related operations.
48. State the difference between structures and class.
By default the members of a structure are public whereas the members of a class are private. Structure is created with the keyword struct and class is created with the keyword class.
49. Define a class.
A class is a way to bind the data and its function together. It allows the data to be hidden from external use. The general form of a class is,
class class_name
{
private:
variable declarations;
function declaration;
public:
variable declarations;
function declaration;
};
51. List the access modes used within a class.
• Private – The class members are private by default. The members declared private are completely hidden from the outside world. They can be accessed from only within the class.
• Public – The class members declared public can be accessed from anywhere.
• Protected – The class members declared protected can be access from within the class and also by the friend classes.
52. How can we access the class members?
The class members can be accessed only when an object is created to that class. They are accessed with the help of the object name and a dot operator. They can be accessed using the general format,
Object_name.function_name (actual_arguments);
53. Where can we define member functions?
Member functions can be defined in two places:
• Outside the class definition – The member functions can be defined outside the class definition with the help of the scope resolution operator.
The general format is given as,
return_type class_name :: function_name (argument declaration)
{
function body
}
Inside the class definition – The member function is written inside the class in place of the member declaration. They are treated as inline functions.
54. What are the characteristics of member functions?
The various characteristics of member functions are,
• Different classes can use the same function name and their scope can be resolved using the membership label.
• Member functions can access the private data of a class while a nonmember function cannot.
• A member function can call another member function directly without using a dot operator.
55. How can an outside function be made inline?
An outside function can be made inline by just using the qualifier ‘inline’ in the header line of the function definition.
The general format is,
inline return_type class_name :: function_name (argument declaration)
{
function body
}
56. What are the properties of a static data member?
The properties of a static data member are,
• It is initialized to zero when the first object is created and no other initialization is permitted.
• Only one copy of that member is created and shared by all the objects of that class.
• It is visible only within the class, but the life time is the entire program.
57. What are the properties of a static member function?
• A static member function can access only other static members declared in the same class.
• It can be called using the class name instead of objects as follows,
class_name :: function_name;
58. How can objects be used as function arguments?
An object can be used as a function argument in two ways,
• A copy of the entire object is passed to the function. (Pass by value)
• Only the address of the object is transferred to the function. (Pass by reference)
59. Define friend function?
An outside function can be made a friend to a class using the qualifier ‘friend’. The function declaration should be preceded by the keyword friend. A friend function has full access rights to the private members of a class.
60. List out the special characteristics of a friend function.
• It is not in the scope of a class in which it is declared as friend.
• It cannot be called using the object of that class.
• It can be invoked without an object.
• It cannot access the member names directly and uses the dot operator.
• It can be declared as either public or private.
• It has the objects as arguments.
61. Define Constructor.
A constructor is a special member function whose task is to initialize the objects of its class. It has the same name as the class. It gets invoked whenever an object is created to that class. It is called so since it constructs the values of data members of the class.
62. List some of the special characteristics of constructor.
• Constructors should be declared in the public section.
• They are invoked automatically when the objects are created.
• They do not have return types
• They cannot be inherited.
63. Give the various types of constructors.
There are four types of constructors. They are
• Default constructors – A constructor that accepts no parameters
• Parameterized constructors – The constructors that can take arguments
• Copy constructor – It takes a reference to an object of the same class as itself as an argument
• Dynamic constructors – Used to allocate memory while creating objects
64. What are the ways in which a constructor can be called?
The constructor can be called by two ways. They are,
• By calling the constructor explicitly
e.g., integer int1 = integer (0, 100);
• By calling the constructor implicitly
e.g., integer int1 (0, 100);
65. State dynamic initialization of objects.
Class objects can be initialized dynamically. The initial values of an object may be provided during run time. The advantage of dynamic initialization is that various initialization formats can be used. It provides flexibility of using different data formats.
66. Define Destructor.
A destructor is used to destroy the objects that have been created by a constructor. It is a special member function whose name is same as the class and is preceded by a tilde ‘~’ symbol.
67. Give the general form of an operator function.
The general form of an operator function is given as,
return-type class-name :: operator op (arglist)
{
function body
}
Where,
Return-type -> type of value returned
operator -> keyword
op -> operator being overloaded
68. List some of the rules for operator overloading.
• Only existing operators can be overloaded.
• We cannot change the basic meaning of an operator.
• The overloaded operator must have at least one operand.
• Overloaded operators follow the syntax rules of the original operators.
69. What are the types of type conversions?
There are three types of conversions. They are
• Conversion from basic type to class type – done using constructor
• Conversion from class type to basic type – done using a casting operator
• Conversion from one class type to another – done using constructor or casting operator
70. What are the conditions should a casting operator satisfy?
The conditions that a casting operator should satisfy are,
• It must be a class member.
• It must not specify a return type.
• It must not have any arguments.
71. What are the types of inheritance?
The various types of inheritance are,
• Single inheritance
• Multi-level inheritance
• Multiple inheritance
• Hierarchical inheritance
• Hybrid inheritance
72. Give the syntax for inheritance.
The syntax of deriving a new class from an already existing class is given by,
class derived-class : visibility-mode base-class
{
body of derived class
}
73. Define single inheritance.
In single inheritance, one class is derived from an already existing base class.
Here A is the base class and B is the derived class.



 

 

74. Define multi-level inheritance.
n multi-level inheritance, a new class is derived from a class already derived from the base class.Here, class B is derived from class A and class C is further derived from the derived class B.
 75. Define multiple inheritance.
In multiple inheritance, a single class is derived from more than one base class.
ere class C is derived from two base classes A and B.



76. Define Hierarchical inheritance.
In hierarchical inheritance, more than one class is derived from a single base class.
Here class B and C are derived from class A.



77. Define Hybrid inheritance.
Hybrid inheritance is defined as a combination of more than one inheritance.
78. What is a virtual base class?
Here, class D inherits both classes B and C which are derived from the same base class A. Hence D has two copies of the properties of class A. This can be avoided by declaring classes B and C as virtual base classes.
79. What is an abstract class?
An abstract class is one that is not used to create objects. It is designed only to act as a base class to be inherited by other classes.
80. What are the types of polymorphism?
The two types of polymorphism are,
• Compile time polymorphism – The compiler selects the appropriate function for a particular call at the compile time itself. It can be achieved by function overloading and operator overloading.
• Run time Polymorphism - The compiler selects the appropriate function for a particular call at the run time only. It can be achieved using virtual functions.
81. Define ‘this’ pointer.
A ‘this’ pointer refers to an object that currently invokes a member function. For e.g., the function call a. show () will set the pointer ‘this’ to the address of the object ‘a’.
82. What is a virtual function?
When a function is declared as virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type of the pointer.
83. What is a pure virtual function?
A virtual function, equated to zero is called a pure virtual function. It is a function declared in a base class that has no definition relative to the base class.
84. How can a private member be made inheritable?
A private member can be made inheritable by declaring the data members as protected. When declared as protected the data members can be inherited by the friend classes.
85.Write some of the basic rules for virtual functions
·         Virtual functions must be member of some class.
·         They cannot be static members and they are accessed by using object pointers
·         Virtual function in a base class must be defined.
·         Prototypes of base class version of a virtual function and all the derived class versions must be identical.
·         If a virtual function is defined in the base class, it need not be redefined in the derived class.
86. What are pure virtual functions? Write the syntax.
A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such cases, the compiler requires each derived class to either define the function or redeclare it as a pure virtual function. A class containing pure virtual functions cannot be used to declare any object of its own. It is also known as “donothing” function.
The “do-nothing” function is defined as follows:
virtual void display ( ) =0;