CS304 MCQS+ Notce For Final Term
CS304 MCQS+ Notce For Final Term
Q 2: Can a constructor throw an exception? How to handle the errors, when the constructor fails?
Yes, Constructor can throw an exception But In constructor if an exception is thrown than all objects
created so far are destroyed. Exception due to constructor of any contained object or the constructor of a
parent class can be caught in the member initialization list.
They take actual current object name, rather than value in their argument.
Q 4: What is graceful termination method of Error handling; give its example using C++.
Program can be designed instead of unusual termination, that causes the wastage of resources, program
performs clean up tasks, mean we add check for expected errors (using if conditions), Example – Graceful
Termination
Q 5: How can you describe Virtual destructor? Why Virtual destructor is used?
When delete operator is applied to a base class pointer, base class destructor is called. Only base class
object will be deleted other parts will remain than as a result in memory leakage (wastage of memory),
Virtual Destructors make it possible that complete object will be deleted so there is no memory leak
(waste of memory).
Q 6: Motor car can be called a good candidate for being an object so, this object contains
Encapsulation characteristic. You are required to apply the concept of Encapsulation on “Motor
Car”.
Yes Motor Car is a good candidate for apply the concept of encapsulation In Motor Car we have the
following Objects which fully describes the phenomena of encapsulation.
1. Steer Wheels 2. Accelerate 3. Change Gear 4. Apply Brakes 5. Turn Lights On/Off
In encapsulation we just provide the user an interface and hide the Full implementation because user don’t
want to know the implementation he just want about his work should be done perfectly.
Q 8: How can we set the default values for non type parameters?
We can set default value for these non type parameters, because we can passed parameters in ordinary
functions,
Template< class T, int SIZE = 10 >
Class Array { Private: T ptr
[SIZE]; Public:
Void doSomething ();
…}
Q 10: Tell the procedure of making a class abstract in C++ by giving an example.
In C++, we can make a class abstract by making its function(s) pure virtual. On the other hand a class
with no pure virtual function is a concrete class. For example, Class Shape {
…
Public:
Virtual void draw () = 0 ;}
Q 12: Give the pseudo code of non case sensitive comparison function of string class.
Int caseSencompare (char* str1, char* str2) {
For (int i = 0; i < strlen (str1) && i < strlen (str2); ++i)
If (str1 [i]! = str2 [i])
Return str1 [i] - str2 [i];
Return strlen (str1) - strlen (str2) ;}
Q 14: Mobile is a good entity for being an object. Write characteristics, behavior and unique ID.
Characteristics: Buttons, Battery, Screen
Behavior: Calling, Sending Message, Internet, Multimedia
Unique ID: Nokia
Q 15: What are the container requirements? Write details.
As each container need to perform certain operations on the elements like their copy or their comparison
and their sorting, so the elements that we add in containers should provide these kind of basic functions.
Examples of these functions are given below,
When an element is inserted into a container, a copy of that element is made using, P-2 Copy
Constructor or Assignment Operator.
Containers and many algorithms compare elements.
C++ does not provide function of comparison operator (==) or less than operator (<) by itself so we have
to provide these function in element class if we use containers.
Q 17: What is random-iterator? What relation b/w random iterator and vector.
Random Access Iterators: -
They have all the capabilities of bidirectional Iterators plus they can directly access with element of a
container. Vectors are as a smart array. They manage storage allocation for you, accessing elements with
the [] operator. Such random access is very fast with vectors. It’s also fast to add a new data item in the
end of the vector. When this happens, the vector’s size is automatically increased to hold the new item.
Q 20: Give the name of three operation that a cursor or iterator generally provide.
T* first ()
T* beyond ()
T* next (T*)
Q 25: what are container classes? How many types of container classes are there?
Container is an object that contains a collection of data elements. STL (Standard Template Library)
provides three kinds of containers,
1. Sequence Containers: 2. Associative Containers: 3. Container Adapters:
Q 27: Can a constructor throw exception? If it fails, how should this error is handled? One-stage
constructors should throw if they fail to fully initialize the object. If the object cannot be initialized, it
must not be allowed to exist, so the constructor must throw.
Q 31: Fill in the blanks below with public, protected or private keyword.
Answer:
Public members of base class are _____ public _____ members of derived class.
Protected members of base class are _____ protected _____members of derived class.
Q 32: What is the difference (if any) between the two types of function declarations?
Template function_declaration;
Template function_declaration; P-4
The format for declaring function templates with type parameters is:
Template <class identifier> function_declaration;
Template <typename identifier> function_declaration;
The only difference between both prototypes is the use of either the keyword class or the keyword
typename. Both expressions have exactly the same meaning and same way.
Q 33: State any two reasons why the virtual methods cannot be static?
The virtual method implies membership; a virtual function cannot be a nonmember function. So a virtual
function is not static, a virtual function declared in one class can be declared a friend in another class.
Q 37: In which situation do we need to implement Virtual inheritance? Explain with an example. In
multiple inheritance while solving diamond problem virtual inheritance need to implement. It can be used
in the situations when programmer wants to use two distinct data members inherited from base class. For
example, Class Vehicle { Protected:
Int weight;
};
Class LandVehicle: public virtual Vehicle { };
Class WaterVehicle: public virtual Vehicle {
};
Q 38: if iter is an iterator to a container. Write an expression that will have the value of the object
pointed to by iterator, and will then cause iterator to point to the next element. *iter++
Q 40: Sort data in the order in which compiler searches a function. Complete specialization, generic
template, Partial specialization, Ordinary function.
First of all compiler will look for complete specialization. If it cannot find any required complete
specialization then it searches partial specialization. In the end it searches some general template.
Q 41: Give the names of two types of containers basically known as first class containers. Sequence
and associative containers are referred to as a first-class container.
Q 42: Describe the way to declare a template function as a friend of any class.
Rule 4 says that when we declare a template as friend of any class then all kinds’ specializations of that
function – explicit, implicit and partial, also becomes friends of the class granting friendship.
Q 51: State any conflict that may rise due to multiple inheritances?
If more than one base class has a function with same signature then the child will have two copies of that
function. Calling such function will result in doubt.
Q 52: What will be the output after executing the following code?
Class c1 { Public:
Virtual void function () {
cout<<”I am in c1”<<endl ;}};
Class c2: Public c1 { Public:
Void function () {
cout<<”I am in c2”<<endl ;}};
Class c3: public c1 { Public:
Void function () { P-7
cout<<”I am in c3”<<endl ;}}; Int main (){ c1 * test1 = new c2 (); c1 * test2 = new c3 (); test1-
>function (); test2->function (); System (“PAUSE”); Return 0 ;} Answer:
Am in c2
I am in c3
Q 53: Write the syntax of declaring a pure virtual function in a class? Class
Shape {
…
Public:
Virtual void draw () = 0 ;}
Q 56: Can we use compiler generated default assignment operator in case our class is using
dynamic memory? Justify your answer.
In case our class involves dynamic memory allocation we write assignment operator code by our self
because we write the user defined code for copy constructor.
Q 61: Give code of template function to print the value of any type of array.
Template<Typename T>
Void PrintArray (T* array, int size)
{
For (int i = 0; i<size; i++)
Cout<<Array[i] <<,” ”; // Here data type of array is T} P-8
Q 63: In which situation we should use operator overloading and in which situation we should use
Templates?
To use operator overloading in situation where we want to perform basic operations (like addition,
subtraction, multiplication, division and so on), C++ allows us to overload common operators like +, - or
* etc.
The situation in which we are required generic programming in C++ then to use Templates.
Q 64: Define public and private inheritance with examples.
Private Inheritance:
We use private inheritance when we want to reuse code of some class. Private Inheritance is used to
model “Implemented in terms of” relationship. For Example,
Suppose we have a class collection to store element collection as shown below, Class
Collection {
...
Public:
Void AddElement (int);
Bool SearchElement (int);
Bool SearchElementAgain (int);
Bool DeleteElement (int);
};
Public Inheritance:
In case of public inheritance it is “IS-A” relationship.
Q 67: While overloading stream insertion and extraction operators a programmer cannot declare
ostream and istream referenced as a constant why?
Ostream reference cannot be const as it store the data in its buffer to insert on output stream, istream cannot be
constant and istream buffer will change. Complex object cannot be constant for stream extraction operator
because we will add data to it.
Q 70: List down any three cases in which copy constructors are called. P-9 When
one object is initialize with another object.
When passing an object by value.
When an object is return from a function by value.
Q 71: Name the technique which is used to implement Generic Algorithms in C++. Generic
algorithms in C++ are written using C++ templates.
Q 72: Binding is the process that connects the function call to function implementation you are
required to name the type of binding in which this process is done at run time instead of compile
time.
The simplest form of run-time binding is polymorphism. In context of C++ polymorphism is achieved
through virtual functions.
Q 75: If we declare a function as friend of a template class will it be a friend for a particular data
type or for all data types of that class.
If a function is defined as a friend function then, the private and protected data of a class can be accessed
using the function. A nonmember function cannot access an object’s private or protected data. A friend
can be a function, function template, or member function, or a class or class template, in which case the
entire class and all of its members are friends.
Q 76: Suppose the base class and the derived class each have a member function with the same
signature. When you have a pointer to a base class object and call a function member through the
pointer, discuss what determines which function is actually called, the base class member function
or the derived-class function?
You can call a derived class member function through a pointer to a base class as the method is virtual
method.
Q 77: Write down the Advantages of Constructor and destructors in respect of class with no
dynamic data members?
Constructor:
A constructor is a special member function to initialize the objects of its class.
They are called automatically when the objects are created.
They do not have return types and they cannot return values.
Like other C++ functions, Constructors can have default arguments.
Constructors cannot be virtual constructor.
Destructor:
A destructor is used to destroy the objects that have been created by a constructor.
Like constructor, the destructor is a member function.
A destructor never takes any argument nor does it return any value. P-10
Q 78: Can we initialize a class if a constructor and destructor are declared as private? Justify. If
the constructor/destructor is declared as private, then the class cannot be represent. This is true;
however it can be represent from another method in the class. Similarly, if the destructor is private, then
the object can only be deleted from inside the class as well.
Q 79: Suppose person class is an abstract class which has some specific features. Briefly describe
important features of abstract class.
An abstract class cannot be representing.
An abstract class may contain abstract methods and assessors.
It is not possible to modify an abstract class with the sealed modifier because the two modifiers have
opposite meanings.
Q 81: Can derive template class take more parameters than parent template?
No, Template class derives from another template class, and where a template class derives from a
template class which shares one or more template parameters with it.
P-11