Cs304 Oop Compiled MCQ
Cs304 Oop Compiled MCQ
Cs304 Oop Compiled MCQ
MIDTERM EXAMINATION
Spring 2010
CS304- Object Oriented Programming
Time: 60 min
Marks: 38
Which one of the following terms best represents the statement given below,
► Obfustication.
► Data Mining.
► Compilation.
► Encapsulation
Question No: 2 ( Marks: 1 ) - Please choose one
► Protection
► Encapsulation
► Argumentation
► Abstraction
1. Name
2. Past experience
3. Age
4. Relatives
Keeping in view the principle of abstraction, which of the above information the company needs
to save as employee’s record?
► 2, 4
► 1, 3, 5
► 1, 2, 3
► 1, 2, 3, 4
► All of given
► inheritance
► encapsulation
► structure
► array
Your chief Software designer has shown you a sketch of the new Computer parts system she is
about to create. At the top of the hierarchy is a Class called Computer and under this are two
child classes. One is called LinuxPC and one is called WindowsPC. The main difference between
the two is that one runs the Linux operating System and the other runs the Windows System (of
course another difference is that one needs constant re-booting and the other runs reliably). Under
the WindowsPC are two Sub classes one called Server and one Called Workstation. How might
you appraise your designers work?
► Give the goahead for further design using the current scheme
► Ask for a re-design of the hierarchy with changing the Operating System to a field rather
than Class type
► Change the hierarchy to remove the need for the superfluous Computer Class.
class Fred {
public:
Fred();
...
};
int main()
Fred a[10];
Fred* p = new Fred[10];
...
► Produce an error
► New operator
► Delete operator
► True
► False
When a variable is define as static in a class then all object of this class,
► None of given
_______ remain in memory even when all objects of a class have been destroyed.
► Static variables
► Instance variable
► Primitive variables
► None of given
int Age=33;
How you can improve above class with respect to accessing the field Age?
► Define the variable Age as private and create a get method that returns it and a set method
that updates it
► Define the variable Age as protected and create a set method that returns it and a get
method that updates it
Question No: 13 ( Marks: 1 ) - Please choose one
The life of sub object is not dependant on the life of master class in ___________.
► Composition
► Aggregation
► Separation
► operator
► B_op
► const
► None of given
► True
► False
Identify which of the following overloaded operator function’s declaration is appropriate for the
given call?
Rational_number_1 + 2.325
► operator+(double& obj);
Ternary Association
• Association between three classes
• Diamond
• May have association class connected at one corner of the diamond
A type of programming in which programmers define not only the data type of a data structure,
but also the types of operations (functions) that can be applied to the data structure. In addition,
programmers can create relationships between one object and another. For example, objects can
inherit characteristics from other objects.
a.Public members of base class are _____ public _____ members of derived class
b.Protected members of base class are __ protected or private ________members of derived
class.
Question No: 20 ( Marks: 3 )
Principle of abstraction:
Capture only those details about an object that are relevant to current perspective
Abstraction
Example:
Suppose we want implement abstraction for the following statement, "Ali is a PhD student and
teaches BS Students"
Here object Ali has two perspectives one is his student perspective and second is his teacher
perspective
Abstraction Advantages,
1. It helps us understanding and solving a problem using object oriented approach as it hides extra
irrelevant details of objects.
Similar to Encapsulation Abstraction is also used for achieving information hiding as we show
only relevant details to related objects, and hide other details.
Derived class can inherit base class features? Explain it with example.
Inheritance is a process by which an object inherits parent
Object quality. inheritance gives reusability, The derived classes have all the features of
the base class and the programmer can choose to add new features specific to the newly
created derived class. The relationship between a parent and child class under private
inheritance¤ is not "is a", but "is implemented as a"
class B : public A
{ public:
};
The class B in inherits the data member d from class A. When one class inherits from another, it
acquires all of its methods and data. We can then instantiate an object of class B and call into that
data member.
Part A.
Suppose we have a furniture store with the following types of furniture, Chairs, Tables, Computer
Tables, Dining Tables and Beds.
1.You have to model this store using inheritance by describing base class, derived classes.
2.You also have to describe the IS–A relationship between these classes.
Part B.
What is IS-A relationship, show how it is implemented using c++ code (you do not have to give
the detailed code simply show in a single line or two how it will be implemented).
► Dangling pointer
► Memory Leakage
► All of the given
► System crash
► Declared
► Define
► Instantiated
► Called
► accesser function
► private function
► inline function
► None of the given
► True
► False
► None of given
► ‘has-a’
► ‘is-a’
► ‘be-
Question No: 8 ( Marks: 1 ) - Please choose one
Which one of the following features of OOP is used to derive a class from another?
► Encapsulation
► Polymorphism
► Data hiding
► Inheritance
► Inheritance
► Polymarphism
► abstraction
► encapsulation
► Carbon
► Diamond
► Coal
► all of the given
► data hiding
► friend classes
► encapsulation
► inheritance
► True
► False
► Constructor
► Destructor
► Shallow Copy
► Deep Copy
► Mermaid
► Woman
► None of the given
► Amphibious Vehicle
Question No: 15 ( Marks: 1 ) - Please choose one
Static variable can be initialized more than once.
► True
► False
Question No: 16 ( Marks: 1 ) - Please choose one
A generic class showing all the common attributes and a behavior of other classes
represents a very important feature in oop called --------
► Inheritance
► Encapsulation
► Polymarphism
► Abstraction
Give c++ code to overload unary ‘-- ‘ operator for complex numbers class.
Hint: You can use c++ built in function power(x,y) that returns the result of x y.
Top
FINALTERM EXAMINATION
Fall 2009
CS304- Object Oriented Programming (Session - 4)
Ref No: 1130772
Time: 120 min
Marks: 75
► Static allocation
► Static typing
► Dynamic binding
► Dynamic allocation
► The compiler can only generate copy for the int type data
Which of the following is the best approach if it is required to have more than one
functions having exactly same functionality and implemented on different data types?
► Templates
► Overloading
► Data hiding
► Encapsulation
template <>
class Vector<char*> { }
► False
► True
► False
► True
► False
Assume a class Derv that is privately derived from class Base. An object of class Derv
located in main() can access
In order to define a class template, the first line of definition must be:
If there is a pointer p to objects of a base class, and it contains the address of an object
of a derived class, and both classes contain a nonvirtual member function, ding(), then
the statement p->ding(); will cause the version of ding() in the _____ class to be executed.
► Base
► Derived
► Abstract
► virtual
When the base class and the derived class have a member function with the same
name, you must be more specific which function you want to call (using ___________).
► dot operator
► null operator
► Operator overloading
► All
► One specific
If you define a vector v with the default constructor, and define another vector w with a
one-argument constructor to a size of 11, and insert 3 elements into each of these
vectors with push_back(), then the size() member function will return ______ for v and
_____ for w.
► Behavior
► All of given
class DocElement
public:
virtual void Print() { cout << "Generic element"; }
};
public:
};
public:
};
void main()
p->Print();
When you run this program, it will print out a single line to the console output.
► Generic element
► Heading element
► Paragraph element
When a virtual function is called by referencing a specific object by name and using the
dot member selection operator (e.g., squareObject.draw()), the reference is resolved at
compile time.
► True
► False
► Both public data members and member functions of all its base classes
► class class-name()
class template<class_name>
► template<class T>
► Class<template T>
► True
► False
Consider a class named Vehicle, which of the following can be the instance of class
Vehicle?
1.Car
2.Computer
3.Desk
4.Ahmed
5.Bicycle
6.Truck
► 1, 4, 5
► 2, 5, 6
► 1, 2, 3, 6
► 1, 5, 6
class Fred {
public:
Fred();
...
};
int main()
Fred a[10];
...
► Produce an error
► Fred a[10]; calls the default constructor 11 times
When a variable is define as static in a class then all object of this class,
► None of given
The life of sub object is dependant on the life of master class in _____________.
► Separation
► Composition
► Aggregation
___________, which means if A declares B as its friend it does NOT mean that A can
access private data of B. It only means that B can access all data of A.
►-
►+
► ++
In Private -------------- only member functions and friend classes or functions of a derived
class can convert pointer or reference of derived object to that of parent object
► specialization
► inheritance
► abstraction
► composition
Ans:
class classname //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};
Ans
When a class-type is included in the class-base, it specifies the direct base class of the
class being declared. If a class declaration has no class-base, or if the class-base lists
only interface types, the direct base class is assumed to be object. A class inherits
members from its direct base class,
Deriving a class from more than one direct base class is called multiple inheritance.
Describe the way to declare a template class as a friend class of any other class.
Ans
Ans:
• non-type
• template
You can interchange the keywords class and typename in a template parameter
declaration. You cannot use storage class specifiers (static and auto) in a template
parameter declaration.
Describe in simple words how we can use template specialization to enforce case
sensitive specialization in String class.
Ans”
The act of creating a new definition of a function, class, or member of a class from a
template declaration and one or more template arguments is called template
instantiation. The definition created from a template instantiation is called a
specialization. A primary template is the template that is being specialized.
Can we use compiler generated default assignment operator in case our class is using
dynamic memory? Justify your answer.
Ans:
the compiler does not make a separate copy of the object. Even if the types are not the
same, the compiler is usually able to do a better job with initialization lists than with
assignments.
Consider the following constructor that initializes member object x_ using an initialization
list: square::square() : x_(whatever) { }. The most common benefit of doing this is
improved performance. For example, if the expression whatever is the same type as
member variable x_, the result of the whatever expression is constructed directly inside
x_ — the compiler does not make a separate copy of the object. Even if the types are
not the same, the compiler is usually able to do a better job with initialization lists than
with assignments.
As if that wasn't bad enough, there's another source of inefficiency when using
assignment in a constructor: the member object will get fully constructed by its default
constructor, and this might, for example, allocate some default amount of memory or
open some default file. All this work could be for naught if the whatever expression
and/or assignment operator causes the object to close that file and/or release that
memory (e.g., if the default constructor didn't allocate a large enough pool of memory or
if it opened the wrong file).
Ans:
The function will throw DivideByZero as an exception that can then be caught by an
exception-handling catch statement that catches exceptions of type int. The necessary
construction for catching exceptions is a try catch system. If you wish to have your
program check for exceptions, you must enclose the code that may have exceptions
thrown in a try block.
The catch statement catches exceptions that are of the proper type. You can, for
example, throw objects of a class to differentiate between several different exceptions.
As well, once a catch statement is executed, the program continues to run from the end
of the catch.
the errors can be handled outside of the regular code. This means that it is easier to
structure the program code, and it makes dealing with errors more centralized. Finally,
because the exception is passed back up the stack of calling functions, you can handle
errors at any place you choose.
class Base{
private:
void base1();
protected:
void base2();
public:
void base3();
};
private:
void derived1();
protected:
void derived2();
public:
void derived3();
};
int main(){
Fill the table below to tell which member functions of Base and Derived classes we
can access using the Derived pointer in the code indicated in bold.
Ans:
base2() no
base3() yes
derived1() No
derived2() No
derived3() Yes
#include<iostream.h>
try
cout <<”Trying.\n”;
sample_function(98.6);
catch(int)
return 0;
throw 42;
Ans:
Starting sample_function
Trying
Catching
End program
Write a publicly derived class “Employee” that is derived from base class named
“Company”. Both classes will have function “create()”. Make virtual function of base
class and override same function in derived class. Function create will have an output
statement of your own choice.
In “main” Create an object of base class and call both functions with same object
type.
1.Equation
2.Linear
3.Quadratic
Each class has the method Graph. Graph method should be pure virtual in Equation
class.
This method should be overridden in both the inherited classes. It is meant to display the
Graph shape of its respective class. Graph method of Linear will display the message;
Straight line
Parabola
In main, call the Graph method of both the Linear and Quadratic equations
polymorphically through the parent class (Equation).
Ans:
#include "fraction.h"
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
class equation;
class equation {
int a, b;
public:
int c ()
{return (c);}
void convert (Cequation);
};
class linear {
private:
int side;
public:
void set_side (int a)
{side=a;}
friend class equation;
};
int main () {
cequation sqr;
CRectangle rect;
sqr.set_side(4);
rect.convert(sqr);
cout << rect.area();
return 0;
}
Question No: 1 ( Marks: 1 ) - Please choose one
A template provides a convenient way to make a family of
► variables and data members
► Static allocation
► Static typing
► Dynamic binding
► Dynamic allocation
► True
► False
► True
► False
► Base
► Derived
► Abstract
► virtual
► All
► One specific
► All instances of one date type
► None of the given options
► State
► Behavior
► Protected data members
► All of given
p->Print();
}
When you run this program, it will print out a single line to the console output.
► Heading element
► Paragraph element
► True
► False
► template<class T>
► template <typename U>
► Class<template T>
► template < class T, class U>
► True
► False
► 1, 4, 5
► 2, 5, 6
► 1, 2, 3, 6
► 1, 5, 6
Question No: 25 ( Marks: 1 ) - Please choose one
Consider the code below,
class Fred {
public:
Fred();
...
};
int main()
{
Fred a[10];
Fred* p = new Fred[10];
...
}
Select the best option,
► Fred a[10]; calls the default constructor 09 times
Fred* p = new Fred[10]; calls the default constructor 10 times
► Produce an error
►/
►-
►+
► ++
► specialization
► inheritance
► abstraction
► composition
class Base{
private:
void base1();
protected:
void base2();
public:
void base3();
};
int main(){
Derived * derived = new Derived();
return 0;
}
Fill the table below to tell which member functions of Base and Derived
classes we can access using the Derived pointer in the code indicated in
bold.
#include<iostream.h>
int main()
{
try
{
cout <<”Trying.\n”;
sample_function(98.6);
cout << “Trying after call.\n”;
}
catch(int)
{
cout << “Catching.\n”;
}
1. Equation
2. Linear
3. Quadratic
Each class has the method Graph. Graph method should be pure virtual in
Equation class.
Straight line
Parabola
In main, call the Graph method of both the Linear and Quadratic equations
polymorphically through the parent class (Equation).
Top
MIDTERM EXAMINATION
Spring 2010
CS304- Object Oriented Programming (Session - 6)
Time: 60 min
Marks: 38
► Age
► Work()
► Interface
► Private data members
► Private member functions
► Both public and private members
► True
► False
http://vustudents.ning.com
Question No: 5 ( Marks: 1 ) - Please choose one
A static member function cannot be declared.
► Static
► Implicit
► Explicit
► Virtual
► False
► True
► Define the variable Age as protected and create a set method that returns it and a
get method that updates it
► True
► False
►/
►-
►+
► ++
► Encapsulation
► Information Hiding
► Abstraction
► both encapsulation and information hiding
► Simple Assocation
► Inheritance
► Aggregation
► Composition
► Class
► Function
► Data Members
► Instances
► Integer
► Chracter
► Double
► No type
class ABC{
int x;
int y;
int z;
public:
ABC();
};
ABC::ABC():x(10),z(x),y(x)
{
…
}
Top
► Static typing
► Dynamic binding
► Dynamic allocation
► Public
► Private
► Protected
► All of the given
► overload
► overriding
► copy riding
► none of given
► True
► False
► True
► False
► True
► False
► 1
► 2
► 3
► As many as necessary.
► set,map
► sequence,mapping
► setmet,multipule
► sit,mat
► Reusability
► Writability
► Maintainability
► All of given
► 0
► 0.0
► 1
► null
vector<int> evec;
After adding the statment,
evec.push_back(21);
what will happen?
► The following statement will add an element to the
start (the back) of evec and will initialize it with the
value 21.
► The following statement will add an element to the
center of evec and will reinitialize it with the value 21.
► The following statement will delete an element to the
end (the back) of evec and will reinitialize it with the
value 21.
► The following statement will add an element to the
end (the back) of evec and initialize it with the value
21.
► True
► False
► Produce an error
► True
► False
Question No: 26 ( Marks: 1 ) - Please choose one
A normal C++ operator that acts in special ways on newly
defined data types is said to be
► glorified.
► encapsulated.
► classified.
► overloaded.
► A operator + ( A &obj);
► int + operator();
► int operator (plus) ();
► A operator(A &obj3);
Yes, deque behaves like queue (line) such that we can add
elements on both sides of it.
<<
max
(
3, 7
)
;
// outputs 7
{
return
x
<
y
?
y
:
x
;
}
template
<
typename
T
>
T max
(
T x, T y
)
{
return
x
<
y
?
y
:
x
;
}
Top
FINALTERM EXAMINATION
Fall 2009
CS304- Object Oriented Programming (Session - 1)
Time: 120 min
Marks: 75
Question No: 1 ( Marks: 1 ) - Please choose one
Which one of the following terms must relate to polymorphism?
► Static allocation
► Static typing
► Dynamic binding
► Dynamic allocation
► Public
► Private
► Protected
► All of the given
► overload
► overriding
► copy riding
► none of given
► True
► False
Question No: 5 ( Marks: 1 ) - Please choose one
It is sometimes useful to specify a class from which no objects will ever be created.
► True
► False
► True
► False
►1
►2
►3
► As many as necessary.
► set,map
► sequence,mapping
► setmet,multipule
► sit,mat
► late binding
► static binding
► virtual binding
► None of the given options
► template<class T>
► template <typename U>
► Class<template T>
► template < class T, class U>
►0
► 0.0
►1
► null
vector<int> evec;
After adding the statment,
evec.push_back(21);
what will happen?
► The following statement will add an element to the start (the back) of evec and
will initialize it with the value 21.
► The following statement will add an element to the center of evec and will
reinitialize it with the value 21.
► The following statement will delete an element to the end (the back) of evec and
will reinitialize it with the value 21.
► The following statement will add an element to the end (the back) of evec and
initialize it with the value 21.
► True
► False
► To define an object
► To define a data member
► To link the definition of an identifier to its declaration
► To make a class private
► Produce an error
► True
► False
► glorified.
► encapsulated.
► classified.
► overloaded.
Question No: 27 ( Marks: 1 ) - Please choose one
Which operator can not be overloaded?
► A operator + ( A &obj);
► int + operator();
► int operator (plus) ();
► A operator(A &obj3);
Specializations of this function template, instantiations with specific types, can be called
just like an ordinary function:
cout << max(3, 7); // outputs 7
The compiler examines the arguments used to call max and determines that this is a call
to max(int, int). It then instantiates a version of the function where the parameterizing
type T is int, making the equivalent of the following function:
int max(int x, int y)
{
return x < y ? y : x;
}
the C++ Standard Template Library contains the function template max(x, y) which creates
functions that return either x or y, whichever is larger. max() could be defined like this:
template <typename T>
T max(T x, T y)
{
return x < y ? y : x;
}
classPoweredDevice
{
};
classScanner: publicPoweredDevice
{
};
classPrinter: publicPoweredDevice
{
};
classCopier: publicScanner, publicPrinter
{
};
Scanners and printers are both powered devices, so they derived from
PoweredDevice. However, a copy machine incorporates the functionality of both
Scanners and Printers.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Shape{
public:
void Draw(){cout<<"shape"<<endl;}
};
class Line : public Shape{
public:
void Draw(){cout<<"Line"<<endl;}
};
class Circle : public Shape{
public:
void Draw(){cout<<"Circle"<<endl;}
};
int main(int argc, char *argv[])
{
Shape * ptr1 = new Shape();
Shape * ptr2 = new Line();
Shape * ptr3 = new Circle();
ptr1->Draw();
ptr2->Draw();
ptr3->Draw();
system("PAUSE");
return 0;
}
Shape
Shape
Shape
Shape
Line
Circle
};
class B : public A
{
public:
void method() { cout<<"B's method\n"; }
};
Ans:
The STL is the containers, iterators and algorithms component of the proposed C++
Standard Library [ANSI95]. It represents a novel application of principles which have
their roots in styles of programming other than Object-orientation.
void listWords(istream& in, ostream& out)
{
string s;
sort(v.begin(), v.end());
vector<string>::iterator e
= unique(v.begin(), v.end()); // (2)
Exceptions in Destructors:
An object is presumably created to do something. Some of the changes made by an object
should persist after an object dies (is destructed) and some changes should not. Take an
object implementing a SQL query. If a database field is updated via the SQL object then
that change should persist after the SQL objects dies. To do its work the SQL object
probably created a database connection and allocated a bunch of memory. When the SQL
object dies we want to close the database connection and deallocate the memory,
otherwise if a lot of SQL objects are created we will run out of database connections
and/or memory.
The logic might look like:
Sql::~Sql()
{
delete connection;
delete buffer;
}
Let's say an exception is thrown while deleting the database connection. Will the buffer
be deleted? No. Exceptions are basically non-local gotos with stack cleanup. The code for
deleting the buffer will never be executed creating a gaping resource leak.
Special care must be taken to catch exceptions which may occur during object
destruction. Special care must also be taken to fully destruct an object when it throws an
exception.
#include<iostream.h>
#include<conio.c>
class Exception {
private:
char message[30] ;
public:
int stock ;
int required_quantity;
public:
int get_required_quantity()
{
return required_quantity;
}
void order()
{
if (get_stock()< get_required_quantity())
throw Exception();
else
cout<<"The required quantity of item is available in the stock";
}
~Item(){}
};
void main()
{
try
{
obj.order();
}
catch(Exception & exp2)
{
getch();
cout << "Exception: " << exp2.get_message() << endl;
}
getch();
Top
FINALTERM EXAMINATION
Fall 2009
CS304- Object Oriented Programming (Session - 1)
Time: 120 min
Marks: 75
Question No: 1 ( Marks: 1 ) - Please choose one
Which one of the following terms must relate to polymorphism?
► Static allocation
► Static typing
► Dynamic binding
► Dynamic allocation
► Public
► Private
► Protected
► All of the given
► overload
► overriding
► copy riding
► none of given
► True
► False
► True
► False
► True
► False
►1
►2
►3
► As many as necessary.
► late binding
► static binding
► virtual binding
► None of the given options
► template<class T>
► template <typename U>
► Class<template T>
► template < class T, class U>
Question No: 16 ( Marks: 1 ) - Please choose one
Which of the following is/are advantage[s] of generic programming?
► Reusability
► Writability
► Maintainability
► All of given
►0
► 0.0
►1
► null
vector<int> evec;
After adding the statment,
evec.push_back(21);
what will happen?
► The following statement will add an element to the start (the back) of evec and
will initialize it with the value 21.
► The following statement will add an element to the center of evec and will
reinitialize it with the value 21.
► The following statement will delete an element to the end (the back) of evec and
will reinitialize it with the value 21.
► The following statement will add an element to the end (the back) of evec and
initialize it with the value 21.
► True
► False
► To define an object
► To define a data member
► To link the definition of an identifier to its declaration
► To make a class private
► Produce an error
► True
► False
► A operator + ( A &obj);
► int + operator();
► int operator (plus) ();
► A operator(A &obj3);
Yes, deque behaves like queue (line) such that we can add elements on both
sides of it.
Specializations of this function template, instantiations with specific types, can be called
just like an ordinary function:
cout << max(3, 7); // outputs 7
The compiler examines the arguments used to call max and determines that this is a call
to max(int, int). It then instantiates a version of the function where the parameterizing
type T is int, making the equivalent of the following function:
int max(int x, int y)
{
return x < y ? y : x;
}
the C++ Standard Template Library contains the function template max(x, y) which creates
functions that return either x or y, whichever is larger. max() could be defined like this:
template <typename T>
T max(T x, T y)
{
return x < y ? y : x;
}
classPoweredDevice
{
};
classScanner: publicPoweredDevice
{
};
classPrinter: publicPoweredDevice
{
};
classCopier: publicScanner, publicPrinter
{
};
Scanners and printers are both powered devices, so they derived from
PoweredDevice. However, a copy machine incorporates the functionality of both
Scanners and Printers.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Shape{
public:
void Draw(){cout<<"shape"<<endl;}
};
class Line : public Shape{
public:
void Draw(){cout<<"Line"<<endl;}
};
class Circle : public Shape{
public:
void Draw(){cout<<"Circle"<<endl;}
};
int main(int argc, char *argv[])
{
Shape * ptr1 = new Shape();
Shape * ptr2 = new Line();
Shape * ptr3 = new Circle();
ptr1->Draw();
ptr2->Draw();
ptr3->Draw();
system("PAUSE");
return 0;
}
Shape
Shape
Shape
Shape
Line
Circle
};
class B : public A
{
public:
void method() { cout<<"B's method\n"; }
};
Ans:
t.test(a);
t.test(b);
}
}
The STL is the containers, iterators and algorithms component of the proposed C++
Standard Library [ANSI95]. It represents a novel application of principles which have
their roots in styles of programming other than Object-orientation.
void listWords(istream& in, ostream& out)
{
string s;
sort(v.begin(), v.end());
vector<string>::iterator e
= unique(v.begin(), v.end()); // (2)
Exceptions in Destructors:
An object is presumably created to do something. Some of the changes made by an object
should persist after an object dies (is destructed) and some changes should not. Take an
object implementing a SQL query. If a database field is updated via the SQL object then
that change should persist after the SQL objects dies. To do its work the SQL object
probably created a database connection and allocated a bunch of memory. When the SQL
object dies we want to close the database connection and deallocate the memory,
otherwise if a lot of SQL objects are created we will run out of database connections
and/or memory.
The logic might look like:
Sql::~Sql()
{
delete connection;
delete buffer;
}
Let's say an exception is thrown while deleting the database connection. Will the buffer
be deleted? No. Exceptions are basically non-local gotos with stack cleanup. The code for
deleting the buffer will never be executed creating a gaping resource leak.
Special care must be taken to catch exceptions which may occur during object
destruction. Special care must also be taken to fully destruct an object when it throws an
exception.
#include<iostream.h>
#include<conio.c>
class Exception {
private:
char message[30] ;
public:
int stock ;
int required_quantity;
public:
int get_required_quantity()
{
return required_quantity;
}
void order()
{
if (get_stock()< get_required_quantity())
throw Exception();
else
cout<<"The required quantity of item is available in the stock";
}
~Item(){}
};
void main()
{
try
{
obj.order();
}
catch(Exception & exp2)
{
getch();
cout << "Exception: " << exp2.get_message() << endl;
}
getch();
Top
2. Aggregation
3. Separation
4. non of the given
2. false
2. false
3. Aggregation
4. Inheritance
2. false
__________ Operator will take only one operand.
1. New
2. int
3. object
1. ++
2. *
3. %
2. Non-Static Member
3. Instance Number
Operator overloading is
Question # 1 of 10
Information hiding can be achieved through__________.
1. Encapsulation, Inheritance
2. Encapsulation, Polymorphism
3. Encapsulation, Abstraction
4. Overloading
Question # 1 of 10
Which of the following is the way to extract common behavior and attributes from the
given classes and make a separate class of those common behaviors and attributes?
1. Generalization
2. Sub-typing
3. Specialization
4. Extension
Question # 2 of 10
The ability to derive a class from more than one class is called
1. Single inheritance
2. Encapsulation
3. Multiple inheritance
4. Polymorphism
Question # 3 of 10:
If MyClass has a destructor what is the destructor named?
1. MyClass
2. ~MyClass
3. My~Class
4. MyClass~
Question # 4 of 10:
Class abc{ ----- }; Is a valid class declaration?
1. yes
2. no
Question # 6 of 10:
If only one behaviour of a derived class is incompatible with base class, then it is:
1. Generalization
2. Specialization
3. Extension
4. Inheritance
Question # 7 of 10:
Which of the following may not be an integral part of an object?
1. state
2. behavior
3. Protected data members
4. All of given
Question # 8 of 10:
Only tangible things can be chosen as an object.
1. True
2. False
Which of the following features of OOP is used to derive a class from another?
1. Encapsulation
2. Polymorphism
3. Data hiding
4. Inheritance
1. Inheritance
2. Composition
3. Aggregation
4. None of given
1. Overriding
2. Specific
3. General
1. Child class
2. Derived class
3. Parent class
1. Inheritance
2. Composition
3. There is no association
4. Aggregation
The dot operator (or class member access operator) connects the following two entities
(reading from left to
right):
Top
MIDTERM EXAMINATION
Spring 2010
CS304- Object Oriented Programming (Session - 6)
Time: 60 min
Marks: 38
► Age
► Work()
► Interface
► Private data members
► Private member functions
► Both public and private members
► True
► False
► False
► True
► Define the variable Age as protected and create a set method that returns it and a
get method that updates it
►/
►-
►+
► ++
► Encapsulation
► Information Hiding
► Abstraction
► both encapsulation and information hiding
► Simple Assocation
► Inheritance
► Aggregation
► Composition
► Class
► Function
► Data Members
► Instances
► Integer
► Chracter
► Double
► No type
Question No: 17 ( Marks: 2 )
Can we create an array of objects for a class having default constructor?. Justify your
answer.
Ans:
class ABC{
int x;
int y;
int z;
public:
ABC();
};
ABC::ABC():x(10),z(x),y(x)
{
…
}
Top
MIDTERM EXAMINATION
Spring 2010
CS304- Object Oriented Programming (Session - 2)
Student Info
StudentID:
Center: OPKST
ExamDate:
Q No. 9 10 11 12 13 14 15 16
Marks
Q No. 17 18 19 20 21 22 23
Marks
Question No: 1 ( Marks: 1 ) - Please choose one
Which part of an object exhibits its state?
► Data
► Operations
► organize data.
► pass arguments to objects of classes.
► add features to existing classes without rewriting them.
► improve data-hiding and encapsulation.
"A person has a name, age, address and sex. You are designing a class to represent a type
of person called a patient. This kind of person may be given a diagnosis, have a spouse
and may be alive".
Given that the person class has already been created, what of the following would be
appropriate to include when you design the patient class?
► Dangling pointer
► Memory Leakage
► All of the given
► System crash
► True
► False
► Static
► Implicit
► Explicit
► Virtual
► Static variables
► Instance variable
► Primitive variables
► None of given
► None of given
► object member
► non-member
► data member
► True
► False
► Structure
► Header File
► Library File
► None of the given
class Exam
{
char *ExamName;
int No_of_paper;
public:
Exam()
{
ExamName = "Final Term";
No_of_paper = 5;
}
int main()
{
const Exam exam1;
getch();
return 0;
}
Top
MIDTERM EXAMINATION
Spring 2010
CS304- Object Oriented Programming (Session - 6)
Time: 60 min
Marks: 38
► Age
► Work()
► Interface
► Private data members
► Private member functions
► Both public and private members
► Static
► Implicit
► Explicit
► Virtual
► False
► True
► Define the variable Age as protected and create a set method that returns it and a
get method that updates it
► True
► False
►/
►-
►+
► ++
► Encapsulation
► Information Hiding
► Abstraction
► both encapsulation and information hiding
► Class
► Function
► Data Members
► Instances
Question No: 16 ( Marks: 1 ) - Please choose one
The return type of a constructor is of -------------
► Integer
► Chracter
► Double
► No type
Ans:
class ABC{
int x;
int y;
int z;
public:
ABC();
};
ABC::ABC():x(10),z(x),y(x)
{
…
}
http://vustudents.ning.com
Question No: 22 ( Marks: 5 )
What is composition? Explain it with the help of an example.
Top