0% found this document useful (0 votes)
64 views

Java Questions Answer

Uploaded by

ar9954010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Java Questions Answer

Uploaded by

ar9954010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

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. }

Which are true? (Choose all that apply.)


A. Compilation fails.
B. The output will include "+"
C. The output will include "10001"
D. The output will include "0000010001"
E. The output will include today’s date.
F. The output will include the number of milliseconds from January 1,
1970 until today.

Ans: A

2. Given:

1. public class WeatherTest {


2. static Weather w;
3. public static void main(String[] args) {
4. System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
5. }
6. }
7. enum Weather {
8. RAINY, Sunny;
9. int count = 0;
10. Weather() {
11. System.out.print("c ");
12. count++;
13. }
14. }
What is the result?
A. c 1 c 1
B. c 1 c 2
C. c c 1 1
D. c c 1 2
E. c c 2 2
F. Compilation fails.
G. An exception is thrown at runtime.

Ans: C
3 Given:

2. public class Bunnies {


3. static int count = 0;
4. Bunnies() {
5. while(count < 10) new Bunnies(++count);
6. }
7. Bunnies(int x) { super(); }
8. public static void main(String[] args) {
9. new Bunnies();
10. new Bunnies(count);
11. System.out.println(count++);
12. }
13. }
What is the result?
A. 9
B. 10
C. 11
D. 12
E. Compilation fails.
F. An exception is thrown at runtime.

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. }

Which are true? (Choose all that apply.)


A. Compilation succeeds.
B. The output is "throw away key 4".
C. The output is "throw away key 6".
D. Compilation fails due to an error on line 5.
E. Compilation fails due to an error on line 6.
F. Compilation fails due to an error on line 9.

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. }

Which, inserted independently at line 7, compile? (Choose all that


apply.)

A. if(b instanceof ff) System.out.print("1 ");


B. if(b.instanceof(ff)) System.out.print("2 ");
C. if(b instanceof Feline) System.out.print("3 ");
D. if(b instanceOf Feline) System.out.print("4 ");
E. if(b.instanceof(Feline)) System.out.print("5 ");

Ans: C, D

6. Given:

2. public class Choosy {


3. public static void main(String[] args) {
4. String result = "";
5. int x = 7, y = 8;
6. if(x == 3) { result += "1"; }
7. else if (x > 9) { result += "2"; }
8. else if (y < 9) { result += "3"; }
9. else if (x == 7) { result += "4"; }
10. else { result += "5"; }
11. System.out.println(result);
12. }
13. }
What is the result? (Choose all that apply.)
A. 3
B. 34
C. 35
D. 345
E. Compilation fails due to an error on line 5.
F. Compilation fails due to errors on lines 8 and 9.
G. Compilation fails due to errors on lines 7, 8, and 9.
Ans: A

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

8. Which are true? (Choose all that apply.)


A. All classes of Exception extend Error.
B. All classes of Error extend Exception.
C. All Errors must be handled or declared.
D. All classes of Exception extend Throwable.
E. All Throwables must be handled or declared.
F. All Exceptions must be handled or declared.
G. RuntimeExceptions need never be handled or declared.
Ans: D, F, G

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

10. Which statement(s) are true? (Choose all that apply.)


A. Coupling is the OO principle most closely associated with hiding a
class’s implementation details.
B. Coupling is the OO principle most closely associated with making sure
classes know about other classes only through their APIs.
C. Coupling is the OO principle most closely associated with making sure
a class is designed with a single, well-focused purpose.
D. Coupling is the OO principle most closely associated with allowing a
single object to be seen as having many types.
Ans: B

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. }

What is the result?


A. -sb i s2
B. -sb s2 i
C. -s2 i sb
D. -s2 sb i
E. Compilation fails.
F. An exception is thrown at runtime.
Ans: E

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

What is the result?


A. x
B. x x2
C. An Error is thrown at runtime.
D. Compilation fails due to an error on line 3.
E. Compilation fails due to an error on line 8.
F. Compilation fails due to an error on line 9.
Ans: A

18. Given the proper import statements and:


23. try {
24. File file = new File("myFile.txt");
25. PrintWriter pw = new PrintWriter(file);
26. pw.println("line 1");
27. pw.close();
28. PrintWriter pw2 = new PrintWriter("myFile.txt");
29. pw2.println("line 2");
30. pw2.close();
31. } catch (IOException e) { }

What is the result? (Choose all that apply.)


A. No file is created.
B. A file named "myFile.txt" is created.
C. Compilation fails due to an error on line 24.
D. Compilation fails due to an error on line 28.
E. "myFile.txt" contains only one line of data, "line 1"
F. "myFile.txt" contains only one line of data, "line 2"
G. "myFile.txt" contains two lines of data, "line 1" then "line 2"
Ans: B, F

19. Given this code in a method:


3. String s = "-";
4. boolean b = false;
5. int x = 7, y = 8;
6. if((x < 8) ^ (b = true)) s += "^";
7. if(!(x > 8) | ++y > 5) s += "|";
8. if(++y > 9 && b == true) s += "&&";
9. if(y % 8 > 1 || y / (x - 7) > 1) s += "%";
10. System.out.println(s);
What is the result?
A. -
B. -|%
C. -^|%
D. -|&&%
E. -^|&&%
F. Compilation fails.
G. An exception is thrown at runtime.
Ans: E

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. } }

What is the result?


A. 2 3 4 5
B. 2 7 4 9
C. 6 3 8 4
D. 6 7 8 9
E. Compilation fails due to multiple errors.
F. Compilation fails due only to an error on line 11.
G. Compilation fails due only to an error on line 14.
Ans: A

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

What is the result?


A. First: f1, then: x t1
B. First: f1, then: x t1 f1
C. First: c1, then: x t1
D. First: c1, then: x t1 f1
E. First: c1 f1, then: x t1
F. First: c1 f1, then: x t1 f1
G. Compilation fails.
Ans: F

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 + " ");

What is the result?


A. 4 x4
B. 4 x4 .
C. 4 x4 3
D. 4 5x4 a 3 4 x4
E. 4.5x4.a.3 4 x4
F. 4.5x4.a.3 4 x4 .
Ans: E

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. }

What is the result?


A. Compilation fails.
B. over over poly poly
C. over super poly poly
D. super super poly poly
E. An exception is thrown at runtime.
Ans: A

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. }

What is the result? (Choose all that apply.)


A. 22 42 42
B. 22 63 63
C. 33 42 42
D. 33 63 42
E. Compilation fails.
F. An exception is thrown at runtime.
Ans: D
27. Given:
3. public class Chopper {
4. String a = "12b";
5. public static void main(String[] args) {
6. System.out.println(new Chopper().chop(args[0]));
7. }
8. int chop(String a) {
9. if(a == null) throw new IllegalArgumentException();
10. return Integer.parseInt(a);
11. } }
And, if the code compiles, the invocation:
java Chopper
What is the result?
A. 12
B. 12b
C. Compilation fails.
D. A NullPointerException is thrown.
E. A NumberFormatException is thrown.
F. An IllegalArgumentException is thrown.
G. An ArrayIndexOutOfBoundsException is thrown.
Ans: F

28. Given that:


Exception is the superclass of IOException, and
IOException is the superclass of FileNotFoundException, and
3. import java.io.*;
4. class Physicist {
5. void think() throws IOException { }
6. }
7. public class Feynman extends Physicist {
8. public static void main(String[] args) {
9. new Feynman().think();
10. }
11. // insert method here
12. }

Which of the following methods, inserted independently at line 11,


compiles? (Choose all that apply.)
A. void think() throws Exception { }
B. void think() throws FileNotFoundException { }
C. public void think() { }
D. protected void think() throws IOException { }
E. private void think() throws IOException { }
F. void think() { int x = 7/0; }
Ans: A

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. }

Which set(s) of code, inserted independently at line 3, produce the


output "Homer Bart"?(Choose all that apply.)
A. Kant() { this("Bart"); }
Kant(String s) { super(s); }
B. Kant() { super("Bart"); }
Kant(String s) { super(s); }
C. Kant() { super(); }
Kant(String s) { super(s); }
D. Kant() { super("Bart"); }
Kant(String s) { this(); }
E. Kant() { super("Bart"); }
Kant(String s) { this("Homer"); }
Ans: A, B

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. }

9. static Kaput go(Kaput k) {


10. final Kaput k1 = new Kaput();
11. k.myK = k1;
12. k.myK.degree = "7";
13. return k.myK;
14. } }
What is the result?
A. 0
B. 7
C. null
D. Compilation fails.
E. An exception is thrown at runtime.

Ans: 7

You might also like