Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Wednesday, September 2, 2009

Java Quiz -14

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 ClassA {
2. private int data1;
3. public void test1() {
4. int tips = data1;
5. data1 = tips + 1;
6. }
7. public void test2() {
8. for(int data2 = 0; data2 < 5; data2++) {
9. new Thread() {
10. public void test2() {
11. test1();
12. System.out.print(data1 + ", ");
13. } }.start();
14. } }
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ?

A. move the line 12 print statement into the test1() method
B. change line 7 to public synchronized void test2() {
C. change the variable declaration on line 2 to private volatile int data1;
D. wrap the code inside the test1() method with a synchronized( this ) block
E. wrap the for loop code inside the test2() method with a synchronized block synchronized(this) { // for loop
code here }

Answer: A, D

Q: 2 Given:
classA1 and classA2 are public references available to many other threads. classA1 refers to a Thread and classA2 is an
Object. The thread classA1 is currently executing classA2.wait().
From another thread, what provides the most reliable way to ensure that classA1 will stop executing wait()?

A. classA1.notifyAll();
B. classA1.notify();
C. classA2.notify();
D. Object.notify();
E. Thread.notify();
F. classA2.notifyAll();

Answer: F

Q: 3 Given:
1. static void test() throws RuntimeException {
2. try {
3. System.out.print("test ");
4. throw new RuntimeException();
5. }
6. catch (Exception ex) { System.out.print("exception1 "); }
7. }
8. public static void main(String[] args) {
9. try { test(); }
10. catch (RuntimeException ex) { System.out.print("exception2 "); }
11. System.out.print("data ");
12. }
What is the result?

A. test data
B. A Throwable is thrown by main at exception2.
C. test exception2 data
D. test exception data
E. Compilation fails.

Answer: D

Q: 4 Given:
1. Float pi = new Float(10.14f);
2. if (pi > 10) {
3. System.out.print("pi is bigger than 10. ");
4. }
5. else {
6. System.out.print("pi is not bigger than 10. ");
7. }
8. finally {
9. System.out.println("Answer it yourself.");
10. }
What is the result?

A. An exception occurs at runtime.
B. pi is bigger than 10.
C. Compilation fails.
D. pi is bigger than 10. Answer it yourself.
E. pi is not bigger than 10. Answer it yourself.

Answer: C

Q: 5 Given:
1. public static Iterator test(List listdata) {
2. Collections.test(listdata);
3. return listdata.iterator();
4. }
5. public static void main(String[] args) {
6. List listdata = new ArrayList();
7. listdata.add("1"); listdata.add("2"); listdata.add("3");
8. for (Object objdata: test(listdata))
9. System.out.print(objdata + ", ");
10. }
What is the result?

A. Compilation fails.
B. 1, 2, 3,
C. 3, 2, 1,
D. The code runs with no output.
E. An exception is thrown at runtime.

Answer: A

Q: 6 Given:
1. class ClassA {
2. public void process() { System.out.print("ClassA,"); }
3. class ClassB extends ClassA {
4. public void process() throws IOException {
5. super.process();
6. System.out.print("ClassB,");
7. throw new IOException();
8. }
9. public static void main(String[] args) {
10. try { new ClassB().process(); }
11. catch (IOException e) { System.out.println("Exception"); }
What is the result?

A. Exception
B. ClassA,ClassB,Exception
C. Compilation fails because of an error in line 10.
D. Compilation fails because of an error in line 4.
E. ClassA NullPointerException is thrown at runtime.

Answer: D

Q: 7 Given:
1. try {
2. // some code here
3. } catch (NullPointerException e1) {
4. System.out.print("exception1");
5. } catch (RuntimeException e2) {
6. System.out.print("exception2");
7. } finally {
8. System.out.print("exception3");
9. }
What is the result if exception1 NullPointerException occurs on line 34?

A. exception3
B. exception1
C. exception1exception2
D. exception1exception3
E. exception2exception3
F. exception1exception2exception3

Answer: D

Q: 8 Given:
1. public static Collection get() {
2. Collection sorted = new LinkedList();
3. sorted.add("data2"); sorted.add("data3"); sorted.add("data1");
4. return sorted;
5. }
6. public static void main(String[] args) {
7. for (Object obj: get()) {
8. System.out.print(obj + ", ");
9. }
10. }
What is the result?

A. data1, data2, data3,
B. data2, data3, data1,
C. An exception is thrown at runtime.
D. Compilation fails.
E. The code runs with no output.

Answer: B

Q: 9 Given:
1. int z = 5;
2.
3. public void stuff1(int x) {
4. assert (x > 0);
5. switch(x) {
6. case 2: x = 3;
7. default: assert false; } }
8.
9. private void stuff2(int y) { assert (y < 0); }
10.
11. private void stuff3() { assert (stuff4()); }
12.
13. private boolean stuff4() { z = 6; return false; }
Which statement is true?

A. Only the assert statement on line 9 is used appropriately.
B. All of the assert statements are used appropriately.
C. The assert statements on lines 7 and 9 are used appropriately.
D. The assert statements on lines 4 and 7 are used appropriately.
E. The assert statements on lines 7 and 11 are used appropriately.
F. The assert statements on lines 7, 9, and 11 are used appropriately.
G. The assert statements on lines 4, 7, and 9 are used appropriately.

Answer: C

Q: 10 Given:
1. public class Test {
2. public static void main(String [] args) {
3. int data1 = 5;
4. boolean data2 = true;
5. boolean data3 = false;
6.
7. if ((data1 == 4) && !data3 )
8. System.out.print("test1 ");
9. System.out.print("test2 ");
10. if ((data3 = true) && data2 )
11. System.out.print("test3 ");
12. }
13. }
What is the result?

A. test2
B. test3
C. test1 test2
D. test2 test3
E. test1 test2 test3
F. An exception is thrown at runtime.
G. Compilation fails.

Answer: D

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! 

No comments: