BE/ B.Tech DEGREE EXAMINATION, NOVEMBER/DECEMBER ...

estrapadesherbetΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

96 εμφανίσεις

1

CS 1204
-

NOVEMBER/DECEMBER 2007

B.E/ B.Tech DEGREE EXAMINATION, NOVEMBER/DECEMBER 2007

Third Semester


(Regulation 2004)


Computer Science and Engineering

CS 1204

OBJECT ORIENTED PROGRAMMING

(Common to Information Technology )

(Common to BE (Part
-
Time) Second Semester Regulation 2005)


T
ime : Three hours






Maximum : 100 marks

Answer ALL questions


PART A

(10 x 2 = 20 marks)


1. What is object oriented paradigm?

2. What is the use of scope resolution operator : : in C++?

3. What are the operators of C++ that cannot be overloaded?

4. Ho
w does constructor differ from normal functions?

5. What is the default access mode for class members?

6. What is an I/O stream?

7. What is the type of class for which objects cannot be created?

8. What is type of inheritance is supported in Java?

9. What
is java virtual machine?

10. What is multithreading?


PART B

(5 x 16 = 80 marks)


11. (a) (i) Explain object oriented paradigm with all its essential elements.


(12)

(ii) State the merits and demerits of object oriented methodology.


(4)


Or

(b) Explain
the following concepts of object oriented programming in detail

(i). Data abstraction

(ii). Inheritance

(iii). Polymorphism

(iv). Objects


12. (a) (i) Write a C++ program to extract the elements placed in the odd position of the
a
N


(ii) State the rules t
o the followed while overloading an operator. Write a program to
illustrate overloading. (8)


Or

(b) (i) Discuss about polymorphism and its advantages. (8)


2

CS 1204
-

NOVEMBER/DECEMBER 2007

(ii) Write a C++ program that will give the conditions of environment

required, food habits and u
nique characteristics of pet animals fish and dog. Define a base
class called pet that describe any common household pet; two derived classes called fish
and dog with items specific to that type of animal. Write pure virtual functions in the base
class for

operations that are common to both types of animals. Write a program to test the
usage of classes. (8)


13. (a) (i) Explain the 4 functions Seekg, Seekp, tellg, tellp used for setting pointers
during file operation and show how they are derived from fstr
eam class. (4)


(ii) Write a program to append to the contents of a file (10)


Or


(b) (i) Write a program to write the text in a file. Display the contents of file in reverse
order.

(8)


(ii) What are the keywords used in C++ for exception handling? Des
cribe their usage
with suitable example. (8)



14. (a) (i) Explain the interface concepts in
Java
. (8)

(ii) Write a Java program to compute the area of rectangle and squ
are using interface. (8)

Or

(b) (i) Compare the features of C++ versus Java. (6)


(ii) Give an example that fits the following inheritance hierarchy.

(10)


Write a Java program to implement this example.


15. (a) Explain about exception handling in Java

with suitable examples.

(16)

Or

(b) What is a thread? State how synchronization is dealt while using multithreading. (16)

3

CS 1204
-

NOVEMBER/DECEMBER 2007

B.E/ B.Tech DEGREE EXAMINATION, NOVEMBER/DECEMBER 2007

Third Semester


(Regulation 2004)


Computer Science and Engineering

CS 120
4

OBJECT ORIENTED PROGRAMMING

(Common to Information Technology )

(Common to BE (Part
-
Time) Second Semester Regulation 2005)


Answers


Part A

1.

Object oriented paradigm is the one in which, the emphasis is on data rather than
on procedure. Programs will divi
ded into objects. Data is hidden and cannot be
accessed by external functions. The paradigm is data centric rather than function
centric.


2.

The
::
operator links a class name with a member name in order to tell the
compiler what class the member belongs to.

However, the scope resolution
operator has another related use: it can allow access to a name in an enclosing
scope that is "hidden" by a local declaration of the same name.


For example,


int i; // global i

void f()

{

int i; // local i

i = 10; // uses
local i

.

.

}

the assignment
i = 10
refers to the local
i
. But if function
f()
needs to access the
global version of
i,

It can be accessed by preceding the
i
with the
::
operator, as
shown here.


int i; // global i

void f()

{

int i; // local i

::
i = 10; //

now refers to global i

.

.

}


3.

The C++ operators that cannot be overloaded:

.


: :


.*


?

4

CS 1204
-

NOVEMBER/DECEMBER 2007


4.

A
constructor function
is a special function that is a member of a class and has

the
same name as that class.

constructor

functions cannot return values and, thus,
h
ave no return type.

An object's constructor is automatically called when the
object is created.


5.

The
default access mode for
all
class members

is private.


6.

I/O stream
:

A stream is a logical device that either produces or consumes information. A
stream is
linked to a physical device by the I/O system. All streams behave in the
same way even though the actual physical devices they are connected to may
differ substantially.


7.

Objects cannot be created for an Abstract class. Abstract class is a class with one
o
r more pure virtual functions.


8.

not covered in syllabus

9.

not covered in syllabus

10.

not covered in syllabus


Part B


11.

a. i). Discuss about the following topics briefly,

i.

class

ii.

object

iii.

polymorphism

iv.

inheritance

v.

abstraction


11.

a.ii)

Merits of Object oriented programmi
ng

Class


collection of data and code that act upon the data


Object


instance
of
a

class.


Encapsulation


Encapsulation
is the mechanism that binds together code and the data it
manipulates,

and keeps both safe from outside interference and misuse.


Po
lymorphism


polymorphism

is the

attribute that allows one interface to control access to
a general class of actions.


Inheritance


Inheritance
is the process by which one object can acquire the properties
of another

object.

5

CS 1204
-

NOVEMBER/DECEMBER 2007


Demerits

Dynamic binding
func
tion calls that are not resolved until run time



11.

b)Give brief description about the following,

(i). Data abstraction

(ii). Inheritance

(iii). Polymorphism

(iv). Objects


12.

a.i)
//C++ program to extract the elements placed in the odd position of the
array.

#include <iostream.h>

#include<conio.h>

class number

{


int *array;


int n;

public:


number()


{



cout<<"enter the size of the array";



cin>>n;



array = new int[n];


}


void display_odd();


void getval();


~number ()


{



delete array;


}

};


void numbe
r :: getval()


{



cout<<"enter values to store";



for(int i=0;i<n;i=i+1)




cin>>array[i];


}



void number:: display_odd()

{


for(int i=0; i<n; i++)


{

6

CS 1204
-

NOVEMBER/DECEMBER 2007



if(i%2 != 0)




cout<<"the value in position"<<i<<" = "<<array[i]<<endl;


}

}


void main()

{


clrsc
r();


number n;


n.getval();


n.display_odd();


getch();

}


S
ample output:

enter the size of the array10

enter values to store1

2

3

4

5

6

7

8

9

10

the value in position1 = 2

the value in position3 = 4

the value in position5 = 6

the value in position7 = 8

t
he value in position9 = 10



12.a.ii)

Rules for overloading an operator:

1.

Only
existing operators can be overloaded. New operators cannot be
created.

2.

The overloaded operator must have atleast one operand thet is user
defined

3.

We cannot change the basic meaning of an

opearor.

4.

Overload operators follow the suntax and rules of original operators.

5.

(. : : .* ?
) operartors cannot be overloaded.

6.

=, (), [],
-
> opearators cannot be overloaded using friend functions

7.

Member operator function for unary operators take no argum
ents and
binary operators take one explicit argument.

8.

Friend operator function for unary operators takes one reference
arguments and binary operators take two explicit argument.

7

CS 1204
-

NOVEMBER/DECEMBER 2007

9.

When binary operators are overloaded through member function, the
left hand op
erand must be an object of the relevant class.

10.

No of arguments to the operator function cannot be changed.



Sample program:

#include <iostream>

using namespace std;

class loc

{

int longitude, latitude;

public:

loc() {}

loc(int lg, int lt) {

longitude = lg
;

latitude = lt;

}


void show() {

cout << longitude << " ";

cout << latitude << "
\
n";

}


loc operator+(loc op2);

};


// Overload + for loc.

loc loc::operator+(loc op2)

{

loc temp;

temp.longitude = op2.longitude + longitude;

temp.latitude = op2.latitude + l
atitude;

return temp;

}

int main()

{

loc ob1(10, 20), ob2( 5, 30);

ob1.show(); // displays 10 20

ob2.show(); // displays 5 30

ob1 = ob1 + ob2;

ob1.show(); // displays 15 50

return 0;

}


12.b.i)

Discuss in detail the general idea behind polymorphism and state its
a
dvantages

8

CS 1204
-

NOVEMBER/DECEMBER 2007


12.b.ii)

//C++ program to illustrate inheritance

#include <iostream.h>

#include<conio.h>

#include<stdio.h>

#include<ctype.h>

class pet

{

protected:


char name[20];


char breed[20];


char gender;


int age;


bool morning;


bool evening;

public:


void get_d
etails();


void put_details();


virtual void food_timing()=0;

};

void pet::get_details()

{


cout<<"enter the name of the pet
\
n";


cin>>name;


cout<<"enter its breed
\
n";


cin>>breed;


cout<<"enter its gender male(M) or female(F)
\
n";


cin>>gender;


gender =
toupper(gender);


cout<<"enter its age
\
n";


cin>>age;

}

void pet::put_details()

{


cout<<"PET DETAILS
\
n";


cout<<"Name:
\
t"<<name<<endl;


cout<<"Breed:
\
t"<<breed<<endl;


cout<<"gender:
\
t"<<gender<<endl;


cout<<"age:
\
t"<<age<<endl;

}

class fish: public pet

{


int no_of_fins;

public:


void get_data();


void food_timing();

9

CS 1204
-

NOVEMBER/DECEMBER 2007


void display();

};



void fish::get_data()


{



get_details();



cout<<"
\
n enter the no of fins for the fish
\
n";



cin>>no_of_fins;


}


void fish::food_timing()


{



char ch;



cout<<"
\
n whe
n do you feed the fish :
\
n:";



cout<<"press y(yes) or n(no)
\
n";



cout<<"morning:";



cin>>ch;




if(ch == 'y')




morning = true;



else




morning = false;



cout<<"
\
nevening:";



cin>>ch;



if(ch == 'y')




evening = true;



else




evening = fal
se;


}


void fish::display()


{



put_details();



cout<<"no of fins:"<<no_of_fins<<endl;



cout<<"Food timings
\
n morning : "<<morning<<"
\
n evening:"<<evening;


}

class dog: public pet

{

bool trained;

public:


void get_data();


void food_timing();


void d
isplay();

};

void dog::get_data()

{



char ch;



get_details();



cout<<"
\
n Is your dog trained using professional trainer
\
n";

10

CS 1204
-

NOVEMBER/DECEMBER 2007



cout<<"if yes press y or n for no"<<endl;



cin>>ch;



if(ch == 'y')




trained = true;



else




trained = false;





}


void
dog::food_timing()


{



char ch;



cout<<"
\
n when do you feed the dog :
\
n:";



cout<<"press y(yes) or n(no)
\
n";



cout<<"morning:";



cin>>ch;



if(ch == 'y')




morning = true;



else




morning = false;



cout<<"
\
nevening:";



cin>>ch;



if(ch == 'y')




evening = true;



else




evening = false;





}


void dog::display()


{



put_details();



if(trained)




cout<<"
\
nDog is trained
\
n";



else




cout<<"
\
nDog is still not trained
\
n";




cout<<"Food timings
\
n morning :"<<morning<<"
\
n evening: "<<evening;


}

int main()

{


clrscr();


fish f;


dog d;


f.get_data();


clrscr();


d.get_data();


clrscr();

11

CS 1204
-

NOVEMBER/DECEMBER 2007


f.display();


d.display();


getch();


return 0;

}

Sample Output:


enter the name of the pet

:
xx

enter its breed

:
yy

enter its gender male(M) or female(F)

:
m

enter its age

:
2


enter the no of fins for the fish

:
3


enter the name of the pet

:
zz

enter its breed

:
aa

enter its gender male(M) or female(F)

:
f

enter its age

:
3

Is your dog trained using professional trainer

if yes press y or n for no

:
y


PET D
ETAILS

Name: xx

Breed: yy

gender: M

age: 2

no of fins:3

PET DETAILS

Name: zz

Breed: aa

gender: F

age: 3



13.a.i)

Ramdom access pointers:


Whenever the files are opened in bnary mode there are two pointers (put
& get) associated with each file are crea
ted. These pointers are used to traverse
through the file. The get pointer specifies where in the file the next input
operation will occur. The put pointer specifies where in the file the next output
operation will occur.

The seekg() and seekp() functions
allows you to access the file in a
nonsequential fashion.



seekg()
moves the associated file's current get pointer



seekp()
moves the associated file's current put pointer

T
he current position of each file pointer
cn be determined
using
,


12

CS 1204
-

NOVEMBER/DECEMBER 2007



tellg( );



tellp( );


They can be used in conjunction with seekg() and seekp().



These functions allow you to save the current file location, perform
other file operations, and then reset the file location to its previously saved
location.


13.a.ii)

//program to append to the content
s of a file

#include <iostream.h>

#include<conio.h>

#include<string.h>

#include <fstream.h>

int main()

{

clrscr();

fstream inout("text.txt", ios::in | ios::out | ios::app | ios::binary);

if(!inout)

{

cout << "Cannot open input file.
\
n";

return 1;

}

long e,

i, j,k=0;

char c1, c2;

inout.seekg(0,ios::end);

j = inout.tellg();

cout<<"original content of the file : "<<endl;

for(k=0;k<j;k++)

{


inout.seekg(k,ios::beg);


inout.get(c1);


cout<<c1;

}

cout<<endl<<"appended content : "<<endl;

char *s = "
\
ntime is preci
ous
\
n";

inout.write(s,20);


inout.seekg(0,ios::end);

j = inout.tellg();

for(k=0;k<j;k++)

{


inout.seekg(k,ios::beg);


inout.get(c1);


cout<<c1;

}

13

CS 1204
-

NOVEMBER/DECEMBER 2007


inout.close();

getch();

return 0;

}


Sample output:

original content of the file :

abcdefghijklmnop


appended

content :

abcdefghijklmnop

time is precious




13.b.i)

// program to write the text in a file

#include <iostream.h>

#include<conio.h>

#include <fstream.h>

int main()

{

clrscr();

fstream inout("text.txt", ios::in | ios::out | ios::binary);

if(!inout)

{

cout << "C
annot open input file.
\
n";

return 1;

}

long e, i, j,k=0;

char c1, c2;

inout.seekg(0,ios::end);

j = inout.tellg();

cout<<"original content of the file"<<endl;

for(k=0;k<j;k++)

{


inout.seekg(k,ios::beg);


inout.get(c1);


cout<<c1;

}

cout<<endl<<"reversed co
ntent"<<endl;

for(i=j
-
1;i>=0;i
--
)

{


inout.seekg(i,ios::beg);


inout.get(c1);


cout<<c1;

14

CS 1204
-

NOVEMBER/DECEMBER 2007

}

inout.close();

getch();

return 0;

}


Sample output:


original content of the file : abcdefghijklmnop


reversed content :

ponmlkjihgfedcba


13.b.ii)

Exception handling


Discu
ss in detail about the three keywords
, with sample program.




Try



Throw



Catch



14)

not covered in syllabus

15)

not covered in syllabus