Java MCQ Set 1
1. Which of these keywords are used to define an abstract class?
a) abst
b) abstract
c) Abstract
d) abstract class
Answer
Answer: b [Reason:] None.
2. Which of these is not abstract?
a) Thread
b) AbstractList
c) List
d) None of the Mentioned
Answer
Answer: a [Reason:] Thread is not an abstract class.
3. If a class inheriting an abstract class does not define all of its function then it will be known as?
a) Abstract
b) A simple class
c) Static class
d) None of the mentioned
Answer
Answer: a [Reason:] Any subclass of an abstract class must either implement all of the abstract method in the superclass or be itself declared abstract.
4. Which of these is not a correct statement?
a) Every class containing abstract method must be declared abstract
b) Abstract class defines only the structure of the class not its implementation
c) Abstract class can be initiated by new operator
d) Abstract class can be inherited
Answer
Answer: c [Reason:] Abstract class cannot be directly initiated with new operator, Since abstract class does not contain any definition of implementation it is not possible to create an abstract object.
5. Which of these packages contains abstract keyword?
a) java.lang
b) java.util
c) java.io
d) java.system
Answer
Answer: a [Reason:] None.
6. What is the output of this program?
-
class A
-
{
-
public int i;
-
private int j;
-
}
-
class B extends A
-
{
-
void display()
-
{
-
super.j = super.i + 1;
-
System.out.println(super.i + " " + super.j);
-
}
-
}
-
class inheritance
-
{
-
public static void main(String args[])
-
{
-
B obj = new B();
-
obj.i=1;
-
obj.j=2;
-
obj.display();
-
}
-
}
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Answer
Answer: d [Reason:] Class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
output:
$ javac inheritance.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The field A.j is not visible
7. What is the output of this program?
-
class A
-
{
-
public int i;
-
public int j;
-
A()
-
{
-
i = 1;
-
j = 2;
-
}
-
}
-
class B extends A
-
{
-
int a;
-
B()
-
{
-
super();
-
}
-
}
-
class super_use
-
{
-
public static void main(String args[])
-
{
-
B obj = new B();
-
System.out.println(obj.i + " " + obj.j)
-
}
-
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
Answer
Answer: a [Reason:] Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.
output:
$ javac super_use.java
$ java super_use
1 2
8. What is the output of this program?
-
abstract class A
-
{
-
int i;
-
abstract void display();
-
}
-
class B extends A
-
{
-
int j;
-
void display()
-
{
-
System.out.println(j);
-
}
-
}
-
class Abstract_demo
-
{
-
public static void main(String args[])
-
{
-
B obj = new B();
-
obj.j=2;
-
obj.display();
-
}
-
}
a) 0
b) 2
c) Runtime Error
d) Compilation Error
Answer
Answer: b [Reason:] class A is an abstract class, it contains a abstract function display(), the full implementation of display() method is given in its subclass B, Both the display functions are the same. Prototype of display() is defined in class A and its implementation is given in class B.
output:
$ javac Abstract_demo.java
$ java Abstract_demo
2
9. What is the output of this program?
-
class A
-
{
-
int i;
-
void display()
-
{
-
System.out.println(i);
-
}
-
}
-
class B extends A
-
{
-
int j;
-
void display()
-
{
-
System.out.println(j);
-
}
-
}
-
class method_overriding
-
{
-
public static void main(String args[])
-
{
-
B obj = new B();
-
obj.i=1;
-
obj.j=2;
-
obj.display();
-
}
-
}
a) 0
b) 1
c) 2
d) Compilation Error
Answer
Answer: c [Reason:] class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
output:
$ javac method_overriding.java
$ java method_overriding
2
10. What is the output of this program?
-
class A
-
{
-
public int i;
-
protected int j;
-
}
-
class B extends A
-
{
-
int j;
-
void display()
-
{
-
super.j = 3;
-
System.out.println(i + " " + j);
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
B obj = new B();
-
obj.i=1;
-
obj.j=2;
-
obj.display();
-
}
-
}
a) 1 2
b) 2 1
c) 1 3
d) 3 1
Answer
Answer: a [Reason:] Both class A & B have member with same name that is j, member of class B will be called by default if no specifier is used. I contains 1 & j contains 2, printing 1 2.
output:
$ javac Output.java
$ java Output
1 2
Java MCQ Set 2
1. What is the range of short data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
Answer
Answer: b [Reason:] Short occupies 16 bits in memory. Its range is from -32768 to 32767.
2. What is the range of byte data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
Answer
Answer: a [Reason:] Byte occupies 8 bits in memory. Its range is from -128 to 127.
3. Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct.
Answer
Answer: d [Reason:] Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal.(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.(3) actually works, even though a cast is not necessary, because a long can store a byte.
4. An expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float
Answer
Answer: a [Reason:] An expression involving bytes, ints, shorts, literal numbers, the entire expression is promoted to int before any calculation is done.
5. Which of these literals can be contained in float data type variable?
a) -1.7e+308
b) -3.4e+038
c) +1.7e+308
d) -3.4e+050
Answer
Answer: b [Reason:] Range of float data type is -(3.4e38) To +(3.4e38)
6. Which data type value is returned by all transcendental math functions?
a) int
b) float
c) double
d) long
Answer
Answer: c [Reason:] None.
7. What is the output of this program?
-
class average {
-
public static void main(String args[])
-
{
-
double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
-
double result;
-
result = 0;
-
for (int i = 0; i < 6; ++i)
-
result = result + num[i];
-
System.out.print(result/6);
-
-
}
-
}
a) 16.34
b) 16.566666644
c) 16.46666666666667
d) 16.46666666666666
Answer
Answer: c [Reason:] None.
output:
$ javac average.java
$ java average
16.46666666666667
8. What will be the output of these statement?
-
class output {
-
public static void main(String args[])
-
{
-
double a, b,c;
-
a = 3.0/0;
-
b = 0/4.0;
-
c=0/0.0;
-
-
System.out.println(a);
-
System.out.println(b);
-
System.out.println(c);
-
}
-
}
a) Infinity
b) 0.0
c) NaN
d) all of the mentioned
Answer
Answer: d [Reason:] For floating point literals, we have constant value to represent (10/0.0) infinity either positive or negative and also have NaN (not a number for undefined like 0/0.0), but for the integral type, we don’t have any constant that’s why we get an arithmetic exception.
9. What is the output of this program?
-
class increment {
-
public static void main(String args[])
-
{
-
int g = 3;
-
System.out.print(++g * 8);
-
}
-
}
a) 25
b) 24
c) 32
d) 33
Answer
Answer: c [Reason:] Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
output:
$ javac increment.java
$ java increment
32
10. What is the output of this program?
-
class area {
-
public static void main(String args[])
-
{
-
double r, pi, a;
-
r = 9.8;
-
pi = 3.14;
-
a = pi * r * r;
-
System.out.println(a);
-
}
-
}
a) 301.5656
b) 301
c) 301.56
d) 301.56560000
Answer
Answer: a [Reason:] None.
output:
$ javac area.java
$ java area
301.5656
Java MCQ Set 3
1. What is the return type of a method that does not returns any value?
a) int
b) float
c) void
d) double
Answer
Answer: c [Reason:] Return type of an method must be made void if it is not returning any value.
2. What is the process of defining more than one method in a class differentiated by method signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
Answer
Answer: b [Reason:] Function overloading is a process of defining more than one method in a class with same name differentiated by function signature i:e return type or parameters type and number. Example – int volume(int length, int width) & int volume(int length , int width , int height) can be used to calculate volume.
3. Which of the following is a method having same name as that of it’s class?
a) finalize
b) delete
c) class
d) constructor
Answer
Answer: d [Reason:] A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
4. Which method can be defined only once in a program?
a) main method
b) finalize method
c) static method
d) private method
Answer
Answer: a [Reason:] main() method can be defined only once in a program. Program execution begins from the main() method by java’s run time system.
5. Which of these statement is incorrect?
a) All object of a class are allotted memory for the all the variables defined in the class
b) If a function is defined public it can be accessed by object of other class by inheritation
c) main() method must be made public
d) All object of a class are allotted memory for the methods defined in the class
Answer
Answer: d [Reason:] All object of class share a single copy of methods defined in a class, Methods are allotted memory only once. All the objects of the class have access to methods of that class are allotted memory only for the variables not for the methods.
6. What is the output of this program?
-
class box
-
{
-
int width;
-
int height;
-
int length;
-
int volume;
-
void volume(int height, int length, int width)
-
{
-
volume = width*height*length;
-
}
-
}
-
class Prameterized_method
-
{
-
public static void main(String args[])
-
{
-
box obj = new box();
-
obj.height = 1;
-
obj.length = 5;
-
obj.width = 5;
-
obj.volume(3,2,1);
-
System.out.println(obj.volume);
-
}
-
}
a) 0
b) 1
c) 6
d) 25
Answer
Answer: c [Reason:] None.
output:
$ Prameterized_method.java
$ Prameterized_method
6
7. What is the output of this program?
-
class equality
-
{
-
int x;
-
int y;
-
boolean isequal()
-
{
-
return(x == y);
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
equality obj = new equality();
-
obj.x = 5;
-
obj.y = 5;
-
System.out.println(obj.isequal());
-
}
-
}
a) false
b) true
c) 0
d) 1
Answer
Answer: b [Reason:] None.
output:
$ javac Output.java
$ java Output
true
8. What is the output of this program?
-
class box
-
{
-
int width;
-
int height;
-
int length;
-
int volume;
-
void volume()
-
{
-
volume = width*height*length;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
box obj = new box();
-
obj.height = 1;
-
obj.length = 5;
-
obj.width = 5;
-
obj.volume();
-
System.out.println(obj.volume);
-
}
-
}
a) 0
b) 1
c) 25
d) 26
Answer
Answer:c [Reason:] None.
output:
$ javac Output.java
$ java Output
25
9. In the below code, which call to sum() method is appropriate?
-
class Output
-
{
-
-
public static int sum(int ...x)
-
{
-
return;
-
}
-
static void main(String args[])
-
{
-
sum(10);
-
sum(10,20);
-
sum(10,20,30);
-
sum(10,20,30,40);
-
}
-
}
a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
Answer
Answer: d [Reason:] sum is a variable argument method and hence it can take any number as argument.
10. What is the output of this program?
-
class area
-
{
-
int width;
-
int length;
-
int volume;
-
area()
-
{
-
width=5;
-
length=6;
-
}
-
void volume()
-
{
-
volume = width*length*height;
-
}
-
}
-
class cons_method
-
{
-
public static void main(String args[])
-
{
-
area obj = new area();
-
obj.volume();
-
System.out.println(obj.volume);
-
}
-
}
a) 0
b) 1
c) 30
d) error
Answer
Answer: d [Reason:] Variable height is not defined.
output:
$ javac cons_method.java
$ java cons_method
error: cannot find symbol height
Java MCQ Set 4
1. Which of these return type of hasNext() method of an iterator?
a) Integer
b) Double
c) Boolean
d) Collections Object
Answer
Answer: c [Reason:] hasNext() returns boolean values true or false.
2. Which of these methods is used to obtain an iterator to the start of collection?
a) start()
b) begin()
c) iteratorSet()
d) iterator()
Answer
Answer: d [Reason:] To obtain an iterator to the start of the start of the collection we use iterator() method.
3. Which of these methods can be used to move to next element in a collection?
a) next()
b) move()
c) shuffle()
d) hasNext()
Answer
Answer: a [Reason:] None.
4. Which of these iterators can be used only with List?
a) Setiterator
b) ListIterator
c) Literator
d) None of the mentioned
Answer
Answer: b [Reason:] None.
5. Which of these is a method of ListIterator used to obtain index of previous element?
a) previous()
b) previousIndex()
c) back()
d) goBack()
Answer
Answer: b [Reason:] previousIndex() returns index of previous element. if there is no previous element then -1 is returned.
6. Which of these exceptions is thrown by remover() method?
a) IOException
b) SystemException
c) ObjectNotFoundExeception
d) IllegalStateException
Answer
Answer: d [Reason:] None.
7. What is the output of this program?
-
import java.util.*;
-
class Collection_iterators
-
{
-
public static void main(String args[])
-
{
-
ListIterator a = list.listIterator();
-
if(a.previousIndex()! = -1)
-
while(a.hasNext())
-
System.out.print(a.next() + " ");
-
else
-
System.out.print("EMPTY");
-
}
-
}
a) 0
b) 1
c) -1
d) EMPTY
Answer
Answer: d [Reason:] None.
Output:
$ javac Collection_iterators.java
$ java Collection_iterators
EMPTY
8. What is the output of this program?
-
import java.util.*;
-
class Collection_iterators
-
{
-
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_iterators.java
$ java Collection_iterators
1 5 8 2
9. What is the output of this program?
-
import java.util.*;
-
class Collection_iterators
-
{
-
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_iterators.java
$ java Collection_iterators
1 5 8 2
10. What is the output of this program?
-
import java.util.*;
-
class Collection_iterators
-
{
-
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);
-
i.next();
-
i.remove();
-
while(i.hasNext())
-
System.out.print(i.next() + " ");
-
}
-
}
a) 2 8 5
b) 2 1 8
c) 2 5 8
d) 8 5 1
Answer
Answer: b [Reason:] i.next() returns the next element in the iteration. i.remove() removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next(). The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.
Output:
$ javac Collection_iterators.java
$ java Collection_iterators
2 1 8
(output will be different on your system)
Java MCQ Set 5
1. Which of these classes is used for input and output operation when working with bytes?
a) InputStream
b) Reader
c) Writer
d) All of the mentioned
Answer
Answer: a [Reason:] InputStream & OutputStream are designed for byte stream. Reader and writer are designed for character stream.
2. Which of these class is used to read and write bytes in a file?
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader
Answer
Answer: c [Reason:] None.
3. Which of these method of InputStream is used to read integer representation of next available byte input?
a) read()
b) scanf()
c) get()
d) getInteger()
Answer
Answer: a [Reason:] None.
4. Which of these data type is returned by every method of OutputStream?
a) int
b) float
c) byte
d) none of the mentioned
Answer
Answer: d [Reason:] Every method of OutputStream returns void and throws an IOExeption in case of errors.
5. Which of these is a method to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()
Answer
Answer: b [Reason:] None.
6. Which of these method(s) is/are used for writing bytes to an outputstream?
a) put()
b) print() and write()
c) printf()
d) write() and read()
Answer
Answer: b [Reason:] write() and print() are the two methods of OutputStream that are used for printing the byte data.
7. What is the output of this program?
-
import java.io.*;
-
class filesinputoutput
-
{
-
public static void main(String args[])
-
{
-
InputStream obj = new FileInputStream("inputoutput.java");
-
System.out.print(obj.available());
-
}
-
}
Note: inputoutput.java is stored in the disk.
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
Answer
Answer: c [Reason:] obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
(Output will be different in your case)
8. What is the output of this program?
-
import java.io.*;
-
public class filesinputoutput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abc";
-
byte b[] = obj.getBytes();
-
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
-
for (int i = 0; i < 2; ++ i)
-
{
-
int c;
-
while ((c = obj1.read()) != -1)
-
{
-
if(i == 0)
-
{
-
System.out.print((char)c);
-
}
-
}
-
}
-
}
-
}
a) abc
b) ABC
c) ab
d) AB
Answer
Answer: a [Reason:] None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
abc
9. What is the output of this program?
-
import java.io.*;
-
public class filesinputoutput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abc";
-
byte b[] = obj.getBytes();
-
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
-
for (int i = 0; i < 2; ++ i)
-
{
-
int c;
-
while ((c = obj1.read()) != -1)
-
{
-
if (i == 0)
-
{
-
System.out.print(Character.toUpperCase((char)c));
-
}
-
}
-
}
-
}
-
}
a) abc
b) ABC
c) ab
d) AB
Answer
Answer: b [Reason:] None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
ABC
10. What is the output of this program?
-
import java.io.*;
-
public class filesinputoutput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abc";
-
byte b[] = obj.getBytes();
-
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
-
for (int i = 0; i < 2; ++ i)
-
{
-
int c;
-
while ((c = obj1.read()) != -1)
-
{
-
if (i == 0)
-
{
-
System.out.print(Character.toUpperCase((char)c));
-
obj2.write(1);
-
}
-
}
-
System.out.print(obj2);
-
}
-
}
-
}
a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
Answer
Answer: d [Reason:] None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa