aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Correct import statement? from Khalid Mughal Review Questions- 4.3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Correct import statement? from Khalid Mughal Review Questions- 4.3" Watch "Correct import statement? from Khalid Mughal Review Questions- 4.3" New topic
Author

Correct import statement? from Khalid Mughal Review Questions- 4.3

Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
4.3 Given the following code:
// (1) INSERT ONE IMPORT STATEMENT HERE
public class RQ700_10 {
public static void main(String[] args) {
System.out.println(Locale.UK); // Locale string for UK is "en_GB".
}}
Which statements, when inserted at (1), will result in a program that prints en_GB, when compiled and run? Select the two correct answers.
(a) import java.util.*;
(b) import java.util.Locale;
(c) import java.util.Locale.UK;
(d) import java.util.Locale.*;
(e) import static java.util.*;
(f) import static java.util.Locale;
(g) import static java.util.Locale.UK;
(h) import static java.util.Locale.*;

I am not able to differentiate between import and import static. Please help.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

the name import static alone should have given you a hint. as the name suggest import static are used to import static members of the class , so that you need not use the name of class while accessing such members. for example when we use System.out.println , out is a static field of System class. instead of writing System.out.println we can use "STATIC IMPORTS"(beware they are called static imports but written as import static). so what you can do is do this

import static java.lang.System.* // this will import all the static members in System class

or

import static java.lang.System.out // the specific member to import.

In the question given below the author has cleverly tried to confused you. UK is a static member of Locale class. You normally access it using Locale.UK(normal import statement specified). in this case you can rule out all the static imports directly. now from amongst the others the correct choices are the one with the proper hierarchy of Locale class which is java.util.* or java.util.Locale hence option (a) and (b) is the correct choice.

option (c) and (d) are clearly not a proper syntax of import . (e) is not valid static import

(f) is also not a valid static import

(g) and (h) are valid static imports but it will give compile error that it cannot find symbol Locale. had you done like this System.out.println(UK); and used one of the option (g) or (h) , the program would have compiled and ran since now you have "statically" imported UK field of Locale class with either option (g) or (h)
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

I think b and g should be correct.


OCA7
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

ankita modi. wrote:I think b and g should be correct.


if b is correct as you say , why can't (a) be ???

(g) and (h) are valid static imports but they will result in compiler error since they provide for the static member and not for the class Locale. you can run the program and check for yourself
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

gurpeet singh wrote:
ankita modi. wrote:I think b and g should be correct.


if b is correct as you say , why can't (a) be ???

(g) and (h) are valid static imports but they will result in compiler error since they provide for the static member and not for the class Locale. you can run the program and check for yourself


Yes,it may be, i didn't check.
Rodine Villar
Greenhorn

Joined: Apr 16, 2011
Posts: 6
g and h are the correct ones. a is not correct because Locale.UK is a static field in the Locale class. So you must use import static.


I believe in God not in religion.
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
The right answer is (a) & (b).

That's a nice explanation Gurpreet.. Thanks a lot...
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
Got stuck in one similar question:
Which sequence of import statements, when inserted at (1), will result in the code
compiling, and the execution of the main() method printing:
|Giffy||Tiffy||Jpeggy||Jpeggy||Jpeggy||Jpeggy|
Select the three correct answers.
(a) import p3.Util;
import p3.Util.Format;
import static p3.Util.print;
import static p3.Util.Format.*;
(b) import p3.Util;
import static p3.Util.Format;
import static p3.Util.print;
import static p3.Util.Format.*;
(c) import p3.*;
import static p3.Util.*;
import static p3.Util.Format.*;
(d) import p3.*;
import p3.Util.*;
import static p3.Util.Format.*;

-Answer is a,b,c.
I dont understand how import static p3.Util.Format.* works ?? As we use ststic import to access static members only.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

As you already know that enums are the solution to java's requirement for enumerated data types. also the enum constants are nothing but public final static fields. so you can use static imports to import them. this thing is explained very good in kb6 book. you can refer that if you have it
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
I think Format is implicitly static. JPEG, GIF, TIFF are implicity static too.
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
Thanks Gurpreet!!

I did read that. Was missing this enum thing.
 
Consider Paul's rocket mass heater.
 
subject: Correct import statement? from Khalid Mughal Review Questions- 4.3