Software 1 with Java. Initialization. Initialization. Initialization. Pass by Value. Initialization. Recitation No. 11 (Summary)

Similar documents
הנכות 1 םוכיס לוגרת 14 1

CSCI 201L Written Exam #1 Fall % of course grade

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

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)

Practice for Chapter 11

Class, Variable, Constructor, Object, Method Questions

Practice Questions for Chapter 9

CSCI 201L Written Exam #1 - SOLUTION Fall % of course grade

Questions Answer Key Questions Answer Key Questions Answer Key

CSCI 201L Written Exam #1 Fall % of course grade

הנכות 1 םוכיס לוגרת 13 1

JAVA Programming Language Homework I - OO concept

Questions Answer Key Questions Answer Key Questions Answer Key

CSCI 201L Written Exam #1 Fall % of course grade

Overriding Variables: Shadowing

Full file at Chapter 2 - Inheritance and Exception Handling

CMSC 132: Object-Oriented Programming II

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

University of Palestine. Mid Exam Total Grade: 100

Inheritance and Polymorphism

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Exercise: Singleton 1

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Midterm Exam CS 251, Intermediate Programming March 12, 2014

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

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

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

GlobalLogic Technical Question Paper

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

ECE 461 Fall 2011, Final Exam

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

Implements vs. Extends When Defining a Class

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

CS260 Intro to Java & Android 03.Java Language Basics

EKT472: Object Oriented Programming. Overloading and Overriding Method

Questions Answer Key Questions Answer Key Questions Answer Key

Lecture 13 Summary. Assignment 3 Discussion. Polymorphism 3/4/2009. Assignment 3 Polymorphism Interfaces

Introduction to Java

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Instance Members and Static Members

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

Introduction to Programming Using Java (98-388)

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

Chapter 5 Object-Oriented Programming

CMSC 132: Object-Oriented Programming II

Java Exceptions Java, winter semester

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

COE318 Lecture Notes Week 10 (Nov 7, 2011)

15CS45 : OBJECT ORIENTED CONCEPTS

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Inheritance (Part 5) Odds and ends

Object-Oriented Programming More Inheritance

Sun Certified Programmer for the Java 2 Platform. SE6.0

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

ECE Fall 2014, Final Exam

Last name:... First name:... Department (if not D-INFK):...

Object-Oriented Programming (Java)

ECE Fall 20l2, Second Exam

ECE 462 Fall 2011, Third Exam

Exam Name: Java Standard Edition 6 Programmer Certified Professional Exam

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1

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

CLASS DESIGN. Objectives MODULE 4

Making New instances of Classes

What is Inheritance?

Create a Java project named week10

Fundamentals of Object Oriented Programming

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

Midterm Exam CS 251, Intermediate Programming October 8, 2014

More on Objects in JAVA TM

Object-Oriented ProgrammingInheritance & Polymorphism

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

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

JAVA Programming Language Homework I - Nested Class

Distributed Systems Recitation 1. Tamim Jabban

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Object- Oriented Analysis, Design and Programming. Instructions. Medialogy, 4 th Semester, Aalborg. Tuesday 30 August 2011,

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

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

PASS4TEST IT 인증시험덤프전문사이트

CH. 2 OBJECT-ORIENTED PROGRAMMING

Notes on Java Programming IST JAV 2012 S. Frénot

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Character Stream : It provides a convenient means for handling input and output of characters.

1. Class Relationships

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

Introductory Programming Inheritance, sections

Java. Massimo Dong. November 27, Massimo Dong Java November 27, / 26

Example: Count of Points

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Polymorphism. Arizona State University 1

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

CS 113 PRACTICE FINAL

SUN Certified Programmer for J2SE 5.0 Upgrade. Download Full Version :

Why Types Matter. Zheng Gao CREST, UCL

1 Shyam sir JAVA Notes

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Transcription:

Software 1 with Java Recitation No. 11 (Summary) public class Foo { static int bar; public static void main (String args []) { bar += 1; System.out.println("bar = " + bar); The Does output the code is: compile? If no, why? bar Does = 1the code throw a runtime exception? June 28, 2007 1 June 28, 2007 2 private int a = getb(); private int b = 5; private int b = 5; private int a = getb(); private int getb() { return b; System.out.println((new Test()).a); Does 0 the code throw a runtime exception? June 28, 2007 3 private int getb() { return b; System.out.println((new Test()).a); Does 5 the code throw a runtime exception? June 28, 2007 4 int i; System.out.println(i); The Does code the code does compile? not If no, since why? the local variable Does the i is code not throw initialized. a runtime exception? June 28, 2007 5 public class PassTest1{ public static void changeint(int value) { value = 55; int val = 11; changeint(val); System.out.println(val); Does 11 the code throw a runtime exception? June 28, 2007 6 1

public class PassTest2 { public static void changeobjectref(mypoint ref) { ref = new MyPoint(1, 1); MyPoint point; point = new MyPoint(22, 7); changeobjectref(point); System.out.println(point); public class MyPoint { private int x, y; public MyPoint(int x, int y) { this.y = y; public String tostring() { return x + "," + y; Does 22,7 the code throw a runtime exception? June 28, 2007 7 public class PassTest3 { public static void changeobjectattr(mypoint ref) { ref.setx(4); MyPoint point; point = new MyPoint(22, 7); changeobjectattr(point); System.out.println(point); public class MyPoint { private int x, y; public MyPoint(int x, int y) { this.y = y; public String tostring() { return x + "," + y; public void setx(int x){ June 28, 2007 8 Does 4,7 the code throw a runtime exception? A Word about Interfaces private static class Value { int v = 1; Test test = new Test(); int v = 2; Value value = new Value(); value.v = 3; foo(value, v); System.out.println(value.v + " " + v); 1 4 5 2 private static void foo(value value, int v) { v= 4; value.v = 5; value = new Value(); System.out.println(value.v + " " + v); June 28, 2007 9 An interface can extend several interfaces Interface methods are by definition public and abstract: public interface MyInterface { public abstract int foo1(int i); int foo2(int i); The type of foo1 and foo2 is the same. June 28, 2007 10 Interfaces public interface Foo { public void bar() throws Exception; public class FooImpl implements Foo { System.out.println( No exception is thrown"); Foo foo = new FooImpl(); foo.bar(); Does Compilation the code Error: compile? If no, why? Does "Unhandled the code exception throw type a runtime Exception" exception? June 28, 2007 11 Interfaces public interface Foo { public void bar() throws Exception; public class FooImpl implements Foo { System.out.println( No exception is thrown"); FooImpl foo = new FooImpl(); foo.bar(); Does Output: the code compile? If no, why? Does No exception the code is thrown a runtime exception? June 28, 2007 12 2

Interfaces and Consider the following class hierarchy: Interface Animal { class Dog implements Animal{ class Poodle extends Dog { class Labrador extends Dog { Which of the following lines (if any) will not compile? Poodle poodle = new Poodle(); Animal animal = (Animal) poodle; Dog dog = new Labrador(); animal = dog; poodle = dog; poodle = (Poodle) dog; -No compilation error -Runtime Exception Labrador labrador = (Labrador) animal; -No compilation error -No Runtime Exception June 28, 2007 13 Interfaces and class A { class B extends A implements C { interface C { void print(); public by default No Is there compilation any error? errors June 28, 2007 14 Interfaces and Method Overloading & Overriding class A { void print() { class B extends A implements C { interface C { void print(); Is Compilation there any error? error: The inherited package method A.print() cannot hide the public abstract method in C June 28, 2007 15 public float foo(float a, float b) throws IOException{ Which of the following methods can be defined in B: 1. float foo(float a, float b){ 2. public int foo(int a, int b) throws Exception{ 3. public float foo(float a, float b) throws Exception{ 4. public float foo(float p, float q) { Answer: 2 and 4 June 28, 2007 16 Method Overriding Method Overriding & Visibility public void print(){ public static void main(string args[]){ A a = b; a.print(); June 28, 2007 17 Casting is unneeded Does B the code throw a runtime exception? If B yes, why? If no, what is the output? protected void print() { Does Compilation the code error: compile? If no, why? Does "Cannot the reduce code the throw visibility a runtime exception? If of yes, the inherited why? If method" no, what is the output? June 28, 2007 18 3

Method Overriding & Visibility protected void print() { B June 28, 2007 19 System.out.println("A.foo()"); System.out.println("A.bar()"); foo(); System.out.println("B.foo()"); a.bar(); A.bar() Does the code throw a runtime exception? If B.foo() yes, why? If no, what is the output? June 28, 2007 20 private void foo() { System.out.println("A.foo()"); System.out.println("B.foo()"); How can you invoke the foo method of A within B? Answer: Use super.foo() System.out.println("A.bar()"); foo(); a.bar(); A.bar() Does the code throw a runtime exception? A.foo() June 28, 2007 21 June 28, 2007 22 & Constructors public class C extends B { How can you invoke the foo method of A within C? Answer: Not possible (super.super.foo() is illegal) String bar = "A.bar"; A() { foo(); System.out.println("A.foo(): bar = " + bar); String bar = "B.bar"; B() { foo(); System.out.println("B.foo(): bar = " + bar); System.out.println( a.bar = + a.bar); a.foo(); What is the output? The output is: B.foo(): bar = null B.foo(): bar = B.bar a.bar = A.bar B.foo(): bar = B.bar June 28, 2007 23 June 28, 2007 24 4

& Constructors & Constructors protected public A() { System.out.println(""); public A(String s) { System.out.println("in A: s = " + s); public B() { System.out.println(""); public C() { System.out.println("in C: no args."); public C(String s) { System.out.println("in C: s = " + s); in C: no args. in C: no args. C c = new C(); A a = new C(); June 28, 2007 25 protected public A() { System.out.println(""); public A(String s) { System.out.println("in A: s = " + s); public B() { System.out.println(""); public C() { System.out.println("in C: no args."); public C(String s) { System.out.println("in C: s = " + s); in C: s = c in C: s = a C c = new C("c"); A a = new C("a"); June 28, 2007 26 & Constructors protected public A() { System.out.println(""); public A(String s) { System.out.println("in A: s = " + s); public B() { System.out.println(""); public C() { System.out.println("in C: no args."); public C(String s) { System.out.println("in C: s = " + s); Compilation What will happen error if we without remove this line? C c = new C("c"); A a = new C("a"); June 28, 2007 27 June 28, 2007 28 5