Java MCQ Number 01064

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?

  1.     class A 
  2.     {
  3.         public int i;
  4.         private int j;
  5.     }    
  6.     class B extends A 
  7.     {
  8.         void display() 
  9.         {
  10.             super.j = super.i + 1;
  11.             System.out.println(super.i + " " + super.j);
  12.         }
  13.     }    
  14.     class inheritance 
  15.    {
  16.         public static void main(String args[])
  17.         {
  18.             B obj = new B();
  19.             obj.i=1;
  20.             obj.j=2;   
  21.             obj.display();     
  22.         }
  23.    }

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?

  1.     class A 
  2.     {
  3.         public int i;
  4.         public int j;
  5.         A() 
  6.        {
  7.             i = 1;
  8.             j = 2;
  9. }
  10.     }    
  11.     class B extends A 
  12.     {
  13.         int a;
  14. 	B() 
  15.         {
  16.             super();
  17.         } 
  18.     }    
  19.     class super_use 
  20.     {
  21.         public static void main(String args[])
  22.         {
  23.             B obj = new B();
  24.             System.out.println(obj.i + " " + obj.j)     
  25.         }
  26.    }

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?

  1.     abstract class A 
  2.     {
  3.         int i;
  4.         abstract void display();
  5.     }    
  6.     class B extends A 
  7.     {
  8.         int j;
  9.         void display() 
  10.         {
  11.             System.out.println(j);
  12.         }
  13.     }    
  14.     class Abstract_demo 
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             B obj = new B();
  19.             obj.j=2;
  20.             obj.display();    
  21.         }
  22.    }

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?

  1.     class A 
  2.     {
  3.         int i;
  4.         void display() 
  5.         {
  6.             System.out.println(i);
  7.         }
  8.     }    
  9.     class B extends A 
  10.     {
  11.         int j;
  12.         void display() 
  13.         {
  14.             System.out.println(j);
  15.         }
  16.     }    
  17.     class method_overriding 
  18.     {
  19.         public static void main(String args[])
  20.         {
  21.             B obj = new B();
  22.             obj.i=1;
  23.             obj.j=2;   
  24.             obj.display();     
  25.         }
  26.    }

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?

  1.     class A 
  2.     {
  3.         public int i;
  4.         protected int j;
  5.     }    
  6.     class B extends A 
  7.     {
  8.         int j;
  9.         void display() 
  10.         {
  11.             super.j = 3;
  12.             System.out.println(i + " " + j);
  13.         }
  14.     }    
  15.     class Output 
  16.     {
  17.         public static void main(String args[])
  18.         {
  19.             B obj = new B();
  20.             obj.i=1;
  21.             obj.j=2;   
  22.             obj.display();     
  23.         }
  24.    }

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?

  1.     class average {
  2.         public static void main(String args[])
  3.         {
  4.             double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
  5.             double result;
  6.             result = 0;
  7.             for (int i = 0; i < 6; ++i) 
  8.                 result = result + num[i];
  9. 	    System.out.print(result/6);
  10.  
  11.         } 
  12.     }

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?

  1. class output {
  2.         public static void main(String args[]) 
  3.         {
  4.             double a, b,c;
  5.             a = 3.0/0;
  6.             b = 0/4.0;
  7.             c=0/0.0;
  8.  
  9. 	    System.out.println(a);
  10.             System.out.println(b);
  11.             System.out.println(c);
  12.         } 
  13.     }

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?

  1.     class increment {
  2.         public static void main(String args[]) 
  3.         {        
  4.              int g = 3;
  5.              System.out.print(++g * 8);
  6.         } 
  7.     }

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?

  1.     class area {
  2.         public static void main(String args[]) 
  3.         {    
  4.              double r, pi, a;
  5.              r = 9.8;
  6.              pi = 3.14;
  7.              a = pi * r * r;
  8.              System.out.println(a);
  9.         } 
  10.     }

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?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.         int volume;
  7.         void volume(int height, int length, int width) 
  8.         {
  9.              volume = width*height*length;
  10.         } 
  11.     }    
  12.     class Prameterized_method
  13.     {
  14.         public static void main(String args[])
  15.         {
  16.             box obj = new box();
  17.             obj.height = 1;
  18.             obj.length = 5;
  19.             obj.width = 5;
  20.             obj.volume(3,2,1);
  21.             System.out.println(obj.volume);        
  22.         } 
  23.      }

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?

  1.     class equality 
  2.     {
  3.         int x;
  4.         int y;
  5.         boolean isequal()
  6.         {
  7.             return(x == y);  
  8.         } 
  9.     }    
  10.     class Output 
  11.     {
  12.         public static void main(String args[])
  13.         {
  14.             equality obj = new equality();
  15.             obj.x = 5;
  16.             obj.y = 5;
  17.             System.out.println(obj.isequal());
  18.         } 
  19.     }

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?

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

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?

  1. class Output 
  2. {
  3.  
  4.         public static int sum(int ...x)
  5.         {
  6.              return; 
  7.         }
  8.         static void main(String args[]) 
  9.         {    
  10.              sum(10);
  11.              sum(10,20);
  12.              sum(10,20,30);
  13.              sum(10,20,30,40);
  14.         } 
  15. }

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?

  1.     class area 
  2.     {
  3.         int width;
  4.         int length;
  5.         int volume;
  6.         area() 
  7.         {
  8.            width=5;
  9.            length=6;
  10.         }
  11.         void volume() 
  12.         {
  13.              volume = width*length*height;
  14.         } 
  15.     }    
  16.     class cons_method 
  17.     {
  18.         public static void main(String args[])
  19.         {
  20.             area obj = new area();
  21.             obj.volume();
  22.             System.out.println(obj.volume);        
  23.         } 
  24.     }

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?

  1.     import java.util.*;
  2.     class Collection_iterators 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             ListIterator a = list.listIterator();
  7.                 if(a.previousIndex()! = -1)
  8.                     while(a.hasNext())
  9. 	                System.out.print(a.next() + " ");
  10.                 else
  11.                    System.out.print("EMPTY");
  12.         }
  13.     }

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?

  1.     import java.util.*;
  2.     class Collection_iterators 
  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_iterators.java
$ java Collection_iterators
1 5 8 2

9. What is the output of this program?

  1.     import java.util.*;
  2.     class Collection_iterators 
  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_iterators.java
$ java Collection_iterators
1 5 8 2

10. What is the output of this program?

  1.     import java.util.*;
  2.     class Collection_iterators 
  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.             i.next();
  15.             i.remove();
  16.             while(i.hasNext())
  17. 	        System.out.print(i.next() + " ");
  18.         }
  19.     }

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?

  1.     import java.io.*;
  2.     class filesinputoutput 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             InputStream obj = new FileInputStream("inputoutput.java");
  7.             System.out.print(obj.available());
  8.         }
  9.     }

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?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if(i == 0) 
  15.                    {
  16.             	       System.out.print((char)c); 
  17.             	   }
  18.                }
  19.            }
  20.         }
  21.     }

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?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if (i == 0) 
  15.                    {
  16.                        System.out.print(Character.toUpperCase((char)c)); 
  17.             	   }
  18.                }
  19.            }
  20.         }
  21.     }

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?

  1.     import java.io.*;
  2.     public class filesinputoutput 
  3.     {
  4.     public static void main(String[] args) 
  5.         {
  6.  	   String obj  = "abc";
  7.            byte b[] = obj.getBytes();
  8.            ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
  9.            for (int i = 0; i < 2; ++ i) 
  10.            {
  11.                int c;
  12.                while ((c = obj1.read()) != -1) 
  13.                {
  14.             	   if (i == 0) 
  15.                    {
  16.                        System.out.print(Character.toUpperCase((char)c));
  17.                        obj2.write(1); 
  18.             	   }
  19.                }
  20.                System.out.print(obj2);
  21.            }
  22.         }
  23.     }

a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa

Answer

Answer: d [Reason:] None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa

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.