Java Questions Answer
Java Questions Answer
Given:
1. import java.io.*;
2. import java.util.*;
3. import static java.lang.Short.*;
4. import static java.lang.Long.*;
5. public class MathBoy {
6. public static void main(String[] args) {
7. long x = 123456789;
8. short y = 22766; // maximum value of a short is 32767
9. System.out.printf("%1$+10d %2$010d ", x, MAX_VALUE - y);
10. System.out.println(new Date());
11. }
12. }
Ans: A
2. Given:
Ans: C
3 Given:
Ans: B
4 Given:
2. public class Jail {
3. private int x = 4;
4. public static void main(String[] args) {
5. protected int x = 6;
6. new Jail().new Cell().slam();
7. }
8. class Cell {
9. void slam() { System.out.println("throw away key " + x); }
10. }
11. }
Ans: D
5. Given:
2. class Feline { }
3. public class BarnCat2 extends Feline {
4. public static void main(String[] args) {
5. Feline ff = new Feline();
6. BarnCat2 b = new BarnCat2();
7. // insert code here
8. }
9. }
Ans: C, D
6. Given:
7. Given:
1. public class Twine {
2. public static void main(String[] args) {
3. String s = "";
4. StringBuffer sb1 = new StringBuffer("hi");
5. StringBuffer sb2 = new StringBuffer("hi");
6. StringBuffer sb3 = new StringBuffer(sb2);
7. StringBuffer sb4 = sb3;
8. if(sb1.equals(sb2)) s += "1 ";
9. if(sb2.equals(sb3)) s += "2 ";
10. if(sb3.equals(sb4)) s += "3 ";
11. String s2 = "hi";
12. String s3 = "hi";
13. String s4 = s3;
14. if(s2.equals(s3)) s += "4 ";
15. if(s3.equals(s4)) s += "5 ";
16. System.out.println(s);
17. }
18. }
What is the result?
A. 1 3
B. 1 5
C. 1 2 3
D. 1 4 5
E. 3 4 5
F. 1 3 4 5
G. 1 2 3 4 5
H. Compilation fails.
Ans: E
9. Given:
3. public class RediMix extends Concrete {
4. RediMix() { System.out.println("r "); }
5. public static void main(String[] args) {
6. new RediMix();
7. }
8. }
9. class Concrete extends Sand {
10. Concrete() { System.out.print("c "); }
11. private Concrete(String s) { }
12. }
13. abstract class Sand {
14. Sand() { System.out.print("s "); }
15. }
What is the result?
A. r
B. c r
C. r c
D. s c r
E. r c s
F. Compilation fails due to a single error in the code.
G. Compilation fails due to multiple errors in the code.
Ans: D
11. Given:
37. boolean b = false;
38. int i = 7;
39. double d = 1.23;
40. float f = 4.56f;
41.
42. // insert code here
Which line(s) of code, inserted independently at line 42, will compile
and run without exception? (Choose all that apply.)
A. System.out.printf(" %b", b);
B. System.out.printf(" %i", i);
C. System.out.format(" %d", d);
D. System.out.format(" %d", i);
E. System.out.format(" %f", f);
Ans: A, D , E
12. Given:
2. class Game {
3. static String s = "-";
4. String s2 = "s2";
5. Game(String arg) { s += arg; }
6. }
7. public class Go extends Game {
8. Go() { super(s2); }
9. { s += "i "; }
10. public static void main(String[] args) {
11. new Go();
12. System.out.println(s);
13. }
14. static { s += "sb "; }
15. }
13. Given:
4. public static void main(String[] args) {
5. try {
6. if(args.length == 0) throw new Exception();
7. }
8. catch (Exception e) {
9. System.out.print("done ");
10. doStuff(); // assume this method compiles
11. }
12. finally {
13. System.out.println("finally ");
14. }
15. }
Which are possible outputs? (Choose all that apply.)
A. "done "
B. "finally "
C. "done finally "
D. Compilation fails.
E. No output is produced.
Ans: C
14. Concerning Java’s Garbage Collector (GC), which are true? (Choose
all that apply.)
A. If Object X has a reference to Object Y, then Object Y cannot be
GCed.
B. A Java program can request that the GC runs, but such a request does
NOT guarantee that the GC will actually run.
C. If the GC decides to delete an object, and if finalize() has never
been invoked for that object, it is guaranteed that the GC will invoke
finalize() for that object before the object is deleted.
D. Once the GC invokes finalize() on an object, it is guaranteed that
the GC will delete that object once finalize() has completed.
E. When the GC runs, it decides whether to remove objects from the heap,
the stack, or both.
Ans: B, C
15. Given:
1. public class BackHanded {
2. int state = 0;
3. BackHanded(int s) { state = s; }
4. public static void main(String... hi) {
5. BackHanded b1 = new BackHanded(1);
6. BackHanded b2 = new BackHanded(2);
7. System.out.println(b1.go(b1) + " " + b2.go(b2));
8. }
9. int go(BackHanded b) {
10. if(this.state == 2) {
11. b.state = 5;
12. go(this);
13. }
14. return ++this.state;
15. } }
What is the result?
A. 1 2
B. 1 3
C. 1 6
D. 1 7
E. 2 6
F. 2 7
G. Compilation fails.
H. An exception is thrown at runtime.
Ans: F
16. Given:
3. class Sport {
4. Sport play() { System.out.print("play "); return new Sport(); }
5. Sport play(int x) { System.out.print("play x "); return new
Sport(); }
6. }
7. class Baseball extends Sport {
8. Baseball play() { System.out.print("baseball "); return new
Baseball(); }
9. Sport play(int x) { System.out.print("sport "); return new
Sport(); }
10.
11. public static void main(String[] args) {
12. new Baseball().play();
13. new Baseball().play(7);
14. super.play(7);
15. new Sport().play();
16. Sport s = new Baseball();
17. s.play();
18. } }
What is the result?
A. baseball sport sport play play
B. baseball sport play x play sport
C. baseball sport play x play baseball
D. Compilation fails due to a single error.
E. Compilation fails due to errors on more than one line.
Ans: D
17. Given:
3. class Stereo { void makeNoise() { assert true; } }
4. public class BoomBox2 extends Stereo {
5. public static void main(String[] args) {
6. new BoomBox2().go(args);
7. }
8. void go(String[] args) {
9. if(args.length > 0) makeNoise();
10. if(args[0].equals("x")) System.out.print("x ");
11. if(args[0] == "x") System.out.println("x2 ");
12. } }
And (if the code compiles), the invocation:
java -ea Boombox2 x
20. Given:
3. public class Limits {
4. private int x = 2;
5. protected int y = 3;
6. private static int m1 = 4;
7. protected static int m2 = 5;
8. public static void main(String[] args) {
9. int x = 6; int y = 7;
10. int m1 = 8; int m2 = 9;
11. new Limits().new Secret().go();
12. }
13. class Secret {
14. void go() { System.out.println(x + " " + y + " " + m1 + " " +
m2); }
15. } }
21. Given:
2. abstract interface Pixie {
3. abstract void sprinkle();
4. static int dust = 3;
5. }
6. abstract class TinkerBell implements Pixie {
7. String fly() { return "flying "; }
8. }
9. public class ForReal extends TinkerBell {
10. public static void main(String[] args) {
11. new ForReal().sprinkle();
12. }
13. public void sprinkle() { System.out.println(fly() + " " +
dust); }
14. }
What is the result? (Choose all that apply.)
A. flying 3
B. Compilation fails because TinkerBell doesn’t properly implement
Pixie.
C. Compilation fails because ForReal doesn’t properly extend TinkerBell.
D. Compilation fails because Pixie is not a legal interface.
E. Compilation fails because ForReal doesn’t properly implement Pixie.
F. Compilation fails because TinkerBell is not a legal abstract class.
Ans: A
22. Given:
2. public class Errrrr {
3. static String a = null;
4. static String s = "";
5. public static void main(String[] args) {
6. try {
7. a = args[0];
8. System.out.print(a);
9. s += "t1 ";
10. }
11. catch (RuntimeException re) { s += "c1 "; }
12. finally { s += "f1 "; }
13. System.out.println(" " + s);
14. } }
And two command-line invocations:
java Errrrr
java Errrrr x
23. Given:
51. String s = "4.5x4.a.3";
52. String[] tokens = s.split("\\s");
53. for(String o: tokens)
54. System.out.print(o + " ");
55.
56. System.out.print(" ");
57. tokens = s.split("\\..");
58. for(String o: tokens)
59. System.out.print(o + " ");
24. Given:
2. abstract class Tool {
3. int SKU;
4. abstract void getSKU();
5. }
6. public class Hammer {
7. // insert code here
8. }
Which line(s), inserted independently at line 7, will compile? (Choose
all that apply.)
A. void getSKU() { ; }
B. private void getSKU() { ; }
C. protected void getSKU() { ; }
D. public void getSKU() { ; }
Ans: A, C, D
25. Given:
2. class Super {
3. static String os = "";
4. void doStuff() { os += "super "; }
5. }
6. public class PolyTest extends Super {
7. public static void main(String[] args) { new PolyTest().go(); }
8. void go() {
9. Super s = new PolyTest();
10. PolyTest p = (PolyTest)s;
11. p.doStuff();
12. s.doStuff();
13. p.doPoly();
14. s.doPoly();
15. System.out.println(os);
16. }
17. void doStuff() { os += "over "; }
18. void doPoly() { os += "poly "; }
19. }
26. Given:
3. class MotorVehicle {
4. protected int doStuff(int x) { return x * 2; }
5. }
6. class Bicycle {
7. void go(MotorVehicle m) {
8. System.out.print(m.doStuff(21) + " ");
9. } }
10. public class Beemer extends MotorVehicle {
11. public static void main(String[] args) {
12. System.out.print(new Beemer().doStuff(11) + " ");
13. new Bicycle().go(new Beemer());
14. new Bicycle().go(new MotorVehicle());
15. }
16. int doStuff(int x) { return x * 3; }
17. }
29. Given:
2. public class Kant extends Philosopher {
3. // insert code here
5. public static void main(String[] args) {
6. new Kant("Homer");
7. new Kant();
8. }
9. }
10. class Philosopher {
11. Philosopher(String s) { System.out.print(s + " "); }
12. }
30. Given
1. public class Kaput {
2. Kaput myK;
3. String degree = "0";
4. public static void main(String[] args) {
5. Kaput k1 = new Kaput();
6. go(k1);
7. System.out.println(k1.degree);
8. }
Ans: 7