C++ Assignment
1) Difference between C and C++ ?
C C++
It is procedural oriented It is procedural and object oriented
Function is the building block of c Object is the building block of C++
programming,so it is function driven programming,so it is object driven
C doesn’t use reference variable C++ have use reference variable
We can’t use function inside the structure is not We can use the funtion inside the structure is
possible possible
Data is not secured Data is secured
2) How many keywords are present in C++?
Ans: Totally 63 keyword are present in C++
3) What is C++?
Ans: It is an extension and implementation of new features in C
4) What is the goal of C++?
Ans: To provide Data security
5) What are the features available in C++?
Ans:
Encapsulation
Data Abstraction
Polymorphism
Inheritance
6) what is meant by Encapsulation?
Ans: Encapsulation is the mechanism that binds together data and the member
function in a single unit(class) and keeps that secure from outside interference
and misuse
7) what is meant by Data Abstraction?
Ans:It refers to the act of essential features and hiding the background
details
8) what is meant by Polymorphism?
Ans: A single entity can exist in more than one form.In c++ it is divided
into two types
1)Compile time polymorphism
a)Function overloading
b)Operator overloading
2)Runtime polymorphism
a)Virtual function
9) What is meant by inheritance ?
Ans: It is the process of acquiring the properties of existing
class(base class) into the new class(Derived class)
10) Benefits of Inheritance?
Ans: 1)code reusability and extensibility
2)Ability to add extra features to the derived class without
disturbing the base class
11) what is meant by iostream ?
Ans: Combination of istream and ostream is called iostream.
C++ deals the input and output operation with streams?objects.
12) cout & cin in C++ are overloaded functions ?
Ans: Yes, cout and cin are overloaded functions.
13) cout and cin are predefined object?
Ans: cout and cin are predefined object and present inside the std
namespace library.
14) What are the manipulators available in C++?
a) endl
b) hex
c) dec
d) octal
15) using namespace std ?
using --> directive
namespace --> keyword
std --> namespace name
16) what is meant by Namespace?
Ans:
Namespace is the keyword which is used to avoid the name collision for global
variable
17) what is meant by Using namespace std?
Ans: using-------------> Directive
namespace------> keyword
std-----------------> namespace name
18) what is syntax of Namespace?
Ans:
namespace namespace_identifier
{
--------------
}
19) The operator >> is called____Extraction__________.
20) The operator << is called___Insertion___________.
21) __Scope resolution operator__ an operator, which is
used to access global version of data.
22) what is meant by reference variable?
Ans: It is an alias name for existing variable which are pointing
the same memory location
23) Difference between pointer vs Reference?
POINTER REFERENCE VARIABLE
The pointer points address of the variable,which It is alias name of existing variable that are
are stored in a seperate memory location stored in a same memory location
It can be initilazed at any time It should be initailiazed at the time of declaration
Pointer can point the NULL It can’t be NULL
Pointer can be pointed the object at any time Reference variable can’t pointed to another
memory
24) Types of New DataType in C++?
Ans: bool
string
wchar_t
25) A function declared as _Inline function__indicates to the compiler that the
body of the function can be substituted in place of the function call.
26) _Encapsulation_ means that both the data and the methods which
may access it are defined together in the same unit.
27) Size of an object is equal to_size of the data member____
28) Difference between Malloc and New?
NEW MALLOC
New is a operator Malloc is a function
Size of operator is not required Size of operator is required
Memory initialization is possible Memory initialization is not possible
Typecasting is not required Typecasting is required
29) what is meant by Mutable in C++?
Ans: Mutable is a keyword,even though the variable is constant
using mutable we can modified it.
30 ) what is meant by Function overloading ?
Ans: Defining multiple function with same name but
arguments are different is called function overloading.
31) What is meant by Name mangling?
Ans: Namemangling is the technique used by compiler that
provides name to the function based on the argument type.
32) Rules of Function Overloading in C++ ?
Ans: 1. These functions have different parameter type
2. These function have different number of parameter
3. These function have different sequence of parameter
33) what is meant by Ambiguity issue to function bind ?
Ans: In function overloading,whenever the function is called,the compiler tries to bind their
exact argument matching function art some times compiler gets confused which one
is going to execute first,then at that time compiler will generate the ambiguity error.
34) what is meant by Default Arguments?
Ans: A default argument is a function parameter that has a default value is
provided to it.
35) Advantages of default Arguments ?
Ans: It provide greater flexibility to the programmers
Default argument can be used to add new parameter to the existing
function
It can also be used to combine similiar function into one.
36) what is meant by Anonymous classes in C++ ?
Ans: Anonymous class is a class which has no name is given to it
37) difference between member function and non-member function?
Ans:
MEMBER FUNCTION NON MEMBER FUNCTION
It is part of the class It is not a part of the class
It is possible to access structure member It is possible to access structure member with
directly dot or arrow operator
Member function is called with dot or arrow Non member function are called directly
operator
38) what are the C++ access specifiers?
Ans: Public
Protected
Private
39) Difference between structure in c vs structure in C++?
STRUCTURE IN C STRUCTURE IN C++
Functions are not allowed inside the structire Funtions are allowed inside the structure
definition definition
Empty structure size is zero byte Empty structure size is one byte
40) Difference between structure in c++ vs class in C++ ?
STRUCTURE IN C++ CLASS IN C++
By default public access specifer to the members By default private access specifiers to the
members
By default,public mode of inheritance By default private mode of inheritance
41) what is meant by class?
Ans: It is an advanced structure in c++,which contains data and member
function
42) why 1byte is allocated to empty stuct/class in c++?
Ans: To show the object existance
In c++,if object is created atleast with 1 byte,then it is possible
to call the member functions
43) what is meant by Friend function?
Ans: Friend function is the non member function which is used to access the
private data member of a structure or class
44)what is meant by Friend class?
Ans: Friend class is a class, which is used to access the private member of
another class which it belong as friend
45) difference between member function vs friend function ?
MEMBER FUNTION FRIEND FUNCTION
It is a part of the class It is not a part of the class
This pointer concept is available There is no this pointer
Member function should be called with object Friend funtion should be called directly
and dot operator
It is possible to access class member directly It is possible to access member onlywith object
and dot operator
The scope of member function belong to same The scope of friend funtion can be many no of
class class
46) A __Constructor__ helps to perform member wise
initialization.
47) A Copy Constructor takes ____One____arguments.
48) A destructor takes ____Zero_____arguments.
49) Destructors can be overload(true/false) ?Ans: False
50) __Friend__ function and __Friend_ classes breaks the rules of the data
hiding in C++.Ans: Friend function and Friend class
51) …Friend Function...can be invoked like a normal function without the help
of any object.
52) Can I create new operators using operator overloading?
a) Yes
b) NO
53) For overloaded function, the compiler chooses the right specific version on
the basis of the parameter, is
A) Type B) order C) number D) all
54) The function that can not modify the object that invokes it, is said to be---
Const Member function--------
55) Why reference is not same as a pointer?
A) A reference can never be null
B) A reference once established cannot be changed.
C) Reference doesn't need an explicit dereferencing mechanism.
d) All of the above.
56) If class A is a friend of B, then B doesn’t become a friend of A automatically
(True / False) Ans: True
57) A function with the same name as the class, but preceded with a tilde
character (~) is called ___Destructor_______ of that class.
58) How many times a constructor is called in the life-time of an object?
Ans: Only once
59) How many times a destructor is called in the life-time of an object?
Ans: Only once
60) Write the syntax for copy constructor.
Ans: Classname (Classname & ob)
{
--------------
}
61) what is meant by Constructor ?
Ans: A constructor is a special member function of a class,which is
invoked automatically when the object of the same class is created,the
object gets initialized automatically by the constructor
62) Types of Constructor
Ans: 1)Default constructor
2)Parameterized constructor
3)Dynamic constructor
4)Copy constructor
63)what are the default member functions are provided by compiler
in a class?
Ans: 1. Default constructor
2. Copy constructor
3. Assignment operator overload function
4. Destructor
64) what is meant by Destructor ?
Ans: A Destructor is a special member function of a class which is
automatically invokes to destroy the object when the scope of the object is
completed.
65) syntax of Destructor?
Ans: ~classname(void);
66) difference between shallow copy and Deep
copy?
SHALLOW COPY DEEP COPY
It is type of copy,where an object is getting It is type of copy,where an object is getting
initalized with another object with the help of intialized with another object with the help of
compiler will provided an implicit copy user will provided an explicit copy constructor
constructor
If an object contains data members as pointer If an objects contains data member as pointer
then shallow copy will create a problem then deep copy will not create a problem
Both objects are pointing to same memory Both objects are pointing to different memory
location location
If we modified one object then automatically If we modified one object then automatically
another object gets affected another object will not be affected
67) what is meant by this pointer?
Ans: this pointer is a keyword, which is used to store the address of the
object that invokes member functions,whenever the member function is
called this pointer is an implicitly holds the address of the object
68) difference between Assignment operator
overloading vs copy constructor?
Ans:
In copy constructor, at the time of object creation
object1 data is copied into object2 data.
Ex: A ob1(10),ob2;
A ob2=ob1;//internally A ob2(ob1)
In Assignment operator, first object is intialized with
garbage value,then after object1 data is assigned to
object2
Ex. A ob1(10),ob2;
ob2=ob1;//internally ob2.operator=(ob1)
69) What is meant by operator overloading?
Ans: The facilities of giving a special meaning to the operator
without changing the existing meaning is referred to as operator
overloading
70) write a syntax Overload pre-increment operator(++) using member
function:
Ans:
return type operator ++(arg..)
{
--------------
}
71) write a syntax Overload pre-increment operator(++) using friend
function ?
Ans: return type operator ++(classname & ob)
------------------
72) Overload post-increment operator(++) using member function ?
Ans:
return type operator ++(int )
{
--------------
}
73) Overload post-increment operator(++) using friend function ?
Ans:
return type operator ++(classname & ob,int)
{
---------------
}
74) Overloaded operators only through member function ?
Ans:
1. =(Assignment)
2. ()(Function call)
3. [](Subscripting)
4. ->(Arrow)
75) Friend function cannot be overload the following operators?
Ans:
1. =(Assignment)
2. ()(Function call)
3. [](Subscripting)
4. ->(Arrow)
76) Why ' = ' operator can't be overloaded with friend function?
Ans:
If we define the member function for “=” (Assignment operator),then it becomes
ambigity error for compiler,when we don’t define member function then compiler will
supply assignment operator overload function.
77) Why [ ], ( ), -> operators can't be overloaded by friend functions?
Ans: Overloading these operators are having limitations to the
language design to maintain the data security.
78) Operators that can be overloaded only by friend function?
Ans: >> ------ Extraction operator
<< ------ Insertion operator
79) Operators that cannot be overloaded by member function?
Ans: >> ------ Extraction operator
<< ------ Insertion operator
80) Q) Why '<<' & '>>' operators can't be overloaded with member functions ?
Ans: Whenever ‘<<’ and ‘>>’ operator are overloaded, it would require that the
function be the part of the istream/ostream class, not a part of the class.since we
are not allowed to modified the istream or ostream class.
81) Why cout and cin are being passed as reference objects while overloading
insertion and extraction operators?
Ans: As the copy constructor and overloaded assignment operator in
the classes ‘istream’ and ‘ostream’ are declared in protected
session,so their objects cout and cin are passed as reference.
82) Q) What happence if 'cout' and 'cin' are passed as a non-reference
arguments ?
Ans: If cout and cin are passed as non-reference objects,then the a new
object is created on the stack or data section and tries to calling the copy
constructor of istream or ostream which are in protected section. As they
are declared in the protected section the copy constructor cannot be called
outside the class scope.
83) Operators that can't be Overloaded
Ans: 1) .(dot)Member selection operator
2) .*(member selection through pointer to member operator)
3) sizeof operator
4) ::(scope resolution operator)
5) ?:(conditional operator)
84) List of operators that can be overloaded
Ans:
operator that can be overload only by member functions
1. =(Assignment)
2. ()(Function call)
3. [](Subscripting)
4. ->(Arrow)
operator that can be overload only by friend function
1. <<(Insertion)
2. >>(Extraction)
85) what is meant by static data member?
Ans: 1)If data member is declared as static means,just one copy of this data member
is created for this entire class,since it is shared all the object of that class
2) Memory initialization is only once
86) difference between static data member vs non-static data member>
STATIC DATA MEMBER NON STATIC DATA MEMBER
Just one copy of the data member is created for Each objects contains own data member which
the entire class,it can be shared all the objects of are stored in seperate memory location
that class
It is a part of the class It is a part of the object
Static data member should not be initialized Non-static data member should be initialized
through constructor through constructor
To reduce the memory space Increases the memory space
87) what is meant by static member function?
Ans: It is likely an ordinary function declaration,and it is a part of the class
Syntax:
static return type fun_name(arg)
{
---------------
}
88) what is meant by inheritance?
Ans: An inheritance is the process of the acquiring properties of the existing
class(Base class) to the new class(Derived class).
It is a feature of a process in which new classes are created from the existing
class.
89) Can I inherit the constructor and destructor of a base class?
a)yes
b) NO
90) In an inheritance hierarchy of classes
a) Constructors are invoked top-down and destructors are invoked top-down
b) Constructors are invoked bottom-up and destructors are invoked top-down
c) Constructors are invoked top-down and destructors are invoked bottom-up.
d) Constructors are invoked bottom-up and destructors are invoked bottom-up.
91) Advantages of inheritance
Ans: 1. code reusability and extensibility
2.Ability to add the extra features to the derived class without disturbing the
base class.
92) what is the size of derived class and sizeof base class ?
Ans: Both are same size,until without creation of extra data members in
derived class
93) Types of inheritance?
Ans:
1. Single Inheritance
2. Multi-Level Inheritance
3. Multiple Inheritance
4. Hybrid Inheritance
5. Hierarchical Inheritance
6. Multipath Inheritance
94)what is meant by Virtual Base class ?
Ans: Virtual base classes are used to avoid duplication of data.
95) what is meant by function overriding?
Ans:The mechanism of providing same function name with same
argument in a base class as well as derived class is called
function overriding.
96) difference between function overloading vs function overriding?
FUNCTION OVERLOADING FUNCTION OVERRIDING
Defining multiple function with same name but The mecahnism of providing same function
argument are different is called function name with same arguments in a base class as
overloading well as derived class is called function
overriding
An example of compile time polymorphism An example of runtime polymorphism
Function signatures should be different Function signature should be same
In function overloading, don’t need of In function overriding, we need inheritance
inheritance concept
Overloading function are in same class Overridden function are in different class
97) what is meant by function binding?
Ans: Resolving a function call with its proper function definition is called function
binding
It is of two types:1.compile time function binding
2.Runtime function binding
98) What is meant by Compiletime Function binding ?
Ans: Resolving a function call with its proper function definitions at compile
time is called compile time function binding.
Ex.operator overloading,Function overloading
99) What is meant by Runtime Function binding ?
Ans:
Resolving a function call with its proper function definition at runtime is
called runtime function binding
Ex: Virtual function
100) what is meant by Early binding?
Ans: Resolving a function call with its proper function definitions at compile
time is called compile time function binding.
Ex.operator overloading,Function overloading
101) what is meant by late binding?
Ans:
Resolving a function call with its proper function definition at runtime is
called runtime function binding
Ex: Virtual function
102) what is meant by virtual function?
Ans: Virtual function are the c++ language feature to achieve the runtime
polymorphism.
Virtual function is the function which is must present in base class,which may be
overridded in a derived class and which informs the compiler to perform
runtime polymorphism
103) what is meant by vtable?
Ans: To implement the virtual function,c++ uses a special form of late
binding is called virtual table created in compile time.
The virtual table is a lookup table of addresses of only the virtual
function in the corresponding class to resolve the function with the
proper function call.
104) what is meant by pure virtual function?
Ans:
Definitions are not available,only declaration available in base class is known as
pure virtual function.
105) syntax of pure virtual function?
Ans: virtual return_type fun_name(arg)=0;
or
return_type virtual fun_name(arg)=0;
106) what is meant by Abstract class?
Ans: If a class contains atleast one pure virtual function is called Abstract class.
107) Rules for Virtual Functions:-
Ans: 1.Virtual function should be non-static data member
2.Virtual function are accessed by using pointer to objects
3.Constructor can’t be virtual,but destructor should be virtual
4.Virtual function should be declared in public section
5.Base class pointer can holds the address of derived class
object address,but reverse is not possible
.
108) why an object can't be created in Abstract class?
Ans:
A class having pure virtual function cannot be used to instantiate object
of its own.
109) what is meant by virtual destructor?
Ans: It is destructor, to makes a proper destructor calls from derived
class to base class for dynamically created object.
110) Types of Templates
Ans: Function Templates
Class Templates
111) what is meant by function Template?
Ans: It is a generic funtion
We can create a single function,which can operate an many datatypes is
called function template
It allows programmer to write generic functions
Syntax:
template <class type>
return_type function_name( type T)
{
---------------
}
112) difference between function overloading vs function
Templates?
FUNCTION OVERLOADING FUNCTION TEMPLATE
Defining multiple function with same name but Defining a single function which can operate at
different argument any data types
Code dupilcation is acquire No code duplication acquire
Code size is more Code size is small
113) what is meant by class Templates?
Ans: It is generic class
We can create a single class,which can operate many
datatypes,instead of create a seperate class for each type,it is
called class Templates
114) A template function can be called by value as well as reference.
a) True
b) false
115) Code that you want to monitor for exceptions must have been executed from
within a _____try______
116) Which of the following operator is overloaded for object cout?
a) <<
b) >>
c) 0
d) =
117) If abstract class is inherited by derived class, then_______________ .
a) Derived class should provide definition for all the pure virtual functions
b) Derived class also become abstract if fails to implement pure virtual
functions
c) Objects of derived class can’t be created if it fails to implement pure virtual
functions
d) All of these
118) A virtual function that has no definition within the base class is called
___Pure virtual function________
119) what is meant by Exception handling?
Ans: It is an event,which occurs at runtime and which interrupt the normal flow of
program execution.
Exception which occurs at runtime due to unusual condition
To handle such exception,exception handling mechanism is used.
120) what are the three keywords in Exception handling ?
Ans:
try
catch
throw
121) What is meant by try block?
Ans:
A try block will throw the exception by using throw keyword.
Syntax:
try
{
throw exception_occured;
}
122) what is meant by throw ?
Ans:
throw is a keyword is used to raise the exception when an error is
generated.
123) what is meant by catch block?
Ans: catch block will handle the exception condition which is sent by
throw
syntax:
try
{
throw exception;
}
catch(data_type arg)
{
-----------
}
124) what is Generic catch block ?
Ans: Generic catch block will catch any type of data when
an exception is occurs
Syntax:
catch(...)
{
------------
}
125) Whenever unhandled exception was rasied then it will call one
standard library function is ___terminate____ function
126) cout is a
a) operator
b) function
c) object
d) macro
127) An exception is caused by
a) a hardware problem
b) a problem in the operating system
c) a syntax error
d) a run time error
128) #include<iostream>
using namespace std;
class A
{
A()
{
cout<<”Constructor”<<endl;
}
~A()
{
cout<<”Destructor”<<endl;
}
};
main()
{
A obj1;
}
Ans: Error
129) Throwing an unhandled exception causes the standard
library ___Terminate function______ to be invoked
130) Can constructors be overloaded? (True/False)
Ans: True
131) ____>>(Extraction)____ operator is used to read the data from a file?
132)which of the following object manages input from file?
a) istream
b) stream
c) ifstream
d) iostream
133) A class is_____
a) Data Type
b) Abstract Type
c) User Defined Type
d) All of these options
134) write down syntax of function template?
Ans: template<class Type>
return_type function_name(Type T)
---------------
135) write down syntax of class template?
Ans: template<class Type>
class class_name
Type t;
Type a;
};
136) Define object of template class :
classname<datatype>object
137 ) what is meant by single inheritance?
Ans: Derived class is inherited by only one base class.
138) what is meant by multilevel inheritance?
Ans: Derivation of a class from another derived class.
139) what is meant by Multiple inheritance?
Ans: Derived class is inherited by more than one base class
140) what is meant by Heirarchical inheritance?
Ans: Derivation of multiple derived class from same base
class
141) what is meant by Hybrid inheritance?
Ans: Derivation of a class involving more than one form of
inheritance
142) what is meant by Multipath inheritance?
Ans: Derivation of a class from derived class which are derived
from same base class.