Você está na página 1de 5

Q3.

Given the following code:

import java.util.Date;

public class Example {

public static void main(String args[]) {

Date d1 = new Date (99, 11, 31);

Date d2 = new Date (99, 11, 31);

method(d1, d2);

System.out.println("d1 is " + d1

+ "\nd2 is " + d2);

public static void method(Date d1, Date d2) {

d2.setYear (100);

d1 = d2;

Which one or more of the following correctly describe the behavior when this
program is compiled and run?

a) compilation is successful and the output is:

d1 is Fri December 31 00:00:00 GMT 1999

d2 is Fri December 31 00:00:00 GMT 1999

b) compilation is successful and the output is:

d1 is Fri December 31 00:00:00 GMT 1999

d2 is Sun December 31 00:00:00 GMT 2000

c) compilation is successful and the output is:

d1 is Sun December 31 00:00:00 GMT 2000

d2 is Sun December 31 00:00:00 GMT 2000

d) the assignment 'd1 = d2' is rejected by the compiler because the Date class cannot overload
the operator '='.

e) the expression (d1 is " + d1 + "\nd2 is " + d2) is rejected by the compiler because the Date
class cannot overload the operator '+'.

Correct Answer: B

Explanation:

a) is false because the data in d2 was changed in method.

c) is false because the data in d1 was not changed

d) is false as code is perfectly legal, Object d1 is set to be the same as d2. This is a change of the
actual reference, not in the data at d1.

e) is false, because Java is smart enough to call the method toString() for any object that is used in
a String context. toString() is defined by the Object class and so it is available on all classes in Java.
Most non-trivial classes override toString() to return more explicit information about themselves.

Q4. Consider these classes, defined in separate source files,

public class Test1{

public float aMethod(float a, float b) throws IOException{

}
1. public class Test2 extends Test1{
2.
3. }

Which of the following methods would be legal at line 2 in class Test2?

A. float aMethod(float a, float b){}

B. public int aMethod(int a, int b) throws Exception{ }

C. public float aMethod(float a, float b) throws Exception{ }

D. public float aMethod(float p, float q){ }

Correct Answer: B,D


Explanation:

B and D are correct. B is legal as it is an example of method overloading.

A is illegal because it is less accessible than the original method, because method in
Test1 is public. And for any overriding method, accessibility must not be more
restricted than the original method.

C is illegal because for overriding method, it must not throw checked exception of
classess that are not possible for the origincal classes.

Q8. A monitor called mon has 5 threads in its waiting pool; all these waiting threads have the same
priority. One of the threads is thread1. How can you notify thread1 so that it alone moves from
Waiting state to Ready State?

A. Execute notify(thread1); from within synchronized code of mon.


B. Execute mon.notify(thread1); from synchronized code of any object.
C. Execute thread1.notify(); from synchronized code of any object.
D. Execute thread1.notify(); from any code(synchronized or not) of any object.
E. You cannot specify which thread will get notified.
Correct Answer: E

Explanation:

E is correct, when you call notify() on a monitor, you have no control over which waiting thread gets notified.

Q9. What happens when the following code is compiled and run. Select the one correct
answer.

for(int i = 1; i < 3; i++)


for(int j = 3; j >= 1; j--)
assert i!=j : i;
A. The class compiles and runs, but does not print anything.

B. The number 1 gets printed with AssertionError

C. The number 2 gets printed with AssertionError

D. The number 3 gets printed with AssertionError

E. The program generates a compilation error.


Correct Answer: B

Explanation:

correct answer is B. When i and j are both 1, assert condition is false, and AssertionError gets generated.

Você também pode gostar