S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Similar documents
DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

Introduction to Programming Using Java (98-388)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

1 Shyam sir JAVA Notes

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 6 Introduction to Defining Classes

Subclasses, Superclasses, and Inheritance

An overview of Java, Data types and variables

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

Object Oriented Programming with Java. Unit-1

Getting started with Java

Sri Vidya College of Engineering & Technology

Java Bytecode (binary file)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

(2½ hours) Total Marks: 75

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Computational Expression

Java Object Oriented Design. CSC207 Fall 2014

Compaq Interview Questions And Answers

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

OBJECT ORİENTATİON ENCAPSULATİON

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

CS 231 Data Structures and Algorithms, Fall 2016

F I N A L E X A M I N A T I O N

ITI Introduction to Computing II

STRUCTURING OF PROGRAM

CS-202 Introduction to Object Oriented Programming

ITI Introduction to Computing II

15CS45 : OBJECT ORIENTED CONCEPTS

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Fundamentals of Object Oriented Programming

Programming overview

Java Threads and intrinsic locks

PROGRAMMING FUNDAMENTALS

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Vidyalankar. T.Y. Diploma : Sem. V [CO/CM/IF] Java Programming Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 100

Introduction to Java

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Learning the Java Language. 2.1 Object-Oriented Programming

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CS 11 java track: lecture 1

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

CS260 Intro to Java & Android 03.Java Language Basics

Chapter 1 Introduction to Java

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

13 th Windsor Regional Secondary School Computer Programming Competition

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

COP 3330 Final Exam Review

Inheritance. The Java Platform Class Hierarchy

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Introduction to Computers and Java

Answer1. Features of Java

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Program Fundamentals

Array. Array Declaration:

CT 229 Arrays in Java

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Selected Questions from by Nageshwara Rao

Crash Course Review Only. Please use online Jasmit Singh 2

THE CONCEPT OF OBJECT

Chapter 1: Introduction to Computers, Programs, and Java

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Java Training JAVA. Introduction of Java

Character Stream : It provides a convenient means for handling input and output of characters.

Lecture 02, Fall 2018 Friday September 7

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

CMSC 132: Object-Oriented Programming II

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

Introduction to Computers and Java

Language Features. 1. The primitive types int, double, and boolean are part of the AP

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

Programming Language Concepts: Lecture 2

Object Oriented Java

WOSO Source Code (Java)

Section 2.2 Your First Program in Java: Printing a Line of Text

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

9. APPLETS AND APPLICATIONS

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Object-oriented programming in...

CompSci 125 Lecture 02

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

CS 11 java track: lecture 3

CH. 2 OBJECT-ORIENTED PROGRAMMING

Brief Summary of Java

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Java Primer. CITS2200 Data Structures and Algorithms. Topic 0

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Introduction to Java Unit 1. Using BlueJ to Write Programs

Transcription:

S.E. Sem. III [CMPN] Object Oriented Programming Methodology Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 80 Q.1(a) Write a program to calculate GCD of two numbers in java. [5] (A) import java.util.*; class test public static void main(string args[ ]) int a, b, g, m, i; System.out.println("Enter nos"); Scanner t = new Scanner (system.in); a = t.nextint( ); b = t.nextint( ); if (a < b) m.a; else m.b; for (i=0; i < m; i++) if ((a % i = = 0)&&(b%i = = 0)) g=i; System.out.println("GCD ="+g); Q.1(b) Explain any three features of java. [5] (A) Features of JAVA (i) Object oriented : Java is object oriented programming language because it follows, following object oriented concept: Class & object Data abstraction Data encapsulation Inheritance Polymorphism 1

: S.E. OOPM (b) Plat from independent : Java is platform independent, because in java, we can compile the program on one machine and we can run it on any other machine, irrespective of their operating system. (c) Compiled and Interpreted : Java is compiled and interpreted, because it has 2 phases, compilation and interpretation. Q.1(c) Draw and explain applet lifecycle. [5] (A) Applet life cycle init() Born stop () start () Running start () The life cycle of an applet consist of following phases: (i) Born: When the init() method of applet will get execute, the applet is said to in born phase. (ii) Running: After initialization, appletviewer automatically calls start() method which brings the applet in running phase. In this phase, whatever is displayed in Point () method will get executed. (iii) Idle: When we minimize the appletviewer or open a new window in front of it, the appletviewer automatically calls stop() method which brings the applet in idle phase. (iv) Dead: When we close the appletviewer it automatically calls destroy (), which brings applet in dead phase. Q.1(d) Explain wrapper class and its applications. [5] (A) Wrapper class are special class. Which cantins different wrapper methods, which are mostly used to convert primitive data types, object and strings into each other. Following are different wrapper classes. Premitive data types Wrapper class int Integer float Float double Double long Long char Character Wrapper classes are mainly used to perform conversing among types and objects. Idle destroy() Dead 2

Q.2(a) 1 1 2 1 2 3 1 2 3 4 Write a program in java to display the above pattern. (A) import java.util.*; class test public static void main (string args [ ]) int n = Integer.parseInt (args [0]; int i, j, a; for (i = 1; i < n; i++) a = l; for (j = 1; j < = i; j++) System.out.print(a); att; system.out.println( ); Prelim Question Paper Solution Q.2(b) Explain how threads are created in java. [5] (A) Thread can be considered as a light weight process which can be mode to execute a dedicated task. Multiple threads can executes simultaneously by shortly the time. Creation of thread Java provides following methods for creation of a thread. Method 1 : Extending Thread class Class sample extends Thread public void( ) [5] 3

: S.E. OOPM class.test public static void main (string args [ ]) smaple x.new sample ( ); Method 2 : Implementing Runnable Interface Class sample Implements Runnable public void main () class test public static void main (string args[ ]) Sample x = new sample ( ); Thread t = new Thread (x); x.start ( ); Q.2(c) Write a applet program to draw circle rectangle and line. [10] (A) import java.applet.* ; import java.awt.* ; /* < applet code = shape height = 800 width = 800 > < / applet > */ public class shape extends applet public void init( ) setbackground(color.yellow); setforeground(color.blue); public void point (Graphics g) g.drawline(100, 100, 500, 100); 4

g.drawrect(200, 200, 400, 200); g.drawoval(300, 500, 200, 200); Prelim Question Paper Solution Q.3(a) Write a proper example and explain the steps to create a [10] package and then add a class or a interface. (A) Creating a package : Create a directory (folder) with a name same as package name, within the current working directory. How to add class (or interface) to a Package? Include package keyword as the first statement in a java source file. Classes defined within that file will belong to the specified package. The package statement defines the name space in which classes are stored. If package statement is omitted the classes are put in default package with no name. Syntax : package package_name; Steps : 1. Open a new file and add the first statement as package package_name; 2. Declare a public class (to be added to package). 3. Save it and compile (javac) it and ensure that the resulting.class file is available in a directory(folder) representing a package. Suppose we have a file called HelloWorld.java, and we want to put this file in a package world. First thing we have to do is to specify the keyword package with the name of the package we want to use (world in our case) on top of our source file, before the code that defines the real classes in the package, as shown in our HelloWorld class below: package world; public class HelloWorld public void show() System.out.println("Hello World"); The above code adds class HelloWorld in package world. 5

: S.E. OOPM Q.3(b) Write a program in java to accept the values of a, b, c and d. calculate and display ((a*d) + (b*c) / (b*d)). (A) importjava.util.*; class Example public static void main(string ar[]) inta,b,c,d,r; Scanner kbd=new Scanner(System.in); try System.out.println( enter the values for a,b,c,d ); a=kbd.nextint(); b=kbd.nextint(); c=kbd.nextint(); d=kbd.nextint(); if(b*d==0) throw new DenominatorZeroException(); else r=( (a*d)+(b*c))/(b*d); System.out.println( result= +r); catch(denominatorzeroexception e) System.out.println( denominator is zero ); catch(exception e) System.out.println( runtime problem ); class DenominatorZeroException extends Exception [10] 6

Prelim Question Paper Solution Q.4(a) What is a vector? Explain any five methods of vector. [10] (A) Vector implements a dynamic array. It is similar to ArrayList, but with two differences: (a) Vector is synchronized. (b) Vector contains many legacy methods that are not part of the collections framework. Vector proves to be very useful if you don't know the size of the array in advance or you just need one that can change sizes over the lifetime of a program. void addelement(object element) The object is added to the vector at the last position. int capacity() Returns the capacity of the vector. int size() Returns the size of the vector. final object firstelement() The method returns the first object of the invoking vector. final Object lastelement() The method returns the last object of the invoking vector. final Object clone() The method returns the duplicate of the invoking vector. final boolean contains(object element) The method returns true if the object is in the vector else returns false. final intindexof(object element) The method returns the index of the first occurrence of the element, if the element is not present returns -1. final intindexof(object element, int start) The method returns the index of first occurrence of the element at or after start. If the element is not present returns -1. final intlastindexof(object element) The method returns the index of the last occurrence of the element. If the element is not present returns -1. 7

: S.E. OOPM final intlastindexof(object element, int start) The method returns the index of last occurrence of the element at or before start. If the element is not present returns -1. Q.4(b) Explain abstract class and abstract methods? [10] (A) Abstract Methods and Classes: An abstract class is a class that is declared abstract it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be extended. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveto(double deltax, double deltay); If a class includes abstract methods, then the class itself must be declared abstract, as in: public abstract class GraphicObject // declare fields // declare nonabstract methods abstract void draw(); When an abstract class is extended, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract. In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveto, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for example: position, fill color, and moveto). Others require different implementations (for example, resize or draw). All GraphicObjects must be able to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object (for example, GraphicObject) as shown in the following figure. 8

Prelim Question Paper Solution First, you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveto method. GraphicObject also declares abstract methods for methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this: abstract class GraphicObject int x, y;... void moveto(intnewx, intnewy)... abstract void draw(); abstract void resize(); Each nonabstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods: class Circle extends GraphicObject void draw()... void resize()... Q.5(a) Write a detailed note on the following : [10] (i) Try-catch (ii) Finally keyword (iii) Catching multiple exception (iv) Throwing exception (A) (i) try-catch : Code that can raise exception are written in try block Catch block is used to handle(catch) occurred exception of specific type mentioned in its parameter A single try block can have multiple catch block Code immediate after catch block is executed once exception occur. Once exception occur remaining code in try block is skipped. Appropriate catch block code is executed in case of multiple catch block depending on type of exception occur 9

: S.E. OOPM (ii) finally : Suppose we don t want particular code to be bypassed by exception handling this problem is handled by finally keyword. Code written inside finally block will be executed immediately after try/catch block. finally block code will execute whether exception occur or not. It will run whether exception caught or not finally block can be written immediate after try block or after catch block The finally block is optional whereas try block need atleast one catch block or finally block (iii) Catch multiple exception : A single try block can have multiple catch block, in order to raise proper exception, catch block with parent exception must be written after written after all child class exception. This is needed because catch statement written next to try block is checked first for execution. (iv) Throwing Exception : If a method is capable of causing an exception that it does not handle,it must specify this behavior by writing the type of exception it can throw, so caller of method will know what kind of exception called method can throw. If this is not handled, program will give compile time error. Q.5(b) Write a java program to find out a number of uppercase, lowercase and blank spaces. (A) import java. util. * ; class strprocessing psrm (string args[ ]) Scanner src = new scanner (System.in) ; Sopln ( Enter a string ) ; String x = src.next Line( ) ; int u = 0, = 0, d = 0, b = 0, s = 0 ; for (int i = 0 ; i < x.length( ) ; i + +) if (x.charat (i) > = A && x.char At (i) < = Z ) u + + ; else if (x.charat (i) > = a && x.char At (1) < = Z ) [10] 10

+ + ; else if (x.charat (i) > = 0 && x.char At (i) < = Q ) d + + ; else if (x.charat (i) == ) b + + ; else s + + ; Sopln ( Upper = + u) ; Sopln ( Lower = + ) ; Sopln ( Digits = + d) ; Sopln ( Blank = + b) ; Sopln ( Special = + s) ; Prelim Question Paper Solution Q.6 Write short notes on [20] Q.6(a) Write short note on Wrapper classes. [5] (A) There are certain concept in java like Vector, which can access only object and not the primitive data type. Hence java creates the appropriate class for all primitive data types, which are called as Wrapper class. Primitive data type Wrapper class Int Integer float float char Character double Double Boolean Boolean Q.6(b) Write short note on JVM. [5] (A) It stands for java virtual machine Java is compiled and interpreted programming language Java has 2 phases ie. Compilation and interpretation Compilation is done by compiler whereas interpretation is done by interpreter. In Java, compiler converts source code into a special code called as byte code. Source code Compiler Machine code 11

: S.E. OOPM whereas, interpreter is used to convert bytecode into machine code Byte code Interpreter Bytecode Java interpreter is called as JVM which convert bytecode into machine code to make java as platform independent. Q.6(c) Write short note on Abstract methods and classes. [5] (A) It is something exactly opposite to that of final method. Abstract methods are those methods which we need to compulsory override in its derived class. If we do not override abstract method, compiler will generate error Abstract method normally only declared in the base class and get defined in the derived class. If a class contains one or more abstract method, then the class is called abstract class. We cannot create object of the abstract class. e.g. abstract class A abstract void display( ); = class B extends A void display () = Q.6(d) Write short note on Package. [5] (A) Package is a collection of similar classes. The various advantages of packages is as follows : (1) Organizing Classes : > Classes can be grouped together into packages so it becomes easy to retrieve. (2) Improve Hiding : > Classes kept in packages are hidden from outside world. > When package is imported, then only public classes can be accessed. 12

Prelim Question Paper Solution (3) Avoiding Naming Conflict : > Two classes with same name cannot be kept in same package but can be kept in different packages. > Such packages can be referred with the help of their respective package name. (4) Helps in Reusability : > Classes kept in packages can be easily reused by importing that package. > Hence it helps in reusability of existing code. 13