Java MCQ Number 01075

Java MCQ Set 1

1. What does AWT stands for?
a) All Window Tools
b) All Writing Tools
c) Abstract Window Toolkit
d) Abstract Writing Toolkit

Answer

Answer: c [Reason:] AWT stands for Abstract Window Toolkit, it is used by applets to interact with the user.

2. Which of these is used to perform all input & output operations in Java?
a) streams
b) Variables
c) classes
d) Methods

Answer

Answer: a [Reason:] Like in any other language, streams are used for input and output operations.

3. Which of these is a type of stream in Java?
a) Integer stream
b) Short stream
c) Byte stream
d) Long stream

Answer

Answer: c [Reason:] Java defines only two types of streams – Byte stream and character stream.

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 classes are used by character streams for input and output operations?
a) InputStream
b) Writer
c) ReadStream
d) InputOutputStream

Answer

Answer: b [Reason:] Character streams uses Writer and Reader classes for input & output operations.

6. Which of these class is used to read from byte array?
a) InputStream.
b) BufferedInputStream.
c) ArrayInputStream.
d) ByteArrayInputStream.

Answer

Answer: d [Reason:] None.

7. What is the output of this program if input given is ‘abcqfghqbcd’?

  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 != 'q');
  12.         }
  13.     }

a) abcqfgh
b) abc
c) abcq
d) abcqfghq

Answer

Answer: c [Reason:] None.
Output:
$ javac Input_Output.java
$ java Input_Output
abcq

8. 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’

9. 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.              System.out.println(c.length());
  7.         }
  8.     }

a) 4
b) 5
c) 6
d) 7

Answer

Answer: b [Reason:] length() method is used to obtain length of StringBuffer object, length of “Hello” is 5.
Output:
$ javac output.java
$ java output
5

10. 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.            StringBuffer s2 = s1.reverse();
  7.            System.out.println(s2);
  8.         }
  9.     }

a) Hello
b) olleH
c) HelloolleH
d) olleHHello

Answer

Answer: b [Reason:] reverse() method reverses all characters. It returns the reversed object on which it was called.
Output:
$ javac output.java
$ java output
olleH

Java MCQ Set 2

1. Which of these keywords is used to define interfaces in Java?
a) interface
b) Interface
c) intf
d) Intf

Answer

Answer: a [Reason:] None.

2. Which of these can be used to fully abstract a class from its implementation?
a) Objects
b) Packages
c) Interfaces
d) None of the Mentioned

Answer

Answer: c [Reason:] None.

3. Which of these access specifiers can be used for an interface?
a) Public
b) Protected
c) private
d) All of the mentioned

Answer

Answer: a [Reason:] Access specifier of interface is either public or no specifier. When no access specifier is used then default access specifier is used due to which interface is available only to other members of the package in which it is declared, when declared public it can be used by any code.

4. Which of these keywords is used by a class to use an interface defined previously?
a) import
b) Import
c) implements
d) Implements

Answer

Answer: c [Reason:] interface is inherited by a class using implements.

5. Which of the following is correct way of implementing an interface salary by class manager?
a) class manager extends salary {}
b) class manager implements salary {}
c) class manager imports salary {}
d) none of the mentioned

Answer

Answer: b [Reason:] None.

6. Which of the following is incorrect statement about packages?
a) Interfaces specifies what class must do but not how it does
b) Interfaces are specified public if they are to be accessed by any code in the program
c) All variables in interface are implicitly final and static
d) All variables are static and methods are public if interface is defined pubic

Answer

Answer: d [Reason:] All methods and variables are implicitly public if interface is declared public.

7. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages

Answer

Answer: b [Reason:] None.

8. What is the output of this program?

  1.     interface calculate
  2.     {
  3.         void cal(int item);
  4.     }
  5.     class display implements calculate
  6.     {
  7.         int x;
  8.         public void cal(int item)
  9.         {
  10.             x = item * item;            
  11.         }
  12.     }
  13.     class interfaces
  14.     {
  15.         public static void main(String args[])
  16.         {
  17.             display arr = new display;
  18.             arr.x = 0;      
  19.             arr.cal(2);
  20.             System.out.print(arr.x);
  21.         }
  22.     }

a) 0
b) 2
c) 4
d) None of the mentioned

Answer

Answer: c [Reason:] None.
Output:
$ javac interfaces.java
$ java interfaces
4

9. What is the output of this program?

  1.     interface calculate
  2.     {
  3.         void cal(int item);
  4.     }
  5.     class displayA implements calculate
  6.     {
  7.         int x;
  8.         public void cal(int item)
  9.         {
  10.             x = item * item;            
  11.         }
  12.     }
  13.     class displayB implements calculate
  14.     {
  15.         int x;
  16.         public void cal(int item)
  17.         {
  18.             x = item / item;            
  19.         }
  20.     }
  21.     class interfaces 
  22.     {
  23.         public static void main(String args[])
  24.         {
  25.             displayA arr1 = new displayA;
  26.             displayB arr2 = new displayB;
  27.             arr1.x = 0;
  28.             arr2.x = 0;      
  29.             arr1.cal(2);
  30.             arr2.cal(2);
  31.             System.out.print(arr1.x + " " + arr2.x);
  32.         }
  33.     }

a) 0 0
b) 2 2
c) 4 1
d) 1 4

Answer

Answer: c [Reason:] class displayA implements the interface calculate by doubling the value of item, where as class displayB implements the interface by dividing item by item, therefore variable x of class displayA stores 4 and variable x of class displayB stores 1.
Output:
$ javac interfaces.java
$ java interfaces
4 1

10. What is the output of this program?

  1. interface calculate 
  2. {
  3.             int VAR = 0;
  4.             void cal(int item);
  5. }
  6.         class display implements calculate 
  7.         {
  8.             int x;
  9.           public  void cal(int item)
  10.           {
  11.                 if (item<2)
  12.                     x = VAR;
  13.                 else
  14.                     x = item * item;            
  15.             }
  16.         }
  17.  class interfaces 
  18. {
  19.  
  20.             public static void main(String args[]) 
  21.             {
  22.                 display[] arr=new display[3];
  23.  
  24.                for(int i=0;i<3;i++)
  25.                arr[i]=new display();
  26.                arr[0].cal(0);    
  27.                arr[1].cal(1);
  28.                arr[2].cal(2);
  29.                System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x);
  30.             }
  31. }

a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4

Answer

Answer: c [Reason:] None.
output:
$ javac interfaces.java
$ java interfaces
0 0 4

Java MCQ Set 3

1. Which of these standard collection classes implements all the standard functions on list data structure?
a) Array
b) LinkedList
c) HashSet
d) AbstractSet

Answer

Answer: a [Reason:] None.

2. Which of these classes implements Set interface?
a) ArrayList
b) HashSet
c) LinkedList
d) DynamicList

Answer

Answer: b [Reason:] HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.

3. Which of these method is used to make all elements of an equal to specified value?
a) add()
b) fill()
c) all()
d) set()

Answer

Answer: b [Reason:] fill() method assigns a value to all the elements in an array, in other words it fills the array with specified value.

4. Which of these method of Array class is used sort an array or its subset?
a) binarysort()
b) bubblesort()
c) sort()
d) insert()

Answer

Answer: c [Reason:] None.

5. Which of these methods can be used to search an element in a list?
a) find()
b) sort()
c) get()
d) binaryserach()

Answer

Answer: d [Reason:] binaryserach() method uses binary search to find a specified value. This method must be applied to sorted arrays.

6. Which of these method is used to change an element in a LinkedList Object?
a) change()
b) set()
c) redo()
d) add()

Answer

Answer: b [Reason:] An element in a LinkedList object can be changed by first using get() to obtain the index or location of that object and the passing that location to method set() along with its new value.

7. What is the output of this program?

  1.     import java.util.*;
  2.     class Arraylist 
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             ArrayList obj1 = new ArrayList();
  7.             ArrayList obj2 = new ArrayList();
  8.             obj1.add("A");
  9.             obj1.add("B");
  10.             obj2.add("A");
  11.             obj2.add(1, "B");
  12.             System.out.println(obj1.equals(obj2));
  13.         }
  14.     }

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

Answer

Answer: c [Reason:] obj1 and obj2 are an object of class ArrayList hence it is a dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Both the objects obj1 and obj2 contain same elements i:e A & B thus obj1.equals(obj2) method returns true.
Output:
$ javac Arraylist.java
$ java Arraylist
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 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.fill(array, 1, 4, 8);
  10.             for (int i = 0; i < 5 ; i++)
  11.                 System.out.print(array[i]);
  12.         }
  13.     }

a) 12885
b) 12845
c) 58881
d) 54881

Answer

Answer: c [Reason:] array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.
Output:
$ javac Array.java
$ java Array
58881

10. 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.             System.out.print(Arrays.binarySearch(array, 4));
  11.         }
  12.     }

a) 2
b) 3
c) 4
d) 5

Answer

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

Java MCQ Set 4

1. Which of these standard collection classes implements a dynamic array?
a) AbstractList
b) LinkedList
c) ArrayList
d) AbstractSet

Answer

Answer: c [Reason:] ArrayList class implements a dynamic array by extending AbstractList class.

2. Which of these class can generate an array which can increase and decrease in size automatically?
a) ArrayList()
b) DynamicList()
c) LinkedList()
d) DynamicList()

Answer

Answer: a [Reason:] None.

3. Which of these method can be used to increase the capacity of ArrayList object manually?
a) Capacity()
b) increaseCapacity()
c) increasecapacity()
d) ensureCapacity()

Answer

Answer: d [Reason:] When we add an element, the capacity of ArrayList object increases automatically, but we can increase it manually to specified length x by using function ensureCapacity(x);

4. Which of these method of ArrayList class is used to obtain present size of an object?
a) size()
b) length()
c) index()
d) capacity()

Answer

Answer: a [Reason:] None.

5. Which of these methods can be used to obtain a static array from an ArrayList object?
a) Array()
b) covertArray()
c) toArray()
d) covertoArray()

Answer

Answer: c [Reason:] None.

6. Which of these method is used to reduce the capacity of an ArrayList object?
a) trim()
b) trimSize()
c) trimTosize()
d) trimToSize()

Answer

Answer: d [Reason:] trimTosize() is used to reduce the size of the array that underlines an ArrayList object.

7. What is the output of this program?

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

a) [A, B, C, D].
b) [A, D, B, C].
c) [A, D, C].
d) [A, B, C].

Answer

Answer: b [Reason:] obj is an object of class ArrayList hence it is an dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Hence obj.add(1,”D”) stores D at index position 1 of obj and shifts the previous value stored at that position by 1.
Output:
$ javac Arraylist.java
$ java Arraylist
[A, D, B, C].

8. What is the output of this program?

  1.     import java.util.*;
  2.     class Output 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             ArrayList obj = new ArrayList();
  7.             obj.add("A");
  8.             obj.add(0, "B");
  9.             System.out.println(obj.size());
  10.         }
  11.     }

a) 0
b) 1
c) 2
d) Any Garbage Value

Answer

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

9. What is the output of this program?

  1.     import java.util.*;
  2.     class Output 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             ArrayList obj = new ArrayList();
  7.             obj.add("A");
  8.             obj.ensureCapacity(3);
  9.             System.out.println(obj.size());
  10.         }
  11.     }

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

Answer

Answer: a [Reason:] Although obj.ensureCapacity(3); has manually increased the capacity of obj to 3 but the value is stored only at index 0, therefore obj.size() returns the total number of elements stored in the obj i:e 1, it has nothing to do with ensureCapacity().
Output:
$ javac Output.java
$ java Output
1

10. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             ArrayList obj = new ArrayList();
  6.             obj.add("A");
  7.             obj.add("D");
  8.             obj.ensureCapacity(3);
  9.             obj.trimToSize();
  10.             System.out.println(obj.size());
  11.          }      
  12.     }

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

Answer

Answer: b [Reason:] trimTosize() is used to reduce the size of the array that underlines an ArrayList object.
Output:
$ javac Output.java
$ java Output
2

Java MCQ Set 5

1. Which of these class object has architecture similar to that of array?
a) Bitset
b) Map
c) Hashtable
d) All of the mentioned

Answer

Answer: a [Reason:] Bitset class creates a special type of array that holds bit values. This array can increase in size as needed.

2. Which of these method is used to make a bit zero specified by the index?
a) put()
b) set()
c) remove()
d) clear()

Answer

Answer: d [Reason:] None.

3. Which of these method is used to calculate number of bits required to hold the BitSet object?
a) size()
b) length()
c) indexes()
d) numberofBits()

Answer

Answer: b [Reason:] None.

4. Which of these is a method of class Date which is used to search weather object contains a date before the specified date?
a) after()
b) contains()
c) before()
d) compareTo()

Answer

Answer: c [Reason:] before() returns true if the invoking Date object contains a date that is earlier than one specified by date, otherwise it returns false.

5. Which of these methods is used to retrieve elements in BitSet object at specific location?
a) get()
b) Elementat()
c) ElementAt()
d) getProperty()

Answer

Answer: a [Reason:] None.

6. 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}

7. 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.length() + " " + obj.size());
  11.         }
  12.     }

a) 4 64
b) 5 64
c) 5 128
d) 4 128

Answer

Answer: b [Reason:] obj.length() returns the length allotted to object obj at time of initialization and obj.size() returns the size of current object obj, each BitSet element is given 16 bits therefore the size is 4 * 16 = 64, whereas length is still 5.
Output:
$ javac Bitset.java
$ java Bitset
5 64

8. 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.             System.out.print(obj.get(3));
  10.         }
  11.     }

a) 2
b) 3
c) 4
d) 5

Answer

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

9. What is the output of this program?

  1.     import java.util.*;
  2.     class date
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Date obj = new Date();
  7.             System.out.print(obj);
  8.         }
  9.     }

a) Prints Present Date
b) Runtime Error
c) Any Garbage Value
d) Prints Present Time & Date

Answer

Answer: d [Reason:] None.
Output:
$ javac date.java
$ java date
Tue Jun 11 11:29:57 PDT 2013

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 obj1 = new BitSet(5);
  7.             BitSet obj2 = new BitSet(10);
  8.             for (int i = 0; i < 5; ++i)
  9.                 obj1.set(i);
  10.             for (int i = 3; i < 13; ++i)
  11.                 obj2.set(i);
  12.             obj1.and(obj2);
  13.             System.out.print(obj1);
  14.         }
  15.     }

a) {0, 1}
b) {2, 4}
c) {3, 4}
d) {3, 4, 5}

Answer

Answer: c [Reason:] obj1.and(obj2) returns an BitSet object which contains elements common to both the object obj1 and obj2 and stores this BitSet in invoking object that is obj1. Hence obj1 contains 3 & 4.
Output:
$ javac Bitset.java
$ java Bitset
{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.