Java 2.0

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

private

Suppose you are writing a class that will provide custom serialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the writeObject() method have? (Choose one.)

Override run().

Suppose you want to create a custom thread class by extending java.lang.Thread in order to provide some special functionality. Which of the following must you do? (Choose one.)

The strategy fails because you cannot subclass java.lang.Math.

Suppose you want to write a class that offers static methods to compute hyperbolic trigonometric functions. You decide to subclass java.lang.Math and provide the new functionality as a set of static methods. Which one statement is true about this strategy?

True

Swing components cannot be combined with AWT components.

javax.swing

The Swing component classes can be found in the ________________ package.

DriverManager

The ______ class is the primary class that has the driver information.

Menu

The ______ class is used to implement a pull-down menu that provides a number of items to select from.

False

The element method alters the contents of a Queue.

Have a nice day!

public class Test{ public static void main(String[] args){ Object ob1= new Object(); Object ob2= new Object(); if(ob1.equals(ob2)) System.out.println("ob1 equals ob2"); if(ob1==ob2) System.out.println("ob1==ob2"); System.out.println("Have a nice day!"); } } What is the output?

ob1 equals ob2 ob1==ob2 Have a nice day!

public class Test{ public static void main(String[] args){ Object ob1= new Object(); Object ob2= ob1; if(ob1.equals(ob2)) System.out.println("ob1 equals ob2"); if(ob1==ob2) System.out.println("ob1==ob2"); System.out.println("Have a nice day!"); } } What is the output?

Line 6

public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = new String("xyz"); if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output?

Line 6

public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = new String(s1); s2.intern(); if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output?

Line 4 Line 6

public class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = new String(s1); s2=s2.intern(); if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output? (choose 1)

No output because of compile error at line: b = b * b1;

public class Test{ public static void main(String[] args){ byte b = 2; byte b1 = 3; b = b * b1; System.out.println("b="+b); } } What is the output?

Line 4 Line 6

ublic class Test{ public static void main(String[] args){ String s1 = "xyz"; String s2 = "xyz"; if (s1 == s2) System.out.println("Line 4"); if (s1.equals(s2)) System.out.println("Line 6"); } } What is the output?

line 3: private; line 8: protected

Given the following code, and making no other changes, which combination of access modifiers (public, protected, or private) can legally be placed before aMethod() on line 3 and be placed before aMethod() on line 8? (Choose one.) 1. class SuperDuper 2. { 3. void aMethod() { } 4. } 5. 6. class Sub extends SuperDuper 7. { 8. void aMethod() { } 9. }

Spice sp = Spice.NUTMEG; Object ob = sp; Spice sp = Spice.NUTMEG; Object ob = (Object)sp; Object ob = new Object(); Spice sp = (Spice)ob;

Given the following code, which of the following will compile? (Choose three.) enum Spice { NUTMEG, CINNAMON, CORIANDER, ROSEMARY; }

terator<String> iter = names.iterator(); or (String s:names)

Given the following: List<String> names = new ArrayList<String>(); which of the following are legal? (Choose two.)

StringBuffer s = new StringBuffer("123456789"); s.delete(0,3).replace( 1,3, "24").delete(4,6); StringBuilder s = new StringBuilder("123456789"); s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, "24");

Given: 1. public class TestString3 { 2. public static void main(String[] args) { 3. // insert code here 5. System.out.println(s); 6. } 7. } Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two.)

Connection

Interface ____ helps manage the connection between a Java program and a database.

Yes

Is it possible to define a class called Thing so that the following method can return true under certain circumstances? boolean weird(Thing s) { Integer x = new Integer(5); return s.equals(x); }

Yes

Is it possible to write code that can execute only if the current thread owns multiple locks?

Two-tier and three-tier

JDBC supports ______ and ______ models

Model-View-Controller

MVC is short call of

WHERE

SQL keyword ___ is followed by the selection criteria that specify the rows to select in a query

We use readObject() method of ObjectOutputStream class to deserialize.

Select INCORRECT statement about deserialize. (choose 1)

When an Object Output Stream serializes an object that contains references to another object, every referenced object is not serialized along with the original object.

Select INCORRECT statement about serialization. (choose 1

All the above

Select correct statement about RMI. (choose 1)

All the others choice

Select correct statement(s) about remote class.(choose one)

All the others choice

Select correct statements about remote interface. (choose 1

A client accesses a remote object by specifying only the server name.

Select incorrect statement about RMI server.(choose 1)

To make the new object available for client connections, call its accept() method, which returns an instance of ServerSocket

Select incorrect statement about ServerSocket class. (choose 1)

The java.net.Socket class contains code that knows how to find and communicate with a server through UDP.

Select incorrect statement about Socket class. (choose 1)

A user's commands are delivered to the database or other data source, and the results of those statements are sent back to the user.

Select the correct statement about JDBC two-tier processing model.

ResultSet

Statement objects return SQL query results as ___ objects

Both 1 and 2 are true

Study the statements: 1)When a JDBC connection is created, it is in auto-commit mode 2)Once auto-commit mode is disabled, no SQL statements will be committed until you call the method commit explicitly

If the JVM doesn't crash and the code does not execute a System.exit() call, the finally block will always execute.

Suppose a method called finallyTest() consists of a try block, followed by a catch block, followed by a finally block. Assuming the JVM doesn't crash and the code does not execute a System.exit() call, under what circumstances will the finally block not begin to execute? (Choose one.)

Class loading takes no additional time.

Suppose a source file contains a large number of import statements and one class definition. How do the imports affect the time required to load the class? (Choose one.)

Compilation takes slightly more time.

Suppose a source file contains a large number of import statements. How do the imports affect the time required to compile the source file? (Choose one.)

C must have a no-args constructor.

Suppose class A extends Object; Class B extends A; and class C extends B. Of these, only class C implements java.io.Externalizable. Which of the following must be true in order to avoid an exception during deserialization of an instance of C? (Choose one.)

B must have a no-args constructor.

Suppose class A extends Object; class B extends A; and class C extends B. Of these, only class C implements java.io.Serializable. Which of the following must be true in order to avoid an exception during deserialization of an instance of C? (Choose one.)

private

Suppose class A has a method called doSomething(), with default access. Suppose class B extends A and overrides doSomething(). Which access modes may not apply to B's version of doSomething()? (Choose one)

public protected

Suppose class Supe, in package packagea, has a method called doSomething(). Suppose class Subby, in package packageb, overrides doSomething(). What access modes may Subby's version of the method have? (Choose two.)

public void doSomething(int a, float b) { ... }

Suppose class X contains the following method: void doSomething(int a, float b) { ... } Which of the following methods may appear in class Y, which extends X? (Choose one.)

The class will compile if it is declared abstract. The class may not be instantiated.

Suppose interface Inty defines five methods. Suppose class Classy declares that it implements Inty but does not provide implementations for any of the five interface methods. Which are true? (Choose two.)

All the above

Suppose prim is an int and wrapped is an Integer. Which of the following are legal Java statements? (Choose one.)

for (float f:salaries)

Suppose salaries is an array containing floats. Which of the following are valid loop control statements for processing each element of salaries? (Choose one.)

When the type of x is Object

Suppose the declared type of x is a class, and the declared type of y is an nterface. When is the assignment x = y; legal? (Choose one.)

Sometimes

Suppose the type of xarr is an array of XXX, and the type of yarr is an rray of YYY. When is the assignment xarr = yarr; legal? (Choose one.)

if (x == y)

Suppose x and y are of type TrafficLightState, which is an enum. What is the best way to test whether x and y refer to the same constant? (Choose one.)

private

Suppose you are writing a class that will provide custom deserialization. The class implements java.io.Serializable (not java.io.Externalizable). What access mode should the readObject() method have? (Choose one.)

An exception is thrown at runtime. Line 13 creates a File object named "d." Line 14 creates a File object named "f.'

10. class MakeFile { 11. public static void main(String[] args) { 12. try { 13. File directory = new File("d"); 14. File file = new File(directory,"f"); 15. if(!file.exists()) { 16. file.createNewFile(); 17. } 18. }catch (IOException e) { 19. e.printStackTrace (); 20. } 21. } 22. } The current directory does NOT contain a directory named "d." Which three are true? (Choose three.)

The ownerName variable breaks encapsulation.

20. public class CreditCard { 22. private String cardlD; 23. private Integer limit; 24. public String ownerName; 26. public void setCardlnformation(String cardlD, 27. String ownerName, 28. Integer limit) { 29. this.cardlD = cardlD; 30. this.ownerName = ownerName; 31. this.limit = limit; 32. } 33. } Which is true? (Choose one.)

public interface B extends A { }

Given: 1. public interface A { 2. String DEFAULT_GREETING = "Hello World"; 3. public void method1(); 4. } A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct? (Choose one.)

public double getSalesAmount() { return 1230.45; } protected double getSalesAmount() { return 1230.45; }

Given: 10. abstract public class Employee { 11. protected abstract double getSalesAmount(); 12. public double getCommision() { 13. return getSalesAmount() * 0.15; 14. } 15. } 16. class Sales extends Employee { 17. // insert method here 18. } Which two methods, inserted independently at line 17, correctly complet he Sales class? (Choose two.)

Line.Point p = new Line.Point();

Given: 10. class Line { 11. public static class Point { } 12. } 13. 14. class Triangle { 15. // insert code here 16. } Which code, inserted at line 15, creates an instance of the Point class defined in Line? (Choose one.)

Nav.Direction d = Nav.Direction.NORTH;

Given: 10. class Nav{ 11. public enum Direction { NORTH, SOUTH, EAST, WEST } 12. } 13. public class Sprite{ 14. // insert code here 15. } Which code, inserted at line 14, allows the Sprite class to compile? Choose one.)

new Foo() { public int bar(){return 1; } }

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 here 16. ); 17. } 18. } Which code, inserted at line 15, allows the class Sprite to compile? (Choose one.)

for(int z : x) System.out.println(z); for( int i=0; i< x.length; i++ ) System.out.println(x[i]);

Given: 10. public class Bar { 11. static void foo(int...x) { 12. // insert code here 13. } 14. } Which two code fragments, inserted independently at line 12, will allow the class to compile? (Choose two.)

StackOverflowError

Given: 10. public class ClassA { 11. public void count(int i) { 12. count(++i); 13. } 14. } And: 20. ClassA a = new ClassA(); 21. a.count(3); Which exception or error should be thrown by the virtual machine? (Choose one.)

Compilation fails.

Given: 11. String test = "This is a test"; 12. String[] tokens = test.split("\s"); 13. System.out.println(tokens.length); What is the result? (Choose one.)

Shape s = new Circle(); s.setAnchor(10,10); s.draw();

Given: 11. public abstract class Shape { 12. int x; 13. int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } and a class Circle that extends and fully implements the Shape class.

final static public

Given: 11. public interface Status { 12. /* insert code here */ int MY_VALUE = 10; 13. } Which three are valid on line 12? (Choose three.)

1 2 3

Given: 11. public static void main(String[] args) { 12. Object obj =new int[] { 1,2,3 }; 13. int[] someArray = (int[])obj; 14. for (int i: someArray) System.out.print(i +" "); 15. } What is the result? (Choose one.)

Compilation fails.

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. } What is the result? (Choose one.)

Compilation fails.

Given: 11. public static void parse(String str) { 12. try { 13. float f= Float.parseFloat(str); 14. } catch (NumberFormatException nfe) { 15. f = 0; 16. } finally { 17. System.out.println(f); 18. } 19. } 20. public static void main(String[] args) { 21. parse("invalid"); 22. } What is the result? (Choose one.)

Compilation fails because of an error in line 19.

Given: 11. static class A { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println("B "); } 16. } 17.public static void main(String[] args) { 18. A a=new B(); 19. a.process(); 20. } What is the result? (Choose one.)

passed An AssertionException is thrown with the word "stuff" added to the stack trace.

Given: 12. public class AssertStuff { 14. public static void main(String [] args) { 15. int x= 5; 16. int y= 7; 18. assert (x > y): "stuff"; 19. System.out.println("passed"); 20. } 21. } And these command line invocations: java AssertStuff java -ea AssertStuff What is the result? (Choose one.)

collie harrier

Given: 12. public class Test { 13. public enum Dogs {collie, harrier}; 14. public static void main(String [] args) { 15. Dogs myDog = Dogs.collie; 16. switch (myDog) { 17. case collie: 18. System.out.print("collie "); 19. case harrier: 20. System.out.print("harrier "); 21. } 22. } 23. } What is the result? (Choose one.)

doStuff x = 5 main x = 5

Given: 13. public class Pass { 14. public static void main(String [] args) { 15. int x = 5; 16. Pass p = new Pass(); 17. p.doStuff(x); 18. System.out.print(" main x = "+ x); 19. } 20. 21. void doStuff(int x) { 22. System.out.print("doStuff x = "+ x++); 23. } 24. } What is the result? (Choose one.)

doStuffx = 5 main x = 5

Given: 13. public class Pass { 14. public static void main(String [] args) { 15. int x = 5; 16. Pass p = new Pass(); 17. p.doStuff(x); 18. System.out.print(" main x = "+ x); 19. } 20. 21. void doStuff(int x) { 22. System.out.print("doStuffx = "+ x++); 23. } 24. } What is the result? (Choose one.)

Compilation fails due to an error in line 29.

Given: 23. Object [] myObjects = { 24. new Integer(12), 25. new String("foo"), 26. new Integer(5), 27. new Boolean(true) 28. }; 29. java.util.Array.sort(myObjects); 30. for( int i=0; i<myObjects.length; i++) { 31. System.out.print(myObjects[i].toString()); 32. System.out.print(" "); 33. }

The code on line 33 throws an exception. The code on line 35 throws an exception. The code on line 33 executes successfully.

Given: 31. // some code here 32. try { 33. // some code here 34. } catch (SomeException se) { 35. // some code here 36. } finally { 37. // some code here 38. } Under which three circumstances will the code on line 37 be executed? (Choose three.)

ac

Given: 33. try { 34. // some code here 35. }catch (NullPointerException e1) { 36. System.out.print("a"); 37. }catch (RuntimeException e2) { 38. System.out.print("b"); 39. } finally { 40. System.out.print("c"); 41. } What is the result if a NullPointerException occurs on line 34? (Choose one.)

Line 57 will print the value 3.

Given: 55. int []x= {1, 2,3,4, 5}; 56. int y[] =x; 57. System.out.println(y[2]); Which is true? (Choose one.)

java -ea test java -ea test file1 file2

Given: 8. public class test { 9. public static void main(String [] a) { 10. assert a.length == 1; 11. } 12.} Which two will produce an AssertionError? (Choose two.)

A Exception

Given: class A { public void process() { System.out.print("A "); } public static void main(String[] args) { try { ((A)new B()).process(); } catch (Exception e) { System.out.print("Exception "); } } } class B extends A { public void process() throws RuntimeException { super.process(); if (true) throw new RuntimeException(); System.out.print("B"); } } What is the result? (Choose one.)

2 3

Given: public class Bar { public static void main(String [] args) { int x =5; boolean b1 = true; boolean b2 = false; if((x==4) && !b2) System.out.print("l "); System.out.print("2 "); if ((b2 = true) && b1) System.out.print("3"); } } What is the result? (Choose one.)

There is no single technique that can guarantee non-deadlocking code.

How can you ensure that multithreaded code does not deadlock? (Choose one.)

Garbage collection cannot be forced.

How can you force garbage collection of an object? (Choose one.)

Access the variables only via synchronized methods.

How do you prevent shared data from being corrupted in a multithreaded environment? (Choose one.)

String[] contents = myFile.list();

How do you use the File class to list the contents of a directory? (Choose one

8

How many bytes does the following code write to file dest? (Choose one.) 1. try { 2. FileOutputStream fos = newFileOutputStream("dest"); 3. DataOutputStream dos = new DataOutputStream(fos); 4. dos.writeInt(3); 5. dos.writeFloat(0.0001f); 6. dos.close(); 7. fos.close(); 8. } 9. catch (IOException e) { }

One

How many locks does an object have? (Choose one.)

Package declaration, imports, class/interface/enum definitions.

If all three top-level elements occur in a source file, they must appear in which order? (Choose one.)

Yes

If you attempt to compile and execute the following application, will it ever print out the message In xxx? 1. class TestThread3 extends Thread { 2. public void run() { 3. System.out.println("Running"); 4. System.out.println("Done"); 5. } 6. 7. private void xxx() { 8. System.out.println("In xxx"); 9. } 10. 11. public static void main(String args[]) { 12. TestThread3 ttt = new TestThread3(); 13. ttt.xxx(); 14. ttt.start(); 12. } 13. }

TreeSet

If you need a Set implementation that provides value-ordered iteration, which class should you use? (Choose one.) HashSet

Comparable interface and its compareTo method.

In order for objects in a List to be sorted, those objects must implement which interface and method? (Choose one.)

True

In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("FPT"); 2. sbuf.insert(3, "-University");

True

In the following code fragment, after execution of line 1, sbuf references an instance of the StringBuffer class. After execution of line 2, sbuf still references the same instance. 1. StringBuffer sbuf = new StringBuffer("FPT"); 2. sbuf.append("-University");

int, long, float, double

In the following code, what are the possible types for variable result? (Choose the most complete true answer.) 1. byte b = 11; 2. short s = 13; 3. result = b * ++s;

DataPacket and DataSocket

There are two classes in Java to enable communication using datagrams namely.

The program will compile and execute. The output will be Before: 0 After: 2.

This question concerns the following class definition: 1. package abcde; 2. 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class Nightingale? (Choose one.) 1. package singers; 2. 3. class Nightingale extends abcde.Bird { 4. Nightingale() { referenceCount++; } 5. 6. public static void main(String args[]) { 7. System.out.print("Before: " + referenceCount); 8. Nightingale florence = new Nightingale(); 9. System.out.println(" After: " + referenceCount); 10. florence.fly(); 11. } 12. }

Compilation of Parrot.java fails at line 7 because method getRefCount() is static in the superclass, and static methods may not be overridden to be nonstatic.

This question concerns the following class definition: 1. package abcde; 2. 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class Parrot? (Choose one.) 1. package abcde; 2. 3. class Parrot extends abcde.Bird { 4. public void fly() { 5. /* Parrot-specific flight code. */ 6. } 7. public int getRefCount() { 8. return referenceCount;

void doSomething() { ... } void doSomething() throws AWTException { ... } void doSomething() throws IOException, EOFException { ... }

This question involves IOException, AWTException, and EOFException. They are all checked exception types. IOException and AWTException extend Exception, and EOFException extends OException. Suppose class X contains the following method: void doSomething() throws IOException{ ... } Which of the following methods may appear in class Y, which extends X? (Choose three.)

protocol:subprotocol:datasoursename

URL referring to databases use the form:

There are no possible legal types

What are the legal types for whatsMyType? (Choose one.) short s = 10; whatsMyType = !s;

Throws an exception.

What does the following code do? Integer i = null; if (i != null & i.intValue() == 5) System.out.println("Value is 5");

The output is i = 20.

What does the following code fragment print out at line 9? (Choose one.) 1. FileOutputStream fos = new FileOutputStream("xx"); 2. for (byte b=10; b<50; b++) 3. fos.write(b); 4. fos.close(); 5. RandomAccessFile raf = new RandomAccessFile("xx", "r"); 6. raf.seek(10); 7. int i = raf.read(); 8. raf.close() 9. System.out.println("i = " + i);

False

f class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y.


Set pelajaran terkait

2. Ask Questions to Make Data-Driven Decisions

View Set

INDUCTIVE AND DEDUCTIVE REASONING

View Set

NRSG 102 lecture review for test1 17?'s

View Set

Government Chapter 4 Reading Quiz

View Set

Classes of life Insurance Policies

View Set