Thursday, December 22, 2016

JAVA Interview Question 31-40

Q: 31 Given:
15. public class Yippee {
16. public static void main(String[] args) {
17. for (int x = 1; x < args.length; x++) {
18. System.out.print(args[x] + “ “);
19. }
20. }
21. }
and two separate command line invocations:
java Yippee
java Yippee 1 2 3 4

What is the result?
A. No output is produced.
1 2 3
B. No output is produced.
2 3 4
C. No output is produced.
1 2 3 4
D. An exception is thrown at runtime.
1 2 3
E. An exception is thrown at runtime.
2 3 4
F. An exception is thrown at runtime.
1 2 3 4

Q. 32 Given:

10. class Nav{
11. public enum Direction {NORTH, SOUTH, EAST, WEST}
12. }
13. public class Sprite {
14. // insert code here
15. }

Which code, inserted at line 14, allows the Sprite class to compile?
A. Direction d = NORTH;
B. Nav. Direction d = NORTH;
C. Direction d = Direction.NORTH;
D. Nav.Direction d = Nav.Direction.NORTH;

Q: 33 Given:
11. public class Ball{
12. public enum Color {RED, GREEN, BLUE};
13. public void fool(){
14. // insert code here
15. { System.out.println(c);}
16. }
17. }
Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?
A. for(Color c : Color.values() }
B. for(Color c = RED; c <= BLUE; c++)
C. for(Color c; c.hasNext(); c.next() )
D. for(Color c = Color[0]; c <= Color[2]; c++)
E. for(Color C = Color.RED; c <= Color.BLUE; c++)

Q. 34 Given:
3. public class Spock{
4. public static void main(String[] args){
5. Long tail = 2000L
6. Long distance = 1999L
7. Long story = 1000L
8. if((tail > distance) ^ ((story * 2) == tail))
9. System.out.print(“1”);
10. if((distance + 1 != tail) ^ ((story * 2) == distance))
11. System.out.print(“2”) ;
12. }
13. }
What is the result?
A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.

Q. 35 Given:
25. int x = 12;
26. while (x<10) {
27. x--;
28. }
29. System.out.print(x);
What is the result?
A. 0
B. 10
C. 12
D. Line 29 will never be reached.

Q. 36 Given:
11. static void test() throws Error {
12. if (true) throw new AssertionError ();
13. System.out.print(“test”);
14. }
15. public static void main(String[] args) {
16. try(test); }
17. catch (Exception ex} {System.out.print(“exception”); }
18. System.out.print(“end”);
19. }
What is the result?
A. end
B. Compilation fails.
C. exception end.
D. exception test end
E. A Throwable is thrown by main.
F. An Exception is thrown by main.

Q. 37 Given:
33. try {
34. // some cade here
35. } catch (NullPointerException e1) {
36. System.out.print(“a”);
37. } catch (RuntimeException e2) {
38. System.out.print(“b”);
39. } finally {
40. System.out.print(“c”);
41. }
What is the result if a NullPointerException occurs on line 34?
A. c
B. a
C. ab
D. ac
E. bc
F. abc

Q. 38
Click the Exhibit Button.
1. public class Test {
2.
3. public static void main( String[] args) {
4. Boolean assert = true;
5. if(assert) {
6. System.out.println(“assert is true”);
7. }
8. }
9.
10. }
Given:
Javac –source 1.3 Test.java

What is the result?
A. Compilation fails.
B. Compilatin Succeeds with errors.
C. Compilation succeeds with warnings.
D. Compilation succeeds without warnings or errors.

Q. 39 Given:
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; I<10; i++) {
14. int value = i * ((int) Math.random());
15. integer intObj = new Integer(Value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }

Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?
A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.

Q. 40 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 statement 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


No comments:

Post a Comment