Practice for Chapter 11

Similar documents
Questions Answer Key Questions Answer Key Questions Answer Key

Questions Answer Key Questions Answer Key Questions Answer Key

Questions Answer Key Questions Answer Key Questions Answer Key

Chapter 5 Object-Oriented Programming

CS-202 Introduction to Object Oriented Programming

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

What is Inheritance?

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Instruction to students:

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and Polymorphism

Inheritance -- Introduction

25. Generic Programming

Class, Variable, Constructor, Object, Method Questions

Inheritance and Polymorphism

Rules and syntax for inheritance. The boring stuff

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Inheritance and object compatibility

CH. 2 OBJECT-ORIENTED PROGRAMMING

Programming II (CS300)

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Chapter 14 Abstract Classes and Interfaces

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

Inheritance (continued) Inheritance

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

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Java: introduction to object-oriented features

CMSC 132: Object-Oriented Programming II

C++ Important Questions with Answers

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Java Object Oriented Design. CSC207 Fall 2014

Object-Oriented Concepts

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

CS 251 Intermediate Programming Inheritance

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

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

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

PROGRAMMING LANGUAGE 2

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

More about inheritance

More On inheritance. What you can do in subclass regarding methods:

CS Programming I: Inheritance

Object Oriented Programming: Based on slides from Skrien Chapter 2

15CS45 : OBJECT ORIENTED CONCEPTS

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

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

Object-Oriented Programming More Inheritance

Lecture 4: Extending Classes. Concept

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

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

Chapter 11 Inheritance and Polymorphism

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

C++ Inheritance and Encapsulation

Inheritance. A key OOP concept

[ L5P1] Object-Oriented Programming: Advanced Concepts

CLASS DESIGN. Objectives MODULE 4

CS1150 Principles of Computer Science Objects and Classes

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Overriding Variables: Shadowing

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Introduction to Programming Using Java (98-388)

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

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

Lecture 18 CSE11 Fall 2013 Inheritance

CO Java SE 8: Fundamentals

Implements vs. Extends When Defining a Class

Full file at Chapter 2 - Inheritance and Exception Handling

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

OVERRIDING. 7/11/2015 Budditha Hettige 82

Example: Count of Points

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

JAVA MOCK TEST JAVA MOCK TEST II

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

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 5. Inheritance

Introduction to Inheritance

Programming overview

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Classes and Inheritance Extending Classes, Chapter 5.2

Binghamton University. CS-140 Fall Dynamic Types

Type Hierarchy. Lecture 6: OOP, autumn 2003

Object Orientated Programming Details COMP360

Object Orientated Analysis and Design. Benjamin Kenwright

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

More on Inheritance. Interfaces & Abstract Classes

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

More Relationships Between Classes

C08: Inheritance and Polymorphism

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

First IS-A Relationship: Inheritance

C++ Inheritance and Encapsulation

Transcription:

Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing classes. This is called. A) inheritance B) generalization C) encapsulation D) abstraction 1) 2) Which of the following statements are true? 2) A) A subclass is usually extended to contain more functions and more detailed information than its superclass. B) A subclass is a subset of a superclass. C) "class A extends B" means B is a subclass of A. D) "class A extends B" means A is a subclass of B. 3) Analyze the following code: 3) public class Test extends A { Test t = new Test(); t.print(); String s; A(String s) { this.s = s; public void print() { System.out.println(s); A) The program would compile if a default constructor A(){ is added to class A explicitly. B) The program compiles, but it has a runtime error due to the conflict on the method name print. C) The program has an implicit default constructor Test(), but it cannot be compiled, because its super class does not have a default constructor. The program would compile if the constructor in the class A were removed. D) The program does not compile because Test does not have a default constructor Test(). 1

4) Analyze the following code: 4) B b = new B(); b.m(5); System.out.println("i is " + b.i); int i; public void m(int i) { this.i = i; class B extends A { public void m(string s) { A) The program has a compilation error, because b.m(5) cannot be invoked since the method m(int) is hidden in B. B) The method m is not overridden in b. B inherits the method m from A and defines an overloaded method m in b. C) The program has a compilation error, because m is overridden with a different signature in B. D) The program has a runtime error on b.i, because i is not accessible from b. 2

5) Analyze the following code: 5) new B(); int i = 7; public A() { System.out.println("i from A is " + i); public void seti(int i) { this.i = 2 * i; class B extends A { public B() { seti(20); // System.out.println("i from B is " + i); public void seti(int i) { this.i = 3 * i; A) The constructor of class A is not called. B) The constructor of class A is called and it displays "i from A is 60". C) The constructor of class A is called and it displays "i from A is 7". D) The constructor of class A is called and it displays "i from A is 40". 6) Which of the following statements are true? 6) A) It is a compilation error if two methods differ only in return type in the same class. B) To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. C) A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. D) A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. E) Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. 3

7) What is the output of the following code? 7) new Person().printPerson(); new Student().printPerson(); class Student extends Person { public String getinfo() { return "Student"; class Person { public String getinfo() { return "Person"; public void printperson() { System.out.println(getInfo()); A) Person Student B) Person Person C) Stduent Student D) Student Person 8) Composition means. 8) A) that a class extends another class B) that a variable of supertype refers to a subtype object C) that a class contains a data field that references another object D) that data fields should be declared private 9) Inheritance means. 9) A) that a class can contain another class B) that a variable of supertype can refer to a subtype object C) that a class can extend another class D) that data fields should be declared private 10) Encapsulation means. 10) A) that data fields should be declared private B) that a variable of supertype can refer to a subtype object C) that a class can extend another class D) that a class can contain another class 11) Polymorphism means. 11) A) that a class can extend another class B) that data fields should be declared private C) that a variable of supertype can refer to a subtype object D) that a class can contain another class 4

12) Which of the following classes cannot be extended? 12) A) final B) C) protected A(); D) private A(); 13) Which statements are most accurate regarding the following classes? 13) private int i; protected int j; class B extends A { private int k; protected int m; A) An object of B contains data fields j, m. B) An object of B contains data fields i, j, k, m. C) An object of B contains data fields k, m. D) An object of B contains data fields j, k, m. 14) What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package? A) private B) Use the default modifier. C) public D) protected 15) What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it? A) public B) protected C) Use the default modifier. D) private 14) 15) 16) Invoking returns the number of the elements in an ArrayList x. 16) A) x.length(1) B) x.getlength(0) C) x.size() D) x.getsize() 17) You can create an ArrayList using. 17) A) ArrayList() B) new ArrayList[] C) new ArrayList[100] D) new ArrayList() 18) What is the output of the following code? 18) Object o1 = new Object(); Object o2 = new Object(); System.out.print((o1 == o2) + " " + (o1.equals(o2))); A) true true B) true false C) false true D) false false 5

19) Given the following code: 19) class C1 { class C2 extends C1 { class C3 extends C2 { class C4 extends C1 { C1 c1 = new C1(); C2 c2 = new C2(); C3 c3 = new C3(); C4 c4 = new C4(); Which of the following expressions evaluates to false? A) c2 instanceof C1 B) c4 instanceof C2 C) c3 instanceof C1 D) c1 instanceof C1 20) Given the following classes and their objects: 20) class C1 {; class C2 extends C1 {; class C3 extends C1 {; C2 c2 = new C2(); C3 c3 = new C3(); Analyze the following statement: c2 = (C2)((C1)c3); A) You will get a runtime error because you cannot cast objects from sibling classes. B) You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form. C) c3 is cast into c2 successfully. D) The statement is correct. 21) Which of the following are Java keywords? 21) A) cast B) casting C) instanceof D) instanceof 6