Java Practice Test
Java Practice Test
Java Practice Test
Given:
3. class Foo {
4. public int i = 3;
5. }
8. System.out.println(“i = “ + foo.i);
9. }
10. }
A. i = 3
B. Compilation fails.
Answer: A
QUESTION NO: 2
Answer: A, B
The F. statement is incorrect. The float numbers should be separated with commas and not
dots.
12. do {
14. continue;
15. }
A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 5
D. i = 5 and j = 6
E. i = 6 and j = 6
Given:
1. class Test {
2. private Demo d;
3. void start() {
4. d = new Demo();
5. this.takeDemo(d);
6. }
7.
9. demo = null;
11. }
12. }
When is the Demo object, created on line 3, eligible for garbage collection?
A. After line 5.
B. After line 9.
E. When the instance running this code is made eligible for garbage collection.
Answer: E
QUESTION NO: 5
Given:
1. interface Animal {
2. void soundOff();
3. }
4.
7. System.out.println(“Trumpet”);
8. }
9. }
10.
13. System.out.println(“Roar”);
14. }
15. }
16.
21. } else {
23. }
24. }
25. }
Which compiles?
A. new Animal().soundOff();
D. new Alpha1().get(“veggie”).soundOff();
Answer: D
QUESTION NO: 6
B. Objects are not collected if they are accessible from live threads.
D. Objects that have finalize() methods always have their finalize() methods called before
Answer: B
QUESTION NO: 7
Given:
1. class A {
2. A() { }
3. }
4.
5. class B extends A {
6. }
QUESTION NO: 8
Given:
12. do {
13. if(i>j) {
14. break;
15. }
16. j--;
A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 4
D. i = 5 and j = 6
E. i = 6 and j = 6
Answer: D
QUESTION NO: 9
an assertion fails.
Answer: A
QUESTION NO: 10
You want a class to have access to members of another class in the same package. Which
A. public
B. private
C. protected
D. transient
E. default access
Answer: E
QUESTION NO: 11
Given:
11. int x = 3;
12. int y = 1;
13. if (x = y) {
15. }
B. x = 3
C. Compilation fails.
Answer: C
QUESTION NO: 12
Given:
3. try {
5. } finally {
6. System.out.println(“finally”);
7. }
8. }
10. try {
11. aMethod();
13. System.out.println(“exception”);
14. }
15. System.out.println(“finished”);
16. }
17. }
A. finally
B. exception
finished
C. finally
exception
finished
D. Compilation fails.
Answer: C
QUESTION NO: 13
Given:
2. int k = 4;
3. }
A. final int k = 4;
B. public int k = 4;
C. static int k = 4;
D. abstract int k = 4;
E. volatile int k = 4;
F. protected int k = 4;
Answer: A, B, C
QUESTION NO: 14
Given:
1. package test1;
4. }
1. package test2;
4. System.out.println(“x = “ + x);
5. }
6. }
A. x = 0
B. x = 42
Answer: C
QUESTION NO: 15
Given:
1. class A {
3. }
Which two are valid in a class that extends class A? (Choose two)
Answer: A, C
QUESTION NO: 16
Given:
3. System.out.print(c);
4. return true;
5. }
7. int i =0;
9. i++ ;
10. foo(‘D’);
12. }
13. }
14. }
A. ABDCBDCB
B. ABCDABCD
C. Compilation fails.
Answer: A