| 1. | Given:
import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[o]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group()); } } } And the command line:
java Regex2 "\d*" ab34ef What is the result?
| |
| 2. | Given:
import java.io.*; class Player { Player() { System.out.print("p"); } } class CardPlayer extends Player implements Serializable { CardPlayer() { System.out.print("c"); } public static void main(String[] args){ CardPlayer cl = new CardPlayer(); try { FileOutputStream fos = new FileOutputStream("play.txt"); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(c1); os.close() ; FileInputStream fis = new FileInputStream("play.txt"); ObjectInputStream is = new ObjectInputStream(fis); CardPlayer c2 = (CardPlayer) is.readObject(); is.close(); } catch (Exception x ) { } } } What is the result?
| |
| 3. | Given that bw is a reference to a valid BufferedWriter And the snippet:
15. BufferedWriter b1 = new BufferedWriter(new File("f")); 16. BufferedWriter b2 = new BufferedWriter(new FileWriter("f1")); 17. BufferedWriter b3 = new BufferedWriter(new PrintWriter("f2")); 18. BufferedWriter b4 = new BufferedWriter(new BufferedWriter(bw)); What is the result?
| |
| 4. | Given:
Class TKO { public static void main(String[] args) { String s = "-"; Integer x = 343; long L343 = 343L; if(x.equals(L343)) s += ".e1 "; if(x.equals(343)) s += ".e2 "; Short s1 = (short)((new Short((short)343)) / (new Short((short)49))); if (s1 == 7) s += "=s "; if(sl < new Integer(7+1)) s += "fly "; System.out.println(s); } } Which of the following will be included in the output String s? (Choose all that apply.)
| |
| 5. | Given:
1. import java.text.*; 2. class DateOne { 3. public static void main(String[] args) { 4. Date d = new Date(1123631685981L); 5. DateFormat df = new DateFormat(); 6. System.out.println(df.format(d)); 7. } 8. } And given that 1123631685981L is the number of milliseconds between Jan. 1, 1970, and sometime on Aug. 9, 2005, what is the result? (Note: the time of day in option A may vary.)
| |
| 6. | Given:
import java.io.*; class Keyboard { } public class Computer implements Serializable { private Keyboard k = new Keyboard(); public static void main(String[] args) Computer c = new Computer(); c.storeIt(c) ; } void storeIt(Computer c) { try { ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream("myFile")); os.writeObject(c); os.close() ; System.out.println("done"); } catch (Exception x) {System.out.println("exc"); } } } } What is the result? (Choose all that apply.)
| |
| 7. | Using the fewest fragments possible (and filling the fewest slots possible), complete the code below so that the class builds a directory named "dir3" and creates a file named "file3" inside "dir3". Note you can use each fragment either zero or one times. Code:
import java.io.________________ class Maker { public static void main(String [] args) { ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ } } Fragments:
File; FileDescriptor; FileWriter; Directory; try { .createNewDir(); File dir File { } (Exception x) ("dir3"); file file .createNewFile(); = new File = new File dir (dir, "file3"); (dir, file); .createFile() ; } catch ("dir3", "file3"); .mkdir(); File file | |
| 8. | Which are true? (Choose all that apply.)
| |
| 9. | Which will compile and run without exception? (Choose all that apply.)
| |
| 10. | Which about the three java.lang classes String, StringBuilder, and StringBuffer are true? (Choose all that apply.)
| |
| 11. | Given that 1119280000000L is roughly the number of milliseconds from Jan. 1, 1970, to June 20, 2005, and that you want to print that date in German, using the LONG style such that "June" will be displayed as "juni", complete the code using the fragments below. Note: you can use each fragment either zero or one times, and you might not need to fill all of the slots. Code:
import Java.___________ import Java.___________ class DateTwo { public static void main(String[] args) { Date d = new Date(1119280000000L); DateFormat df =__________________________ _____________ , ______________ ) ; System.out.println(_____________ } } Fragments:
io.*; new DateFormat( Locale.LONG nio.*; DateFormat.getlnstance( Locale.GERMANY util.*; DateFormat.getDatelnstance( DateFormat.LONG text.*; util.regex; DateFormat.GERMANY date.*; df.format(d)); d.format(df)); | |
| 12. | Given:
import java.io.*; class Directories { static String [] dirs = {"dirl", "dir2"}; public static void main(String [] args){ for (String d : dirs) { // insert code 1 here File file = new File(path, args[0]) ; // insert code 2 here } } } and that the invocation
java Directories file2.txt is issued from a directory that has two subdirectories "dir1" and "dir1", and that "dir1" has a file "file1.txt" and "dir2" has a file "file2.txt", and the output is "false true"; which set(s) of code fragments must be inserted? (Choose all that apply.)
| |
| 13. | Given:
class Polish { public static void main(String[] args) { int x = 4 ; StringBuffer sb = new StringBuffer("..fedcba"); sb.delete(3,6); sb.insert(3, "az"); if(sb.length() > 6) x = sb.indexOf("b"); sb.delete((x-3), (x-2)); System.out.println(sb); } } What is the result?
| |
| 14. | Given:
1. import java.util.*; 2. class Brain { 3. public static void main(String[] args) { 4. // insert code block here 5. } 6. } Which, inserted independently at line 4, compile and produce the output "123 82"? (Choose all that apply.)
| |
| 15. | Given:
import java.io.*; public class TestSer { public static void main(String[] args) { SpecialSerial s = new SpecialSerial() ; try { ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream("myFile")); os.writeObject(s); os. close(); System.out.print( + + s.z + " "); ObjectlnputStream is = new ObjectlnputStream( new FileInputStream("myFile")); SpecialSerial s2 = (SpecialSerial)is.readObject(); is.close(); System.out.println(s2.y + " " + s2.z); } catch (Exception x) {System.out.println("exc"); } } } class SpecialSerial implements Serializable { transient int y = 7; static int z = 9; } Which are true? (Choose all that apply.)
| |
Answers
| 1. |
| ||||
| 2. |
| ||||
| 3. |
| ||||
| 4. |
| ||||
| 5. |
| ||||
| 6. |
| ||||
| 7. |
import java.io.File; class Maker public static void main(String[] args) { try { File dir = new File("dir3"); dir,mkdir(); File file = new File(dir, "file3"); file.createNewFile(); } catch (Exception x) {} } } Notes: The new File statements don't make actual files or directories, just objects. You need the mkdir() and createNewFile() methods to actually create the directory and the file. (Objective 3.2) | ||||
| 8. |
| ||||
| 9. |
| ||||
| 10. |
| ||||
| 11. |
import java.util.*; import java.text.*; class DateTwo { public static void main(String[] args) { Date d = new Date(1119280000000L); DateFormat df = DateFormat.getDateInstance( DateFormat.LONG, Locale.GERMANY); System.out,println(df.format(d)); } } Notes: Remember that you must build DateFormat objects using static methods. Also remember that you must specify a Locale for a DateFormat object at the time of instantiation. The getInstance() method does not take a Locale. (Objective 3.4) | ||||
| 12. |
| ||||
| 13. |
| ||||
| 14. |
| ||||
| 15. |
|