Java Programming - Declarations and Access Control - Discussion


/* Missing statements ? */
public class NewTreeSet extends java.util.TreeSet
    public static void main(String [] args) 
        java.util.TreeSet t = new java.util.TreeSet();
    public void clear() 
        TreeMap m = new TreeMap();
which two statements, added independently at beginning of the program, allow the code to compile?
  1. No statement is required
  2. import java.util.*;
  4. import java.util.TreeSet;
  5. import java.util.TreeMap;

[A]. 1 only
[B]. 2 and 5
[C]. 3 and 4
[D]. 3 and 5

Answer: Option B


(2) and (5). TreeMap is the only class that must be imported. TreeSet does not need an import statement because it is described with a fully qualified name.

(1) is incorrect because TreeMap must be imported. (3) is incorrect syntax for an import statement. (4) is incorrect because it will not import TreeMap, which is required.

Sarang said: (May 18, 2012)  
I dont understand why import java.util.* statement is required when we use import java.util.TreeMap;
Can somebody explain? because TreeMap is the only class that needs to be imported.

Joshi Saryu said: (Jun 15, 2012)  
I don't understand why import java. Util. * statement is required when we use import java.Util.TreeMap;.

Can somebody explain? because TreeMap is the only class that needs to be imported.

Ramesh said: (Aug 14, 2013)  
I don't understand the util, treeset and treemap please give brief explanation about these?

Muthupandian said: (Nov 16, 2013)  
I want to know, you import import java.util.* -> then you import - import java.util.TreeMap, in import java.util.*( what is the purpose of the .*) which will bring all util packages, is it? please explain.

Shashi said: (Nov 26, 2013)  
As per my understanding:

Only import java.util.TreeMap; is sufficient to execute the above code.

If you have used import java.util.*; then no need to import anything.

Bhavishya said: (Aug 24, 2014)  
If * includes all the classes in util. Why to import treemap separately?

Aroosh said: (Dec 12, 2014)  
There is no need of java.util.TreeMap if you are using java.util.*.

Cesar Navarro said: (Feb 20, 2015)  
The correct answer it's "B" because the question say: added - "independently"-.

You can just add.

import java.util.treeMap;

Or just add.

import java.util.*;

And those two statements works independently.

Syed Shahzad said: (Apr 16, 2015)  
Option D and B both are correct, if TreeMap is only required to be imported.

Papa Rao said: (Apr 28, 2017)  
According to me, import java.util.*; is enough.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.