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?
-
import java.io.*;
-
class filesinputoutput
-
{
-
public static void main(String args[])
-
{
-
InputStream obj = new FileInputStream("inputoutput.java");
-
System.out.print(obj.available());
-
}
-
}
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?
-
import java.io.*;
-
class Chararrayinput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abcdef";
-
int length = obj.length();
-
char c[] = new char[length];
-
obj.getChars(0,length,c,0);
-
CharArrayReader input1 = new CharArrayReader(c);
-
CharArrayReader input2 = new CharArrayReader(c, 0, 3);
-
int i;
-
try
-
{
-
while ((i = input1.read()) != -1)
-
{
-
System.out.print((char)i);
-
}
-
}
-
catch (IOException e)
-
{
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
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?
-
import java.io.*;
-
class Chararrayinput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abcdef";
-
int length = obj.length();
-
char c[] = new char[length];
-
obj.getChars(0, length, c, 0);
-
CharArrayReader input1 = new CharArrayReader(c);
-
CharArrayReader input2 = new CharArrayReader(c, 0, 3);
-
int i;
-
try
-
{
-
while ((i = input2.read()) != -1)
-
{
-
System.out.print((char)i);
-
}
-
}
-
catch (IOException e)
-
{
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
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?
-
import java.io.*;
-
class Chararrayinput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abcdefgh";
-
int length = obj.length();
-
char c[] = new char[length];
-
obj.getChars(0, length, c, 0);
-
CharArrayReader input1 = new CharArrayReader(c);
-
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
-
int i;
-
int j;
-
try
-
{
-
while ((i = input1.read()) == (j = input2.read()))
-
{
-
System.out.print((char)i);
-
}
-
}
-
catch (IOException e)
-
{
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
}
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?
-
import java.io.*;
-
class files
-
{
-
public static void main(String args[])
-
{
-
File obj = new File("/java/system");
-
System.out.print(obj.getName());
-
}
-
}
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?
-
import java.io.*;
-
class files
-
{
-
public static void main(String args[])
-
{
-
File obj = new File("/java/system");
-
System.out.print(obj.getAbsolutePath());
-
}
-
}
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?
-
import java.io.*;
-
class files
-
{
-
public static void main(String args[])
-
{
-
File obj = new File("/java/system");
-
System.out.print(obj.canWrite());
-
System.out.print(" " + obj.canRead());
-
}
-
}
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?
-
import java.io.*;
-
class files
-
{
-
public static void main(String args[])
-
{
-
File obj = new File("/java/system");
-
System.out.print(obj.getParent());
-
System.out.print(" " + obj.isFile());
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = a.getClass();
-
System.out.print(obj.getName());
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = b.getClass();
-
System.out.print(obj.getSuperclass());
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = b.getClass();
-
System.out.print(b.equals(a));
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = b.getClass();
-
System.out.print(obj.isInstance(a));
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = b.getClass();
-
System.out.print(obj.isLocalClass());
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
char a[] = {'a', '5', 'A', ' '};
-
System.out.print(Character.isDigit(a[0]) + " ");
-
System.out.print(Character.isWhitespace(a[3]) + " ");
-
System.out.print(Character.isUpperCase(a[2]));
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Integer i = new Integer(257);
-
byte x = i.byteValue();
-
System.out.print(x);
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Integer i = new Integer(257);
-
float x = i.floatValue();
-
System.out.print(x);
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Long i = new Long(256);
-
System.out.print(i.hashCode());
-
}
-
}
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?
-
class isinfinite_output
-
{
-
public static void main(String args[])
-
{
-
Double d = new Double(1 / 0.);
-
boolean x = d.isInfinite();
-
System.out.print(x);
-
}
-
}
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?
-
class isNaN_output
-
{
-
public static void main(String args[])
-
{
-
Double d = new Double(1 / 0.);
-
boolean x = d.isNaN();
-
System.out.print(x);
-
}
-
}
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?
-
class binary
-
{
-
public static void main(String args[])
-
{
-
int num = 17;
-
System.out.print(Integer.toBinaryString(num));
-
}
-
}
a) 1001
b) 10011
c) 11011
d) 10001
Answer
Answer: d [Reason:] None.
output:
$ javac binary.java
$ java binary
10001