Java MCQ Number 01071

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?

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

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?

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

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?

  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. 	    Double i = new Double(257.578123456789);  
  6.             float x = i.floatValue();
  7.             System.out.print(x);
  8.         }
  9.     }

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?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Double y = new Double(257.57812);
  6. 	    Double i = new Double(257.578123456789);  
  7.             try
  8.             {
  9. 	        int x = i.compareTo(y);
  10.                 System.out.print(x);
  11.             }
  12.             catch(ClassCastException e)
  13.             {
  14.                 System.out.print("Exception");
  15.             }
  16. }
  17.     }

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?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             int a = Character.MAX_VALUE;
  6.             System.out.print((char)a);
  7.         }
  8.     }

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?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int a = Character.MIN_VALUE;
  6.             System.out.print((char)a);
  7.         }
  8.     }

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?

  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

9. What is the output of this program?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5. 	    char a = (char) 98;
  6.             a = Character.toUpperCase(a);
  7.             System.out.print(a);
  8.         }
  9.     }

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?

  1.     class Output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5. 	    char a = '@';
  6.             boolean x = Character.isLetter(a);
  7.             System.out.print(x);
  8.         }
  9.     }

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?

  1.     class main_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int x = 9;
  6.             if (x == 9) 
  7.             { 
  8.                 int x = 8;
  9.                 System.out.println(x);
  10.             }
  11.         } 
  12.     }

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?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.     } 
  7.     class mainclass 
  8.     {
  9.         public static void main(String args[]) 
  10.         {        
  11.              box obj = new box();
  12.              obj.width = 10;
  13.              obj.height = 2;
  14.              obj.length = 10;
  15.              int y = obj.width * obj.height * obj.length; 
  16.              System.out.print(y);
  17.         } 
  18.     }

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?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.     } 
  7.     class mainclass 
  8.     {
  9.         public static void main(String args[]) 
  10.         {        
  11.             box obj1 = new box();
  12.             box obj2 = new box();
  13.             obj1.height = 1;
  14.             obj1.length = 2;
  15.             obj1.width = 1;
  16.             obj2 = obj1;
  17.             System.out.println(obj2.height);
  18.         } 
  19.     }

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?

  1.    class box 
  2.    {
  3.         int width;
  4.         int height;
  5.         int length;
  6.    } 
  7.     class mainclass 
  8.     {
  9.         public static void main(String args[]) 
  10.         {        
  11.             box obj = new box();
  12.             System.out.println(obj);
  13.         } 
  14.     }

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?

  1.     import java.util.*;
  2.     class Collection_Algos 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12. 	    while(i.hasNext())
  13. 	        System.out.print(i.next() + " ");
  14.         }
  15.     }

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?

  1.     import java.util.*;
  2.     class Collection_Algos 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    while(i.hasNext())
  14. 	        System.out.print(i.next() + " ");
  15.         }
  16.     }

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?

  1.     import java.util.*;
  2.     class Collection_Algos 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    Collections.sort(list);
  14.             while(i.hasNext())
  15. 	        System.out.print(i.next() + " ");
  16.         }
  17.     }

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?

  1.     import java.util.*;
  2.     class Collection_Algos 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList list = new LinkedList();
  7.             list.add(new Integer(2));
  8.             list.add(new Integer(8));
  9.             list.add(new Integer(5));
  10.             list.add(new Integer(1));
  11.             Iterator i = list.iterator();
  12.             Collections.reverse(list);
  13. 	    Collections.shuffle(list);
  14.             while(i.hasNext())
  15. 	        System.out.print(i.next() + " ");
  16.         }
  17.     }

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?

  1.     import java.util.*;
  2.     class Maps 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             TreeMap obj = new TreeMap();
  7.             obj.put("A", new Integer(1));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(3));
  10.             System.out.println(obj.entrySet());
  11.         }
  12.     }

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?

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             obj.removeAll(obj);
  11.             System.out.println(obj.isEmpty());
  12.         }
  13.     }

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?

  1.     import java.util.*;
  2.     class Array 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             int array[] = new int [5];
  7.             for (int i = 5; i > 0; i--)
  8.                 array[5 - i] = i;
  9.             Arrays.sort(array);
  10.             for (int i = 0; i < 5; ++i)
  11.             System.out.print(array[i]);;
  12.         }
  13.     }

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?

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             obj.removeAll(obj);
  11.             System.out.println(obj.isEmpty());
  12.         }
  13.     }

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?

  1.     import java.util.*;
  2.     class Bitset 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             BitSet obj = new BitSet(5);
  7.             for (int i = 0; i < 5; ++i)
  8.                 obj.set(i);
  9.             obj.clear(2);
  10.             System.out.print(obj);
  11.         }
  12.     }

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}

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.