Java MCQ Number 01065

Java MCQ Set 1

1. Which of these stream contains the classes which can work on character stream?
a) InputStream
b) OutputStream
c) Character Stream
d) All of the mentioned

Answer

Answer: c [Reason:] InputStream & OutputStream classes under byte stream they are not streams. Character Stream contains all the classes which can work with Unicode.

2. Which of these class is used to read characters in a file?
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader

Answer

Answer: a [Reason:] None.

3. Which of these method of FileReader class is used to read characters from a file?
a) read()
b) scanf()
c) get()
d) getInteger()

Answer

Answer: a [Reason:] None.

4. Which of these class can be used to implement input stream that uses a character array as the source?
a) BufferedReader
b) FileReader
c) CharArrayReader
d) FileArrayReader

Answer

Answer: c [Reason:] CharArrayReader is an implementation of an input stream that uses character array as a source. Here array is the input source.

5. Which of these is a method to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()

Answer

Answer: b [Reason:] None.

6. Which of these classes can return more than one character to be returned to input stream?
a) BufferedReader
b) Bufferedwriter
c) PushbachReader
d) CharArrayReader

Answer

Answer: c [Reason:] PushbackReader class allows one or more characters to be returned to the input stream. This allows looking ahead in input stream and performing action accordingly.

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.     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 = input1.read()) != -1) 
  16.                 {
  17.                     System.out.print((char)i);
  18.                 }
  19.        	    } 
  20.             catch (IOException e) 
  21.             {
  22. 	        // TODO Auto-generated catch block
  23.                 e.printStackTrace();
  24. 	    }
  25. }
  26.     }

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

Answer

Answer: d [Reason:] None.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
abcdef

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. 	        // TODO Auto-generated catch block
  23.                 e.printStackTrace();
  24. 	    }
  25. }
  26.     }

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. 	        // TODO Auto-generated catch block
  24.                 e.printStackTrace();
  25. 	    }
  26. }
  27.     }

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 2

1. Which of these packages contain classes and interfaces used for input & output operations of a program?
a) java.util
b) java.lang
c) java.io
d) all of the mentioned

Answer

Answer: c [Reason:] java.io provides support for input and output operations.

2. Which of these class is not a member class of java.io package?
a) String
b) StringReader
c) Writer
d) File

Answer

Answer: a [Reason:] None.

3. Which of these interface is not a member of java.io package?
a) DataInput
b) ObjectInput
c) ObjectFilter
d) FileFilter

Answer

Answer: c [Reason:] None.

4. Which of these class is not related to input and output stream in terms of functioning?
a) File
b) Writer
c) InputStream
d) Reader

Answer

Answer: a [Reason:] A File describes properties of a file, a File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time date, and directories path, and to navigate subdirectories.

5. Which of these is specified by a File object?
a) a file in disk
b) directory path
c) directory in disk
d) none of the mentioned

Answer

Answer: c [Reason:] None.

6. Which of these is method for testing whether the specified element is a file or a directory?
a) IsFile()
b) isFile()
c) Isfile()
d) isfile()

Answer

Answer: b [Reason:] isFile() returns true if called on a file and returns false when called on a directory.

7. What is the output of this program?

  1.     import java.io.*;
  2.     class files 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             File obj = new File("/java/system");
  7.             System.out.print(obj.getName());
  8.         }
  9.     }

a) java
b) system
c) java/system
d) /java/system

Answer

Answer: b [Reason:] obj.getName() returns the name of the file.
Output:
$ javac files.java
$ java files
system

8. What is the output of this program?

  1.     import java.io.*;
  2.     class files 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             File obj = new File("/java/system");
  7.             System.out.print(obj.getAbsolutePath());
  8.         }
  9.     }

Note: file is made in c drive.
a) java
b) system
c) java/system
d) /java/system

Answer

Answer: d [Reason:] None.
Output:
$ javac files.java
$ java files
javasystem

9. What is the output of this program?

  1.     import java.io.*;
  2.     class files 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             File obj = new File("/java/system");
  7.             System.out.print(obj.canWrite());
  8.             System.out.print(" " + obj.canRead());
  9.         }
  10.     }

Note: file is made in c drive.
a) true false
b) false true
c) true true
d) false false

Answer

Answer: d [Reason:] None.
Output:
$ javac files.java
$ java files
false false

10. What is the output of this program?

  1.     import java.io.*;
  2.     class files 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             File obj = new File("/java/system");
  7.             System.out.print(obj.getParent());
  8.             System.out.print(" " + obj.isFile());
  9.         }
  10.     }

Note: file is made in c drive.
a) java true
b) java false
c) java false
d) java true

Answer

Answer: c [Reason:] getparent() giver the parent directory of the file and isfile() checks weather the present file is a directory or a file in the disk
Output:
$ javac files.java
$ java files
java false

Java MCQ Set 3

1. Which of these classes encapsulate run-time state of an object?
a) Class
b) System
c) Runtime
d) Catche

Answer

Answer: a [Reason:] None.

2. Which of the following constant are defined in Boolean wrapper?
a) TRUE
b) FLASE
c) TYPE
d) All of the mentioned

Answer

Answer: d [Reason:] Boolean wrapper defines 3 constants – TRUE, FLASE & TYPE.

3. Which of these methods returns the class of an object?
a) getClass()
b) Class()
c) WhoseClass()
d) WhoseObject()

Answer

Answer: a [Reason:] None.

4. Which of these methods is used to know whether a string contains “true”?
a) valueOf()
b) valueOfString()
c) getString()
d) None of the mentioned

Answer

Answer: a [Reason:] valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.

5. Which of these class have only one field?
a) Character
b) Boolean
c) Byte
d) void

Answer

Answer: d [Reason:] Void class has only one field – TYPE, which holds a reference to the Class object for type void. We do not create instance of this class.

6. What is the output of this program?

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = a.getClass();
  18.             System.out.print(obj.getName());
  19.         }
  20.     }

a) X
b) Y
c) a
d) b

Answer

Answer: a [Reason:] getClass() is used to obtain the class of an object, here ‘a’ is an object of class ‘X’. hence a.getClass() returns ‘X’ which is stored in class Class object obj.
Output:
$ javac Output.java
$ java Output
X

7. What is the output of this program?

  1.     class X
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.getSuperclass());
  19.         }
  20.     }

a) X
b) Y
c) class X
d) class Y

Answer

Answer: c [Reason:] getSuperClass() returns the super class of an object. b is an object of class Y which extends class X , Hence Super class of b is X. therefore class X is printed.
Output:
$ javac Output.java
$ java Output
class X

8. What is the output of this program?

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(b.equals(a));
  19.         }
  20.     }

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

Answer

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

9. What is the output of this program?

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.isInstance(a));
  19.         }
  20.     }

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

Answer

Answer: d [Reason:] Although class Y extends class X but still a is not considered related to Y. hence isInstance() returns false.
Output:
$ javac Output.java
$ java Output
false

10. What is the output of this program?

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.isLocalClass());
  19.         }
  20.     }

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

Answer

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

Java MCQ Set 4

1. Which of these is a wrapper for data type int?
a) Integer
b) Long
c) Byte
d) Double

Answer

Answer: a [Reason:] None.

2. Which of the following methods is a method of wrapper Integer for obtaining hash code for the invoking object?
a) int hash()
b) int hashcode()
c) int hashCode()
d) Integer hashcode()

Answer

Answer: c [Reason:] None.

3. Which of these is a super class of wrappers Long, Character & Integer?
a) Long
b) Digits
c) Float
d) Number

Answer

Answer: d [Reason:] Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.

4. Which of these is wrapper for simple data type char?
a) Float
b) Character
c) String
d) Integer

Answer

Answer: b [Reason:] None.

5. Which of the following is method of wrapper Integer for converting the value of an object into int?
a) bytevalue()
b) int intValue();
c) Bytevalue()
d) Byte Bytevalue()

Answer

Answer: b [Reason:] None.

6. Which of these methods is used to obtain value of invoking object as a long?
a) long value()
b) long longValue()
c) Long longvalue()
d) Long Longvalue()

Answer

Answer: b [Reason:] long longValue() is used to obtain value of invoking object as a long.

7. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             char a[] = {'a', '5', 'A', ' '};   
  6.             System.out.print(Character.isDigit(a[0]) + " ");
  7.             System.out.print(Character.isWhitespace(a[3]) + " ");
  8.             System.out.print(Character.isUpperCase(a[2]));
  9.         }
  10.     }

a) true false true
b) false true true
c) true true false
d) false false false

Answer

Answer: b [Reason:] Character.isDigit(a[0]) checks for a[0], whether it is a digit or not, since a[0] i:e ‘a’ is a character false is returned. a[3] is a whitespace hence Character.isWhitespace(a[3]) returns a true. a[2] is an upper case letter i:e ‘A’ hence Character.isUpperCase(a[2]) returns true.
Output:
$ javac Output.java
$ java Output
false true true

8. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Integer i = new Integer(257);  
  6.             byte x = i.byteValue();
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 1
c) 256
d) 257

Answer

Answer: b [Reason:] i.byteValue() method returns the value of wrapper i as a byte value. i is 257, range of byte is 256 therefore i value exceeds byte range by 1 hence 1 is returned and stored in x.
Output:
$ javac Output.java
$ java Output
1

9. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Integer i = new Integer(257);  
  6.             float x = i.floatValue();
  7.             System.out.print(x);
  8.         }
  9.     }

a) 0
b) 1
c) 257
d) 257.0

Answer

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

10. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Long i = new Long(256);  
  6.             System.out.print(i.hashCode());
  7.         }
  8.     }

a) 256
b) 256.0
c) 256.00
d) 257.00

Answer

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

Java MCQ Set 5

1. Which of these classes is not included in java.lang?
a) Byte
b) Integer
c) Array
d) Class

Answer

Answer: c [Reason:] Array class is a member of java.util.

2. Which of these is a process of converting a simple data type into a class?
a) type wrapping
b) type conversion
c) type casting
d) none of the Mentioned.

Answer

Answer: a [Reason:] None.

3. Which of these is a super class of wrappers Double & Integer?
a) Long
b) Digits
c) Float
d) Number

Answer

Answer: d [Reason:] Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.

4. Which of these is wrapper for simple data type float?
a) float
b) double
c) Float
d) Double

Answer

Answer: c [Reason:] None.

5. Which of the following is method of wrapper Float for converting the value of an object into byte?
a) bytevalue()
b) byte byteValue()
c) Bytevalue()
d) Byte Bytevalue().

Answer

Answer: b [Reason:] None.

6. Which of these methods is used to check for infinitely large and small values?
a) isInfinite()
b) isNaN()
c) Isinfinite()
d) IsNaN()

Answer

Answer: a [Reason:] isinfinite() method returns true is the value being tested is infinitely large or small in magnitude.

7. Which of the following package stores all the simple data types in java?
a) lang
b) java
c) util
d) java.packages

Answer

Answer: a [Reason:] None.

8. What is the output of this program?

  1.     class isinfinite_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Double d = new Double(1 / 0.);  
  6.             boolean x = d.isInfinite();
  7.             System.out.print(x);
  8.         }
  9.     }

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

Answer

Answer: c [Reason:] isInfinite() method returns true is the value being tested is infinitely large or small in magnitude. 1/0. is infinitely large in magnitude hence true is stored in x.
Output:
$ javac isinfinite_output.java
$ java isinfinite_output
true

9. What is the output of this program?

  1.     class isNaN_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             Double d = new Double(1 / 0.);  
  6.             boolean x = d.isNaN();
  7.             System.out.print(x);
  8.         }
  9.     }

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

Answer

Answer: d [Reason:] isisNaN() method returns true is the value being tested is a number. 1/0. is infinitely large in magnitude, which cant not be defined as a number hence false is stored in x.
Output:
$ javac isNaN_output.java
$ java isNaN_output
false

10. What is the output of this program?

  1.     class binary 
  2.     {
  3.          public static void main(String args[]) 
  4.          {
  5.              int num = 17;
  6.              System.out.print(Integer.toBinaryString(num));
  7.          }
  8.     }

a) 1001
b) 10011
c) 11011
d) 10001

Answer

Answer: d [Reason:] None.
output:
$ javac binary.java
$ java binary
10001

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.