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

Similar documents
Chapter 14 Abstract Classes and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Chapter 5 Object-Oriented Programming

JAVA MOCK TEST JAVA MOCK TEST II

Java Fundamentals (II)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Inheritance and Polymorphism

Practice for Chapter 11

CS 251 Intermediate Programming Inheritance

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

CS-202 Introduction to Object Oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (Outsource: )

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

Chapter 5. Inheritance

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Inheritance, Polymorphism, and Interfaces

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

COP 3330 Final Exam Review

Inheritance and Interfaces

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

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

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

Inheritance -- Introduction

Introduction to Programming Using Java (98-388)

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

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

CS Programming I: Inheritance

CLASS DESIGN. Objectives MODULE 4

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

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

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

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

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

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

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

Data Structures (list, dictionary, tuples, sets, strings)

PROGRAMMING LANGUAGE 2

Chapter 10 Classes Continued. Fundamentals of Java

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

More about inheritance

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

15CS45 : OBJECT ORIENTED CONCEPTS

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

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

Lecture 2: Java & Javadoc

Object-Oriented Programming More Inheritance

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

Framework Fundamentals

Inheritance and Polymorphism

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

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

ITI Introduction to Computing II

Java for Non Majors Spring 2018

First IS-A Relationship: Inheritance

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

index.pdf January 21,

ITI Introduction to Computing II

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

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Method Overriding in Java

Inheritance and Polymorphism

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Rules and syntax for inheritance. The boring stuff

Full file at Chapter 2 - Inheritance and Exception Handling

Objects and Iterators

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

Questions Answer Key Questions Answer Key Questions Answer Key

Polymorphism. Agenda

CS260 Intro to Java & Android 03.Java Language Basics

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Programming II (CS300)

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

Class, Variable, Constructor, Object, Method Questions

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

Inheritance. Transitivity

Outline. 15. Inheritance. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Inheritance and Interfaces

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Object Oriented Programming: Based on slides from Skrien Chapter 2

ECE 122. Engineering Problem Solving with Java

More Relationships Between Classes

Inheritance (Part 2) Notes Chapter 6

Object-Oriented Programming

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

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

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

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

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Transcription:

CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring ( String a, String b) { 2 return a == b; Given the two strings a and b, checkstring returns true if both string objects have the same memory address. That means they are both references to the same thing. The function does not check to see if the two strings have the same sequence of characters. It is possible for two different strings to have the same value ( hellos ) but with different memory addresses. By default, Object s equals method compares memory addresses, but it can be extended by subclasses to check the objects attributes. In Java, it is not possible to override how == works. To check if the contents of the strings are the same, use the equals method, defined in the Object class and overridden in the String class. For example, "yes".equals("yes"). 2. If an instance variable is declared with protected access, who can access it? The class that the variable is defined in, any subclasses of that class, and any classes in the same package as that class. 3. What is the difference between overriding and overloading methods? Give a example situation where each should be used. Overriding: method with the exact same method declaration as a method in a superclass; overwrites and/or extends the functionality of the superclass s method. Overloading: 2 or more methods with the same name that either: (a) have a different number of parameters OR (b) have parameters of different types Override a method when you want to replace the implementation of a superclass s method. Overload a method (such as a constructor) when you want to have more than one implementation of a method for different circumstances.. Explain the differences between: (a) class vs object A class defines methods and fields it can be viewed as a template. An object is an instance of a class. Think of classes as molds and objects as individual things created by those molds. (b) constant vs non-constant field (variable). Declare a constant. A constant field cannot be changed during run time (it may be set once). public final int NUM_PEOPLE_WHO_LIKE_JAVA = 1; Note that marking mutable types final only prevents them from being reassigned; they can still call methods that mutate themselves. For example, you can declare a final ArrayList, but this does not prevent you from modifying the contents of it, such as through the use of the add(t toinsert) method. (c) final vs non-final method. A final method can t be overridden by a subclass. (d) class vs instance variable. Declare variables of both types. Class (static) variables are associated with the class, not to instances of that class. All instances of a class access the same static variable. However, instance (non-static) variables are unique to each instance. class Types { public static int im_a_class_var = 1; public int im_an_instance_var = 2; } 1

5. Define polymorphism and explain a situation in which you would use it. Polymorphism: when objects of various types define a common interface of operations (in other words, objects of different subclasses appear and act as a shared superclass). References to and collections of a super class may hold instances of subclasses. Methods invoked on these objects determine the correct (type-specific) behavior at runtime. That is, an object instantiated from a subclass will use the subclass s implementation of a method, even if it is stored in a reference to its superclass. This allows different subclasses to be plugged in and used by superclass variable. Example: 1 ArrayList<Shape> shapes = new ArrayList<Shape >(); 2 3 // Square with s i d e l e n g t h o f 1 shapes. add (new Square ( 1. 0 ) ) ; 5 6 // C i r c l e with r a d i u s o f 9 7 shapes. add (new C i r c l e ( 9. 0 ) ) ; 8 9 // Triangle with base o f and h e i g h t o f 5 10 shapes. add (new T r i a n g l e (. 0, 5. 0 ) ) ; 11 12 // Outputs the c o r r e c t area o f each shape 13 for ( int i = 0 ; i < shapes. s i z e ( ) ; i++ ){ 1 double area = shapes. get ( i ). getarea ( ) ; 15 System. out. p r i n t l n ( area ) ; 16 } 6. What is the difference between an abstract class and an interface? Why might you want to use an interface over an abstract class? Abstract classes can have fields, whereas interfaces cannot. Classes can implement multiple interfaces, but can only extend a single class. Because of this, if you want a class to be able to be treated as two or more unrelated types, make those types interfaces. In Java 7 and earlier, interfaces can only have method signatures, without any implementation. Abstract classes can have a mix of abstract methods and methods with an implementation. This allows abstract classes to provide default behavior for their subclasses, as well as common base behavior that subclasses can extend from. 7. Describe the difference between Comparable and Comparator. Both are interfaces. Comparable specifies a method called compareto() that takes one argument: the object to compare with the one on which the method was called. Comparator specifies a method called compare() that instead takes two arguments and compares those objects to each other. Classes implementing Comparator are classes whose sole purpose is to define a comparing mechanism for a particular type. There can be multiple Comparators for a type, if that type has multiple ways to be compared. If there is a single natural way to compare instances of a particular class, that class can implement the Comparable interface and define the comparison itself. This removes the need to store and pass the comparison mechanism separate from the objects it is comparing. A Comparator can also override a class s natural ordering. 8. What is the Java Collections Framework? A unified set of interfaces, algorithms and concrete implementations provided by Java to support collections of objects. 9. What gets printed by the following code? 1 public class Class1 { 2 public Class1 () { 3 System. out. println ( " Class1 ()" ); } 2

5 public void print1 () { 6 System. out. println ( " Class1. print1 ()" ); 7 } 8 public void print2 () { 9 System. out. println (" Class1. print2 ()" ); 10 } 11 } 12 13 public class Class2 extends Class1 { 1 public Class2 () { 15 System. out. println ( " Class2 ()" ); 16 } 17 public void print1 () { 18 System. out. println (" Class2. print1 ()"); 19 } 20 } 21 22 public class Class3 extends Class1 { 23 private Class2 class2 ; 2 public Class3 () { 25 System. out. println ( " Class3 ()" ); 26 class2 = new Class2 (); 27 } 28 public void print1 () { 29 class2. print1 (); 30 } 31 public void print2 (){ 32 System. out. println (" Class3. print2 ()"); 33 super. print2 (); 3 } 36 37 public class TestClass { 38 public static void main ( String [] args ) { 39 Class1 c1 = new Class2 (); 0 c1. print1 (); 1 c1. print2 (); 2 System. out. println (); 3 Class1 c2 = new Class3 (); c2. print1 (); 5 c2. print2 (); 6 } 7 } Class2() Class2.print1() Class1.print2() Class3() Class2() Class2.print1() Class3.print2() Class1.print2() 3

10. Find at least 3 errors related to inheritance and interfaces in the following code: 1 public interface Vehicle { 2 public int getspeed (); 3 public void accelerate ( int speed_ inc ); public void brake ( int speed_ dec ); 5 } 6 public class Car implements Vehicle { 7 private int speed ; 8 public Car ( int initialspeed ){ 9 this. speed = initialspeed ; 10 } 11 public int getspeed (){ 12 return speed ; 1 1 public int accelerate ( int speed_ inc ) { 15 speed += speed_ inc ; 16 return speed ; 17 } 18 public int brake ( int speed_ dec ) { 19 speed -= speed_ dec ; 20 return speed ; 21 } 22 } 23 2 public class Toyota extends Car { 25 public long getspeed (){ 26 return speed ; 27 } 28 } 29 public class Truck implements Vehicle { 30 public void accelerate ( int speed_ inc ) { 31 super. accelerate ( speed_ inc /2); 32 } 33 public void brake ( int speed_ dec ) { 3 super. brake ( speed_dec /2); 36 } 37 public class demolitonderby { 38 public static void main ( String [] args ) { 39 Vechicle prius, mack, impreza ; 0 prius = new Toyota (); 1 mack = new Truck (); 2 impreza = new Car (); 3 impreza. accelerate (5); 5 prius. brake (2); 6 prius. accelerate ( impreza. getspeed ()); 7 mack. accelerate (5); 8 } 9 } Line # Error 1, 18 Returns int, should return void. 25 getspeed s return type (long) differs from that of the getspeed from Car or Vehicle. 26 speed was declared private in Car, and so is inaccessible from Toyota. 29 Truck implements Vehicle, but does not have getspeed. 31, 3 Truck doesn t extend a class with accelerate or brake methods, so it can t call those methods on super. 2 the constructor for Car requires an int parameter

11. Convert the following code to use generics. 1 interface StringCondition { 2 boolean checkstring ( String s); 5 interface IntegerCondition { 6 boolean checkinteger ( Integer i); 7 } 8 9 class StringContainer { 10 ArrayList < String > values ; 11 12 // Add and other methods are defined correctly here... 13 1 String getfirstwhereholds ( StringCondition condition ) { 15 for ( String s : values ) { 16 if ( condition. checkstring (s)) 17 return s; 18 } 19 return null ; 20 } 21 } 22 23 class IntegerContainer { 2 ArrayList < Integer > values ; 25 26 // Add and other methods are defined correctly here... 27 28 Integer getfirstwhereholds ( IntegerCondition condition ) { 29 for ( Integer i : values ) { 30 if ( condition. checkinteger ( i)) 31 return i; 32 } 33 return null ; 3 } 1 interface Condition <E> { 2 boolean check (E value ); 5 public class Container <E> { 6 ArrayList <E> values ; 7 E getfirstwhereholds ( Condition <E> condition ){ 8 for (E v : values ) { 9 if ( condition. check (v)) 10 return v; 11 } 12 return null ; 1 1 } 5