Core Java Full Mock Test1

35 Questions | Total Attempts: 72

SettingsSettingsSettings
Core Java Quizzes & Trivia

Questions and Answers
  • 1. 
    Which statement is true?
    • A. 

      A class's finalize() method CANNOT be invoked explicitly.

    • B. 

      Super.finalize() is called implicitly by any overriding finalize() method.

    • C. 

      The finalize() method for a given object is called no more than once by the garbage collector.

    • D. 

      The order in which finalize() is called on two objects is based on the order in which the two objects became finalizable.

  • 2. 
    Given:   11. class Snoochy {   12. Boochy booch;   13. public Snoochy() { booch = new Boochy(this); }   14. }   15.   16. class Boochy {   17. Snoochy snooch;   18. public Boochy(Snoochy s) { snooch = s; }   19. }   And the statements:   21. public static void main(String[] args) {   22. Snoochy snoog = new Snoochy();   23. snoog = null;   24. // more code here   25. }   Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line 23 executes?
    • A. 

      None of these objects are eligible for garbage collection.

    • B. 

      Only the object referenced by booch is eligible for garbage collection.

    • C. 

      Only the object referenced by snoog is eligible for garbage collection.

    • D. 

      Only the object referenced by snooch is eligible for garbage collection.

    • E. 

      The objects referenced by snooch and booch are eligible for garbage collection.

  • 3. 
    Given:   3. interface Animal { void makeNoise(); }   4. class Horse implements Animal {   5. Long weight = 1200L;   6. public void makeNoise() { System.out.println("whinny"); }   7. }   8. public class Icelandic extends Horse {   9. public void makeNoise() { System.out.println("vinny"); }   10. public static void main(String[] args) {   11. Icelandic i1 = new Icelandic();   12. Icelandic i2 = new Icelandic();   13. Icelandic i3 = new Icelandic();   14. i3 = i1; i1 = i2; i2 = null; i3 = i1;   15. }   16. }   When line 15 is reached, how many objects are eligible for the garbage collector?  
    • A. 

      0

    • B. 

      1

    • C. 

      4

    • D. 

      6

  • 4. 
    Given:   5. class Payload {   6. private int weight;   7. public Payload (int w) { weight = w; }   8. public void setWeight(int w) { weight = w; }   9. public String toString() { return Integer.toString(weight); }   10. }   11. public class TestPayload {   12. static void changePayload(Payload p) { /* insert code */ }   13. public static void main(String[] args) {   14. Payload p = new Payload(200);   15. p.setWeight(1024);   16. changePayload(p);   17. System.out.println("p is " + p);   18. } }   Which code fragment, inserted at the end of line 12, produces the output p is 420?
    • A. 

      P.setWeight(420);

    • B. 

      P.changePayload(420);

    • C. 

      Payload.setWeight(420);

    • D. 

      P = Payload.setWeight(420);

  • 5. 
    Given:   11. public static void test(String str) {   12. int check = 4;   13. if (check = str.length()) {   14. System.out.print(str.charAt(check -= 1) +", ");   15. } else {   16. System.out.print(str.charAt(0) + ", ");   17. }   18. } and the invocation:   21. test("four");   22. test("tee");   23. test("to");   What is the result?
    • A. 

      R, t, t,

    • B. 

      R, e, o,

    • C. 

      Compilation fails.

    • D. 

      An exception is thrown at runtime

  • 6. 
    Given:   15. public class Pass2 {   16. public void main(String [] args) {   17. int x = 6;   18. Pass2 p = new Pass2();   19. p.doStuff(x);   20. System.out.print(" main x = " + x);   21. }   22.   23. void doStuff(int x) {   24. System.out.print(" doStuff x = " + x++);   25. }   26. }   And the command-line invocations:   javac Pass2.java   java Pass2 5   What is the result?
    • A. 

      Compilation fails.

    • B. 

      An exception is thrown at runtime.

    • C. 

      DoStuff x = 6 main x = 6

    • D. 

      DoStuff x = 6 main x = 7

    • E. 

      DoStuff x = 7 main x = 6

    • F. 

      DoStuff x = 7 main x = 7

  • 7. 
    12. public class Test { 13. public enum Dogs {collie, harrier}; 14. public static void main(String [] args) { 15. Dogs myDog = Dogs.collie; 16. switch (myDog) { 17. case collie: 18. System.out.print("collie "); 19. case harrier: 20. System.out.print("harrier "); 21. } 22. } 23. } What is the result?
    • A. 

      Collie

    • B. 

      Harrier

    • C. 

      Compilation fails.

    • D. 

      Collie harrier

    • E. 

      An exception is thrown at runtime.

  • 8. 
    Given:   11. static void test() {   12. try {   13. String x = null;   14. System.out.print(x.toString() + " ");   15. }   16. finally { System.out.print("finally "); }   17. }   18. public static void main(String[] args) {   19. try { test(); }   20. catch (Exception ex) { System.out.print("exception "); }   21. }   What is the result?
    • A. 

      Null

    • B. 

      Finally

    • C. 

      Null finally

    • D. 

      Compilation fails.

    • E. 

      Finally exception

  • 9. 
    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

  • 10. 
    Given:   11. static class A {   12. void process() throws Exception { throw new Exception(); }   13. }   14. static class B extends A {   15. void process() { System.out.println("B"); }   16. }   17. public static void main(String[] args) {   18. new B().process();   19. }   What is the result?
    • A. 

      The code runs with no output.

    • B. 

      B

    • C. 

      Compilation fails because of an error in line 12.

    • D. 

      Compilation fails because of an error in line 15.

    • E. 

      Compilation fails because of an error in line 18.

  • 11. 
    Given:   10. public class Foo {   11. static int[] a;   12. static { a[0]=2; }   13. public static void main( String[] args ) {}   14. }   Which exception or error will be thrown when a programmer attempts to run this code?
    • A. 

      Java.lang.StackOverflowError

    • B. 

      Java.lang.IllegalStateException

    • C. 

      Java.lang.ExceptionInInitializerError

    • D. 

      Java.lang.ArrayIndexOutOfBoundsException

  • 12. 
    Given:   11. public static Iterator reverse(List list) {   12. Collections.reverse(list);   13. return list.iterator();   14. }   15. public static void main(String[] args) {   16. List list = new ArrayList();   17. list.add("1"); list.add("2"); list.add("3");   18. for (Object obj: reverse(list))   19. System.out.print(obj + ", ");   20. }   What is the result?
    • A. 

      3, 2, 1,

    • B. 

      1, 2, 3,

    • C. 

      Compilation fails.

    • D. 

      The code runs with no output.

    • E. 

      An exception is thrown at runtime.

  • 13. 
    Given:   1. public class BuildStuff {   2. public static void main(String[] args) {   3. Boolean test = new Boolean(true);   4. Integer x = 343;   5. Integer y = new BuildStuff().go(test, x);   6. System.out.println(y);   7. }   8. int go(Boolean b, int i) {   9. if(b) return (i/7);   10. return (i/49);   11. }   12. }   What is the result?
    • A. 

      7

    • B. 

      49

    • C. 

      343

    • D. 

      Compilation fails.

    • E. 

      An exception is thrown at runtime.

  • 14. 
    Which capability exists only in java.io.FileWriter?
    • A. 

      Closing an open stream.

    • B. 

      Flushing an open stream.

    • C. 

      Writing to an open stream

    • D. 

      Writing a line separator to an open stream.

  • 15. 
    1. public class LineUp {   2. public static void main(String[] args) {   3. double d = 12.345;   4. // insert code here   5. }   6. }   Which code fragment, inserted at line 4, produces the output | 12.345|?
    • A. 

      System.out.printf("|%7.3f| \n", d);

    • B. 

      System.out.printf("|%7d| \n", d);

    • C. 

      System.out.printf("|%7f| \n", d);

    • D. 

      System.out.printf("|%3.7d| \n", d);

  • 16. 
    Given:   1. interface A { public void aMethod(); }   2. interface B { public void bMethod(); }   3. interface C extends A,B { public void cMethod(); }   4. class D implements B {   5. public void bMethod(){}   6. }   7. class E extends D implements C {   8. public void aMethod(){}   9. public void bMethod(){}   10. public void cMethod(){}   11. }   What is the result?
    • A. 

      Compilation fails because of an error in line 3.

    • B. 

      Compilation fails because of an error in line 7.

    • C. 

      Compilation fails because of an error in line 9.

    • D. 

      If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.

    • E. 

      If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.

  • 17. 
    Given:   1. public class Base {   2. public static final String FOO = "foo";   3. public static void main(String[] args) {   4. Base b = new Base();   5. Sub s = new Sub();   6. System.out.print(Base.FOO);   7. System.out.print(Sub.FOO);   8. System.out.print(b.FOO);   9. System.out.print(s.FOO);   10. System.out.print(((Base)s).FOO);   11. } }   12. class Sub extends Base {public static final String FOO="bar";}   What is the result?
    • A. 

      Foofoofoofoofoo

    • B. 

      Foobarfoobarbar

    • C. 

      Foobarfoofoofoo

    • D. 

      Foobarfoobarfoo

    • E. 

      Barbarbarbarbar

  • 18. 
    Given:   1. package geometry;   2. public class Hypotenuse {   3. public InnerTriangle it = new InnerTriangle();   4. class InnerTriangle {   5. public int base;   6. public int height;   7. }   8. }   Which statement is true about the class of an object that can reference the variable base?
    • A. 

      It can be any class.

    • B. 

      No class has access to base.

    • C. 

      The class must belong to the geometry package.

    • D. 

      The class must be a subclass of the class Hypotenuse.

  • 19. 
    Given:   1. class Alligator {   2. public static void main(String[] args) {   3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};   4. int [][]y = x;   5. System.out.println(y[2][1]);   6. }   7. }   What is the result?
    • A. 

      3

    • B. 

      4

    • C. 

      5

    • D. 

      6

    • E. 

      7

    • F. 

      Compilation Error

  • 20. 
    Given:   21. abstract class C1 {   22. public C1() { System.out.print(1); }   23. }   24. class C2 extends C1 {   25. public C2() { System.out.print(2); }   26. }   27. class C3 extends C2 {   28. public C3() { System.out.println(3); }   29. }   30. public class Ctest {   31. public static void main(String[] a) { new C3(); }   32. }   What is the result?
    • A. 

      3

    • B. 

      23

    • C. 

      32

    • D. 

      123

    • E. 

      321

    • F. 

      Compilation error

    • G. 

      An exception is thrown at runtime.

  • 21. 
    Given:   10. class One {   11. public One foo() { return this; }   12. }   13. class Two extends One {   14. public One foo() { return this; }   15. }   16. class Three extends Two {   17. // insert method here   18. }   Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
    • A. 

      Public void foo() {}

    • B. 

      Public int foo() { return 3; }

    • C. 

      Public Two foo() { return this; }

    • D. 

      Public One foo() { return this; }

    • E. 

      Public Object foo() { return this; }

  • 22. 
    Given:   11. public interface A { public void m1(); }   12.   13. class B implements A { }   14. class C implements A { public void m1() { } }   15. class D implements A { public void m1(int x) { } }   16. abstract class E implements A { }   17. abstract class F implements A { public void m1() { } }   18. abstract class G implements A { public void m1(int x) { } }   What is the result?
    • A. 

      Compilation succeeds.

    • B. 

      Exactly one class does NOT compile

    • C. 

      Exactly two classes do NOT compile.

    • D. 

      Exactly four classes do NOT compile

    • E. 

      Exactly three classes do NOT compile

  • 23. 
    Given:   1. class TestA {   2. public void start() { System.out.println("TestA"); }   3. }   4. public class TestB extends TestA {   5. public void start() { System.out.println("TestB"); }   6. public static void main(String[] args) {   7. ((TestA)new TestB()).start();   8. }   9. }   What is the result?
    • A. 

      TestA

    • B. 

      TestB

    • C. 

      Compilation fails.

    • D. 

      An exception is thrown at runtime

  • 24. 
    Given:   1. public class TestOne implements Runnable {   2. public static void main (String[] args) throws Exception {   3. Thread t = new Thread(new TestOne());   4. t.start();   5. System.out.print("Started");   6. t.join();   7. System.out.print("Complete");   8. }   9. public void run() {   10. for (int i = 0; i < 4; i++) {   11. System.out.print(i);   12. }   13. }   14. }   What can be a result?
    • A. 

      Compilation fails.

    • B. 

      An exception is thrown at runtime.

    • C. 

      The code executes and prints "StartedComplete".

    • D. 

      The code executes and prints "StartedComplete0123".

    • E. 

      The code executes and prints "Started0123Complete".

  • 25. 
    11. public class PingPong implements Runnable {   12. synchronized void hit(long n) {   13. for(int i = 1; i < 3; i++)   14. System.out.print(n + "-" + i + " ");   15. }   16. public static void main(String[] args) {   17. new Thread(new PingPong()).start();   18. new Thread(new PingPong()).start();   19. }   20. public void run() {   21. hit(Thread.currentThread().getId());   22. }   23. }   Which two statements are true? (Choose two.)
    • A. 

      The output could be 8-1 7-2 8-2 7-1

    • B. 

      The output could be 7-1 7-2 8-1 6-1

    • C. 

      The output could be 8-1 7-1 7-2 8-2

    • D. 

      The output could be 8-1 8-2 7-1 7-2

Back to Top Back to top