Java MCQ Number 01078

Java MCQ Set 1

1. Which of these is a process of extracting/removing the state of an object from a stream?
a) Serialization
b) Externalization
c) File Filtering
d) Deserialization

Answer

Answer: d [Reason:] Deserialization is a process by which the data written in the stream can be extracted out from the stream.

2. Which of these process occur automatically by java run time system?
a) Serialization
b) Memory allocation
c) Deserialization
d) All of the mentioned

Answer

Answer: d [Reason:] Serialization, deserialization and Memory allocation occur automatically by java run time system.

3. Which of these is an interface for control over serialization and deserialization?
a) Serializable
b) Externalization
c) FileFilter
d) ObjectInput

Answer

Answer: b [Reason:] None.

4. Which of these interface extends DataInput interface?
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput

Answer

Answer: d [Reason:] ObjectInput interface extends the DataInput interface and supports object serialization.

5. Which of these is a method of ObjectInput interface used to deserialize an object from a stream?
a) int read()
b) void close()
c) Object readObject()
d) Object WriteObject()

Answer

Answer: c [Reason:] None.

6. Which of these class extend InputStream class?
a) ObjectStream
b) ObjectInputStream
c) ObjectOutput
d) ObjectInput

Answer

Answer: b [Reason:] ObjectInputStream class extends the InputStream class and implements the ObjectInput interface.

7. What is the output of this program?

  1.     import java.io.*;
  2.     class streams 
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6.             try 
  7.             {
  8. 	        FileOutputStream fos = new FileOutputStream("serial");
  9. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  10. 	        oos.writeInt(5);
  11. 	        oos.flush();
  12. 	        oos.close();
  13. 	    }
  14. 	    catch(Exception e)
  15.             {
  16. 	        System.out.println("Serialization" + e);
  17.                 System.exit(0);
  18.             }
  19. 	    try
  20.             {
  21. 	        int z;
  22. 	        FileInputStream fis = new FileInputStream("serial");
  23. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  24. 	        z = ois.readInt();
  25. 	        ois.close();
  26. 	        System.out.println(x);		    
  27. 	    }
  28. 	    catch (Exception e) 
  29.             {
  30.                 System.out.print("deserialization");
  31. 	        System.exit(0);
  32. 	    }
  33.         }
  34.     }

a) 5
b) void
c) serialization
d) deserialization

Answer

Answer: a [Reason:] oos.writeInt(5); writes integer 5 in the Output stream which is extracted by z = ois.readInt(); and stored in z hence z contains 5.
Output:
$ javac streams.java
$ java streams
5

8. What is the output of this program?

  1.     import java.io.*;
  2.     class serialization
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6.             try
  7.             {
  8. 	        Myclass object1 = new Myclass("Hello", -7, 2.1e10);
  9. 	        FileOutputStream fos = new FileOutputStream("serial");
  10. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  11. 	        oos.writeObject(object1);
  12. 	        oos.flush();
  13. 	        oos.close();
  14. 	    }
  15. 	    catch(Exception e)
  16.             {
  17. 	        System.out.println("Serialization" + e);
  18.                 System.exit(0);
  19.             }
  20. 	    try
  21.             {
  22. 	        int x;
  23. 	        FileInputStream fis = new FileInputStream("serial");
  24. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  25. 	        x = ois.readInt();
  26. 	        ois.close();
  27. 	        System.out.println(x);		    
  28. 	    }
  29. 	    catch (Exception e)
  30.             {
  31.                 System.out.print("deserialization");
  32. 	        System.exit(0);
  33. 	    }
  34.         }
  35.     }
  36.     class Myclass implements Serializable
  37.     {
  38. String s;
  39. int i;
  40. double d;
  41. 	Myclass(String s, int i, double d)
  42.         {
  43. 	    this.d = d;
  44. 	    this.i = i;
  45. 	    this.s = s;
  46. }
  47.     }

a) -7
b) Hello
c) 2.1E10
d) deserialization

Answer

Answer: d [Reason:] x = ois.readInt(); will try to read an integer value from the stream ‘serial’ created before, since stream contains an object of Myclass hence error will occur and it will be catched by catch printing deserialization.
Output:
$ javac serialization.java
$ java serialization
deserialization

9. What is the output of this program?

  1.     import java.io.*;
  2.     class streams
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6.             try
  7.             {
  8. 	        FileOutputStream fos = new FileOutputStream("serial");
  9. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  10. 	        oos.writeFloat(3.5);
  11. 	        oos.flush();
  12. 	        oos.close();
  13. 	    }
  14. 	    catch(Exception e)
  15.             {
  16. 	        System.out.println("Serialization" + e);
  17.                 System.exit(0);
  18.             }
  19. 	    try
  20.             {
  21. 	        FileInputStream fis = new FileInputStream("serial");
  22. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  23. 	        ois.close();
  24. 	        System.out.println(ois.available());		    
  25. 	    }
  26. 	    catch (Exception e)
  27.             {
  28.                 System.out.print("deserialization");
  29. 	        System.exit(0);
  30. 	    }
  31.         }
  32.     }

a) 1
b) 2
c) 3
d) 0

Answer

Answer: d [Reason:] New input stream is linked to streal ‘serials’, an object ‘ois’ of ObjectInputStream is used to access this newly created stream, ois.close(); closes the stream hence we can’t access the stream and ois.available() returns 0.
Output:
$ javac streams.java
$ java streams
0

10. What is the output of this program?

  1.     import java.io.*;
  2.     class streams
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6.             try
  7.             {
  8. 	        FileOutputStream fos = new FileOutputStream("serial");
  9. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  10. 	        oos.writeFloat(3.5);
  11. 	        oos.flush();
  12. 	        oos.close();
  13. 	    }
  14. 	    catch(Exception e)
  15.             {
  16. 	        System.out.println("Serialization" + e);
  17.                 System.exit(0);
  18.             }
  19. 	    try
  20.             {
  21. 	        FileInputStream fis = new FileInputStream("serial");
  22. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  23. 	        System.out.println(ois.available());		    
  24. 	    }
  25. 	    catch (Exception e) 
  26.             {
  27.                 System.out.print("deserialization");
  28. 	        System.exit(0);
  29. 	    }
  30.         }
  31.     }

a) 1
b) 2
c) 3
d) 4

Answer

Answer: d [Reason:] oos.writeFloat(3.5); writes 3.5 in output stream. A new input stream is linked to stream ‘serials’, an object ‘ois’ of ObjectInputStream is used to access this newly created stream, ois.available() gives the total number of byte in the input stream since a float was written in the stream thus the stream contains 4 byte, hence 4 is returned and printed.
Output:
$ javac streams.java
$ java streams
4

Java MCQ Set 2

1. Which of these exceptions is thrown by methods of System class?
a) IOException
b) SystemException
c) SecurityException
d) InputOutputException

Answer

Answer: c [Reason:] System class methods throw SecurityException.

2. Which of these methods initiates garbage collection?
a) gc()
b) garbage()
c) garbagecollection()
d) Systemgarbagecollection()

Answer

Answer: a [Reason:] None.

3. Which of these methods loads the specified dynamic library?
a) load()
b) library()
c) loadlib()
d) loadlibrary()

Answer

Answer: a [Reason:] load() methods loads the dynamic library whose name is specified.

4. Which of these method can set the out stream to OutputStream?
a) setStream()
b) setosteam()
c) setOut()
d) streamtoOstream()

Answer

Answer: c [Reason:] None.

5. Which of these values are returns under the case of normal termination of a program?
a) 0
b) 1
c) 2
d) 3

Answer

Answer: a [Reason:] None.

6. What is the output of this program?

  1.     import java.lang.System;
  2.     class Output
  3.     {
  4.          public static void main(String args[])
  5.          {
  6.              long start, end;   
  7.              start = System.currentTimeMillis();
  8.              for (int i = 0; i < 10000000; i++);
  9.              end = System.currentTimeMillis();
  10.              System.out.print(end - start);
  11.          }
  12.     }

a) 0
b) 1
c) 1000
d) System Dependent

Answer

Answer: d [Reason:] End time is the time taken by loop to execute it can be any non zero value depending on the System.
Output:
$ javac Output.java
$ java Output
78

7. What is the output of this program?

  1.     import java.lang.System;
  2.     class Output 
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             byte a[] = { 65, 66, 67, 68, 69, 70 };
  7.             byte b[] = { 71, 72, 73, 74, 75, 76 };  
  8.             System.arraycopy(a, 0, b, 0, a.length);
  9.             System.out.print(new String(a) + " " + new String(b));
  10.         }
  11.     }

a) ABCDEF ABCDEF
b) ABCDEF GHIJKL
c) GHIJKL ABCDEF
d) GHIJKL GHIJKL

Answer

Answer: a [Reason:] System.arraycopy() is a method of class System which is used to copy a string into another string.
Output:
$ javac Output.java
$ java Output
ABCDEF ABCDEF

8. What is the output of this program?

  1.     import java.lang.System;
  2.     class Output 
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             byte a[] = { 65, 66, 67, 68, 69, 70 };
  7.             byte b[] = { 71, 72, 73, 74, 75, 76 };  
  8.             System.arraycopy(a, 0, b, 3, a.length - 3);
  9.             System.out.print(new String(a) + " " + new String(b));
  10.         }
  11.     }

a) ABCDEF ABCDEF
b) ABCDEF GHIJKL
c) ABCDEF GHIABC
d) GHIJKL GHIJKL

Answer

Answer: c [Reason:] System.arraycopy() is a method of class System which is used to copy a string into another string.
Output:
$ javac Output.java
$ java Output
ABCDEF GHIABC

9. What is the output of this program?

  1.     import java.lang.System;
  2.     class Output
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             byte a[] = { 65, 66, 67, 68, 69, 70 };
  7.             byte b[] = { 71, 72, 73, 74, 75, 76 };  
  8.             System.arraycopy(a, 2, b, 3, a.length - 4);
  9.             System.out.print(new String(a) + " " + new String(b));
  10.         }
  11.     }

a) ABCDEF ABCDEF
b) ABCDEF GHIJKL
c) ABCDEF GHIABC
d) ABCDEF GHICDL

Answer

Answer: d [Reason:] System.arraycopy() is a method of class System which is used to copy a string into another string.
Output:
$ javac Output.java
$ java Output
ABCDEF GHICDL

10. What value will this program return to Java run-time system?

  1.     import java.lang.System;
  2.     class Output 
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             System.exit(5);
  7.         }
  8.     }

a) 0
b) 1
c) 4
d) 5

Answer

Answer: d [Reason:] None.

Java MCQ Set 3

1. Which of these package is used for text formatting in Java programming language?
a) java.text
b) java.awt
c) java.awt.text
d) java.io

Answer

Answer: a [Reason:] java.text allows formatting, searching and manipulating text.

2. Which of this class can be used to format dates and times?
a) Date
b) SimpleDate
c) DateFormat
d) textFormat

Answer

Answer: c [Reason:] DateFormat is an abstract class that provides the ability to format and parse dates and times.

3. Which of these method returns an instance of DateFormat that can format time information?
a) getTime()
b) getTimeInstance()
c) getTimeDateinstance()
d) getDateFormatinstance()

Answer

Answer: b [Reason:] getTimeInstance() method returns an instance of DateFormat that can format time information.

4. Which of these class allows us to define our own formatting pattern for dates and time?
a) DefinedDateFormat
b) SimpleDateFormat
c) ComplexDateFormat
d) UsersDateFormat

Answer

Answer: b [Reason:] The DateFormat is a concrete subclass of DateFormat. It allows you to define your own formatting patterns that are used to display date and time information.

5. Which of these formatting strings of SimpleDateFormat class is used to print AM or PM in time?
a) a
b) b
c) c
d) d

Answer

Answer: a [Reason:] By using format string “a” we can print AM/PM in time.

6. Which of these formatting strings of SimpleDateFormat class is used to print week of the year?
a) w
b) W
c) s
d) S

Answer

Answer: a [Reason:] By using format string “w” we can print week in a year whereas by using ‘W’ we can print week of month.

7. What is the output of this program?

  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting
  4.     {	 
  5.         public static void main(String args[])
  6.         {
  7. 	    Date date = new Date();
  8. 	    SimpleDateFormat sdf;
  9.             sdf = new SimpleDateFormat("mm:hh:ss");
  10.             System.out.print(sdf.format(date));
  11.         }
  12.     }

Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time).
a) 3:55:4
b) 3.55.4
c) 55:03:04
d) 03:55:04

Answer

Answer: c [Reason:] None.
Output:
$ javac Date_formatting.java
$ java Date_formatting
55:03:04

8. What is the output of this program?

  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting
  4.     {	 
  5.         public static void main(String args[])
  6.         {
  7. 	    Date date = new Date();
  8. 	    SimpleDateFormat sdf;
  9.             sdf = new SimpleDateFormat("hh:mm:ss");
  10.             System.out.print(sdf.format(date));
  11.         }
  12.     }

Note : The program is executed at 3 hour 55 minutes and 4 sec (24 hours time).
a) 3:55:4
b) 3.55.4
c) 55:03:04
d) 03:55:04

Answer

Answer: d [Reason:] The code “sdf = new SimpleDateFormat(“hh:mm:ss”);” create a SimpleDataFormat class with format hh:mm:ss where h is hours, m is month and s is seconds.
Output:
$ javac Date_formatting.java
$ java Date_formatting
55:03:04

9. What is the output of this program?

  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting
  4.     {	 
  5.         public static void main(String args[])
  6.         {
  7. 	    Date date = new Date();
  8. 	    SimpleDateFormat sdf;
  9.             sdf = new SimpleDateFormat("E MMM dd yyyy");
  10.             System.out.print(sdf.format(date));
  11.         }
  12.     }

Note: The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time).
a) Mon Jul 15 2013
b) Jul 15 2013
c) 55:03:04 Mon Jul 15 2013
d) 03:55:04 Jul 15 2013

Answer

Answer: a [Reason:] None.
Output:
$ javac Date_formatting.java
$ java Date_formatting
Mon Jul 15 2013

10. What is the output of this program?

  1.     import java.text.*;
  2.     import java.util.*;
  3.     class Date_formatting
  4.     {	 
  5.         public static void main(String args[])
  6.         {
  7. 	    Date date = new Date();
  8. 	    SimpleDateFormat sdf;
  9.             sdf = new SimpleDateFormat("z");
  10.             System.out.print(sdf.format(date));
  11.         }
  12.     }

Note : The program is executed at 3 hour 55 minutes and 4 sec on Monday, 15 July(24 hours time).
a) z
b) Jul
c) Mon
d) PDT

Answer

Answer: d [Reason:] format string “z” is used to print time zone.
Output:
$ javac Date_formatting.java
$ java Date_formatting
PDT

Java MCQ Set 4

1. Which of these class is used to make a thread?
a) String
b) System
c) Thread
d) Runnable

Answer

Answer: c [Reason:] Thread class is used to make threads in java, Thread encapsulates a thread of execution. To create a new thread the program will either extend Thread or implement the Runnable interface.

2. Which of these interface is implemented by Thread class?
a) Runnable
b) Connections
c) Set
d) MapConnections

Answer

Answer: a [Reason:] None.

3. Which of these method of Thread class is used to find out the priority given to a thread?
a) get()
b) ThreadPriority()
c) getPriority()
d) getThreadPriority()

Answer

Answer: c [Reason:] None.

4. Which of these method of Thread class is used to Suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()

Answer

Answer: a [Reason:] None.

5. Which function of pre defined class Thread is used to check weather current thread being checked is still running?
a) isAlive()
b) Join()
c) isRunning()
d) Alive()

Answer

Answer:a [Reason:]isAlive() function is defined in class Thread, it is used for implementing multithreading and to check whether the thread called
upon is still running or not.

6. What is the output of this program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             t.setName("New Thread");
  7.             System.out.println(t);        
  8.         }
  9.     }

a) Thread[5,main].
b) Thread[New Thread,5].
c) Thread[main,5,main].
d) Thread[New Thread,5,main].

Answer

Answer: d [Reason:] None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,5,main]

7. What is the priority of the thread in output of this program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             t.setName("New Thread");
  7.             System.out.println(t.getName());        
  8.         }
  9.     }

a) main
b) Thread
c) New Thread
d) Thread[New Thread,5,main].

Answer

Answer: c [Reason:] The getName() function is used to obtain the name of the thread, in this code the name given to thread is ‘New Thread’.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
New Thread

8. What is the name of the thread in output of this program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.getPriority());        
  7.         }
  8.     }

a) 0
b) 1
c) 4
d) 5

Answer

Answer: d [Reason:] The default priority given to a thread is 5.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
5

9. What is the name of the thread in output of this program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t.isAlive());        
  7.         }
  8.     }

a) 0
b) 1
c) true
d) false

Answer

Answer: c [Reason:] Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘t.isAlive’ returns true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true

Java MCQ Set 5

1. Which of these keywords is used to generate an exception explicitly?
a) try
b) finally
c) throw
d) catch

Answer

Answer: c [Reason:] None.

2. Which of these class is related to all the exceptions that are explicitly thrown?
a) Error
b) Exception
c) Throwable
d) Throw

Answer

Answer: c [Reason:] None.

3. Which of these operator is used to generate an instance of an exception than can be thrown by using throw?
a) new
b) malloc
c) alloc
d) thrown

Answer

Answer: a [Reason:] new is used to create instance of an exception. All of java’s built in run-time exceptions have two constructors : one with no parameters and one that takes a string parameter.

4. Which of these handles the exception when no catch is used?
a) Default handler
b) finally
c) throw handler
d) Java run time system

Answer

Answer: a [Reason:] None.

5. Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?
a) try
b) throw
c) throws
d) catch

Answer

Answer: c [Reason:] If a method is capable of causing an exception that it does not handle. It must specify this behaviour the behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.

6. What is the output of this program?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 int a = args.length;
  8.                 int b = 10 / a;
  9.                 System.out.print(a);
  10.                 try 
  11.                 {
  12.                     if (a == 1)
  13.                         a = a / a - a;
  14.                     if (a == 2) 
  15.                     {
  16.                         int []c = {1};
  17.                         c[8] = 9;
  18.                     }
  19.                 }
  20.                 catch (ArrayIndexOutOfBoundException e) 
  21.                 {
  22.                     System.out.println("TypeA");
  23.                 }
  24.                 catch (ArithmeticException e) 
  25.                 {
  26.                     System.out.println("TypeB");
  27.                 }
  28.             }
  29.         }
  30.     }

a) TypeA
b) TypeB
c) Compiler Time Error
d) 0TypeB

Answer

Answer: c [Reason:] Because we can’t go beyond array limit

7. What is the output of this program?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 System.out.print("A");
  8.                 throw new NullPointerException ("Hello");
  9.             }
  10.             catch(ArithmeticException e) 
  11.             {
  12.                 System.out.print("B");        
  13.             }
  14.         }
  15.     }

a) A
b) B
c) Hello
d) Runtime Exception

Answer

Answer: d [Reason:] None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread “main” java.lang.NullPointerException: Hello
at exception_handling.main

8. What is the output of this program?

  1.     class exception_handling 
  2.     {
  3.         static void throwexception() throws ArithmeticException 
  4.         {        
  5.             System.out.print("0");
  6.             throw new ArithmeticException ("Exception");
  7.         }
  8.         public static void main(String args[]) 
  9.         {
  10.             try 
  11.             {
  12.                 throwexception();
  13.             }
  14.             catch (ArithmeticException e) 
  15.             {
  16.                 System.out.println("A");
  17.             }
  18.         }
  19.     }

a) A
b) 0
c) 0A
d) Exception

Answer

Answer: c [Reason:] None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A

9. What is the output of this program?

  1. public class San 
  2.  {  
  3.     public static void main(String[] args) 
  4.     {
  5.         try 
  6.         { 
  7.             return; 
  8.         } 
  9.         finally 
  10.         {
  11.             System.out.println( "Finally" ); 
  12.         } 
  13.     } 
  14. }

a) Finally
b) Compilation fails
c) The code runs with no output
d) An exception is thrown at runtime

Answer

Answer: a [Reason:] Because finally will execute always.

10. What is the output of this program?

  1. public class San 
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         try 
  6.         {
  7.             System.out.print("Hello world ");
  8.         }
  9.         finally 
  10.         {
  11.             System.out.println("Finally executing ");
  12.         }
  13.     }
  14. }

a) The program will not compile because no exceptions are specified
b) The program will not compile because no catch clauses are specified
c) Hello world
d) Hello world Finally executing

Answer

Answer: d [Reason:] None

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.