SCJP 6.0 Lecturer Kuo-Yi Chen 151, 153, 154, 155, 156, 157

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
The ArrayList Class and the enum Keyword
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Chapter 3A Review boolean fun = true; if(fun) System.out.print(“yeah!”);
Simple Programs Simple Strings Writing some programs Only in Parameters? I/O Gadget.
Computer Programming Lab(7).
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
CSCI 160 Midterm Review Rasanjalee DM.
Java Review Interface, Casting, Generics, Iterator.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Java Programming Strings Chapter 7.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Computer Programming Lab 8.
SCJP 6.0 Lecturer Kuo-Yi Chen 102, 104, 114, 115, 118, 123, , 130, 131, 132, 134, 137, , 140, 143, , 147, 149, 150.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
JAVA API Strings, I/O, Formatting and Parsing
Introduction to programming in java
Lecture 14 Throwing Custom Exceptions
I/O Basics.
Object Oriented Programming (OOP) LAB # 8
An Introduction to Java – Part I, language basics
Comparing Python and Java
Methods/Functions.
Presentation transcript:

SCJP 6.0 Lecturer Kuo-Yi Chen 151, 153, 154, 155, 156, 157 160, 166, 173, 175, 176, 179 180, 181, 182, 183, 184, 192, 195

151 A. Compilation fails. B B. Nothing is added to the file system. C. Only a new file is created on the file system. D. Only a new directory is created on the file system. E. Both a new file and a new directory are created on the file system. B Given that the current directory is empty, and that the user has read and write privileges to the current directory, and the following: 1. import java.io.*; 2. public class Maker{ 3. public static void main(String args){ 4. File dir = new File("dir"); 5. File f = new File(dir, "f"); 6. } 7. } Which statement is true? 4/5行只是宣告目錄、檔案 真正建立檔案的方法是createNewFile(),例子如145

153 Chain these constructors to create objects to read from a file named "in" and to write to a file named "out".

154 AE Given that c is a reference to a valid java.io.Console object, which two code fragments read a line of text from the console? (Choose two.) A. String s = c.readLine(); B. char[] c = c.readLine(); C. String s = c.readConsole(); D. char[] c = c.readConsole(); E. String s = c.readLine("%s", "name "); F. char[] c = c.readLine("%s", "name ");

155 The doesFileExist method takes an array of directory names representing a path from the root filesystem and a file name. The method returns true if the file exists, false if it does not. Place the code fragments in position to complete this method.

156

157 E Given that c is a reference to a valid java.io.Console object, and: 11. String pw = c.readPassword("%s", "pw: "); 12. System.out.println("got " + pw); 13. String name = c.readLine("%s", "name: "); 14. System.out.println(" got", name); If the user types fido when prompted for a password, and then responds bob when prompted for a name, what is the result? A. pw: got fido name: bob got bob B. pw: fido C. pw: name: bob got bob D. pw: fido got lido E. Compilation fails. F. An exception is thrown at runtime. 此程式片段有二個錯誤 String pw = c.readPassword(“%s”, “pw: ”); 改: char[] pw = c.readPassword("%s", "pw: "); System.out.println(“ got”, name); 改 System.out.println(" got“ + name);

160 E Given: 22. StringBuilder sb1 = new StringBuilder("123"); 23. String s1 = "123"; 24. //insert code here 25. System.out.println(sb1 + " " + s1); Which code fragment, inserted at line 24, outputs "123abc 123abc"? A. sb1.append("abc"); s1.append("abc"); B. sb1.append("abc"); s1.concat("abc"); C. sb1.concat("abc"); s1.append("abc"); D. sb1.concat("abc"); s1.concat("abc"); E. sb1.append("abc"); s1 = s1.concat("abc"); F. sb1.concat("abc"); s1 = s1.concat("abc"); G. sb1.append("abc"); s1 = s1 + s1.concat("abc"); H. sb1.concat("abc"); s1 = s1 + s1.concat("abc"); StringBuilder說明,請參考1.5答案解析28頁 基本上StringBuilder與StringBuffer一樣,字串內容可以改變,不像String是一個內容不可變的字串物件,String型態的任何操作都會產生新的字串物件,必須將參考指向新的字串物件,才能存取改變後的字串。

166 Given: 1. public class BuildStuff{ 2. public static void main(String[] args){ 3. Boolean test = new Boolean(true); 4. Integer x = 343; 5. Integer y = new BuildStuff().go(test, x); 6. System.out.println(y); 7. } 8. int go(Boolean b, int i){ 9. if(b) return (i/7); 10. return (i/49); 11. } 12. } What is the result? A. 7 B. 49 C. 343 D. Compilation fails. E. An exception is thrown at runtime.

173 A A. int Long B. Short Long C. Compilation fails. Given: 11. public class Yikes{ 12. 13. public static void go(Long n){System.out.print("Long ");} 14. public static void go(Short n){System. outprint("Short ");} 15. public static void go(int n){System.out.print("int ");} 16. public static void main(String[] args){ 17. short y = 6; 18. long z = 7; 19. go(y); 20. go(z); 21. } 22. } What is the result? A. int Long B. Short Long C. Compilation fails. D. An exception is thrown at runtime.

175 AB two scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object? (Choose two.) A. When using versions of Java technology earlier than 5.0. B. When sharing a StringBuffer among multiple threads. C. When using the java.io class StringBufferInputStream. D. When you plan to reuse the StringBuffer to build more than one string. E. Enitiation of separate design processes to the separation of users 此題問: 那二個情境在置換StringBuffer物件是不安全的? A 使用JDK5.0之前的版本 B 在多個執行緒之前分享StringBuffer物件

176

179 Wheels為複數,表示是用集合型態物件來表示擁有多個其他物件關係 宣告實作隱含繼承,若一個類別同時是二個以上類別的子類別,則會以implements來宣告多個介面,或extends B implements C…

180

181

182 the class definitions: class Animal{} class Dog extends Animal{} and the code: public void go(){ ArrayList<Dog> aList = new ArrayList<Dog>(); takeList(aList); } //insert definition of the takeList() method here Place the correct Compilation Result on each takeList() method definition to indicate whether or not the go() method would compile given that definition. 泛型代入參數使用方法 ArrayList < ? extends Animal> 表示可代入任何是Animal的子類別型態 ArrayList <?> 表任一型態皆可

183 CompareTo 傳回值>0 表示本身物件的值 大於 傳入的參數值 因此 用i-i2.i來直接表示這個關係

184

192 BCD Given: 3. import java.util.*; 4. public class G1{ 5. public void takeList(List<? extends String> list){ 6. //insert code here 7. } 8. } Which three code fragments, inserted independently at line 6, will compile? (Choose three.) A. list.add("foo"); B. Object o = list; C. String s = list.get(0); D. list = new ArrayList<String>(); E. list = new ArrayList<Object>(); list是指向一個放置String與其子類別型態的集合物件 B是ok的,因此任何物件皆可用Object型態的參考存取 list.get(0)傳回集合物件中第0個位置的物件,其型態是String或其子類別 list = new ArrayList<String>(); list指向內存String物件的集合物件,符合list的型態宣告。

195 A. 2 1 0 B B. 2 1 1 C. 3 2 1 D. 3 2 2 E. Compilation fails. F. An exception is thrown at runtime. B Given: 3. import java.util.*; 4. public class Mapit{ 5. public static void main(String[] args){ 6. Set<Integer> set = new HashSet<Integer>(); 7. Integer i1 = 45; 8. Integer i2 = 46; 9. set.add(i1); 10. set.add(i1); 11. set.add(i2); System.out.print(set.size() + " "); 12. set.remove(i1); System.out.print(set.size() + " "); 13. i2 = 47; 14. set.remove(i2); System.out.print(set.size() + " "); 15. } 16. } What is the result? HashSet湊雜集合,用物件的特徵值計算出雜湊值,此題用Integer物件計算雜湊值,只要Integer的數字一樣計算出的雜湊值就一樣,一樣的雜湊值物件不論加(add)幾次都不會重複加入雜湊集合中。移除物件也是依據雜湊值來找出對應的物件。