Tutorial 8 Date: 15/04/2014

Similar documents
1.Which four options describe the correct default values for array elements of the types indicated?

d. If a is false and b is false then the output is "ELSE" Answer?

1. What details should never be found in the top level of a top-down design?

For more details on SUN Certifications, visit

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Full file at Chapter 2 - Inheritance and Exception Handling


CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

2. How many runtime error messages associated with exception? a) 2 b) 4 c) 5 d) infinite

CSCE145 Test 2-Review 03/29/2015 Hongkai Yu

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

BBM 102 Introduction to Programming II Spring Exceptions

15CS45 : OBJECT ORIENTED CONCEPTS

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

More on Exception Handling

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Class, Variable, Constructor, Object, Method Questions

Object-Oriented Design. March 2005 Object Oriented Design 1

Fundamentals of Object Oriented Programming

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Recitation 3. 2D Arrays, Exceptions

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Java Errors and Exceptions. Because Murphy s Law never fails

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

CS 113 PRACTICE FINAL

More on Exception Handling

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Introduction to Computer Science Unit 2. Exercises

BBM 102 Introduction to Programming II Spring 2017

Practice for Chapter 11

Object Oriented Programming Exception Handling

CIS265 - Spring Exam 2 First Name Last Name CSU#

Exception-Handling Overview

Object-Oriented Concepts

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Exceptions - Example. Exceptions - Example

Chapter 5 Object-Oriented Programming

Introduction to Software Design

Sun Certified Programmer for Java 2 Platform 1.4. Version 3.0

AP CS Unit 6: Inheritance Notes

What are Exceptions?

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

CS 11 java track: lecture 3

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

2.6 Error, exception and event handling

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

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

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

SECTION-1 Q.1.Which two code fragments are most likely to cause a StackOverflowError? (Choose two.)

Module 4 - 异常和断言 一 选择题 :

ITI Introduction to Computing II

EXCEPTIONS. Java Programming

Exception Examples. All examples written by Edith Hemaspaandra and modified by Jessica Bayliss

Exceptions. What exceptional things might our programs run in to?

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?

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

ITI Introduction to Computing II

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Types, Values and Variables (Chapter 4, JLS)

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

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

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

AP CS Unit 8: Inheritance Exercises

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Unit 5 - Exception Handling & Multithreaded

Java is an Object Oriented Programming language i.e. it supports four major pillars of OOPS.

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

Full file at

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Introduction to Computing II (ITI 1121) Final Examination

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Programming II (CS300)

Here is a hierarchy of classes to deal with Input and Output streams.

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)

ECE 122. Engineering Problem Solving with Java

Introduction to Object-Oriented Programming

Exceptions Handling Errors using Exceptions

Chapter 12 Exception Handling

Exceptions. Author: Boaz Kantor The Interdisciplinary Center, Herzliya Introduction to Computer Science Winter Semester

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

Data Abstraction: The Walls

RZQ 12/19/2008 Page 1 of 11

Programming II (CS300)

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

Chapter 13 Exception Handling

C16b: Exception Handling

POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OF COMPUTER SCIENCE

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

Visibility, Static, and Exception Handling. June 19, 2017

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

1 Method Signatures and Overloading (3 minutes, 2 points)

Chapter 3. Selections

Transcription:

Tutorial 8 Date: 15/04/2014 1. What is wrong with the following interface? public interface SomethingIsWrong void amethod(int avalue) System.out.println("Hi Mom"); 2. Fix the interface in Question 2. 3. Is the following interface valid? public interface Marker 4. Assess the following Java program codes to discuss how principles of object oriented programming are applied in these codes. a) Class test int x,y; public void Add() System.out.println( x+y +(x+y)); public void Add(int a, int b) System.out.println((x+a)+, +(y+b)); Public void Add(double a, double b) System.out.println((x+a)+, +(y+b));

b) class person String name, address; public void display () public void enterdata() Class student extends person void enterdata() void DisplayStd() c) What will be the output of the program? try int x = 0; int y = 5 / x; catch (Exception e) System.out.println("Exception"); catch (ArithmeticException ae) System.out.println(" Arithmetic Exception"); System.out.println("finished"); A. finished B. Exception C. Compilation fails. D. Arithmetic Exception d 6. What will be the output of the program?

public class X public static void main(string [] args) try badmethod(); System.out.print("A"); catch (Exception ex) System.out.print("B"); finally System.out.print("C"); System.out.print("D"); public static void badmethod() throw new Error(); /* Line 22 */ A. ABCD B. Compilation fails. C. C is printed before exiting with an error message. D. BC is printed before exiting with an error message. 7. what will be the output of the program? public class X

public static void main(string [] args) try badmethod(); System.out.print("A"); catch (RuntimeException ex) /* Line 10 */ System.out.print("B"); catch (Exception ex1) System.out.print("C"); finally System.out.print("D"); System.out.print("E"); public static void badmethod() throw new RuntimeException(); A. BD B. BCD C. BDE D. BCDE Answer 1: It has a method implementation in it. Only default and static methods have implementations.

Answer 2: public interface SomethingIsWrong void amethod(int avalue); Alternatively, you can define amethod as a default method: public interface SomethingIsWrong default void amethod(int avalue) System.out.println("Hi Mom"); Answer 3: Yes. Methods are not required. Empty interfaces can be used as types and to mark classes without requiring any particular method implementations. For an example of a useful empty interface. Answer 4: a) Encapsulation: it uses class that encapsulates data and methods together. Polymorphism: it uses method overloading is implemented in this code. Where Class test contains three methods with the same name Add with different parameters. b) Encapsulation: it uses class that encapsulates data and methods together. The inheritance principle: Class person is superclass of class student. Subclass student can access variables of its superclass. Polymorphism: it uses method overriding where Class student have enterdata() method with the same signature in super class person.

Answer 5: Option C Explanation: Compilation fails because ArithmeticException has already been caught.arithmeticexception is a subclass of java.lang.exception, by time thearithmeticexception has been specified it has already been caught by theexception class. If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses). Answer 6: Option C Explanation: Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.error). Answer 7: Option C Explanation: A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.