Full file at Chapter 2 - Inheritance and Exception Handling

Similar documents
BBM 102 Introduction to Programming II Spring Exceptions

BBM 102 Introduction to Programming II Spring 2017

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Fundamentals of Object Oriented Programming

More on Exception Handling

Inheritance and Polymorphism

More on Exception Handling

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

CMSC 331 Second Midterm Exam

Object-Oriented Design. March 2005 Object Oriented Design 1

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

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

Ch02. True/False Indicate whether the statement is true or false.

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

More on Objects in JAVA TM

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

Exceptions - Example. Exceptions - Example

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Correctness and Robustness

CS 11 java track: lecture 3

ITI Introduction to Computing II

National University. Faculty of Computer Since and Technology Object Oriented Programming

15CS45 : OBJECT ORIENTED CONCEPTS

ITI Introduction to Computing II

An exception is simply an error. Instead of saying an error occurred, we say that an.

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

ECE 122. Engineering Problem Solving with Java

Object Oriented Programming. Java-Lecture 11 Polymorphism

Exception Handling CSCI 201 Principles of Software Development

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Exceptions Handling Errors using Exceptions

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

Exception-Handling Overview

Practice for Chapter 11

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


Tutorial 8 Date: 15/04/2014

C16b: Exception Handling

Recitation 3. 2D Arrays, Exceptions

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

For more details on SUN Certifications, visit

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CS 113 PRACTICE FINAL

17. Handling Runtime Problems

Object-Oriented Concepts

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

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

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

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

EXCEPTIONS. Java Programming

Programming II (CS300)

Object Oriented Java

Inheritance -- Introduction

Chapter 12 Exception Handling

Exception Handling in Java

OVERRIDING. 7/11/2015 Budditha Hettige 82

CH. 2 OBJECT-ORIENTED PROGRAMMING

COMP1008 Exceptions. Runtime Error

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Class, Variable, Constructor, Object, Method Questions

Chapter 13 Exception Handling

APCS Unit 5 Exam. Assuming all four classes have a default constructor, which of the following statements would result in an error from the compiler?

CS159. Nathan Sprague

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Programming II (CS300)

Introduction to Java Written by John Bell for CS 342, Spring 2018

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

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

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Lecture 19 Programming Exceptions CSE11 Fall 2013

What are Exceptions?

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Types, Values and Variables (Chapter 4, JLS)

Full file at

CS-202 Introduction to Object Oriented Programming

Final Exam Practice Questions

Polymorphism. return a.doublevalue() + b.doublevalue();

ILLUSTRATION DES EXCEPTIONS

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

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

Std 12 Lesson-10 Exception Handling in Java ( 1

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Chapter 14 Abstract Classes and Interfaces

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

WOSO Source Code (Java)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Input-Output and Exception Handling

GlobalLogic Technical Question Paper

CS159. Nathan Sprague

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

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

Questions Answer Key Questions Answer Key Questions Answer Key

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Transcription:

Chapter 2 - Inheritance and Exception Handling TRUE/FALSE 1. The superclass inherits all its properties from the subclass. ANS: F PTS: 1 REF: 76 2. Private members of a superclass can be accessed by a subclass. ANS: F PTS: 1 REF: 77 3. Inheritance implies an is-a relationship. ANS: T PTS: 1 REF: 76 4. A subclass cannot directly access public members of a superclass. ANS: F PTS: 1 REF: 78 5. The subclass can override public methods of a superclass. ANS: T PTS: 1 REF: 78 6. In multiple inheritance, the subclass is derived from more than one superclass. ANS: T PTS: 1 REF: 78 7. Java supports both single and multiple inheritance. ANS: F PTS: 1 REF: 78 8. To override a public method of a superclass in a subclass, the corresponding method in the subclass must have the same name but a different number of parameters. ANS: F PTS: 1 REF: 78 9. A subclass can directly access protected members of a superclass. ANS: T PTS: 1 REF: 92 10. The class Object is directly or indirectly the superclass of every class in Java. ANS: T PTS: 1 REF: 97 11. Using the mechanism of inheritance, every public member of the class Object can be overridden and/or can be invoked by every object of any class type. ANS: T PTS: 1 REF: 97 12. In Java, you can automatically make a reference variable of a subclass type point to an object of its superclass.

ANS: F PTS: 1 REF: 98 13. To determine whether a reference variable that points to an object is of a particular class type, Java provides the operator instanceof. ANS: T PTS: 1 REF: 99 14. Composition is a has-a relationship. ANS: T PTS: 1 REF: 105 15. An abstract class can only contain abstract methods. ANS: F PTS: 1 REF: 104 16. You can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass. ANS: T PTS: 1 REF: 104 17. In dynamic binding the method that gets executed is determined at execution time, not at compile time. ANS: T PTS: 1 REF: 99 18. The class Throwable is derived from the class Exception. ANS: F PTS: 1 REF: 111 19. A checked exception is any exception checked for by the programmer. ANS: F PTS: 1 REF: 117 20. Every program with a try block must end with a finally block. ANS: F PTS: 1 REF: 118 21. If in the heading of a catch block you declare an exception using the class Exception, then that catch block can catch all types of exceptions because the class Exception is the superclass of all exception classes. ANS: T PTS: 1 REF: 120 22. If an exception occurs in a try block and that exception is caught by a catch block, then the remaining catch blocks associated with that try block are ignored. ANS: T PTS: 1 REF: 120 23. The order in which catch blocks are placed in a program has no impact on which catch block is executed. ANS: F PTS: 1 REF: 120

24. If you have created an exception class, you can define other exception classes extending the definition of the exception class you created. ANS: T PTS: 1 REF: 130 25. The try block contains statements that should be executed regardless of whether or not an exception occurs. ANS: F PTS: 1 REF: 118 MULTIPLE CHOICE 1. Inheritance is an example of what type of relationship? a. is-a c. was-a b. has-a d. had-a ANS: A PTS: 1 REF: 76 2. Any new class you create from an existing class is called a(n). a. base class c. derived class b. superclass d. extended class ANS: C PTS: 1 REF: 76 3. A subclass can directly access. a. public members of a superclass c. all members of a superclass b. private members of a superclass d. none of the members of a superclass ANS: A PTS: 1 REF: 78 4. If there are three classes, Shape, Circle and Square, what is the most likely relationship between them? a. Square is a superclass, and shape and circle are subclasses of Square. b. Shape is a superclass, and circle and square are subclasses of Shape. c. Shape, circle and square are all sibling classes. d. These three classes cannot be related. ANS: B PTS: 1 REF: 77 5. Which of the following statements about the reference super is true? a. It must be used every time a method from the superclass is called. b. It must be the last statement of the constructor. c. It must be the first statement of the constructor. d. It can only be used once in a program. ANS: C PTS: 1 REF: 84 6. If class Dog has a subclass Retriever, which of the following is true? a. Because of single inheritance, Dog can have no other subclasses. b. Because of single inheritance, Retriever can extend no other class except Dog. c. The relationship between these classes implies that Dog is a Retriever. d. The relationship between these classes implies that Retriever has-a Dog. ANS: B PTS: 1 REF: 78

Suppose a class Car and its subclass Honda both have a method called speed as part of the class definition. rentalh refers to an object of the type Honda and the following statements are found in the code: rentalh.cost(); super.speed(); 7. What will the first statement in the situation described above do? a. The cost method in Honda will be called. b. The cost method in Car will be called. c. Nothing will be called since the code will not compile as a result of multiple definitions of speed. d. Overloading will be used to determine which cost method to use. ANS: A PTS: 1 REF: 82 8. What does the second statement in the description above do? a. The speed method in Honda will be called. b. The speed method in Car will be called. c. Nothing will be called since the code will not compile as a result of multiple definitions of speed. d. Overloading will be used to determine which method of speed to use. ANS: B PTS: 1 REF: 82 9. An abstract class. a. does not have any subclasses c. cannot be instantiated b. is a superclass with many subclasses d. is the base class of all other classes ANS: C PTS: 1 REF: 104 10. An abstract method. a. is any method in the abstract class b. cannot be inherited c. has no body d. is found in a subclass and overrides methods in a superclass using the reserved word abstract ANS: C PTS: 1 REF: 104 11. Composition is a(n) relationship. a. is-a c. was-a b. has-a d. had-a ANS: B PTS: 1 REF: 105 12. An abstract class can define. a. only abstract methods c. abstract and non-abstract methods b. only non-abstract methods d. only abstract instance variables ANS: C PTS: 1 REF: 104 13. What is the correct syntax for defining a new class Parakeet based on the superclass Bird. a. class Parakeet is a Bird b. class Bird defines Parakeet c. class Bird has a Parakeet

d. class Parakeet extends Bird ANS: D PTS: 1 REF: 77 14. Which operator is used to determine if an object is of a particular class type? a. The operator new c. The instanceof operator b. The dot (.) operator d. The + operator ANS: C PTS: 1 REF: 99 15. When is a finally block executed? a. Only when an exception is thrown by a try block. b. Only when there are no exceptions thrown. c. At the end of a program. d. Always after the execution of a try block, regardless of whether or not an exception is thrown. ANS: D PTS: 1 REF: 118 16. How many finally blocks can there be in a try/catch structure? a. There must be 1. b. There can be 1 following each catch block. c. There can be 0 or 1 following the last catch block. d. There is no limit to the number of catch blocks following the last catch block. ANS: C PTS: 1 REF: 118 done=false; do try System.out.print( Enter an integer: ); System.out.flush(); number=integer.parseint(keyboard.readline()); System.out.println(); done=true; catch(thisexception exref) System.out.println( \n Exception + nferef.tostring()); while(!done); 17. Which exception-handling technique is the code above using? a. Terminate the program b. Fix the error and continue c. Log the error and continue d. Fix the error and terminate the program ANS: B PTS: 1 REF: 129 18. What is most likely the type of exception (ThisException) in the code above? a. IllegalArgumentException c. FileNotFoundException

b. NullPointerException d. NumberFormatException ANS: D PTS: 1 REF: 129 19. How many times will the code in the try block above execute? a. Until the user specifies that he/she wants to quit the program. b. Until the user inputs a valid integer. c. If there is an exception thrown it will execute just once, because the program will terminate at that point. d. Zero times; the program will terminate before it reaches the try block. ANS: B PTS: 1 REF: 129 20. Which class of exceptions is not checked? a. IOException c. RuntimeExceptions b. ArithmeticException d. All exceptions are checked ANS: C PTS: 1 REF: 118 21. Which of the following is not a typical action of the catch block? a. Completely handling the exception b. Partially processing the exception. c. Rethrowing the same exception for the calling environment d. Throwing the exception ANS: D PTS: 1 REF: 124 22. Which of the following statements is true? a. The class Object, which is derived from the class Throwable, is the superclass of the class Exception. b. The class Exception, which is derived from the class Object, is the superclass of the class Throwable. c. The class Throwable, which is derived from the class Object, is the superclass of the class Exception. d. The class Throwable, which is derived from the class Exception, is the superclass of the class Object. ANS: C PTS: 1 REF: 111 import java.io.*; public class ExceptionExample1 static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in)); public static void main(string[] args) int dividend, divisor, quotient; try System.out.print( Enter dividend: ); System.out.flush(); dividend = Integer.parseInt(keyboard.readLine()); System.out.println(); System.out.print( Enter divisor: ); System.out.flush();

divisor = Integer.parseInt(keyboard.readLine()); System.out.println(); quotient=dividend/divisor; System.out.println( quotient = +quotient); catch(arithmeticexception aeref) System.out.println( Exception + aeref.tostring()); catch(numberformatexception nferef) System.out.println( Exception + nferef.tostring()); catch(ioexception ioeref) System.out.println( Exception +ioeref.tostring()); 23. Which of the following will cause the first exception to occur in the code above? a. if the divisor is zero b. is the dividend is zero c. if the quotient is zero d. this code will not compile so an exception cannot be triggered ANS: A PTS: 1 REF: 124 24. Which of the following inputs would be caught by the second catch block in the program above? a. 0 c. h3 b. 10 d. -1 ANS: C PTS: 1 REF: 122 25. The term overriding is synonymous with. a. overloading c. binding b. redefining d. referencing ANS: B PTS: 1 REF: 78