DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Similar documents
Lab 14 & 15: String Handling

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Building Strings and Exploring String Class:

Lecture Notes K.Yellaswamy Assistant Professor K L University

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Chettinad College of Engineering & Technology Department of Information Technology Internal Examination II (Answer Key)

String. Other languages that implement strings as character arrays

UNIT - II Object Oriented Programming - Inheritance

appreciate the difference between a char and a string understand and use the String class methods

STRUCTURING OF PROGRAM

We now start exploring some key elements of the Java programming language and ways of performing I/O

Java: introduction to object-oriented features

Lesson:9 Working with Array and String

Arrays Classes & Methods, Inheritance

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)

Chapter 6 Introduction to Defining Classes

Programming with Java

Class, Variable, Constructor, Object, Method Questions

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Sri Vidya College of Engineering & Technology

Introduction to Programming Using Java (98-388)

"Hello" " This " + "is String " + "concatenation"

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

Java Magistère BFA

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

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

CMSC 132: Object-Oriented Programming II

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Array is collection of homogenous Data items. Array is classified into three types.

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Prepared By: Shiba R. Tamrakar

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Selected Questions from by Nageshwara Rao

A variable is a name for a location in memory A variable must be declared

UNIT - V. Inheritance Interfaces and inner classes Exception handling Threads Streams and I/O

STUDENT LESSON A10 The String Class

2. Introducing Classes

15CS45 : OBJECT ORIENTED CONCEPTS

Object-Oriented Concepts

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Java Bytecode (binary file)

CSC 1214: Object-Oriented Programming

CH. 2 OBJECT-ORIENTED PROGRAMMING

Chapter 5. Inheritance

Netbeans tutorial:

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

Unit 4 - Inheritance, Packages & Interfaces

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

Preview from Notesale.co.uk Page 9 of 108

Programming II (CS300)

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Introduction to Java

7. C++ Class and Object

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

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Inheritance, and Polymorphism.


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

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS260 Intro to Java & Android 03.Java Language Basics

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

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Inheritance (Part 5) Odds and ends

Static, Final & Memory Management

Introduction to Java (All the Basic Stuff)

More on variables and methods

Using Java Classes Fall 2018 Margaret Reid-Miller

TeenCoder : Java Programming (ISBN )

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Programming overview

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

UNIT -IV INHERITANCE, PACKAGES AND INTERFACES

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

1 Shyam sir JAVA Notes

Creating Strings. String Length

Lecture 2: Java & Javadoc

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

TEST (MODULE:- 1 and 2)

CS-202 Introduction to Object Oriented Programming

SSE3052: Embedded Systems Practice

Lecture 18 CSE11 Fall 2013 Inheritance

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Classes and Objects Part 1

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Array. Prepared By - Rifat Shahriyar

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

- Thus there is a String class (a large class)

S.E (Computer ) (Second Semester) EXAMINATION, Time : Two Hours Maximum Marks : 50

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

( &% class MyClass { }

Transcription:

Internal Examination 1 Answer Key DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Branch & Sec : CSE Date : 08.08.2014 Semester : V Sem Max Marks : 50 Marks Sub Code& Title : CS2305 Programming Paradigms Staff I/c : R.Venkadeshan PART A Answer all the Questions (5*2=10 Marks) 1. Define Objects and Classes in Java. Objects are the basic runtime entities in an object oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program may handle. They may also represent user defined data types such as vectors and lists. The entire set of data and code of an object can be made a user defined data type using the concept of a class. A class may be thought of as a data type and an object as a variable of that data type. A class is thus a collection of objects of similar type. 2. What is the use of super keyword in Java? 1. Using super to call Superclass Constructors 2. Using super to call Superclass Methods 3. List out the different JavaDoc Comments. Types of Comments Class Comments - /*.*/ and /* * */ Method Comments - @param, @return, @throws Field Comments Data Members - /* * */ General Comments - @author, @version, @since. 4. Define the different forms of Inheritance with its syntax. 1. Classical form of Inheritance is-a relationship 2. Containment form of Inheritance has-a relationship 5. Write the syntax of foreach in Java? Syntax: for (variable : collection) Statement; PART B (40 Marks) Answer All the Questions 6. a) i. Create a class Student, declare the non- static data members such as studid, studname, and static data members such as m1, m2, m3, tot, avg. Define the Constructor to initialize the non-static members, use the static member function to get the marks details from the user and perform total and average calculations. Finally use the member function display () to print all the details to the user. (12)

class Student int studid; String studname; static double m1,m2,m3,tot,avg; Student() Scanner in = new Scanner(System.in); System.out.println( Enter Student ID: ); studid = in.nextint(); System.out.println( Enter Student Name: ); studname = in.next(); static void process() System.out.println( Enter Student s Marks-1: ); Scanner mk = new Scanner(System.in); m1 = mk.nextint(); System.out.println( Enter Student s Marks-2: ); m2 = mk.nextint(); System.out.println( Enter Student s Marks-3: ); m3 = mk.nextint(); total = m1+m2+m3; avg = total/3.0; void display() if(m1>=50 && m2>==50 && m3>=50) System.out.println( Student ID is: +studid); System.out.println( Student Name is: +studname); System.out.println( Mark-1 : +m1); System.out.println( Mark-2 : +m2); System.out.println( Mark-3 : +m3); System.out.println( Total : +total); System.out.println( Average : +avg); System.out.println( Result: Pass ); else System.out.println( Result: Fail );

class Demo public static void main(string args[]) Student s = new Student(); Student.process(); s.display(); ii. Define the use of finalize() method in Java. (4) Sometimes an object will need to perform some action when it is destroyed. By using finalization, object can reclaimed by the garbage collection. To add a finalizer to a class, simply define the finalize() method. Java runtime calls that method whenever it is about to recycle an object of that class. Inside the finalize() method you will specify those actions that must be performed before an object is destroyed. The garbage collector runs periodically, checking for objects that are no longer referenced by any running state or indirectly through other referenced objects. User doesn t know when this method will invoke, it depends JVM. Syntax: protected void finalize() //Body of the Method Protected is a specifier that prevents access to finalize() by code define outside its class. finalize() method is just prior to garbage collection, and not called when an object goes out-of-scope means you cannot know when or even if finalize() will be executed. Java does not support destructors, finalize() method only approximates this function. finalize() method provides other means of releasing System resources. (or) b) i. Define Package in Java. Explain in details about the creation of user-defined package with the support of Access Specifiers. (16) Packages are java s way of grouping a variety of classes and / or interfaces together. The grouping is usually done according to functionality. In fact, packages act as containers for classes. Creating our own package involves the following steps: 1. Declare the package at the beginning of a file using the form package package_name; 2. Define the class that is to be put in the package and declare it public. 3. Create a subdirectory under the directory where the main source files are stored. 4. Save the Package file in.java compile the file creates.class file in the subdirectory. 5. The subdirectory name must match the package name exactly. Note: Java also supports the concept of package hierarchy. This done by specifying multiple names in a package statement, separated by dots. Example: package firstpackage.secondpackage;

package package1; public class ClassA public void displaya() System.out.println("Class A"); Access Modifier Access Location Public Protected friendly (default) private Same Class Yes Yes Yes Yes Subclass in same package Yes Yes Yes No Other classes in same package Yes Yes Yes No Subclass in other package Yes Yes No No Non subclasses in other packages Yes No No No import package1.classa; class PackageTest1 public static void main(string args[]) ClassA objecta = new ClassA() objecta.displaya(); 7. a) i. Explain the following String Handling functions in Java. (16) String Constructor String str = new String(); String(char ch[], int start,int numchar); String(String strobj); String (byte asciichars[]); String(byte asciichars[], int start, int numchars); Character Extraction functions char charat(int where) void getchars(int sourcestart,int sourceend,char target [ ],int targetstart) byte [] getbytes(); char [ ] tochararray() String Modification functions String substring(int startindex) String substring(int startindex,int endindex) String concat(string str); String replace(char original, char replacement)

String trim() String touppercase(); String tolowercase(); String Comparison Functions. boolean equals(string str) boolean equalsignorecase(string str) boolean regionmatches(int str1startindex, String str2, int str2startindex, int numchars) boolean regionmatches(boolean ignorcase, int str1startindex, String str2, int str2startindex, int numchars) boolean startswith(string str) boolean endswith(string str) obj1.equals(obj2); (obj1 == obj2); int compareto(string str); (or) b) i. Give an elaborate discussion on Inheritance and its types with the suitable support of program. (16) Inheritance is the mechanism which allows a class B to inherit properties/characteristics-attributes and methods of a class A. We say B inherits from A". What are the Advantages of Inheritance 1. Reusability of the code. 2. To Increase the reliability of the code. 3. To add some enhancements to the base class. Inheritance achieved in two different forms 1. Classical form of Inheritance 2. Containment form of Inheritance Classical form of Inheritance We can now create objects of classes A and B independently. Example: A a; //a is object of A B b; //b is object of B Containment Inheritance We can also define another form of inheritance relationship known as containership between class A and B. Example: class A -------- class B --------- A a; // a is contained in b B b;

Types of Inheritance 1. Single Inheritance (Only one Super Class and One Only Sub Class) 2. Multilevel Inheritance (Derived from a Derived Class) 3. Hierarchical Inheritance (One Super Class, Many Subclasses) 8. a) Write a program to implement the Method Overriding concept in Java.(8) class Room void Room_Super() System.out.println("The Room Base is Displayed"); class HallRoom extends Room void Room_Super() System.out.println("The Sub Class Room Base is Displayed"); super.room_super(); class MainRoom public static void main(string [] args) HallRoom br = new HallRoom(); br.room_super(); ***** All the Best *****