Java Multiple Choice Questions & Answers on Java.util package Vectors & Stack for Freshers

1. Which of these class object can be used to form a dynamic array?

a) ArrayList
b) Map
c) Vector
d) ArrayList & Vector
Answer: d

Explanation: 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: d

Explanation: 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: b
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: c
5. Which of these methods is used to add elements in vector at specific location?

a) add()
b) set()
c) AddElement()
d) addElement()
Answer: d

Explanation: 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?
  1.     import java.util.*;
  2.     class vector {
  3.         public static void main(String args[]) {
  4.             Vector obj = new Vector(4,2);
  5.             obj.addElement(new Integer(3));
  6.             obj.addElement(new Integer(2));
  7.             obj.addElement(new Integer(5));
  8.             System.out.println(obj.elementAt(1));
  9.         }
  10.     }
a) 0
b) 3
c) 2
d) 5
Answer: c

Explanation: obj.elementAt(1) returns the value stored at index 1, which is 2.
7. What is the output of this program?
  1.     import java.util.*;
  2.     class vector {
  3.         public static void main(String args[]) {
  4.             Vector obj = new Vector(4,2);
  5.             obj.addElement(new Integer(3));
  6.             obj.addElement(new Integer(2));
  7.             obj.addElement(new Integer(5));
  8.             System.out.println(obj.capacity());
  9.         }
  10.     }
a) 2
b) 3
c) 4
d) 6
Answer: c
8. What is the output of this program?
  1.     import java.util.*;
  2.     class vector {
  3.         public static void main(String args[]) {
  4.             Vector obj = new Vector(4,2);
  5.             obj.addElement(new Integer(3));
  6.             obj.addElement(new Integer(2));
  7.             obj.addElement(new Integer(6));
  8.             obj.insertElementAt(new Integer(8), 2);
  9.             System.out.println(obj);
  10.         }
  11.     }
a) [3, 2, 6] 
b) [3, 2, 8] 
c) [3, 2, 6, 8] 
d) [3, 2, 8, 6] 

Answer: d
9. What is the output of this program?
  1.     import java.util.*;
  2.     class vector {
  3.         public static void main(String args[]) {
  4.             Vector obj = new Vector(4,2);
  5.             obj.addElement(new Integer(3));
  6.             obj.addElement(new Integer(2));
  7.             obj.addElement(new Integer(5));
  8.             obj.removeAll(obj);
  9.             System.out.println(obj.isEmpty());
  10.         }
  11.     }
a) 0
b) 1
c) true
d) false
Answer: c

Explanation: 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.
10. What is the output of this program?
  1.     import java.util.*;
  2.     class stack {
  3.         public static void main(String args[]) {
  4.             Stack obj = new Stack();
  5.             obj.push(new Integer(3));
  6.             obj.push(new Integer(2));
  7.             obj.pop();
  8.             obj.push(new Integer(5));
  9.           System.out.println(obj);
  10.         }
  11.     }
a) [3, 5] 
b) [3, 2] 
c) [3, 2, 5] 
d) [3, 5, 2] 
Answer: a

Explanation: 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.

Related

C# Questions & Answers on Polymorphism for Freshers

1. The capability of an object in Csharp to take number of different forms and hence display behaviour as according is known as: a) Encapsulationb) Polymorphismc) Abstractiond) None of the mentione...

C# Questions & Answers on Use of Variable Number of Arguements for Freshers

1. The method in which large or variable number of arguments are handled is known as: a) Value parametersb) Output parametersc) Parameter arraysd) None of the mentioned Answer: c 2. The modifiers...

C# Questions & Answers on Use of Ref and Out Parameters for Freshers

1. What is output for the following code snippet? class Program { static void Main(string[] args) { int i = 5; int j; method1(ref i); m...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item