Java MCQ Set 1
1. Which of these class is superclass of all other classes?
a) Math
b) Process
c) System
d) Object
Answer
Answer: d [Reason:] The object class class is superclass of all other classes.
2. Which of these method of Object class can generate duplicate copy of the object on which it is called?
a) clone()
b) copy()
c) dublicate()
d) dito()
Answer
Answer: a [Reason:] None.
3. What is the value of double constant ‘E’ defined in Math class?
a) approximately 3
b) approximately 3.14
c) approximately 2.72
d) approximately 0
Answer
Answer: c [Reason:] None.
4. Which of these method is a rounding function of Math class?
a) max()
b) min()
c) abs()
d) all of the mentioned
Answer
Answer: d [Reason:] max(), min() and abs() are all rounding functions.
5. Which of these class contains only floating point functions?
a) Math
b) Process
c) System
d) Object
Answer
Answer: a [Reason:] Math class contains all the floating point functions that are used for geometry, trignometry, as well as several general purpose methods. Example : sin(), cos(), exp(), sqrt() etc.
6. Which of these class encapsulate the run time state of an object or an interface?
a) Class
b) Object
c) Runtime
d) System
Answer
Answer: a [Reason:] None.
7. What is the value of “d” after this line of code has been executed?
double d = Math.round ( 2.5 + Math.random() );
a) 2
b) 3
c) 4
d) 2.5
Answer
Answer: b [Reason:] The Math.random() method returns a number greater than or equal to 0 and less than 1. so 2.5 will be greater than or equal to 2.5 and less than 3.5, we can be sure that Math.round() will round that number to 3.
8. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
int x = 3.14;
-
int y = (int) Math.abs(x);
-
System.out.print(y);
-
}
-
}
a) 0
b) 3
c) 3.0
d) 3.1
Answer
Answer: b [Reason:] None.
Output:
$ javac Output.java
$ java Output
3
9. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
double x = 3.1;
-
double y = 4.5;
-
double z = Math.max( x, y );
-
System.out.print(z);
-
}
-
}
a) true
b) flase
c) 3.1
d) 4.5
Answer
Answer: d [Reason:] None.
Output:
$ javac Output.java
$ java Output
4.5
10. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
double x = 2.0;
-
double y = 3.0;
-
double z = Math.pow( x, y );
-
System.out.print(z);
-
}
-
}
a) 2.0
b) 4.0
c) 8.0
d) 9.0
Answer
Answer: c [Reason:] Math.pow(x, y) methods returns value of y to the power x, i:e x ^ y, 2.0 ^ 3.0 = 8.0.
Output:
$ javac Output.java
$ java Output
8.0
Java MCQ Set 2
1. Which of these class have only one field ‘TYPE’?
a) Void
b) Process
c) System
d) Runtime
Answer
Answer: a [Reason:] The Void class has one field, TYPE, which holds a reference to the Class object for the type void.
2. Which of the following method of Process class can terminate a process?
a) void kill()
b) void destroy()
c) void terminate()
d) void exit()
Answer
Answer: b [Reason:] Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.
3. Standard output variable ‘out’ is defined in which class?
a) Void
b) Process
c) Runtime
d) System
Answer
Answer: d [Reason:] Standard output variable ‘out’ is defined in System class. out is usually used in print statement i:e System.out.print().
4. Which of these class can encapsulate an entire executing program?
a) Void
b) Process
c) Runtime
d) System
Answer
Answer: b [Reason:] None.
5. Which of the following is method of System class is used to find how long a program takes to execute?
a) currenttime()
b) currentTime()
c) currentTimeMillis()
d) currenttimeMillis()
Answer
Answer: c [Reason:] None.
6. Which of these class holds a collection of static methods and variables?
a) Void
b) Process
c) Runtime
d) System
Answer
Answer: d [Reason:] System class holds a collection of static methods and variables. The standard input, output and error output of java run time are stored in the in, out and err variables of System class.
7. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
long start, end;
-
start = System.currentTimeMillis();
-
for (int i = 0; i < 10000000; i++);
-
end = System.currentTimeMillis();
-
System.out.print(end - start);
-
}
-
}
a) 0
b) 1
c) 1000
d) System Dependent
Answer
Answer: d [Reason:] end time is the time taken by loop to execute it can be any non zero value depending on the System.
Output:
$ javac Output.java
$ java Output
78
8. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
byte a[] = { 65, 66, 67, 68, 69, 70 };
-
byte b[] = { 71, 72, 73, 74, 75, 76 };
-
System.arraycopy(a , 0, b, 0, a.length);
-
System.out.print(new String(a) + " " + new String(b));
-
}
-
}
a) ABCDEF ABCDEF
b) ABCDEF GHIJKL
c) GHIJKL ABCDEF
d) GHIJKL GHIJKL
Answer
Answer: a [Reason:] System.arraycopy() is a method of class System which is used to copy a string into another string.
Output:
$ javac Output.java
$ java Output
ABCDEF ABCDEF
9. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
byte a[] = { 65, 66, 67, 68, 69, 70 };
-
byte b[] = { 71, 72, 73, 74, 75, 76 };
-
System.arraycopy(a, 2, b, 1, a.length-2);
-
System.out.print(new String(a) + " " + new String(b));
-
}
-
}
a) ABCDEF GHIJKL
b) ABCDEF GCDEFL
c) GHIJKL ABCDEF
d) GCDEFL GHIJKL
Answer
Answer: b [Reason:] None.
Output:
$ javac Output.java
$ java Output
ABCDEF GCDEFL
10. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
byte a[] = { 65, 66, 67, 68, 69, 70 };
-
byte b[] = { 71, 72, 73, 74, 75, 76 };
-
System.arraycopy(a, 1, b, 3, 0);
-
System.out.print(new String(a) + " " + new String(b));
-
}
-
}
a) ABCDEF GHIJKL
b) ABCDEF GCDEFL
c) GHIJKL ABCDEF
d) GCDEFL GHIJKL
Answer
Answer: a [Reason:] Since last parameter of System.arraycopy(a,1,b,3,0) is 0 nothing is copied from array a to array b, hence b remains as it is.
Output:
$ javac Output.java
$ java Output
ABCDEF GHIJKL
Java MCQ Set 3
1. Which of these class object can be used to form a dynamic array?
a) ArrayList
b) Map
c) Vector
d) ArrayList & Vector
Answer
Answer: d [Reason:] Vectors are dynamic arrays, it contains many legacy methods that are not part of collection framework, and hence these methods are not present in ArrayList. But both are used to form dynamic arrays.
2. Which of these are legacy classes?
a) Stack
b) Hashtable
c) Vector
d) All of the mentioned
Answer
Answer: d [Reason:] Stack, Hashtable, Vector, Properties and Dictionary are legacy classes.
3. Which of these is the interface of legacy?
a) Map
b) Enumeration
c) HashMap
d) Hashtable
Answer
Answer: b [Reason:] None.
4. What is the name of data member of class Vector which is used to store number of elements in the vector?
a) length
b) elements
c) elementCount
d) capacity
Answer
Answer: c [Reason:] None.
5. Which of these methods is used to add elements in vector at specific location?
a) add()
b) set()
c) AddElement()
d) addElement()
Answer
Answer: d [Reason:] addElement() is used to add data in the vector, to obtain the data we use elementAt() and to first and last element we use firstElement() and lastElement() respectively.
6. 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));
-
System.out.println(obj.elementAt(1));
-
}
-
}
a) 0
b) 3
c) 2
d) 5
Answer
Answer: c [Reason:] obj.elementAt(1) returns the value stored at index 1, which is 2.
Output:
$ javac vector.java
$ java vector
2
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));
-
System.out.println(obj.capacity());
-
}
-
}
a) 2
b) 3
c) 4
d) 6
Answer
Answer: c [Reason:] None.
Output:
$ javac vector.java
$ java vector
4
8. 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(6));
-
obj.insertElementAt(new Integer(8), 2);
-
System.out.println(obj);
-
}
-
}
a) [3, 2, 6].
b) [3, 2, 8].
c) [3, 2, 6, 8].
d) [3, 2, 8, 6].
Answer
Answer: d [Reason:] None.
Output:
$ javac vector.java
$ java vector
[3, 2, 8, 6].
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 stack
-
{
-
public static void main(String args[])
-
{
-
Stack obj = new Stack();
-
obj.push(new Integer(3));
-
obj.push(new Integer(2));
-
obj.pop();
-
obj.push(new Integer(5));
-
System.out.println(obj);
-
}
-
}
a) [3, 5].
b) [3, 2].
c) [3, 2, 5].
d) [3, 5, 2].
Answer
Answer: a [Reason:] push() and pop() are standard functions of the class stack, push() inserts in the stack and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used which removes 2 from the stack then again push is used to insert 5 hence stack contains elements 3 & 5.
Output:
$ javac stack.java
$ java stack
[3, 5].
Java MCQ Set 4
1. Which of these is long data type literal?
a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246
Answer
Answer: a [Reason:] Data type long literals are appended by an upper or lowercase L. 0x99fffL is hexadecimal long literal.
2. Which of these can be returned by the operator & ?
a) Integer
b) Boolean
c) Character
d) Integer or Boolean
Answer
Answer: d [Reason:] We can use binary ampersand operator on integers/chars (and it returns an integer) or on booleans (and it returns a boolean).
3. Literals in java must be appended by which of these?
a) L
b) l
c) D
d) L and I
Answer
Answer: d [Reason:] Data type long literals are appended by an upper or lowercase L.
4. Literal can be of which of these data types?
a) integer
b) float
c) boolean
d) all of the mentioned
Answer
Answer: d [Reason:] None
5. Which of these can not be used for a variable name in Java?
a) identifier
b) keyword
c) identifier & keyword
d) none of the mentioned
Answer
Answer: b [Reason:] Keywords are specially reserved words which can not be used for naming a user defined variable, example : class, int, for etc.
6. What is the output of this program?
-
class evaluate
-
{
-
public static void main(String args[])
-
{
-
int a[] = {1,2,3,4,5};
-
int d[] = a;
-
int sum = 0;
-
for (int j = 0; j < 3; ++j)
-
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
-
System.out.println(sum);
-
}
-
}
a) 38
b) 39
c) 40
d) 41
Answer
Answer: c [Reason:] None
output:
$ javac evaluate.java
$ java evaluate
40
7. What is the output of this program?
-
class array_output
-
{
-
public static void main(String args[])
-
{
-
int array_variable [] = new int[10];
-
for (int i = 0; i < 10; ++i) {
-
array_variable[i] = i/2;
-
array_variable[i]++;
-
System.out.print(array_variable[i] + " ");
-
i++;
-
}
-
-
}
-
}
a) 0 2 4 6 8
b) 1 2 3 4 5
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
Answer
Answer: b [Reason:] When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.
output:
$ javac array_output.java
$ java array_output
1 2 3 4 5
8. What is the output of this program?
-
class variable_scope
-
{
-
public static void main(String args[])
-
{
-
int x;
-
x = 5;
-
{
-
int y = 6;
-
System.out.print(x + " " + y);
-
}
-
System.out.println(x + " " + y);
-
}
-
}
a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
Answer
Answer: d [Reason:] Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x.
output:
$ javac variable_scope.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem: y cannot be resolved to a variable
9. Which of these is incorrect string literal?
a) “Hello World”
b) “HellonWorld”
c) “”Hello World””
d) “Hello
world”
Answer
Answer: d [Reason:] all string literals must begin and end in same line.
10. What is the output of this program?
-
class dynamic_initialization
-
{
-
public static void main(String args[])
-
{
-
double a, b;
-
a = 3.0;
-
b = 4.0;
-
double c = Math.sqrt(a * a + b * b);
-
System.out.println(c);
-
}
-
}
a) 5.0
b) 25.0
c) 7.0
d) Compilation Error
Answer
Answer: a [Reason:] Variable c has been dynamically initialized to square root of a * a + b * b, during run time.
output:
$ javac dynamic_initialization.java
$ java dynamic_initialization
5.0
Java MCQ Set 5
1. Which of these class contains all the methods present in Math class?
a) SystemMath
b) StrictMath
c) Compiler
d) ClassLoader
Answer
Answer: b [Reason:] SystemMath class defines complete set of mathematical methods that are parallel those in Math class. The difference is that the StrictMath version is guaranteed to generate precisely identical results across all Java implementations.
2. Which of these method return a pseudorandom number?
a) rand()
b) random()
c) randomNumber()
d) randGenerator()
Answer
Answer: b [Reason:] None.
3. Which of these method returns the remainder of dividend / devisor?
a) remainder()
b) getRemainder()
c) CSIremainder()
d) IEEEremainder()
Answer
Answer: d [Reason:] IEEEremainder() returns the remainder of dividend / devisor.
4. Which of these method converts radians to degrees?
a) toRadian()
b) toDegree()
c) convertRadian()
d) converDegree()
Answer
Answer: b [Reason:] None.
5. toRadian() and toDegree() methods were added by which version of Java?
a) Java 1.0
b) Java 1.5
c) Java 2.0
d) Java 3.0
Answer
Answer: c [Reason:] toRadian() and toDegree() methods were added by Java 2.0 before that there was no method which could directly convert degree into radians and vice versa.
6. Which of these method return a smallest whole number greater than or equal to variable X?
a) double ciel(double X)
b) double floor(double X)
c) double max(double X)
d) double min(double X)
Answer
Answer: a [Reason:] ciel(double X) returns the smallest whole number greater than or equal to variable X.
7. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
double x = 3.14;
-
int y = (int) Math.toDegrees(x);
-
System.out.print(y);
-
}
-
}
a) 0
b) 179
c) 180
d) 360
Answer
Answer: b [Reason:] 3.14 in degree 179.9087. We usually take it to be 180. Buts here we have type casted it to integer data type hence 179.
Output:
$ javac Output.java
$ java Output
179
8. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
double x = 3.14;
-
int y = (int) Math.toRadians(x);
-
System.out.print(y);
-
}
-
}
a) 0
b) 3
c) 3.0
d) 3.1
Answer
Answer: a [Reason:] None.
Output:
$ javac Output.java
$ java Output
0
9. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
double x = 102;
-
double y = 5;
-
double z = Math.IEEEremainder(x, y);
-
System.out.print(z);}
-
}
-
}
a) 0
b) 1
c) 2
d) 3
Answer
Answer: b [Reason:] IEEEremainder() returns the remainder of dividend / devisor. Here dividend is 102 and devisor is 5 therefore remainder is 2. It is similar to modulus – ‘%’ operator of C/C++ language.
Output:
$ javac Output.java
$ java Output
2
10. Will this program generate same output is executed again?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
int y = double z = Math.random();
-
System.out.print(y);
-
}
-
}
a) Yes
b) No
c) Compiler Dependent
d) Operating System Dependent
Answer
Answer: b [Reason:] There is no relation between random numbers generated previously in Java.