Java MCQ Number 01077

Java MCQ Set 1

1. Which exception is thrown by read() method?
a) IOException
b) InterruptedException
c) SystemException
d) SystemInputException

Answer

Answer: a [Reason:] read method throws IOException.

2. Which of these is used to read a string from the input stream?
a) get()
b) getLine()
c) read()
d) readLine()

Answer

Answer: c [Reason:] None.

3. Which of these class is used to read characters and strings in Java from console?
a) BufferedReader
b) StringReader
c) BufferedStreamReader
d) InputStreamReader

Answer

Answer: a [Reason:] None.

4. Which of these classes are used by Byte streams for input and output operation?
a) InputStream
b) InputOutputStream
c) Reader
d) All of the mentioned

Answer

Answer: a [Reason:] Byte stream uses InputStream and OutputStream classes for input and output operation.

5. Which of these class is implemented by FilterInputStream class?
a) InputStream
b) InputOutputStream
c) BufferedInputStream
d) SequenceInputStream

Answer

Answer: a [Reason:] FileInputStream implements InputStream.

6. Which of these class is used to read from a file?
a) InputStream
b) BufferedInputStream
c) FileInputStream
d) BufferedFileInputStream

Answer

Answer: c [Reason:] None.

7. What is the output of this program if input given is “Hello stop World”?

  1.     class Input_Output 
  2.     {
  3.         public static void main(String args[]) throws IOException
  4.         {	 
  5.             string str;
  6.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  7.             do
  8.             {
  9.                 str = (char) obj.readLine();
  10. 	        System.out.print(str);
  11.             } while(!str.equals("strong"));
  12.         }
  13.     }

a) Hello
b) Hello stop
c) World
d) Hello stop World

Answer

Answer: d [Reason:] “stop” will be able to terminate the do-while loop only when it occurs singly in a line. “Hello stop World” does not terminate the loop.
Output:
$ javac Input_Output.java
$ java Input_Output
Hello stop World

8. What is the output of this program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.              StringBuffer c = new StringBuffer("Hello");
  6.              StringBuffer c1 = new StringBuffer(" World");
  7.              c.append(c1);
  8.              System.out.println(c);
  9.         }
  10.     }

a) Hello
b) World
c) Helloworld
d) Hello World

Answer

Answer: d [Reason:] append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Output:
$ javac output.java
$ java output
Hello World

9. What is the output of this program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            StringBuffer s1 = new StringBuffer("Hello");
  6.            s1.setCharAt(1,x);
  7.            System.out.println(s1);
  8.         }
  9.     }

a) xello
b) xxxxx
c) Hxllo
d) Hexlo

Answer

Answer: c [Reason:] None.
Output:
$ javac output.java
$ java output
Hxllo

10. What is the output of this program if input given is “abc’def/’egh”?

  1.     class Input_Output
  2.     {
  3.         public static void main(String args[]) throws IOException
  4.         {	 
  5.             char c;
  6.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  7.             do
  8.             {
  9.                 c = (char) obj.read();
  10. 	        System.out.print(c);
  11.             } while(c != ''');
  12.         }
  13.     }

a) abc’
b) abcdef/’
c) abc’def/’egh
d) abcqfghq

Answer

Answer: a [Reason:] ’ is used for single quotes that is for representing ‘ .
Output:
$ javac Input_Output.java
$ java Input_Output
abc’

Java MCQ Set 2

1. Which of these class contains the methods used to write in a file?
a) FileStream
b) FileInputStream
c) BUfferedOutputStream
d) FileBufferStream

Answer

Answer: b [Reason:] None.

2. Which of these exception is thrown in cases when the file specified for writing it not found?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException

Answer

Answer: c [Reason:] In cases when the file specified is not found, then FileNotFoundException is thrown by java run-time system, earlier versions of java used to throw IOException but after Java 2.0 they throw FileNotFoundException.

3. Which of these methods are used to read in from file?
a) get()
b) read()
c) scan()
d) readFileInput()

Answer

Answer: b [Reason:] Each time read() is called, it reads a single byte from the file and returns the byte as an integer value. read() returns -1 when the end of the file is encountered.

4. Which of these values is returned by read() method is end of file (EOF) is encountered?
a) 0
b) 1
c) -1
d) Null

Answer

Answer: c [Reason:] Each time read() is called, it reads a single byte from the file and returns the byte as an integer value. read() returns -1 when the end of the file is encountered.

5. Which of these exception is thrown by close() and read() methods?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputOutputException

Answer

Answer: a [Reason:] Both close() and read() method throw IOException.

6. Which of these methods is used to write() into a file?
a) put()
b) putFile()
c) write()
d) writeFile()

Answer

Answer: c [Reason:] None.

7. What is the output of this program?

  1.     import java.io.*;
  2.     class filesinputoutput
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             InputStream obj = new FileInputStream("inputoutput.java");
  7.             System.out.print(obj.available());
  8.         }
  9.     }

Note: inputoutput.java is stored in the disk.
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file

Answer

Answer: c [Reason:] obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
(Output will be different in your case)

8. What is the output of this program?

  1.     import java.io.*;
  2.     public class filesinputoutput
  3.     {
  4.     public static void main(String[] args)
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i)
  10.            {
  11.                int c;
  12.                while((c = obj1.read()) != -1)
  13.                {
  14.             	   if(i == 0)
  15.                    {
  16.             	       System.out.print(Character.toUpperCase((char)c));
  17.                        obj2.write(1); 
  18.             	   }
  19.                }
  20.                System.out.print(obj2);
  21.            }
  22.         }
  23.     }

a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa

Answer

Answer: d [Reason:] None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa

9. What is the output of this program?

  1.     import java.io.*;
  2.     class Chararrayinput
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6. 	    String obj  = "abcdef";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 0, 3);
  12.             int i;
  13.             try
  14.             {
  15. while((i = input2.read()) != -1)
  16.                 {
  17.                     System.out.print((char)i);
  18.                 }
  19.        	    } 
  20.             catch (IOException e)
  21.             {
  22.                 e.printStackTrace();
  23. 	    }
  24. }
  25.     }

a) abc
b) abcd
c) abcde
d) abcdef

Answer

Answer: a [Reason:] None.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
abc

10. What is the output of this program?

  1.     import java.io.*;
  2.     class Chararrayinput
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6. 	    String obj  = "abcdefgh";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 1, 4);
  12.             int i;
  13.             int j;
  14.             try 
  15.             {
  16. while((i = input1.read()) == (j = input2.read()))
  17.                 {
  18.                     System.out.print((char)i);
  19.                 }
  20.        	    } 
  21.             catch (IOException e)
  22.             {
  23.                 e.printStackTrace();
  24. 	    }
  25. }
  26.     }

a) abc
b) abcd
c) abcde
d) none of the mentioned

Answer

Answer: d [Reason:] No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput

Java MCQ Set 3

1. What is Remote method invocation (RMI)?
a) RMI allows us to invoke a method of java object that executes on another machine
b) RMI allows us to invoke a method of java object that executes on another Thread in multithreaded programming
c) RMI allows us to invoke a method of java object that executes parallely in same machine
d) None of the mentioned

Answer

Answer: a [Reason:] Remote method invocationor RMI allows us to invoke a method of java object that executes on another machine..

2. Which of these package is used for remote method invocation?
a) java.applet
b) java.rmi
c) java.lang.rmi
d) java.lang.reflect

Answer

Answer: b [Reason:] None.

3. Which of these methods are member of Remote class?
a) checkIP()
b) addLocation()
c) AddServer()
d) None of the mentioned

Answer

Answer: d [Reason:] Remote class does not define any methods, its pupose is simply to indicate that an interface uses remote methods.

4. Which of these Exceptions is thrown by remote method?
a) RemoteException
b) InputOutputException
c) RemoteAccessException
d) RemoteInputOutputException

Answer

Answer: a [Reason:] All remote methods throw RemoteException.

5. Which of these class is used for creating a client for a server-client operations?
a) serverClientjava
b) Client.java
c) AddClient.java
d) ServerClient.java

Answer

Answer: c [Reason:] None.

6. Which of these package is used for all the text related modifications?
a) java.text
b) java.awt
c) java.lang.text
d) java.text.mofify

Answer

Answer: a [Reason:] java.text provides capabilities for formatting, searching and manipulating text.

7. What is the output of this program?

  1.     import java.lang.reflect.*;
  2.     class Additional_packages 
  3.     {	 
  4.          public static void main(String args[]) 
  5.          {
  6. 	     try 
  7.              {
  8. 	         Class c = Class.forName("java.awt.Dimension");
  9. 		 Constructor constructors[] = c.getConstructors();
  10. 		 for (int i = 0; i < constructors.length; i++)
  11. 		     System.out.println(constructors[i]);
  12. 	     }
  13. 	     catch (Exception e)
  14.              {
  15.              System.out.print("Exception");
  16.              }
  17.         }
  18.     }

a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the possible constructors of class ‘Class’
c) Program prints “Exception”
d) Runtime Error

Answer

Answer: a [Reason:] None.
Output:
$ javac Additional_packages.java
$ java Additional_packages
public java.awt.Dimension(java.awt.Dimension)
public java.awt.Dimension()
public java.awt.Dimension(int,int)

8. What is the output of this program?

  1.     import java.lang.reflect.*;
  2.     class Additional_packages 
  3.     {	 
  4.          public static void main(String args[])
  5.          {
  6. 	     try 
  7.              {
  8. 	         Class c = Class.forName("java.awt.Dimension");
  9. 		 Field fields[] = c.getFields();
  10. 		 for (int i = 0; i < fields.length; i++)
  11. 		     System.out.println(fields[i]);
  12. 	     }
  13. 	     catch (Exception e)
  14.              {
  15.              System.out.print("Exception");
  16.              }
  17.         }    
  18.     }

a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the methods of ‘java.awt.Dimension’ package
c) Program prints all the data members of ‘java.awt.Dimension’ package
d) program prints all the methods and data member of ‘java.awt.Dimension’ package

Answer

Answer: c [Reason:] None.
Output:
$ javac Additional_packages.java
$ java Additional_packages
public int java.awt.Dimension.width
public int java.awt.Dimension.height

9. What is the length of the application box made by this program?

  1.     import java.awt.*;
  2.     import java.applet.*;
  3.     public class myapplet extends Applet 
  4.     {
  5.         Graphic g;
  6.         g.drawString("A Simple Applet",20,20);    
  7.     }

a) 20
b) Default value
c) Compilation Error
d) Runtime Error

Answer

Answer: c [Reason:] To implement the method drawString we need first need to define abstract method of AWT that is paint() method. Without paint() method we cannot define and use drawString or any Graphic class methods.

10. What is the output of this program?

  1.     import java.lang.reflect.*;
  2.     class Additional_packages
  3.     {	 
  4.          public static void main(String args[])
  5.          {
  6. 	     try
  7.              {
  8. 	         Class c = Class.forName("java.awt.Dimension");
  9. 		 Method methods[] = c.getMethods();
  10. 		 for (int i = 0; i < methods.length; i++)
  11. 		     System.out.println(methods[i]);
  12. 	     }
  13. 	     catch (Exception e)
  14.              {
  15.              System.out.print("Exception");
  16.              }
  17.         }
  18.     }

a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the methods of ‘java.awt.Dimension’ package
c) Program prints all the data members of ‘java.awt.Dimension’ package
d) program prints all the methods and data member of ‘java.awt.Dimension’ package

Answer

Answer: b [Reason:] None.
Output:
$ javac Additional_packages.java
$ java Additional_packages
public int java.awt.Dimension.hashCode()
public boolean java.awt.Dimension.equals(java.lang.Object)
public java.lang.String java.awt.Dimension.toString()
public java.awt.Dimension java.awt.Dimension.getSize()
public void java.awt.Dimension.setSize(double,double)
public void java.awt.Dimension.setSize(int,int)
public void java.awt.Dimension.setSize(java.awt.Dimension)
public double java.awt.Dimension.getHeight()
public double java.awt.Dimension.getWidth()
public java.lang.Object java.awt.geom.Dimension2D.clone()
public void java.awt.geom.Dimension2D.setSize(java.awt.geom.Dimension2D)
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public final native void java.lang.Object.wait(long)
public final void java.lang.Object.wait(long,int)
public final void java.lang.Object.wait()

Java MCQ Set 4

1. Which of these types cannot be used to initiate a generic type?
a) Integer class
b) Float class
c) Primitive Types
d) Collections

Answer

Answer: c [Reason:] None.

2. Which of these instance cannot be created?
a) Integer instance
b) Generic class instance
c) Generic type instance
d) Collection instances

Answer

Answer: c [Reason:] It is not possible to create generic type instances. Example – “E obj = new E()” will give a compilation error.

3. Which of these data type cannot be type parameterized?
a) Array
b) List
c) Map
d) Set

Answer

Answer: a [Reason:] None.

4. Which of these Exception handlers cannot be type parameterized?
a) catch
b) throw
c) throws
d) all of the mentioned

Answer

Answer: d [Reason:] we cannot Create, Catch, or Throw Objects of Parameterized Types as generic class cannot extend the Throwable class directly or indirectly.

5. Which of the following cannot be Type parameterized?
a) Oveloaded Methods
b) Generic methods
c) Class methods
d) Overriding methods

Answer

Answer: a [Reason:] Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type.

6. What is the output of this program?

  1.     public class BoxDemo
  2.     {
  3.         public static <U> void addBox(U u, 
  4.            java.util.List<Box<U>> boxes)
  5.           {
  6.             Box<U> box = new Box<>();
  7.             box.set(u);
  8.             boxes.add(box);
  9.           }
  10.         public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
  11.         {
  12.             int counter = 0;
  13.             for (Box<U> box: boxes)
  14.             {
  15.                 U boxContents = box.get();
  16.                 System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
  17.                 counter++;
  18.             }
  19.         }
  20.         public static void main(String[] args)
  21.         {
  22.             java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
  23.             BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
  24.             BoxDemo.outputBoxes(listOfIntegerBoxes);
  25.         }
  26.     }

a) 10
b) Box #0 [10].
c) Box contains [10].
d) Box #0 contains [10].

Answer

Answer: d [Reason:] None.
Output:
$ javac Output.java
$ java Output
Box #0 contains [10]

7. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj) 
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop() 
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             genericstack <String> gs = new genericstack<String>();
  20.             gs.push("Hello");
  21.             System.out.print(gs.pop() + " ");
  22.             genericstack <Integer> gs = new genericstack<Integer>();
  23.             gs.push(36);
  24.             System.out.println(gs.pop());
  25.         }
  26.     }

a) Error
b) Hello
c) 36
d) Hello 36

Answer

Answer: d [Reason:] None.
Output:
$ javac Output.java
$ java Output
Hello 36

8. What is the output of this program?

  1.     import java.util.*;
  2.     class Output
  3.     {
  4.         public static double sumOfList(List<? extends Number> list) 
  5.         {
  6.             double s = 0.0;
  7.             for (Number n : list)
  8.                 s += n.doubleValue();
  9.             return s;
  10.         }
  11.         public static void main(String args[])
  12.         {
  13.            List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
  14.            System.out.println(sumOfList(ld));
  15.         }
  16.     }

a) 5.0
b) 7.0
c) 8.0
d) 6.0

Answer

Answer: b [Reason:] None.
Output:
$ javac Output.java
$ java Output
7.0

9. What is the output of this program?

  1.     import java.util.*;
  2.     class Output
  3.     {
  4.         public static void addNumbers(List<? super Integer> list)
  5.         {
  6.             for (int i = 1; i <= 10; i++)
  7.             {
  8.                 list.add(i);
  9.             }
  10.         }
  11.         public static void main(String args[])
  12.         {
  13.            List<Double> ld = Arrays.asList();
  14.            addnumbers(10.4);
  15.            System.out.println("getList(2)");
  16.         }
  17.     }

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

Answer

Answer: a [Reason:] None.
Output:
$ javac Output.java
$ java Output
1

10. What is the output of this program?

  1.     import java.util.*;
  2.     public class genericstack <E>
  3.     {
  4.         Stack <E> stk = new Stack <E>();
  5. public void push(E obj)
  6.         {
  7.             stk.push(obj);
  8. }
  9. public E pop()
  10.         {
  11.             E obj = stk.pop();
  12. 	    return obj;
  13. }
  14.     }
  15.     class Output
  16.     {
  17.         public static void main(String args[]) 
  18.         {
  19.             genericstack <Integer> gs = new genericstack<Integer>();
  20.             gs.push(36);
  21.             System.out.println(gs.pop());
  22.         }
  23.     }

a) H
b) Hello
c) Runtime Error
d) Compilation Error

Answer

Answer: d [Reason:] genericstack’s object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:
$ javac Output.java
$ java Output
Hello

Java MCQ Set 5

1. Which of these is a process of writing the state of an object to a byte stream?
a) Serialization
b) Externalization
c) File Filtering
d) All of the mentioned

Answer

Answer: a [Reason:] Serialization is the process of writing the state of an object to a byte stream. This is used when you want to save the state of your program to persistent storage area.

2. Which of these process occur automatically by java run time system?
a) Serialization
b) Garbage collection
c) File Filtering
d) All of the mentioned

Answer

Answer: a [Reason:] Serialization and deserialization occur automatically by java run time system, Garbage collection also occur automatically but is done by CPU or the operating system not by the 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 DataOutput interface?
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput

Answer

Answer: c [Reason:] ObjectOutput interface extends the DataOutput interface and supports object serialization.

5. Which of these is a method of ObjectOutput interface used to finalize the output state so that any buffers are cleared?
a) clear()
b) flush()
c) fflush()
d) close()

Answer

Answer: b [Reason:] None.

6. Which of these is method of ObjectOutput interface used to write the object to input or output stream as required?
a) write()
b) Write()
c) StreamWrite()
d) writeObject()

Answer

Answer: d [Reason:] writeObject() is used to write an object into invoking stream, it can be input stream or output stream.

7. 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.                 Myclass object2;
  23. 	        FileInputStream fis = new FileInputStream("serial");
  24. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  25.                 object2 = (Myclass)ois.readObject();
  26.                 ois.close();
  27. 	        System.out.println(object2);		    
  28. 	    }
  29. 	    catch (Exception e) 
  30.             {
  31.                 System.out.print("deserialization" + e);
  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) s=Hello; i=-7; d=2.1E10
b) Hello; -7; 2.1E10
c) s; i; 2.1E10
d) Serialization

Answer

Answer: a [Reason:] None.
Output:
$ javac serialization.java
$ java serialization
s=Hello; i=-7; d=2.1E10

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 Chararrayinput
  3.     {
  4.         public static void main(String[] args) 
  5.         {
  6. 	    String obj  = "abcdefgh";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 1, 4);
  12.             int i;
  13.             int j;
  14.             try 
  15.             {
  16. while ((i = input1.read()) == (j = input2.read()))
  17.                 {
  18.                     System.out.print((char)i);
  19.                 }
  20.        	    } 
  21.             catch (IOException e) 
  22.             {
  23.                 e.printStackTrace();
  24. 	    }
  25. }
  26.     }

a) abc
b) abcd
c) abcde
d) None of the mentioned

Answer

Answer: d [Reason:] No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput

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. 	        float x;
  22. 	        FileInputStream fis = new FileInputStream("serial");
  23. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  24.                 x = 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) 3
b) 3.5
c) serialization
d) deserialization

Answer

Answer: b [Reason:] oos.writeFloat(3.5); writes in output stream which is extracted by x = ois.readInt(); and stored in x hence x contains 3.5.
Output:
$ javac streams.java
$ java streams
3.5

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.