Main Topics. Study Tactics. Exam Structure, Roughly. Java Fundamentals. CISC 3120 Midterm Review Guide

Similar documents
Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Introduction to Programming Using Java (98-388)

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Selected Questions from by Nageshwara Rao

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

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

Practice for Chapter 11

Design Patterns: State, Bridge, Visitor

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Rules and syntax for inheritance. The boring stuff

Pace University. Fundamental Concepts of CS121 1

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

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Object-Oriented Programming in Java

Fall 2017 CISC124 9/16/2017

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Text User Interfaces. Keyboard IO plus

CH. 2 OBJECT-ORIENTED PROGRAMMING

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Programming with Java

Object-Oriented Programming More Inheritance

APCS Semester #1 Final Exam Practice Problems

COE318 Lecture Notes Week 8 (Oct 24, 2011)

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

1 Shyam sir JAVA Notes

CO Java SE 8: Fundamentals

Object-oriented programming in...

St. Edmund Preparatory High School Brooklyn, NY

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

COP 3330 Final Exam Review

CS 231 Data Structures and Algorithms, Fall 2016

Bjarne Stroustrup. creator of C++

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Java Object Oriented Design. CSC207 Fall 2014

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Methods CSC 121 Fall 2016 Howard Rosenthal

Software Development (cs2500)

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Methods CSC 121 Spring 2017 Howard Rosenthal

XC Total Max Score Grader

Chapter 5 Object-Oriented Programming

1 Short Answer (10 Points Each)

About this exam review

Introduction to Java. Handout-1d. cs402 - Spring

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

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

Over and Over Again GEEN163

Java Bytecode (binary file)

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

type conversion polymorphism (intro only) Class class

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

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

class objects instances Fields Constructors Methods static

Full file at

What is Inheritance?

Values and Variables 1 / 30

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Declarations and Access Control SCJP tips

CSE 143 Lecture 12 Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Casting. References. References

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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

C10: Garbage Collection and Constructors

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Inheritance and Interfaces

CMSC 331 Second Midterm Exam

Lab5. Wooseok Kim

Chapter 4 Defining Classes I

AP CS Unit 6: Inheritance Notes

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Full file at

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Derived and abstract data types. TDT4205 Lecture 15

First IS-A Relationship: Inheritance

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

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

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

INSTRUCTIONS TO CANDIDATES

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Introduction to Inheritance

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Repetition CSC 121 Fall 2014 Howard Rosenthal

Lecture Set 2: Starting Java

Java for Non Majors Spring 2018

Introduction to Java Unit 1. Using BlueJ to Write Programs

Questions Answer Key Questions Answer Key Questions Answer Key

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

Supplementary Test 1

Section 2.2 Your First Program in Java: Printing a Line of Text

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Transcription:

CISC 3120 Midterm Review Guide Main Topics Java Fundamentals I expect you to have a basic mastery of control structures (loops, conditionals), statements, arrays, function declaration, definition, and invocation, and language syntax. I won t be testing you explicitly on this, but of course you won t be able to write much code without this! Exam questions will focus on: Basic syntax, terminology (class, object, instance variable, subclass, etc) The design of Java and the JVM Primitive vs reference types. The Java API, the package system, and import. Public, private, and package access. Basics of inheritance in Java: the IS-A relationship, subtype polymorphism Overloading and overriding Abstract classes and interfaces Software Design Principles and Style Version control and Git/GitHub Study Tactics Review all book exercises to make sure you understand why correct answers are correct. Review all code from lecture/lab. Review CodeLab exercises. Review Application Activities (below) Re-read readings now that you ve worked with ideas in lecture/lab. You should be able to write a basic class/application in Java without consulting notes. This includes using main() and System.in/System.out. If I ask specific questions about (say) the Scanner class, I will provide you enough of the Java API for you to answer the question. Exam Structure, Roughly 50%: short-answer/multiple-choice 30%: writing code 20%: analyzing code

Why is the Java Virtual Machine that runs Java programs called virtual? a) It is run over a network, rather than on the same physical machine on which it appears the Java program is running. b) It is a software machine, rather than a real hardware machine that can run (say) a compiled C++ program. c) It is a conceptual machine that is useful during the compilation process, but real hardware actually runs the program. d) It operates on bytecodes, rather than the low-level bits that are manipulated by hardware. Which of these are advantages of the virtual machine approach? a) Once a program is written, it can be run on any platform with a JVM. b) More security checks can be made before a program runs. c) More people can write Java programs. d) Both a and b. e) Both a and c. Which of the following types is not a primitive type? a) boolean b) int c) String d) double e) char If a variable is not primitive, it is a a) Pointer b) Object c) Reference d) Constant e) Class A reference to an object is best thought of as a) The address of the object in memory. b) Another name for the object. c) A way to get to the object in order to tell it to do something. d) A way to restrict what the object can do. How are parameters passed in Java? a) By value b) By reference c) By pointers d) Both a and b e) All of a, b, and c If an instance variable is declared private, which methods will be allowed to access it? a) All methods belonging to the same object/instance b) All methods that have the same return type as the instance variable c) All methods belonging to objects of the same class d) All methods belonging to objects in the same application

If this code will compile and run correctly, what is its output? If it will not compile/run, explain why. (Note: this is similar, but not identical, to the code on p. 63.) class Book { String title; String author; class BookTestDrive { public static void main(string [] args) { Books [] mybooks = new Book[3]; mybooks[0] = new Book; mybooks[1] = new Book; mybooks[2] = new Book; mybooks[0].title = The Grapes of Java ; mybooks[1].title = The Java Gatsby ; mybooks[2].title = The Java Cookbook ; mybooks[0].author = bob ; mybooks[1].author = sue ; mybooks[2].author = ian ; Book bookref = mybooks[0]; i = 0; while (i < 3) { System.out.print(bookref.title + by + bookref.author); i = i + 1; What happens if you don t initialize a variable? It has an undefined value, which could cause problems when the program runs. If this program needs an initial value when it runs, it will throw an exception and halt. The compiler will issue an error, and you ll have to add the initialization before the code will compile. The value is automatically set to some form of zero. 12. Using the following declarations: int i, j; double d; char c; String s,t; and assuming that we have some code that causes these variables to get non-zero values, which of these comparisons is illegal (will cause a compiler error)? a) c == i b) c == d c) s == t d) s.equals(t) e) None of the above are illegal

13. Write a Java program that reads exactly 10 integers from the keyboard, stores them in an array, calculates the average, then outputs the list of numbers and their average. Write a function called average() that returns the average of an array of integers. 14. If the output of this Java program (excluding prompts) is o The mppn is made pf green cheese. what was the input? import java.util.scanner; public class FindInput { public static void main(string[] args) { int x = 0; String s = ""; Scanner cin = new Scanner(System.in); System.out.print("Input, please: "); if (cin.hasnextint()) { x = cin.nextint(); if (cin.hasnextline()) { s = cin.nextline(); if (x % 2 == 0) { Widget w = new Widget(x, s); System.out.println(w.widge()); else { Gidget g = new Gidget(x, s); System.out.println(g.gidge()); public class Widget { private int i; private String s; public Widget (int i, String s) { this.i = 2*i; this.s = s; public String widge() { char c = s.charat(i); return (c + " " + s.replace(c,++c)); public class Gidget { private int i; private String s; public Gidget(int i, String s) { this.i= 3*i; this.s = s;

public String gidge() { Widget w = new Widget(i,s); char c = s.charat(i); s.replace(c,++c); return (s.charat(i) + w.widge()); What s weird about the method invocation Integer.parseInt( 3 )? a) 3 is a String, not an int, you fool. b) You invoke methods after the name of an instance, not the name of a class, you dunderhead. c) Parse means to examine closely, you flea-ridden son of a compiler. d) In Java, integers are primitive values, not class instances, you arrant knave. e) There s nothing weird about this at all, you unsavory rapscallion. What s wrong with this fragment of code from the book (p. 152)? private static final String alphabet = abcdefg ; private int gridlength = 7; private int gridsize = 49; a) Shouldn t alphabet not be static? b) Isn t alphabet missing, like, 19 letters? c) Shouldn t gridlength and gridsize also be constants? d) Shouldn t gridsize be initialized to gridlength*gridlength? e) Both c and d. What s wrong with this fragment of code from the book (p. 152)? while (!success & attempts++ < 200 ) { location = (int) (Math.random() * gridsize); //System.out.print( try + location); int x = 0; success = true; a) Logical and in Java is a double &&, not a single &. b) What the heck is that 200 for? c) The whole loop condition needs more parentheses to avoid order-of-operations problems. d) The (int) cast is unnecessary. e) In the argument to print(), you can t plus a String and an int. The Java API has many packages, including java.util, java.util.concurrent, and java.util.concurrent.atomic. What is the relationship among these three packages? a) The names suggest how the behaviors of the packages are related, but to the compiler/jvm they are just three different packages. b) They are nested in the sense that when you write import java.util.*, you get the stuff in all three packages. c) They are nested in the sense that you cannot write import java.util.concurrent.atomic.* unless you ve already written import java.util.* or import java.util.concurrent.*. d) They are nested in the sense that higher packages (like java.util) automatically have access to all the public- and package-access code in the lower packages (like java.util.concurrent).

Are these two loops equivalent (they leave sum and niftyarray in the same state)? for (int i=0; i < niftyarray.length; i++) { for (int i : niftyarray) { sum += niftyarray[i]; sum += i; a) Yes. b) No. Are these two loops equivalent (they leave sum and niftyarray in the same state)? for (int i=0; i < niftyarray.length; i++) { for (int i : niftyarray) { sum += niftyarray[i]; sum += i; niftyarray[i] += 10; i += 10; a) Yes. b) No. The method heidipurge() has the following signature: void heidipurge(arraylist<int> yerin, ArrayList<int> yerout, ArrayList<int> target); When the function returns, target should not contain any elements of yerout, but should contain all elements of yerin (except, of course, any that are also listed in yerout). Implement this function, taking full advantage of the ArrayList API. Consider the following code (from p. 169): public class Doctor { boolean worksathospital; void treatpatient() { // perform a checkup public class FamilyDoctor extends Doctor { boolean makeshousecalls; void giveadvice() { // give homespun advice public class Surgeon extends Doctor { void treatpatient() { // perform surgery void makeincision() { // make incision (yikes!) Which of these is true? a) A FamilyDoctor can treatpatient(). b) A FamilyDoctor can makeincision(). c) A Surgeon treatpatient() differently than a FamilyDoctor does. d) Both a and c.

Both a and b. Override means a) A subclass automatically gets the base class implementation of a method, even if it s not stated explicitly. b) A method in a subclass can substitute the base class implementation with its own. c) The base class implementation of a method is used even if a subclass has its own implementation. d) A class can provide multiple implementations of a method. e) If the warp core is about to fail, switch to auxiliary power. In general, overridden methods should provide a) Behaviors unique to the subclass. b) Behaviors common to all subclasses. c) Behaviors that may change in future implementations. d) Behaviors that add to the base class behavior. e) Behaviors that the JVM cannot support. Consider the following code: class Hamburger { private int calories = 800; int getcalories() { return calories; public class Cheeseburger extends Hamburger { int slices; Cheeseburger(int slices) { this.slices = slices; @Override int getcalories() { return super.getcalories() + 100 * slices; What is the result of trying to compile and run the following code? Hamburger[] breakfast = new Hamburger[4]; breakfast[0] = new Cheeseburger(1); //Hamburger(); breakfast[1] = new Cheeseburger(2); //Hamburger(); breakfast[2] = new Cheeseburger(1); breakfast[3] = new Cheeseburger(2); int calories=0; for (Hamburger h : breakfast) { calories += h.getcalories(); System.out.println( Your total calories for the meal: + calories); a) A compilation error. b) A runtime exception complaining about a type mismatch. c) Your total calories for the meal: 3200 d) Your total calories for the meal: 3500

Given the method declaration public int moshify(double goodstuff[], String name) which of these is a legal overload? a) public double moshify(double goodstuff[], String name) b) private int moshify(double goodstuff[], String name, ArrayList<int> badstuff) c) public int moshify (int goodstuff[], String name) d) private int moshify(double badstuff[], String city) e) Both b and c. An abstract class cannot be a) Subclassed b) Changed c) Instantiated d) Overridden e) Compiled Which of these is true? a) Abstract classes can only contain abstract methods. b) Abstract methods do not declare a return type. c) A subclass of an abstract class must implement all abstract methods. d) Implementing an abstract class is very similar to overriding a method. e) Both a and b. What s wrong with following code fragment? ArrayList<Object> mydogarraylist = new ArrayList<Object>(); Dog adog = new Dog(); mydogarraylist.add(adog); Dog d = mydogarraylist.get(0); a) There is no such class as Object. b) get() returns an Object that can t be assigned to a Dog reference. c) It s illegal to add a Dog object to an ArrayList of Objects. d) Dog Adog New Dog is a terrible name for a band. e) There s nothing wrong.

Consider this code fragment (assuming the Dog and Cat classes have been defined): ArrayList<Object> mydogarraylist = new ArrayList<Object>(); Dog adog = new Dog(); Cat acat = new Cat(); mydogarraylist.add(adog); mydogarraylist.add(acat); Cat c = (Cat) mydogarraylist.get(0); What s the worst that could happen? a) The compiler will complain because you can t add Dog objects to an Object collection. b) The compiler will complain because you can t add objects of two different classes to a collection. c) The JVM will complain that a Dog object can t be cast to a Cat object d) The JVM will complain that an Object object can t be cast to an Cat object. e) Someone reading the code won t understand why there s a Cat in mydogarraylist. So why should the book s Animal class be declared abstract? a) Because there are no real animals in the world. b) Because there are too many real animals in the world. c) Because Animal has no subclasses. d) Because there s no way to give specific behavior for a general animal. e) Both c and d.