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’?
-
class Input_Output
-
{
-
public static void main(String args[]) throws IOException
-
{
-
char c;
-
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
-
do
-
{
-
c = (char) obj.read();
-
System.out.print(c);
-
} while(c != 'q');
-
}
-
}
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”?
-
class Input_Output
-
{
-
public static void main(String args[]) throws IOException
-
{
-
char c;
-
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
-
do
-
{
-
c = (char) obj.read();
-
System.out.print(c);
-
} while(c!=''');
-
}
-
}
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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer c = new StringBuffer("Hello");
-
System.out.println(c.length());
-
}
-
}
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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer s1 = new StringBuffer("Hello");
-
StringBuffer s2 = s1.reverse();
-
System.out.println(s2);
-
}
-
}
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?
-
interface calculate
-
{
-
void cal(int item);
-
}
-
class display implements calculate
-
{
-
int x;
-
public void cal(int item)
-
{
-
x = item * item;
-
}
-
}
-
class interfaces
-
{
-
public static void main(String args[])
-
{
-
display arr = new display;
-
arr.x = 0;
-
arr.cal(2);
-
System.out.print(arr.x);
-
}
-
}
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?
-
interface calculate
-
{
-
void cal(int item);
-
}
-
class displayA implements calculate
-
{
-
int x;
-
public void cal(int item)
-
{
-
x = item * item;
-
}
-
}
-
class displayB implements calculate
-
{
-
int x;
-
public void cal(int item)
-
{
-
x = item / item;
-
}
-
}
-
class interfaces
-
{
-
public static void main(String args[])
-
{
-
displayA arr1 = new displayA;
-
displayB arr2 = new displayB;
-
arr1.x = 0;
-
arr2.x = 0;
-
arr1.cal(2);
-
arr2.cal(2);
-
System.out.print(arr1.x + " " + arr2.x);
-
}
-
}
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?
-
interface calculate
-
{
-
int VAR = 0;
-
void cal(int item);
-
}
-
class display implements calculate
-
{
-
int x;
-
public void cal(int item)
-
{
-
if (item<2)
-
x = VAR;
-
else
-
x = item * item;
-
}
-
}
-
class interfaces
-
{
-
-
public static void main(String args[])
-
{
-
display[] arr=new display[3];
-
-
for(int i=0;i<3;i++)
-
arr[i]=new display();
-
arr[0].cal(0);
-
arr[1].cal(1);
-
arr[2].cal(2);
-
System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x);
-
}
-
}
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?
-
import java.util.*;
-
class Arraylist
-
{
-
public static void main(String args[])
-
{
-
ArrayList obj1 = new ArrayList();
-
ArrayList obj2 = new ArrayList();
-
obj1.add("A");
-
obj1.add("B");
-
obj2.add("A");
-
obj2.add(1, "B");
-
System.out.println(obj1.equals(obj2));
-
}
-
}
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?
-
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 Array
-
{
-
public static void main(String args[])
-
{
-
int array[] = new int [5];
-
for (int i = 5; i > 0; i--)
-
array[5-i] = i;
-
Arrays.fill(array, 1, 4, 8);
-
for (int i = 0; i < 5 ; i++)
-
System.out.print(array[i]);
-
}
-
}
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?
-
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);
-
System.out.print(Arrays.binarySearch(array, 4));
-
}
-
}
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?
-
import java.util.*;
-
class Arraylist
-
{
-
public static void main(String args[])
-
{
-
ArrayList obj = new ArrayList();
-
obj.add("A");
-
obj.add("B");
-
obj.add("C");
-
obj.add(1, "D");
-
System.out.println(obj);
-
}
-
}
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?
-
import java.util.*;
-
class Output
-
{
-
public static void main(String args[])
-
{
-
ArrayList obj = new ArrayList();
-
obj.add("A");
-
obj.add(0, "B");
-
System.out.println(obj.size());
-
}
-
}
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?
-
import java.util.*;
-
class Output
-
{
-
public static void main(String args[])
-
{
-
ArrayList obj = new ArrayList();
-
obj.add("A");
-
obj.ensureCapacity(3);
-
System.out.println(obj.size());
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
ArrayList obj = new ArrayList();
-
obj.add("A");
-
obj.add("D");
-
obj.ensureCapacity(3);
-
obj.trimToSize();
-
System.out.println(obj.size());
-
}
-
}
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?
-
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}
7. 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.length() + " " + obj.size());
-
}
-
}
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?
-
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);
-
System.out.print(obj.get(3));
-
}
-
}
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?
-
import java.util.*;
-
class date
-
{
-
public static void main(String args[])
-
{
-
Date obj = new Date();
-
System.out.print(obj);
-
}
-
}
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?
-
import java.util.*;
-
class Bitset
-
{
-
public static void main(String args[])
-
{
-
BitSet obj1 = new BitSet(5);
-
BitSet obj2 = new BitSet(10);
-
for (int i = 0; i < 5; ++i)
-
obj1.set(i);
-
for (int i = 3; i < 13; ++i)
-
obj2.set(i);
-
obj1.and(obj2);
-
System.out.print(obj1);
-
}
-
}
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}