Java MCQ Number 01063

Java MCQ Set 1

1. Which of these method of class String is used to extract more than one character at a time a String object?
a) getchars()
b) GetChars()
c) Getchars()
d) getChars()

Answer

Answer: d [Reason:] None.

2. Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?
a) getBytes()
b) GetByte()
c) giveByte()
d) Give Bytes()

Answer

Answer: a [Reason:] getBytes() stores the character in an array of bytes. It uses default character to byte conversions provided by platform.

3. In below code, what can directly access and change the value of the variable name?

  1.  package test;
  2.  class Target 
  3.  {
  4.   public String name = "hello";
  5.  }

a) any class
b) only the Target class
c) any class in the test package
d) any class that extends Target

Answer

Answer: c [Reason:] Any class in the test package can access and change name.

4. What will be output of the following code?

  1. public class Boxer1 
  2. {
  3.     Integer i;
  4.     int x;
  5.    public Boxer1(int y) 
  6.    {
  7.         x = i+y;
  8.         System.out.println(x);
  9.    }
  10.    public static void main(String[] args) 
  11.    {
  12.        new Boxer1 (new Integer(4));
  13.    }
  14. }

a) The value “4” is printed at the command line
b) Compilation fails because of an error in line
c) A NullPointerException occurs at runtime
d) An IllegalStateException occurs at runtime

Answer

Answer: d [Reason:] Because we are performing operation on reference variable which is null.

5. Which of these methods can be used to convert all characters in a String into a character array?
a) charAt()
b) both getChars() & charAt()
c) both toCharArray() & getChars()
d) all of the mentioned

Answer

Answer: c [Reason:] charAt() return one character only not array of character.

6. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String c = "Hello i love java";
  6.            int start = 2;
  7.            int end = 9;
  8.            char s[]=new char[end-start];
  9.            c.getChars(start,end,s,0);
  10.            System.out.println(s);
  11.         }
  12.     }

a) Hello, i love java
b) i love ja
c) lo i lo
d) llo i l

Answer

Answer: d [Reason:] getChars(start,end,s,0) returns an array from the string c, starting index of array is pointed by start and ending index is pointed by end. s is the target character array where the new string of letters is going to be stored and the new string will be stored from 0th position in s.
Output:
$ javac output.java
$ java output
llo i l

7. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String a = "hello i love java";
  6.             System.out.println(a.indexOf('i')+" "+a.indexOf('o') +" "+a.lastIndexOf('i')+" "+a.lastIndexOf('o'));
  7.         }
  8.     }

a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9

Answer

Answer: a [Reason:] indexof(‘c’) and lastIndexof(‘c’) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
Output:
$ javac output.java
$ java output
6 4 6 9

8. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             char c[]={'a', '1', 'b' ,' ' ,'A' , '0'};
  6.             for (int i = 0; i < 5; ++i)
  7.             {
  8.                    if(Character.isDigit(c[i]))
  9.                        System.out.println(c[i]+" is a digit");
  10.                    if(Character.isWhitespace(c[i]))
  11.                        System.out.println(c[i]+" is a Whitespace character");
  12.                    if(Character.isUpperCase(c[i]))
  13.                        System.out.println(c[i]+" is an Upper case Letter");
  14.                    if(Character.isLowerCase(c[i]))
  15.                        System.out.println(c[i]+" is a lower case Letter");
  16.                i=i+3;
  17.             }
  18.         }
  19.     }

a) a is a lower case Letter
is White space character
b) b is a lower case Letter
is White space character
c) a is a lower case Letter
A is a upper case Letter
d) a is a lower case Letter
0 is a digit

Answer

Answer: c [Reason:] Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i]) are the function of library java.lang. They are used to find weather the given character is of specified type or not. They return true or false i:e Boolean variable.
Output:
$ javac output.java
$ java output
a is a lower case Letter
A is an Upper Case Letter

9. What is the output of this program?

  1.     class String_demo 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             char chars[] = {'a', 'b', 'c'};
  6.             String s = new String(chars);
  7.             System.out.println(s);
  8.         }
  9.    }

a) a
b) b
c) c
d) abc

Answer

Answer: d [Reason:] String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “abc”.
output:
$ javac String_demo.java
$ java String_demo
abc

10. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             char ch;
  6.             ch = "hello".charAt(1);
  7.             System.out.println(ch);
  8.         }
  9.    }

a) h
b) e
c) l
d) o

Answer

Answer: b [Reason:] “hello” is a String literal, method charAt() returns the character specified at the index position. Character at index position 1 is e of hello, hence ch contains e.
output:
$ javac output.java
$ java output
e

Java MCQ Set 2

1. Which of these packages contain all the collection classes?
a) java.lang
b) java.util
c) java.net
d) java.awt

Answer

Answer: b [Reason:] None.

2. Which of these classes is not part of Java’s collection framework?
a) Maps
b) Array
c) Stack
d) Queue

Answer

Answer: a [Reason:] Maps is not a part of collection framework.

3. Which of these interface is not a part of Java’s collection framework?
a) List
b) Set
c) SortedMap
d) SortedList

Answer

Answer: d [Reason:] SortedList is not a part of collection framework.

4. Which of these methods deletes all the elements from invoking collection?
a) clear()
b) reset()
c) delete()
d) refresh()

Answer

Answer: a [Reason:] clear() method removes all the elements from invoking collection.

5. What is Collection in Java?
a) A group of objects
b) A group of classes
c) A group of interfaces
d) None of the mentioned

Answer

Answer: a [Reason:] A collection is a group of objects, it is similar to String Template Library (STL) of C++ programming language.

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

7. What is the output of this program?

  1.     import java.util.*;
  2.     class vector 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Vector obj = new Vector(4,2);
  7.             obj.addElement(new Integer(3));
  8.             obj.addElement(new Integer(2));
  9.             obj.addElement(new Integer(5));
  10.             obj.removeAll(obj);
  11.             System.out.println(obj.isEmpty());
  12.         }
  13.     }

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

8. What is the output of this program?

  1.     import java.util.*;
  2.     class stack 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Stack obj = new Stack();
  7.             obj.push(new Integer(3));
  8.             obj.push(new Integer(2));
  9.             obj.pop();
  10.             obj.push(new Integer(5));
  11.      	    System.out.println(obj);
  12.         }
  13.     }

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].

9. What is the output of this program?

  1.     import java.util.*;
  2.     class hashtable 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             Hashtable obj = new Hashtable();
  7.             obj.put("A", new Integer(3));
  8.             obj.put("B", new Integer(2));
  9.             obj.put("C", new Integer(8));
  10.             obj.remove(new String("A"));
  11.             System.out.print(obj);
  12.         }
  13.     }

a) {C=8, B=2}
b) [C=8, B=2].
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2].

Answer

Answer: b [Reason:] None.
Output:
$ javac hashtable.java
$ java hashtable
{C=8, B=2}

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

Java MCQ Set 3

1. Which of these method is given parameter via command line arguments?
a) main()
b) recursive() method
c) Any method
d) System defined methods

Answer

Answer: a [Reason:] Only main() method can be given parameters via using command line arguments.

2. Which of these data types is used to store command line arguments?
a) Array
b) Stack
c) String
d) Integer

Answer

Answer: c [Reason:] None.

3. How many arguments can be passed to main()?
a) Infinite
b) Only 1
c) System Dependent
d) None of the mentioned

Answer

Answer: a [Reason:] None.

4. Which of these is a correct statement about args in this line of code?
public static viod main(String args[])
a) args is a String
b) args is a Character
c) args is an array of String
d) args in an array of Character

Answer

Answer: c [Reason:] args in an array of String.

5. Can command line arguments be converted into int automatically if required?
a) Yes
b) No
c) Compiler Dependent
d) Only ASCII characters can be converted

Answer

Answer: b [Reason:] All command Line arguments are passed as a string. We must convert numerical value to their internal forms manually.

6. What is the output of this program, Command line execution is done as – “java Output This is a command Line”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print("args[0]");
  6.         }
  7.     }

a) java
b) Oupput
c) This
d) is

Answer

Answer: c [Reason:] None.
Output:
$ javac Output.javac
java Output This is a command Line
This

7. What is the output of this program, Command line exceution is done as – “java Output This is a command Line”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print("args[3]");
  6.         }
  7.     }

a) java
b) is
c) This
d) command

Answer

Answer: d [Reason:] None.
Output:
$ javac Output.javac
java Output This is a command Line
command

8. What is the output of this program, Command line execution is done as – “java Output This is a command Line”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print("args");
  6.         }
  7.     }

a) This
b) java Output This is a command Line
c) This is a command Line
d) Compilation Error

Answer

Answer: c [Reason:] None.
Output:
$ javac Output.javac
java Output This is a command Line
This is a command Line

9. What is the output of this program, Command line execution is done as – “java Output command Line 10 A b 4 N”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print("(int)args[2] * 2");
  6.         }
  7.     }

a) java
b) 10
c) 20
d) b

Answer

Answer: c [Reason:] None.
Output:
$ javac Output.javac
java Output command Line 10 A b 4 N
20

10. What is the output of this program, Command line execution is done as – “java Output command Line 10 A b 4 N”?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             System.out.print("args[6]");
  6.         }
  7.     }

a) java
b) 10
c) b
d) N

Answer

Answer: d [Reason:] None.
Output:
$ javac Output.javac
java Output command Line 10 A b 4 N
N

Java MCQ Set 4

1. What is the return type of Constructors?
a) int
b) float
c) void
d) none of the mentioned

Answer

Answer: d [Reason:] Constructors does not have any return type, not even void.

2. Which keyword is used by method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this

Answer

Answer: d [Reason:] this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked.

3. Which of the following is a method having same name as that of its 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 operator is used by Java run time implementations to free the memory of an object when it is no longer needed?
a) delete
b) free
c) new
d) none of the mentioned

Answer

Answer: d [Reason:] Java handles deallocation of memory automatically, we do not need to explicitly delete an element. Garbage collection only occurs during execution of the program. When no references to the object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed.

5. Which function is used to perform some action when the object is to be destroyed?
a) finalize()
b) delete()
c) main()
d) none of the mentioned

Answer

Answer: a [Reason:] None.

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.         box() 
  8.         {
  9.             width = 5;
  10.             height = 5;
  11.             length = 6;
  12.         }
  13.         void volume() 
  14.         {
  15.              volume = width*height*length;
  16.         } 
  17.     }    
  18.     class constructor_output 
  19.     {
  20.         public static void main(String args[])
  21.         {
  22.             box obj = new box();
  23.             obj.volume();
  24.             System.out.println(obj.volume);
  25.         }
  26.    }

a) 100
b) 150
c) 200
d) 250

Answer

Answer: b [Reason:] None.
output:
$ constructor_output.java
$ constructor_output
150

7. 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 finalize() 
  8.         {
  9.             volume = width*height*length;
  10.             System.out.println(volume);
  11.         }
  12.         protected void volume() 
  13.        {
  14.             volume = width*height*length;
  15.             System.out.println(volume);
  16.        } 
  17.     }    
  18.     class Output 
  19.     { 
  20.         public static void main(String args[])
  21.         {
  22.             box obj = new box();
  23.             obj.width=5;
  24.             obj.height=5;
  25.             obj.length=6;
  26.             obj.volume();
  27.         } 
  28.     }

a) 150
b) 200
c) Run time error
d) Compilation error

Answer

Answer: a [Reason:] None.
output:
$ javac Output.java
$ java Output
150

8. Which of the following statements are incorrect?
a) default constructor is called at the time of object declaration
b) Constructor can be parameterized
c) finalize() method is called when a object goes out of scope and is no longer needed
d) finalize() method must be declared protected

Answer

Answer: c [Reason:] finalize() method is called just prior to garbage collection. it is not called when object goes out of scope.

9. What is the output of this program?

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

a) 0 0
b) 5 6
c) 6 5
d) 5 5

Answer

Answer: c [Reason:] this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked.
output:
$ javac Output.java
$ java Output
6 5

Java MCQ Set 5

1. Which of these keyword must be used to inherit a class?
a) super
b) this
c) extent
d) extends

Answer

Answer: d [Reason:] None.

2. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
d) none of the mentioned

Answer

Answer: b [Reason:] Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.

3. A class member declared protected becomes member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member

Answer

Answer: b [Reason:] A class member declared protected becomes private member of subclass.

4. Which of these is correct way of inheriting class A by class B?
a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}

Answer

Answer: c [Reason:] None.

5. Which two classes use the Shape class correctly?

A. public class Circle implements Shape 
   {
    private int radius;
   }
B. public abstract class Circle extends Shape 
   {
    private int radius;
   }
C. public class Circle extends Shape 
   {
   private int radius;
   public void draw();
   }
D. public abstract class Circle implements Shape 
   {
    private int radius;
    public void draw();
   }
E. public class Circle extends Shape 
   {
    private int radius;
    public void draw()
    {
     /* code here */
    }
   }
F. public abstract class Circle implements Shape 
   {
     private int radius;
     public void draw() 
     { 
      /* code here */ 
     }
   }

a) B,E
b) A,C
c) C,E
d) T,H

Answer

Answer: a [Reason:] If one is extending any class, then they should use extends keyword not implements.

6. 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 inheritance_demo 
  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 inheritance_demo.java
$ java inheritance_demo
2

7. What is the output of this program?

  1.     class A 
  2.     {
  3.         int i;
  4.     }    
  5.     class B extends A 
  6.     {
  7.         int j;
  8.         void display() 
  9.         {
  10.             super.i = j + 1;
  11.             System.out.println(j + " " + i);
  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) 2 3
d) 3 2

Answer

Answer: c [Reason:] None
output:
$ javac inheritance.java
$ java inheritance
2 3

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

9. 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

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

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.