TechFundu

  • Increase font size
  • Default font size
  • Decrease font size
Welcome Guest,  Login | Join Now
Home Read Articles Java Technologies Core Java SCJP Exam Prepration Questions - Part 3

SCJP Exam Prepration Questions - Part 3

Last Updated on Tuesday, 19 January 2010 09:38
(0 votes, average 0 out of 5)

Question 100

Assuming that the serializeBanana() and the deserializeBanana()

methods will correctly use Java serialization and given:

13. import java.io.*;

14. class Food implemertts Serializable {int good = 3;}

15. class Fruit externds 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 serialization

21. b2 = b.deserializeBanana(); // assume correct

22. System.out.println(”restore “+b2.yellow+ b2.juice+b2.good);

24. }

25. // more Banana methods go here

50. }

What is the result?

A. restore 400

B. restore 403

C. restore 453

D. Compilation fails.

E. An exception is thrown at runtime.

Answer: C

Question 101

Assuming that the serializeBanana2() and the deserializeBanana2()

methods will 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 serialization

21. b = b.deserializeBanana2(b); // assume correct

22. System.out.println(” restored “+ b.size + “ “); }

23. // more Banana2 methods

24. }

What is the result?

A. Compilation fails.

B. 1 restored 42

C. 12 restored 42

D. 121 restored 42

E. 1212 restored 42

F. An exception is thrown at runtime.

Answer: D

Question 102

Given:

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 here

16. }

17. }

Which code fragment, inserted at line 15, will allow Foo objects to be

correctly serialized and deserialized?

A. s.writeInt(x);

B. s.serialize(x);

C. s.writeObject(x);

D. s.defaultWriteObject();

Answer: D

Question 103

Given:

12. NumberFormat nf= NumberFormat.getInstance();

13. nf.setMaximumFractionDigits(4);

14. nf.setMinimumFractionDigits(2);

15. String a = nf.format(3.1415926);

16. String b = nf.format(2);

Which two are true about the result if the default locale is Locale.US?

(Choose two.)

A. The value of b is 2.

B. The value of a is 3.14.

C. The value of b is 2.00.

D. The value of a is 3.141.

E. The value of a is 3.1415.

F. The value of a is 3.1416.

G. The value of b is 2.0000.

Answer: CF

Question 104

Given:

11. double input = 314159.26;

12. NumberFormat nf= NumberFormat.getInstance(Locale.ITALIAN);

13. String b;

14. //insert code here

Which code, inserted at line 14, sets the value of b to 3 14.159,26?

A. b = nf.parse( input);

B. b = nf.format( input);

C. b = nf.equals( input);

D. b = nf.parseObject( input);

Answer: B

Question 105

Given:

14. DateFormat df;

15. Date date = new Date();

16. //insert code here

17. String s = df.format( date);

Which two, inserted independently at line 16, allow the code to

compile? (Choose two.)

A. df= new DateFormat();

B. df= Date.getFormatter();

C. df= date.getFormatter();

D. df= date.getDateFormatter();

E. df= Date.getDateFormatter();

F. df= DateFormat.getInstance();

G. df = DateFormat.getDateInstance();

Answer: FG

Question 106

Given:

12. Date date = new Date();

13. df.setLocale(Locale.ITALY);

14. String s = df.format(date);

The variable df is an object of type DateFormat that has been

initialized in line 11. What is the result if this code is run on December

14, 2000?

A. The value of s is 14-dic-2004.

B. The value of s is Dec 14, 2000.

C. An exception is thrown at runtime.

D. Compilation fails because of an error in line 13.

Answer: D

Question 107

Given:

33. Date d = new Date(0);

34. String ds = “December 15, 2004”;

35. // insert code here

36. try {

37. d = df.parse(ds);

38. }

39. catch(ParseException e) {

40. System.out.println(”Unable to parse “+ ds);

41. }

42. // insert code here too

Which will create the appropriate DateFormat object and add a day to

the Date object?

A. 35. DateFormat df= DateFormat.getDateFormat();

42. d.setTime( (60 * 60 * 24) + d.getTime());

B. 35. DateFormat df= DateFormat.getDateJnstance();

42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime());

C. 35. DateFormat df= DateFormat.getDateFormat();

42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime());

D. 35. DateFormat df= DateFormat.getDateJnstance();

42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());

Answer: B

Question 108

Given a valid DateFormat object named df, and

16. Date d = new Date(0L);

17. String ds = “December 15, 2004”;

18. // insert code here

What updates d’s value with the date represented by ds?

A. 18. d = df.parse(ds);

B. 18. d = df.getDate(ds);

C. 18. try {

19. d = df.parse(ds);

20. } catch(ParseException e) { };

D. 18. try {

19. d = df.getDate(ds);

20. } catch(ParseException e) { };

Answer: C

Question 109

Given:

11. String test = “This is a test”;

12. String[] tokens = test.split(”\s”);

13. System.out.println(tokens.length);

What is the result?

A. 0

B. 1

C. 4

D. Compilation fails.

E. An exception is thrown at runtime.

Answer: D

Question 110

Given:

11. String test= “a1b2c3”;

12. String[] tokens = test.split(”\\d”);

13. for(String s: tokens) System.out.print(s +“ “);

What is the result?

A. a b c

B. 1 2 3

C. a1b2c3

D. a1 b2 c3

E. Compilation fails.

F. The code runs with no output.

G. An exception is thrown at runtime.

Answer: A

Question 111

Given:

11. String test = “Test A. Test B. Test C.”;

12. // insert code here

13. String[] result = test.split(regex);

Which regular expression inserted at line 12 will correctly split test into

“Test A,” “Test B,” and “Test C”?

A. String regex = “”;

B. String regex = “ “;

C. String regex = “.*“.

D. String regex = “\\s”

E. String regex = “\\.\\s*”;

F. String regex = “\\w[ \.] +“;

Answer: E

Question 112

Given:

12. System.out.format(”Pi is approximately %d.”, Math.PI);

What is the result?

A. Compilation fails.

B. Pi is approximately 3.

C. Pi is approximately 3.141593.

D. An exception is thrown at runtime.

Answer: D

Question 113

Given:

12. String csv = “Sue,5,true,3”;

13. Scanner scanner = new Scanner( csv);

14. scanner.useDelimiter(”,”);

15. int age = scanner.nextInt();

What is the result?

A. Compilation fails.

B. After line 15, the value of age is 5.

C. After line 15, the value of age is 3.

D. An exception is thrown at runtime.

Answer: D

Question 114

Which two code fragments will execute the method doStuff() in a

separate thread? (Choose two.)

A. new Thread() {

public void run() { doStuff(); }

}

B. new Thread() {

public void start() { doStuff(); }

}

C. new Thread() {

public void start() { doStuff(); }

} .run();

D. new Thread() {

public void run() { doStuff(); }

} .start();

E. new Thread(new Runnable() {

public void run() { doStuff(); }

} ).run();

F. new Thread(new Runnable() {

public void run() { doStuff(); }

}).start();

Answer: DF

Question 115

Given:

1. public class Threads3 implements Runnable {

2. public void run() {

3. System.out.print(”running”);

4. }

5. public static void main(String[] args) {

6. Thread t = new Thread(new Threads3());

7. t.run();

8. t.run();

9. t.start();

10. }

11. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes and prints “running”.

D. The code executes and prints “runningrunning”.

E. The code executes and prints “runningrunningrunning”.

Answer: E

Question 116

Given:

1. public class Threads4 {

2. public static void main (String[] args) {

3. new Threads4().go();

4. }

5. public void go() {

6. Runnable r = new Runnable() {

7. public void run() {

8. System.out.print(”foo”);

9. }

10. };

11. Thread t = new Thread(r);

12. t.start();

13. t.start();

14. }

15. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes normally and prints ‘foo”.

D. The code executes normally, but nothing is printed.

Answer: B

Question 117

Given:

1. public class Threads5 {

2. public static void main (String[] args) {

3. new Thread(new Runnable() {

4. public void run() {

5. System.out.print(”bar”);

6. }}).start();

7. }

8. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes normally and prints “bar”.

D. The code executes normally, but nothing prints.

Answer: C

Question 118

Given:

11. Runnable r = new Runnable() {

12. public void run() {

13. System.out.print(”Cat”);

14. }

15. };

16. Threadt=new Thread(r) {

17. public void run() {

18. System.out.print(”Dog”);

19. }

20. };

21. t.start();

What is the result?

A. Cat

B. Dog

C. Compilation fails.

D. The code runs with no output.

E. An exception is thrown at runtime.

Answer: B

Question 119

Click the Exhibit button.

Given:

10. public class Starter extends Thread {

11. private int x= 2;

12. public static void main(String[] args) throws Exception {

13. new Starter().makeItSo();

14. }

15. public Starter() {

16. x=5;

17. start();

18. }

19. public void makeItSo() throws Exception {

20. join();

21. x=x- 1;

22. System.out.println(x);

23. }

24. public void run() { x *= 2; }

25. }

What is the output if the main() method is rum?

A. 4

B. 5

C. 8

D. 9

E. Compilation fails.

F. An exception is thrown at runtime.

G. It is impossible to determine for certain.

Answer: D

Question 120

Given:

1. public class Threads2 implements Runnable {

2.

3. public void nun() {

4. System.out.println(”run.”);

5. throw new RuntimeException(”Problem”);

6. }

7. public static void main(String[] args) {

8. Thread t = new Thread(new Threads2());

9. t.start();

10. System.out.println(”End of method.”);

11. }

12. }

Which two can be results? (Choose two.)

A. java.lang.RuntimeException: Problem

B. run.

java.lang.RuntimeException: Problem

C. End of method.

java.lang.RuntimeException: Problem

D. End of method.

run.

java.lang.RuntimeException: Problem

E. run.

java.lang.RuntimeException: Problem

End of method.

Answer: DE

Question 121

Given:

1. public class TestOne {

2. public static void main (String[] args) throws Exception {

3. Thread.sleep(3000);

4. System.out.println(”sleep”);

5. }

6. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes normally and prints “sleep”.

D. The code executes normally, but nothing is printed.

Answer: C

Question 122

Given:

1. public class TestOne implements Runnable {

2. public static void main (String[] args) throws Exception {

3. Thread t = new Thread(new TestOne());

4. t.start();

5. System.out.print(”Started”);

6. t.join();

7. System.out.print(”Complete”);

8. }

9. public void run() {

10. for (int i= 0; i< 4; i++) {

11. System.out.print(i);

12. }

13. }

14. }

What can be a result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The code executes and prints “StartedComplete”.

D. The code executes and prints “StartedComplete0123”.

E. The code executes and prints “Started0l23Complete”.

Answer: E

Question 123

Click the Exhibit button.

Given:

1. public class TwoThreads {

2

3. private static Object resource = new Object();

4.

5. private static void delay(long n) {

6. try { Thread.sleep(n); }

7. catch (Exception e) { System.out.print(”Error “); }

8. }

9

10. public static void main(String[] args) {

11. System.out.print(”StartMain “);

12. new Thread1().start();

13. delay(1000);

14. Thread t2 = new Thread2();

15. t2.start();

16. delay(1000);

17. t2.interrupt

18. delay(1000);

19. System.out.print(”EndMain “);

20. }

21.

22. static class Thread 1 extends Thread {

23. public void run() {

24. synchronized (resource) {

25. System.out.print(”Startl “);

26. delay(6000);

27. System.out.print(”End1 “);

28. }

29. }

30. }

31.

32. static class Thread2 extends Thread {

33. public void run() {

34. synchronized (resource) {

35. System.out.print(”Start2 “);

36. delay(2000);

37. System.out.print(”End2 “);

38. }

39. }

40. }

41. }

Assume that sleep(n) executes in exactly m milliseconds, and all other

code executes in an insignificant amount of time. What is the output if

the main() method is run?

A. Compilation fails.

B. Deadlock occurs.

C. StartMain Start1 Error EndMain End1

D. StartMain Start1 EndMain End1 Start2 End2

E. StartMain Start1 Error Start2 EndMain End2 End1

F. StartMain Start1 Start2 Error End2 EndMain End1

G. StartMain Start1 EndMain End1 Start2 Error End2

Answer: G

Question 125

Given:

public class NamedCounter {

private final String name;

private int count;

public NamedCounter(String name) { this.name = name; }

public String getName() { return name; }

public void increment() { coount++; }

public int getCount() { return count; }

public void reset() { count = 0; }

}

Which three changes should be made to adapt this class to be used

safely by multiple threads? (Choose three.)

A. declare reset() using the synchronized keyword

B. declare getName() using the synchronized keyword

C. declare getCount() using the synchronized keyword

D. declare the constructor using the synchronized keyword

E. declare increment() using the synchronized keyword

Answer: ACE

Question 125

Click the Exhibit button:

1. public class Threads 1 {

2. intx=0;

3. public class Runner implements Runnable {

4. public void run() {

5. int current = 0;

6. for(int=i=0;i<4;i++){

7. current = x;

8. System.out.print(current + “, “);

9. x = current + 2;

10. }

11. }

12. }

13.

14. public static void main(String[] args) {

15. new Threads1().go();

16. }

17.

18. public void go() {

19. Runnable r1 = new Runner();

20. new Thread(r1).start();

21. new Thread(r1 ).start();

22. }

23. }

Which two are possible results? (Choose two.)

A. 0, 2, 4, 4, 6, 8, 10, 6,

B. 0, 2, 4, 6, 8, 10, 2, 4,

C. 0, 2, 4, 6, 8, 10, 12, 14,

D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,

E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,

Answer: AC

Question 126

Click the Exhibit button.

1. import java.util.*;

2.

3. public class NameList {

4. private List names = new ArrayList();

5. public synchronized void add(String name) { names.add(name); }

6. public synchronized void printAll() {

7. for (int i = 0; i

8. System.out.print(names.get(i) +“ “);

9. }

10. }

11. public static void main(String[] args) {

12. final NameList sl = new NameList();

13.for(int i=0;i<2;i++) {

14. new Thread() {

15. public void ruin() {

16. sl.add(”A”);

17. sl.add(”B”);

18. sl.add(”C”);

19. sl.printAll();

20. }

21. }.start();

22. }

23. }

24. }

Which two statements are true if this class is compiled and run?

(Choose two.)

A. An exception may be thrown at runtime.

B. The code may run with no output, without exiting.

C. The code may run with no output, exiting normally.

D. The code may rum with output “A B A B C C “, then exit.

E. The code may rum with output “A B C A B C A B C “, then exit.

F. The code may ruin with output “A A A B C A B C C “, then exit.

G. The code may ruin with output “A B C A A B C A B C “, then exit.

Answer: EG

Question 127

Given:

1. public class TestFive {

2. private int x;

3. public void foo() {

4 int current = x;

5. x = current + 1;

6. }

7. public void go() {

8. for(int i=0;i<5;i++) {

9. new Thread() {

10. public void run() {

11. foo();

12. System.out.print(x + “, “);

13. } }.start();

14. }}}

Which two changes, taken together, would guarantee the output: 1, 2,

3, 4, 5, ? (Choose two.)

A. Move the line 12 print statement into the foo() method.

B. Change line 7 to public synchronized void go() {.

C. Change the variable declaration on line 3 to private volatile int x;.

D. Wrap the code inside the foo() method with a synchronized( this )

block.

E. Wrap the for loop code inside the go() method with a synchronized

block synchronized(this) { // for loop code here }.

Answer: AD

Question 128

Which three will compile and rim without exception? (Choose three.)

A. private synchronized Object o;

B. void go() {

synchronized() { /* code here */ }

}

C. public synchronized void go() { /* code here */ }

D. private synchronized(this) void go() { /* code here */ }

E. void go() {

synchronized(Object.class) { /* code here */ }

}

F. void go() {

Object o = new Object();

synchronized(o) { /* code here */ }

}

Answer: CEF

Question 129

Given:

1. public class TestSeven extends Thread {

2. private static int x;

3. public synchronized void doThings() {

4. int current = x;

5. current++;

6. x = current;

7. }

8. public void run() {

9. doThings();

10. }

1 1.}

Which is true?

A. Compilation fails.

B. An exception is thrown at runtime.

C. Synchronizing the run() method would make the class thread-safe.

D. The data in variable “x” are protected from concurrent access

problems.

E. Declaring the doThings() method as static would make the class

thread-safe.

F. Wrapping the statements within doThings() in a synchronized(new

Object()) { } block would make the class thread-safe.

Answer: E

Question 130

Click the Exhibit button.

10. public class Transfers {

11. public static void main(String[] args) throws Exception {

12. Record r1 = new Record();

13. Record r2 = new Record();

14. doTransfer(r1, r2, 5);

15. doTransfer(r2, r1, 2);

16. doTransfer(r1, r2, 1);

17. // print the result

18. System.out.println(”rl = “ + r1.get() +“, r2=” + r2.get());

19. }

20. private static void doTransfer(

21. final Record a, final Record b, final int amount) {

22. Thread t = new Thread() {

23. public void run() {

24. new Clerk().transfer(a, b, amount);

25. }

26. };

27. t.start();

28. }

29. }

30. class Clerk {

31. public synchronized void transfer(Record a, Record b, int amount){

32. synchronized (a) {

33. synchronized (b) {

34. a.add(-amount);

35. b.add(amount);

36. }

37. }

38. }

39. }

40. class Record {

41.int num=10;

42. public int get() { return num; }

43. public void add(int n) { num = num + n; }

44. }

If Transfers.main() is run, which three are true? (Choose three.)

A. The output may be “r1 = 6, r2 = 14”.

B. The output may be “r1 = 5, r2 = 15”.

C. The output may be “r1 = 8, r2 = 12”.

D. The code may run (and complete) with no output.

E. The code may deadlock (without completing) with no output.

F. M IllegalStateException or InterruptedException may be thrown at

runtime.

Answer: ABE

Question 131

Click the Exhibit button.

1. class Computation extends Thread {

2.

3. private int num;

4. private boolean isComplete;

5. private int result;

6.

7. public Computation(int num) { this.num = num; }

8.

9. public synchronized void run() {

10. result = num * 2;

11. isComplete = true;

12. notify();

13. }

14.

15. public synchronized int getResult() {

16. while (!isComplete) {

17. try {

18. wait();

19. } catch (InterruptedException e) { }

20. }

21. return result;

22. }

23.

24. public static void main(String[] args) {

25. Computation[] computations = new Computation [4];

26. for (int i = 0; i < computations.length; i++) {

27. computations[i] = new Computation(i);

28. computations[i] .start();

29. }

30. for (Computation c : computations)

31. System.out.print(c.getResult() +“ “);

32. }

33. }

What is the result?

A. The code will deadlock.

B. The code may run with no output.

C. An exception is thrown at runtime.

D. The code may run with output “0 6”.

E. The code may run with output “2 0 6 4’.

F. The code may ruin with output “0 2 4 6”.

Answer: F

Question 132

Given:

7. void waitForSignal() {

8. Object obj = new Object();

9. synchronized (Thread.currentThread()) {

10. obj.wait();

11. obj.notify();;

12. }

13. }

Which is true?

A. This code may throw an InterruptedException.

B. This code may throw an IllegalStateException.

C. This code may throw a TimeoutException after ten minutes.

D. This code will not compile unless “obj.wait()” is replaced with

“((Thread) obj).wait()”.

E. Reversing the order of obj.wait() and obj.notify() may cause this

method to complete normally.

F. A call to notify() or notifyAll() from another thread may cause this

method to complete normally.

Answer: B

Question 133

Given:

foo and bar are public references available to many other threads. foo

refers to a Thread and bar is an Object. The thread foo is currently

executing bar.wait(). From another thread, which statement is the

most reliable way to ensue that foo will stop executing wait()?

A. foo.notify();

B. bar.notify();

C. foo.notifyAll();

D. Thread.notify();

E. bar.notiFYAll();

F. Object.notify();

Answer: E

Question 134

Which two are true? (Choose two.)

A. An encapsulated, public class promotes re-use.

B. Classes that share the same interface are always tightly

encapsulated.

C. An encapsulated class allows subclasses to overload methods, but

does NOT allow overriding methods.

D. An encapsulated class allows a programmer to change an

implementation without affecting outside code.

Answer: AD

Question 135

Given:

1. package test;

2.

3. class Target {

4. public String name = “hello”;

5. }

What can directly access and change the value of the variable name?

A. any class

B. only the Target class

C. any class in the test package

D. any class that extends Target

Answer: C

Question 136

Given:

1. public class Target {

2. private int i = 0;

3. public int addOne() {

4. return ++i;

5. }

6. }

And:

1. public class Client {

2. public static void main(String[] args) {

3. System.out.println(new Target().addOne());

4. }

5. }

Which change can you make to Target without affecting Client?

A. Line 4 of class Target can be changed to return i++;

B. Line 2 of class Target can be changed to private int i = 1;

C. Line 3 of class Target can be changed to private int addOne() {

D. Line 2 of class Target can be changed to private Integer i = 0;

Answer: D

Question 137

Given:

1. package geometry;

2. public class Hypotenuse {

3. public InnerTriangle it = new InnerTriangle();

4. class InnerTriangle {

5. public int base;

6. public int height;

7. }

8. }

Which is true about the class of an object that can reference the

variable base?

A. It can be any class.

B. No class has access to base.

C. The class must belong to the geometry package.

D. The class must be a subclass of the class Hypotenuse.

Answer: C

Question 138

Given:

11. class ClassA {}

12. class ClassB extends ClassA {}

13. class ClassC extends ClassA {}

and:

21. ClassA p0 = new ClassA();

22. ClassB p1 = new ClassB();

23. ClassC p2 = new ClassC();

24. ClassA p3 = new ClassB();

25. ClassA p4 = new ClassC();

Which three are valid? (Choose three.)

A. p0 = p1;

B. p1 =p2;

C. p2 = p4;

D. p2 = (ClassC)p1;

E. p1 = (ClassB)p3;

F. p2 = (ClassC)p4;

Answer: AEF

Question 139

Given:

11. class Animal { public String noise() { return “peep”; } }

12. class Dog extends Animal {

13. public String noise() { return “bark”; }

14. }

15. class Cat extends Animal {

16. public String noise() { return “meow”; }

17. }

.....

30. Animal animal = new Dog();

31. Cat cat = (Cat)animal;

32. System.out.printIn(cat.noise());

What is the result?

A. peep

B. bark

C. meow

D. Compilation fails.

E. An exception is thrown at runtime.

Answer: E

Question 140

Given:

11. abstract class Vehicle { public int speed() { return 0; } }

12. class Car extends Vehicle { public int speed() { return 60; } }

13. class RaceCar extends Car { public int speed() { return 150; }}

......

21. RaceCar racer = new RaceCar();

22. Car car = new RaceCar();

23. Vehicle vehicle = new RaceCar();

24. System.out.println(racer.speed() + “, ‘ + car.speed()

25. + “, “+ vehicle.speed());

What is the result?

A. 0, 0,0

B. 150, 60, 0

C. Compilation fails.

D. 150, 150, 150

E. An exception is thrown at runtime.

Answer: D

Question 141

 

Given:

10. abstract class A {

11. abstract void al();

12. void a2() { }

13. }

14. class B extends A {

15. void a1() { }

16. void a2() { }

17. }

18. class C extends B { void c1() { } }

and:

A x = new B(); C y = new C(); A z = new C();

Which four are valid examples of polymorphic method calls? (Choose

four.)

A. x.a2();

B. z.a2();

C. z.c1();

D. z.a1();

E. y.c1();

F. x.a1();

Answer: ABDF

Question 142

Given:

10. interface A { void x(); }

11. class B implements A { public void x() { } public voidy() { } }

12. class C extends B { public void x() {} }

And:

20. java.util.List list = new java.util.ArrayList();

21. list.add(new B());

22. list.add(new C());

23. for (A a:list) {

24. a.x();

25. a.y();;

26. }

What is the result?

A. The code runs with no output.

B. An exception is thrown at runtime.

C. Compilation fails because of an error in line 20.

D. Compilation fails because of an error in line 21.

E. Compilation fails because of an error in line 23.

F. Compilation fails because of an error in line 25.

Answer: F

Question 143

Given:

1. class SuperClass {

2. public A getA() {

3. return new A();

4. }

5. }

6. class SubClass extends SuperClass {

7. public B getA() {

8. return new B();

9. }

10. }

Which is true?

A. Compilation will succeed if A extends B.

B. Compilation will succeed if B extends A.

C. Compilation will always fail because of an error in line 7.

D. Compilation will always fail because of an error in line 8.

Answer: B

Question 144

Given:

1. interface A { public void aMethod(); }

2. interface B { public void bMethod(); }

3. interface C extends A,B { public void cMethod(); }

4. class D implements B {

5. public void bMethod() { }

6. }

7. class E extends D implements C {

8. public void aMethod() { }

9. public void bMethod() { }

10. public void cMethod() { }

11. }

What is the result?

A. Compilation fails because of an error in line 3.

B. Compilation fails because of an error in line 7.

C. Compilation fails because of an error in line 9.

D. If you define D e = new E(), then e.bMethod() invokes the version

of bMethod() defined in Line 5.

E. If you define D e = (D)(new E()), then e.bMethod() invokes the

version of bMethod() defined in Line 5.

F. If you define D e = (D)(new E()), then e.bMethod() invokes the

version of bMethod() defined in Line 9.

Answer: F

Question 145

Given:

10. interface A { public int getValue() }

11. class B implements A {

12. public int getValue() { return 1; }

13. }

14. class C extends B {

15. // insert code here

16. }

Which three code fragments, inserted individually at line 15, make use

of polymorphism? (Choose three.)

A. public void add(C c) { c.getValue(); }

B. public void add(B b) { b.getValue(); }

C. public void add(A a) { a.getValue(); }

D. public void add(A a, B b) { a.getValue(); }

E. public void add(C c1, C c2) { c1.getValue(); }

Answer: BCD

Question 146

Given:

1. class ClassA {

2. public int numberOfinstances;

3. protected ClassA(int numberOfinstances) {

4. this.numberOflnstances = numberOfinstances;

5. }

6. }

7. public class ExtendedA extends ClassA {

8. private ExtendedA(int numberOfinstances) {

9. super(numberOflnstances);

10. }

11. public static void main(String[] args) {

12. ExtendedA ext = new ExtendedA(420);

13. System.out.print(ext.numberOflnstances);

14. }

15. }

Which is true?

A. 420 is the output.

B. An exception is thrown at runtime.

C. All constructors must be declared public.

D. Constructors CANNOT use the private modifier.

E. Constructors CANNOT use the protected modifier.

Answer: A

Question 147

Given:

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;}

What is the result?

A. foofoofoofoofoo

B. foobarfoobarbar

C. foobarfoofoofoo

D. foobarfoobarfoo

E. barbarbarbarbar

F. foofoofoobarbar

G. foofoofoobarfoo

Answer: D

Question 148

Which three statements are true? (Choose three.)

A. A final method in class X can be abstract if and only if X is abstract.

B. A protected method in class X can be overridden by any subclass of X.

C. A private static method can be called only within other static methods in class X.

D. A non-static public final method in class X can be overridden in any subclass of X.

E. A public static method in class X can be called by a subclass of X

without explicitly referencing the class X.

F. A method with the same signature as a private final method in class

X can be implemented in a subclass of X.

G. A protected method in class X can be overridden by a subclass of A

only if the subclass is in the same package as X.

Answer: BEF

Question 149

Given:

1. class Pizza {

2. java.util.ArrayList toppings;

3. public final void addTopping(String topping) {

4. toppings.add(topping);

5. }

6. }

7. public class PepperoniPizza extends Pizza {

8. public void addTopping(String topping) {

9. System.out.println(”Cannot add Toppings”);

10. }

11. public static void main(String[] args) {

12. Pizza pizza = new PepperoniPizza();

13. pizza.addTopping(”Mushrooms”);

14. }

15. }

What is the result?

A. Compilation fails.

B. Cannot add Toppings

C. The code runs with no output.

D. A NullPointerException is thrown in Line 4.

Answer: A

Question 150

Given:

1. class Super {

2. private int a;

3. protected Super(int a) { this.a = a; }

4. }

.....

11. class Sub extends Super {

12. public Sub(int a) { super(a); }

13. public Sub() { this.a= 5; }

14. }

Which two, independently, will allow Sub to compile? (Choose two.)

A. Change line 2 to:

public int a;

B. Change line 2 to:

protected int a;

C. Change line 13 to:

public Sub() { this(5); }

D. Change line 13 to:

public Sub() { super(5); }

E. Change line 13 to:

public Sub() { super(a); }

Answer: C

 

Written by :
roselin
 
Trackback(0)
Comments (0)Add Comment

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy
  Page copy protected against web site content infringement by Copyscape

Follow us on Twitter