Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Tuesday, August 25, 2009

Java Quiz -10

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

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

A. StringBuffer s = new StringBuffer("123456789");
s.substring(3,6).delete(1,3).insert(1, "24");
B. StringBuffer s = new StringBuffer("123456789");
s.delete(0,3).replace(1,3,"24").delete(4,6);
C. String s = "123456789";
s = (s-"123").replace(1,3,"24") - "89";
D. StringBuilder s = new StringBuilder("123456789");
s.substring(3,6).delete(1,2).insert(1, "24");
E. StringBuilder s = new StringBuilder("123456789");
s.delete(0,3).delete(1,3).delete(2,5).insert(1, "24");

Answer: B, E

Q: 2 When comparing java.io.BufferedWriter to java.io.FileWriter, which
capability exists as a method in only one of the two?

A. writing a line separator to the stream
B. writing to the stream
C. marking a location in the stream
D. flushing the stream
E. closing the stream

Answer: A

Q: 3 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 Dec 14, 2000.
B. The value of s is 14-dec-2004.
C. Compilation fails because of an error in line 13.
D. An exception is thrown at runtime.

Answer: C

Q: 4 Which three statements concerning the use of the java.io.Serializable
interface are true? (Choose three.)

A. An object serialized on one JVM can be successfully deserialized on a different JVM.
B. The values in fields with the transient modifier will NOT survive serialization and deserialization.
C. It is legal to serialize an object of a type that has a supertype that does NOT implement java.io.Serializable.
D. Objects from classes that use aggregation cannot be serialized.
E. The values in fields with the volatile modifier will NOT survive serialization and deserialization.

Answer: A, B, C

Q: 5 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. 10
B. 2
C. 5
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: D

Q: 6 Given:
d is a valid, non-null Date object
df is a valid, non-null DateFormat object set to the current locale
What outputs the current locale's country name and the appropriate version of d's date?

A. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
B. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
C. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
D. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));

Answer: A

Q: 7 Given classes defined in two different files:
1. package util;
2. public class ClassA {
3. private static void process(byte[] b) {}
4. }
1. package app;
2. public class ClassB {
3. public static void main(String[] args) {
4. byte[] byteobj = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class SomeApp to use the process method of ClassB?

A. process(byteobj);
B. ClassB.process(byteobj);
C. app.ClassB.process(byteobj);
D. util.ClassB.process(byteobj);
E. import util.ClassB.*; process(byteobj);
F. SomeApp cannot use the process method in ClassB.

Answer: F

Q: 8 Given:
1. rbo = new ReallyBigObject();
2. // more code here
3. rbo = null;
4. /* insert code here */
Which statement should be placed at line 4 to suggest that the virtual machine expend effort toward recycling the memory used by the object rbo?

A. Runtime.getRuntime().growHeap();
B. System.freeMemory();
C. Runtime.getRuntime().freeMemory();
D. System.gc();
E. Runtime.gc();

Answer: D

Q: 9 Given classes defined in two different files:
1. package util;
2. public class ClassB {
3. public static void process(byte[]) { /* more code here */ }
4. }
1. package app;
2. public class ClassA {
3. public static void main(String[] args) {
4. byte[] byteobj = new byte[256];
5. // insert code here
6. }
7. }
What is required at line 5 in class ClassA to use the process method of ClassB?

A. ClassB.process(byteobj);
B. util.ClassB.process(byteobj);
C. process(byteobj);
D. import util.ClassB.*; process(byteobj);
E. SomeApp cannot use methods in ClassB.

Answer: B

Q: 10 Given:
11. public static void test(String string) {
12. int value = 4;
13. if ( value = string.length()) {
14. System.out.print(string .charAt( value -= 1) +", ");
15. } else {
16. System.out.print(string .charAt(0) + ", ");
17. }
18. }
and the invocation:
21. test("four");
22. test("tee");
23. test("to");
What is the result?

A. r, e, o,
B. r, t, t,
C. Compilation fails.
D. An exception is thrown at runtime.

Answer: C

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!

2 comments:

swati said...

Please explain why ans of question no 5 is option D compilation fails.

Q: 5 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. 10
B. 2
C. 5
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: D

Chandrasekar said...

Invalid escape sequence!

Valid ones are
\b \t \n \f \r \" \' \\

\s is invalid!

Hope u got the answer! Sorry for the delay! Cheers! :)