Java MCQ Set 1
1. Which of these methods of Byte wrapper can be used to obtain Byte object from a string?
a) toString()
b) getString()
c) decode()
d) encode()
Answer
Answer: c [Reason:] decode() methods returns a Byte object that contains the value specified by string.
2. Which of the following methods Byte wrapper return the value as a double?
a) doubleValue()
b) converDouble()
c) getDouble()
d) getDoubleValue()
Answer
Answer: a [Reason:] doubleValue() returns the value of invoking object as double.
3. Which of these is a super class of wrappers Byte and short wrappers?
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 methods is not defined in both Byte and Short wrappers?
a) intValue()
b) isInfinite()
c) toString()
d) hashCode()
Answer
Answer: b [Reason:] isInfinite() methods is defined in Integer and Long Wrappers, returns true if specified value is an infinite value otherwise it returns false.
5. Which of these exceptions is thrown by compareTo() method defined in double wrapper?
a) IOException
b) SystemException
c) CastException
d) ClassCastException
Answer
Answer: d [Reason:] compareTo() methods compare the specified object to be double, if it is not then ClassCastException is thrown.
6. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Double i = new Double(257.5);
-
Double x = i.MAX_VALUE;
-
System.out.print(x);
-
}
-
}
a) 0
b) 1.7976931348623157E308
c) 1.7976931348623157E30
d) None of the mentioned
Answer
Answer: b [Reason:] The super class of Double class defines a constant MAX_VALUE above which a number is considered to be infinity. MAX_VALUE is 1.7976931348623157E308.
Output:
$ javac Output.java
$ java Output
1.7976931348623157E308
7. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Double i = new Double(257.5);
-
Double x = i.MIN_VALUE;
-
System.out.print(x);
-
}
-
}
a) 0
b) 4.9E-324
c) 1.7976931348623157E308
d) None of the mentioned
Answer
Answer: b [Reason:] The super class of Byte class defines a constant MIN_VALUE below which a number is considered to be negative infinity. MIN_VALUE is 4.9E-324.
Output:
$ javac Output.java
$ java Output
4.9E-324
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[])
-
{
-
Double i = new Double(257.578123456789);
-
float x = i.floatValue();
-
System.out.print(x);
-
}
-
}
a) 0
b) 257.0
c) 257.57812
d) 257.578123456789
Answer
Answer: c [Reason:] floatValue() converts the value of wrapper i into float, since float can measure till 5 places after decimal hence 257.57812 is stored in floating point variable x.
Output:
$ javac Output.java
$ java Output
257.57812
10. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
Double y = new Double(257.57812);
-
Double i = new Double(257.578123456789);
-
try
-
{
-
int x = i.compareTo(y);
-
System.out.print(x);
-
}
-
catch(ClassCastException e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
a) 0
b) 1
c) Exception
d) None of the mentioned
Answer
Answer: b [Reason:] i.compareTo() methods two double values, if they are equal then 0 is returned and if not equal then 1 is returned, here 257.57812 and 257.578123456789 are not equal hence 1 is returned and stored in x.
Output:
$ javac Output.java
$ java Output
1
Java MCQ Set 2
1. Which of these methods of Character wrapper can be used to obtain the char value contained in Character object.
a) get()
b) getVhar()
c) charValue()
d) getCharacter()
Answer
Answer: c [Reason:] To obtain the char value contained in a Character object, we use charValue() method.
2. Which of the following constant are defined in Character wrapper?
a) MAX_RADIX
b) MAX_VALUE
c) TYPE
d) All of the mentioned
Answer
Answer: d [Reason:] Character wrapper defines 5 constants – MAX_RADIX, MIN_RADIX, MAX_VALUE, MIN_VALUE & TYPE.
3. Which of these is a super class of Character wrapper?
a) Long
b) Digits
c) Float
d) Number
Answer
Answer: d [Reason:] Number is an abstract class containing subclasses Double, Float, Byte, Short, Character, Integer and Long.
4. Which of these methods is used to know whether a given Character object is part of Java’s Identifiers?
a) isIdentifier()
b) isJavaIdentifier()
c) isJavaIdentifierPart()
d) none of the mentioned
Answer
Answer: c [Reason:] None.
5. Which of these coding techniques is used by method isDefined()?
a) Latin
b) ASCII
c) ANSI
d) UNICODE
Answer
Answer: d [Reason:] isDefined() returns true if ch is defined by Unicode. Otherwise, it returns false.
6. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
int a = Character.MAX_VALUE;
-
System.out.print((char)a);
-
}
-
}
a) <
b) >
c) ?
d) $
Answer
Answer: c [Reason:] Character.MAX_VALUE returns the largest character value, which is of character ‘?’.
Output:
$ javac Output.java
$ java Output
?
7. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
int a = Character.MIN_VALUE;
-
System.out.print((char)a);
-
}
-
}
a) <
b) !
c) @
d) Space
Answer
Answer: d [Reason:] Character.MIN_VALUE returns the smallest character value, which is of space character ‘ ‘.
Output:
$ javac Output.java
$ java Output
8. 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
9. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
char a = (char) 98;
-
a = Character.toUpperCase(a);
-
System.out.print(a);
-
}
-
}
a) b
b) c
c) B
d) C
Answer
Answer: c [Reason:] None.
Output:
$ javac Output.java
$ java Output
B
10. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
char a = '@';
-
boolean x = Character.isLetter(a);
-
System.out.print(x);
-
}
-
}
a) b
b) c
c) B
d) C
Answer
Answer: c [Reason:] None.
Output:
$ javac Output.java
$ java Output
B
Java MCQ Set 3
1. What is the stored in the object obj in following lines of code?
box obj;
a) Memory address of allocated memory of object
b) NULL
c) Any arbitrary pointer
d) Garbage
Answer
Answer: b [Reason:] Memory is allocated to an object using new operator. box obj; just declares a reference to object, no memory is allocated to it hence it points to NULL.
2. Which of these keywords is used to make a class?
a) class
b) struct
c) int
d) none of the mentioned
Answer
Answer: a [Reason:] None.
3. Which of the following is a valid declaration of an object of class Box?
a) Box obj = new Box();
b) Box obj = new Box;
c) obj = new Box();
d) new Box obj;
Answer
Answer: a [Reason:] None.
4. Which of these operators is used to allocate memory for an object?
a) malloc
b) alloc
c) new
d) give
Answer
Answer: c [Reason:] Operator new dynamically allocates memory for an object and returns a reference to it. This reference is address in memory of the object allocated by new.
5. Which of these statement is incorrect?
a) Every class must contain a main() method
b) Applets do not require a main() method at all
c) There can be only one main() method in a program
d) main() method must be made public
Answer
Answer: a [Reason:] Every class does not need to have a main() method, there can be only one main() method which is made public.
6. What is the output of this program?
-
class main_class
-
{
-
public static void main(String args[])
-
{
-
int x = 9;
-
if (x == 9)
-
{
-
int x = 8;
-
System.out.println(x);
-
}
-
}
-
}
a) 9
b) 8
c) Compilation error
d) Runtime error
Answer
Answer: c [Reason:] Two variables with the same name can’t be created in a class.
output:
$ javac main_class.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Duplicate local variable x
7. Which of the following statements is correct?
a) Public method is accessible to all other classes in the hierarchy
b) Public method is accessible only to subclasses of its parent class
c) Public method can only be called by object of its class
d) Public method can be accessed by calling object of the public class
Answer
Answer: a [Reason:] None.
8. What is the output of this program?
-
class box
-
{
-
int width;
-
int height;
-
int length;
-
}
-
class mainclass
-
{
-
public static void main(String args[])
-
{
-
box obj = new box();
-
obj.width = 10;
-
obj.height = 2;
-
obj.length = 10;
-
int y = obj.width * obj.height * obj.length;
-
System.out.print(y);
-
}
-
}
a) 12
b) 200
c) 400
d) 100
Answer
Answer: b [Reason:] None.
output:
$ javac mainclass.java
$ java mainclass
200
9. What is the output of this program?
-
class box
-
{
-
int width;
-
int height;
-
int length;
-
}
-
class mainclass
-
{
-
public static void main(String args[])
-
{
-
box obj1 = new box();
-
box obj2 = new box();
-
obj1.height = 1;
-
obj1.length = 2;
-
obj1.width = 1;
-
obj2 = obj1;
-
System.out.println(obj2.height);
-
}
-
}
a) 1
b) 2
c) Runtime error
d) Garbage value
Answer
Answer: a [Reason:] When we assign an object to another object of same type, all the elements of right side object gets copied to object on left side of equal to, =, operator.
output:
$ javac mainclass.java
$ java mainclass
1
10. What is the output of this program?
-
class box
-
{
-
int width;
-
int height;
-
int length;
-
}
-
class mainclass
-
{
-
public static void main(String args[])
-
{
-
box obj = new box();
-
System.out.println(obj);
-
}
-
}
a) 0
b) 1
c) Runtime error
Answer
Answer: d
output:
$ javac mainclass.java
$ java mainclass
Java MCQ Set 4
1. Which of these is an incorrect form of using method max() to obtain maximum element?
a) max(Collection c)
b) max(Collection c, Comparator comp)
c) max(Comparator comp)
d) max(List c)
Answer
Answer: c [Reason:] Its illegal to call max() only with comparator, we need to give the collection to be serched into.
2. Which of these methods sets every element of a List to a specified object?
a) set()
b) fill()
c) Complete()
d) add()
Answer
Answer: b [Reason:] None.
3. Which of these methods can randomize all elements in a list?
a) rand()
b) randomize()
c) shuffle()
d) ambigous()
Answer
Answer: c [Reason:] shuffle – randomizes all the elements in a list.
4. Which of these methods can convert an object into a List?
a) SetList()
b) ConvertList()
c) singletonList()
d) CopyList()
Answer
Answer: c [Reason:] singletonList() returns the object as an immutable List. This is a easy way to convert a single object into a list. This was added by Java 2.0.
5. Which of these is true about unmodifiableCollection() method?
a) unmodifiableCollection() returns a collection that cannot be modified
b) unmodifiableCollection() method is available only for List and Set
c) unmodifiableCollection() is defined in Collection class
d) none of the mentioned
Answer
Answer: b [Reason:] unmodifiableCollection() is available for al collections, Set, Map, List etc.
6. Which of these is static variable defined in Collections?
a) EMPTY_SET
b) EMPTY_LIST
c) EMPTY_MAP
d) All of the mentioned
Answer
Answer: d [Reason:] None.
7. What is the output of this program?
-
import java.util.*;
-
class Collection_Algos
-
{
-
public static void main(String args[])
-
{
-
LinkedList list = new LinkedList();
-
list.add(new Integer(2));
-
list.add(new Integer(8));
-
list.add(new Integer(5));
-
list.add(new Integer(1));
-
Iterator i = list.iterator();
-
while(i.hasNext())
-
System.out.print(i.next() + " ");
-
}
-
}
a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5
Answer
Answer: a [Reason:] None.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
2 8 5 1
8. What is the output of this program?
-
import java.util.*;
-
class Collection_Algos
-
{
-
public static void main(String args[])
-
{
-
LinkedList list = new LinkedList();
-
list.add(new Integer(2));
-
list.add(new Integer(8));
-
list.add(new Integer(5));
-
list.add(new Integer(1));
-
Iterator i = list.iterator();
-
Collections.reverse(list);
-
while(i.hasNext())
-
System.out.print(i.next() + " ");
-
}
-
}
a) 2 8 5 1
b) 1 5 8 2
c) 2
d) 2 1 8 5
Answer
Answer: b [Reason:] Collections.reverse(list) reverses the given list, the list was 2->8->5->1 after reversing it became 1->5->8->2.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
1 5 8 2
9. What is the output of this program?
-
import java.util.*;
-
class Collection_Algos
-
{
-
public static void main(String args[])
-
{
-
LinkedList list = new LinkedList();
-
list.add(new Integer(2));
-
list.add(new Integer(8));
-
list.add(new Integer(5));
-
list.add(new Integer(1));
-
Iterator i = list.iterator();
-
Collections.reverse(list);
-
Collections.sort(list);
-
while(i.hasNext())
-
System.out.print(i.next() + " ");
-
}
-
}
a) 2 8 5 1
b) 1 5 8 2
c) 1 2 5 8
d) 2 1 8 5
Answer
Answer: c [Reason:] Collections.sort(list) sorts the given list, the list was 2->8->5->1 after sorting it became 1->2->5->8.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
1 5 8 2
10. What is the output of this program?
-
import java.util.*;
-
class Collection_Algos
-
{
-
public static void main(String args[])
-
{
-
LinkedList list = new LinkedList();
-
list.add(new Integer(2));
-
list.add(new Integer(8));
-
list.add(new Integer(5));
-
list.add(new Integer(1));
-
Iterator i = list.iterator();
-
Collections.reverse(list);
-
Collections.shuffle(list);
-
while(i.hasNext())
-
System.out.print(i.next() + " ");
-
}
-
}
a) 2 8 5 1
b) 1 5 8 2
c) 1 2 5 8
d) Any random order
Answer
Answer: d [Reason:] shuffle – randomizes all the elements in a list.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
1 5 2 8
(output will be different on your system)
Java MCQ Set 5
1. Which of these interface declares core method that all collections will have?
a) set
b) EventListner
c) Comparator
d) Collection
Answer
Answer: d [Reason:] Collection interfaces defines core methods that all the collections like set, map, arrays etc will have.
2. Which of these interface handle sequences?
a) Set
b) List
c) Comparator
d) Collection
Answer
Answer: b [Reason:] None.
3. Which of these interface is not a part of Java’s collection framework?
a) List
b) Set
c) SortedMap
d) SortedList
Answer
Answer: d [Reason:] SortedList is not a part of collection framework.
4. Which of these interface must contain a unique element?
a) Set
b) List
c) Array
d) Collection
Answer
Answer: a [Reason:] Set interface extends collection interface to handle sets, which must contain unique elements.
5. Which of these is Basic interface that all other interface inherits?
a) Set
b) Array
c) List
d) Collection
Answer
Answer: d [Reason:] Collection interface is inherited by all other interfaces like Set, Array, Map etc. It defines core methods that all the collections like set, map, arrays etc will have
6. What is the output of this program?
-
import java.util.*;
-
class Maps
-
{
-
public static void main(String args[])
-
{
-
TreeMap obj = new TreeMap();
-
obj.put("A", new Integer(1));
-
obj.put("B", new Integer(2));
-
obj.put("C", new Integer(3));
-
System.out.println(obj.entrySet());
-
}
-
}
a) [A, B, C].
b) [1, 2, 3].
c) {A=1, B=2, C=3}
d) [A=1, B=2, C=3].
Answer
Answer: d [Reason:] obj.entrySet() method is used to obtain a set that contains the entries in the map. This method provides set view of the invoking map.
Output:
$ javac Maps.java
$ java Maps
[A=1, B=2, C=3].
7. What is the output of this program?
-
import java.util.*;
-
class vector
-
{
-
public static void main(String args[])
-
{
-
Vector obj = new Vector(4,2);
-
obj.addElement(new Integer(3));
-
obj.addElement(new Integer(2));
-
obj.addElement(new Integer(5));
-
obj.removeAll(obj);
-
System.out.println(obj.isEmpty());
-
}
-
}
a) 0
b) 1
c) true
d) false
Answer
Answer: c [Reason:] firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns true.
Output:
$ javac vector.java
$ java vector
true
8. What is the output of this program?
-
import java.util.*;
-
class Array
-
{
-
public static void main(String args[])
-
{
-
int array[] = new int [5];
-
for (int i = 5; i > 0; i--)
-
array[5 - i] = i;
-
Arrays.sort(array);
-
for (int i = 0; i < 5; ++i)
-
System.out.print(array[i]);;
-
}
-
}
a) 12345
b) 54321
c) 1234
d) 5432
Answer
Answer: a [Reason:] Arrays.sort(array) method sorts the array into 1,2,3,4,5.
Output:
$ javac Array.java
$ java Array
12345
9. What is the output of this program?
-
import java.util.*;
-
class vector
-
{
-
public static void main(String args[])
-
{
-
Vector obj = new Vector(4,2);
-
obj.addElement(new Integer(3));
-
obj.addElement(new Integer(2));
-
obj.addElement(new Integer(5));
-
obj.removeAll(obj);
-
System.out.println(obj.isEmpty());
-
}
-
}
a) 0
b) 1
c) true
d) false
Answer
Answer: c [Reason:] firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns true.
Output:
$ javac vector.java
$ java vector
true
10. What is the output of this program?
-
import java.util.*;
-
class Bitset
-
{
-
public static void main(String args[])
-
{
-
BitSet obj = new BitSet(5);
-
for (int i = 0; i < 5; ++i)
-
obj.set(i);
-
obj.clear(2);
-
System.out.print(obj);
-
}
-
}
a) {0, 1, 3, 4}
b) {0, 1, 2, 4}
c) {0, 1, 2, 3, 4}
d) {0, 0, 0, 3, 4}
Answer
Answer: a [Reason:] None.
Output:
$ javac Bitset.java
$ java Bitset
{0, 1, 3, 4}