Core Java Test 6

40 Questions | Total Attempts: 154

SettingsSettingsSettings
Core Java Quizzes & Trivia

Questions and Answers
  • 1. 
    What happens when this code is compiled and executed? String name = null; File file = new File("/folder", name); System.out.print(file.exists());
    • A. 

      A.It prints “true”.

    • B. 

      B.It prints “false”.

    • C. 

      C.NullPointerException at second line.

    • D. 

      D.NullPointerException at third line.

  • 2. 
    Consider an empty file C:/empty.txt. What happens when this code is compiled and executed? void test() throws IOException { BufferedReader reader = new BufferedReader(           new FileReader("C:/empty.txt")); System.out.println(reader.getLine()); }
    • A. 

      A.It prints an empty string “”.

    • B. 

      B.It prints null.

    • C. 

      C.A NullPointerException is thrown at runtime.

    • D. 

      D.Compilation fails.

  • 3. 
    What happens when this code is compiled and executed? void test() throws IOException { FileWriter writer = new FileWriter("/fun.log"); writer.write("Hello!"); writer.close(); }
    • A. 

      A.A file fun.log is created with the content “Hello!”.

    • B. 

      B.A file fun.log is created but it’s empty, because flush() was not called.

    • C. 

      C.A runtime exception is thrown because flush() was not called.

    • D. 

      D.Compilation fails.

  • 4. 
    What happens when this code is compiled and executed? void test() throws IOException { PrintWriter writer = new PrintWriter(new PrintWriter("yo.txt")); writer.print("yo"); writer.flush(); writer.close(); }
    • A. 

      A.Compilation fails because a PrintWriter cannot accept a PrintWriter in its constructor.

    • B. 

      B.Compilation fails because there is no method print().

    • C. 

      C.Two files yo.txt with the same content are created.

    • D. 

      D.A file yo.txt with the content “yo” is created.

  • 5. 
    Which of these classes defined in java.io and used for file-handling are abstract. Select the two correct answers.
    • A. 

      A.InputStream

    • B. 

      B.PrintStream

    • C. 

      C.Reader

    • D. 

      D.FileInputStream

    • E. 

      FileWriter

  • 6. 
    Import java.io.*; class Player { Player() { System.out.print("p"); } } class CardPlayer extends Player implements Serializable { CardPlayer() { System.out.print("c"); } public static void main(String[] args) { CardPlayer c1 = new CardPlayer(); try { FileOutputStream fos = new FileOutputStream("play.txt"); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(c1); os.close(); FileInputStream fis = new FileInputStream("play.txt"); ObjectInputStream is = new ObjectInputStream(fis); CardPlayer c2 = (CardPlayer) is.readObject(); is.close(); } catch (Exception x ) { } } } What is the result?
    • A. 

      A.pc

    • B. 

      B.pcc

    • C. 

      C.pcp

    • D. 

      D.pcpc

    • E. 

      E.Compilation fails

    • F. 

      F.An exception is thrown at runtime

  • 7. 
    Class TKO { public static void main(String[] args) { String s = "-"; Integer x = 343; long L343 = 343L; if(x.equals(L343)) s += ".e1 "; if(x.equals(343)) s += ".e2 "; Short s1 = (short)((new Short((short)343)) / (new Short((short)49))); if(s1 == 7) s += "=s "; if(s1 < new Integer(7+1)) s += "fly "; System.out.println(s); } } Which of the following will be included in the output String s? (Choose all that apply.)
    • A. 

      A).e1

    • B. 

      B).e2

    • C. 

      C)=s

    • D. 

      D)fly

    • E. 

      E)none of the above

    • F. 

      F)compilation fails

    • G. 

      G)run time exception

  • 8. 
    Import java.io.*;  class Vehicle { }  class Wheels { }  class Car extends Vehicle implements Serializable { }  class Ford extends Car { }  class Dodge extends Car {  Wheels w = new Wheels();  } Instances of which class(es) can be serialized? (Choose all that apply.)
    • A. 

      A.Car

    • B. 

      B.Ford

    • C. 

      C.Dodge

    • D. 

      D.Wheels

    • E. 

      E.Vehicle

  • 9. 
    Import java.util.regex.*; public class Archie {  public static void main(String[] args) {  Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); int count = 0;  while(m.find())  count++;  System.out.print(count);  }  } And given the command line invocation: java Archie "\d+" ab2c4d67 What is the result?
    • A. 

      A.0

    • B. 

      B.3

    • C. 

      C.4

    • D. 

      D.8

    • E. 

      E.9

    • F. 

      F.compailation fails

  • 10. 
    Import java.util.*;  public class Looking { public static void main(String[] args) { String input = "1 2 a 3 45 6"; Scanner sc = new Scanner(input); int x = 0; do { x = sc.nextInt(); System.out.print(x + " ");  } while (x!=0);  }  } what is the result??
    • A. 

      A.1 2

    • B. 

      B.1 2 3 45 6

    • C. 

      C.1 2 3 4 5 6

    • D. 

      D.1 2 a 3 45 6

    • E. 

      E.compilation fails

    • F. 

      F.1 2 followed by an exception

  • 11. 
    Which class is used to write streams of raw bytes to a file?
    • A. 

      A.FileReader

    • B. 

      B.FileWriter

    • C. 

      C.FileInputStream

    • D. 

      D.FileOutputStream

  • 12. 
    What happens when this code is compiled and executed? void test() throws IOException { for (int index = 1; index <= 2; index++) { PrintWriter writer = new PrintWriter("/apa"); writer.print("apa"); writer.close(); } }
    • A. 

      A.A file apa is created with content “apa”.

    • B. 

      B.A file apa is created with content “apaapa”.

    • C. 

      C.Two files are created.

    • D. 

      D.An exception is thrown at runtime.

  • 13. 
    A thread that has called the wait() method of an  object     still owns the lock of the object.
    • A. 

      A.True

    • B. 

      B.False

  • 14. 
    A number of threads of the same priority  have relinquished the lock     on a monitor and are in a waiting state after having called the wait()     method of the object. A new thread enters the monitor and calls the     notifyAll() method of the meonitor. Which of these threads will be the     first one to resume?
    • A. 

      A.The thread that has been waiting the longest.

    • B. 

      B.The thread that was the last one to to exit the monitor.

    • C. 

      C.You can never be sure which thread will get to run first.

    • D. 

      D.The the first thread that called the wait() method

  • 15. 
    Which of these are valid contructors of a Thread object?
    • A. 

      A.public Thread(Object obj)x

    • B. 

      B.public Thread(String name)

    • C. 

      C.public Thread(Runnable trgt)

    • D. 

      D.public Thread(ThreadGroup grp, Runnable trgt, String name)

    • E. 

      E.public Thread(ThreadGroup grp, Object ob)

  • 16. 
    If you call  the interrupted() method of a thread object twice     the second call will always return false. 
    • A. 

      A.True

    • B. 

      B.False

  • 17. 
    Which of the following are methods of the Thread class? choose all that apply...
    • A. 

      A.public void run()

    • B. 

      B.public void start()

    • C. 

      C.public void exit()

    • D. 

      D.public final void setAccess()

    • E. 

      E.public final void setPriority(int priNbr)

    • F. 

      F.public final int getPriority()

  • 18. 
    Calling the destroy() method of a thread object relases all the locks held by     the thread ?
    • A. 

      A.True

    • B. 

      B.False

  • 19. 
    What will happen when you attempt to compile and run the following code ? 1. public class Test extends Thread{ 2. public static void main(String argv[]){ 3. Test t = new Test(); 4. t.run(); 5. t.start(); 6. } 7. public void run(){ 8. System.out.println(“run-test”); 9. } 10. }
    • A. 

      A).run-test run-test

    • B. 

      B).run-test

    • C. 

      C)Compilation fails due to an error on line 4

    • D. 

      D)Compilation fails due to an error on line 7

  • 20. 
    What is the output for the below code ? class A implements Runnable{ public void run(){ System.out.println(“run-a”); } } 1. public class Test { 2. public static void main(String… args) { 3. A a = new A(); 4. Thread t = new Thread(a); 5. t.start(); 6. t.start(); 7. } 8. }
    • A. 

      A.run-a

    • B. 

      B.run-a run-a

    • C. 

      C.Compilation fails with an error at line 6

    • D. 

      D.Compilation succeed but Runtime Exception

  • 21. 
    What is the output for the below code ? class A implements Runnable{ public void run(){ try{ for(int i=0;i<4;i++){ Thread.sleep(100); System.out.println(Thread.currentThread().getName()); } }catch(InterruptedException e){ } } } public class Test { public static void main(String argv[]) throws Exception{ A a = new A(); Thread t = new Thread(a,”A”); Thread t1 = new Thread(a,”B”); t.start(); t.join(); t1.start(); } }
    • A. 

      A.A A A A B B B B

    • B. 

      B.A B A B A B A B

    • C. 

      C.Output order is not guaranteed

    • D. 

      D.Compilation succeed but Runtime Exception

  • 22. 
    What is the output for the below code ? class A extends Thread{ int count = 0; public void run(){ System.out.println(“run”); synchronized (this) { for(int i =0; i < 50 ; i++){ count = count + i; } notify(); } } } public class Test{ public static void main(String argv[]) { A a = new A(); a.start(); synchronized (a) { System.out.println(“waiting”); try{ a.wait(); }catch(InterruptedException e){ } System.out.println(a.count); } } }
    • A. 

      A.waiting run 1225

    • B. 

      B.waiting run 0

    • C. 

      C.waiting run and count can be anything

    • D. 

      D.Compilation fails

  • 23. 
    Which of the following statements about this code are true? class A extends Thread{ public void run(){ for(int i =0; i < 2; i++){ System.out.println(i); } } } public class Test{ public static void main(String argv[]){ Test t = new Test(); t.check(new A(){}); } public void check(A a){ a.start(); } }
    • A. 

      A.0 0

    • B. 

      B.Compilation error, class A has no start method

    • C. 

      C.0 1

    • D. 

      D.Compilation succeed but runtime exception

  • 24. 
    The following block of code creates a Thread using a Runnable target: Runnable target = new MyRunnable(); Thread myThread = new Thread(target); Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
    • A. 

      A.public class MyRunnable extends Runnable{public void run(){}}

    • B. 

      B.public class MyRunnable extends Object{public void run(){}}

    • C. 

      C.public class MyRunnable implements Runnable{public void run(){}}

    • D. 

      D.public class MyRunnable implements Runnable{void run(){}}

    • E. 

      E.public class MyRunnable implements Runnable{public void start(){}}

  • 25. 
    Assume you have a class that holds two private variables: a and b. Which of the following pairs can prevent concurrent access problems in that class? (Choose all that apply.)
    • A. 

      Public int read(){return a+b;} a.public void set(int a, int b){this.a=a;this.b=b;}

    • B. 

      Public synchronized int read(){return a+b;} b.public synchronized void set(int a, int b){this.a=a;this.b=b;}

    • C. 

      C.public int read(){synchronized(a){return a+b;}} public void set(int a, int b){synchronized(a){this.a=a;this.b=b;}}

    • D. 

      Public int read(){synchronized(a){return a+b;}} public void set(int a, int b){synchronized(b){this.a=a;d.this.b=b;}}

    • E. 

      Public synchronized(this) int read(){return a+b;} public synchronized(this) void set(int a, int b){this.a=a;e.this.b=b;}

    • F. 

      Public int read(){synchronized(this){return a+b;}} public void set(int a, int b){synchronized(this){this.a=a;f.this.b=b;}}

Back to Top Back to top