Given:1. package sun.scjp;2. public enum Color { RED, GREEN, BLUE }1. package sun.beta;2. // insert code here3. public class Beta {4. Color g = GREEN;5. public static void main( String[] argv)6. { System.out.println( GREEN); }7. }
The class Beta and the enum Color are in different packages.Which two code fragments, inserted individually at line 2 of the Betadeclaration, will allow this code to compile? (Choose two.)
A. System.gc();
Given:
11. public static void test(String str) {12. int check = 4;13. if (check = str.length()) {14. System.out.print(str.charAt(check -= 1) +", ");15. } else {16. System.out.print(str.charAt(0) + ", ");17. }18. }and the invocation:21. test("four");22. test("tee");23. test("to");What is the result?
Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)A programmer wants to use this method like this:// INSERT DECLARATIONS HEREoutput = process(input);Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile? (Choose all that apply).
class CardBoard {Short story = 5;CardBoard go(CardBoard cb) {cb = null;return cb;}public static void main(String[] args) {CardBoard c1 = new CardBoard();CardBoard c2 = new CardBoard();CardBoard c3 = c1.go(c2);c1 = null;// do Stuff} }When // doStuff is reached, how many objects are eligible for GC?
A. 0
12. import java.io.*;13. public class Forest implements Serializable {14. private Tree tree = new Tree();15. public static void main(String [] args) {16. Forest f = new Forest();17. try {18. FileOutputStream fs = new FileOutputStream("Forest.ser");19. ObjectOutputStream os = new ObjectOutputStream(fs);20. os.writeObject(f); os.close();21. } catch (Exception ex) { ex.printStackTrace(); }22. } }23.24. class Tree { }
What is the result?
10. public class Foo implements java.io.Serializable {11. private int x;12. public int getX() { return x; }12.publicFoo(int x){this.x=x; }13. private void writeObject( ObjectOutputStream s)14. throws IOException {15. // insert code here16. }17. }Which code fragment, inserted at line 15, will allow Foo objects to becorrectly serialized and deserialized?
Given:class Mixer {Mixer() { }Mixer(Mixer m) { m1 = m; }Mixer m1;public static void main(String[] args) {Mixer m2 = new Mixer();Mixer m3 = new Mixer(m2); m3.go();Mixer m4 = m3.m1; m4.go();Mixer m5 = m2.m1; m5.go();}void go() { System.out.print("hi "); }}
Given classes defined in two different files:1. package util;2. public class BitUtils {3. public static void process(byte[]) { /* more code here */ }4. }1. package app;2. public class SomeApp {3. public static void main(String[] args) {4. byte[] bytes = new byte[256];5. // insert code here6. }7. }
What is required at line 5 in class SomeApp to use the process method of BitUtils?
Assuming that the serializeBanana2() and the deserializeBanana2() methodswill correctly use Java serialization and given:
13. import java.io.*;14. class Food {Food() { System.out.print("1"); } }15. class Fruit extends Food implements Serializable {16. Fruit() { System.out.print("2"); } }17. public class Banana2 extends Fruit { int size = 42;18. public static void main(String [] args) {19. Banana2 b = new Banana2();20. b.serializeBanana2(b); // assume correct serialization21. b = b.deserializeBanana2(b); // assume correct22. System.out.println(" restored " + b.size + " "); }23. // more Banana2 methods24. }What is the result?
Given:10. interface Foo { int bar(); }11. public class Sprite {12. public int fubar( Foo foo ) { return foo.bar(); }13. public void testFoo() {14. fubar(15. // insert code here16. );17. }18. }
Which code, inserted at line 15, allows the class Sprite to compile?
Given:10. package com.sun.scjp;11. public class Geodetics {12. public static final double DIAMETER = 12756.32; // kilometers13. }
Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)
Given:1. import java.util.*;2.3. public class LetterASort{4. public static void main(String[] args) {5. ArrayList<String> strings = new ArrayList<String>();6. strings.add("aAaA");7. strings.add("AaA");8. strings.add("aAa");9. strings.add("AAaa");10. Collections.sort(strings);11. for (String s : strings) { System.out.print(s + " "); }12. }13. }
Given these classes in different files:package xcom;public class Useful {int increment(int x) { return ++x; }}import xcom.*; // line 1class Needy3 {public static void main(String[] args) {xcom.Useful u = new xcom.Useful(); // line 2System.out.println(u.increment(5));}}Which statements are true? (Choose all that apply.)
Your web page includes a Java SE v1.5 applet with the following declaration:
11. <object classid='clsid:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA'
12. width='200' height='200'>
13. <param name='code' value='Applet.class' />
14. </object>
Which HTTP method is used to retrieve the applet code?
GET
Given two files:1. class One {2. public static void main(String[] args) {3. int assert = 0;4. }5. }1. class Two {2. public static void main(String[] args) {3. assert(false);4. }5. }And the four command-line invocations:javac -source 1.3 One.javajavac -source 1.4 One.javajavac -source 1.3 Two.javajavac -source 1.4 Two.java
What is the result? (Choose all that apply.)
A. Only one compilation will succeed.
Given classes defined in two different files:1. package util;2. public class BitUtils {3. private static void process(byte[] b) {}4. }1. package app;2. public class SomeApp {3. public static void main(String[] args) {4. byte[] bytes = new byte[256];5. // insert code here6. }7. }
Given:1. class Plant {2. String getName() { return "plant"; }3. Plant getType() { return this; }4. }5. class Flower extends Plant {6. // insert code here7. }8. class Tulip extends Flower { }
Which statement(s), inserted at line 6, will compile? (Choose all that apply.)
10. class One {11. public One foo() { return this; }12. }13. class Two extends One {14. public One foo() { return this; }15. }16. class Three extends Two {17. // insert method here18. }Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
Given:11. public static void main(String[] args) {12. try {13. args = null;14. args[0] = "test";15. System.out.println(args[0]);16. } catch (Exception ex) {17. System.out.println("Exception");18. } catch (NullPointerException npe) {19. System.out.println("NullPointerException");20. }21. }
Given:11. public void genNumbers() {12. ArrayList numbers = new ArrayList();13. for (int i=0; i<10; i++) {14. int value = i * ((int) Math.random());15. Integer intObj = new Integer(value);16. numbers.add(intObj);17. }18. System.out.println(numbers);19. }
Which line of code marks the earliest point that an object referenced by int Obj becomes a candidate forgarbage collection?
A. Line 16
A. An encapsulated, public class promotes re-use.
Compilation fails due to an error on line 7.
static final int[] a = { 100,200 };
Assuming that the serializeBanana() and the deserializeBanana() methods willcorrectly use Java serialization and given:
13. import java.io.*;14. class Food implements Serializable {int good = 3;}15. class Fruit extends Food {int juice = 5;}16. public class Banana extends Fruit {17. int yellow = 4;18. public static void main(String [] args) {19. Banana b = new Banana(); Banana b2 = new Banana();20. b.serializeBanana(b); // assume correct serialization21. b2 = b.deserializeBanana(); // assume correct22. System.out.println("restore "+b2.yellow+ b2.juice+b2.good);24. }25. // more Banana methods go here 50. }What is the result?
A. 420 is the output.
A. None of these objects are eligible for garbage collection.
ArrayList a = new ArrayList();containing the values {�1�, �2�, �3�, �4�, �5�, �6�, �7�, �8�}Which code will return 2?
1. public class Base {2. public static final String FOO = "foo";3. public static void main(String[] args) {4. Base b = new Base();5. Sub s = new Sub();6. System.out.print(Base.FOO);7. System.out.print(Sub.FOO);8. System.out.print(b.FOO);9. System.out.print(s.FOO);10. System.out.print(((Base)s).FOO);11. } }12. class Sub extends Base {public static final String FOO="bar";}
A. Line 5
Given:11. static void test() {12. try {13. String x = null;14. System.out.print(x.toString() + " ");15. }16. finally { System.out.print("finally "); }17. }18. public static void main(String[] args) {19. try { test(); }20. catch (Exception ex) { System.out.print("exception "); }21. }