Java Questions & Answers – Locale & Random Classes

This section of our 1000+ Java MCQs focuses on Local & Random classes of Java Programming Language.

1. Which of these class produce objects with respect to geographical locations?
a) TimeZone
b) Locale
c) Date
d) SimpleTimeZone
View Answer

Answer: b
Explanation: The Locale class isinstantiated to produce objects that each describe a geographical or cultural region.

2. Which of these methods is not a Locale class?
a) UK
b) US
c) INDIA
d) KOREA
View Answer

Answer: c
Explanation: INDIA is not a Locale class.

3. Which of these class can generate pseudorandom numbers?
a) Locale
b) Rand
c) Random
d) None of the mentioned
View Answer

Answer: c
Explanation: The Random class in the java.util package is used to generate pseudorandom numbers in Java. It provides various methods to generate random values of different data types like integers, floats, doubles, etc. The other options are either incorrect or unrelated to random number generation.
advertisement

4. Which of these method of Locale class can be used to obtain country of operation?
a) getCountry()
b) whichCountry()
c) DisplayCountry()
d) getDisplayCountry()
View Answer

Answer: d
Explanation: The getCountry() method of the Locale class returns the country code for the locale. To get the displayable (human-readable) country name, the method getDisplayCountry() is used. However, for obtaining the actual country code, getCountry() is correct.

5. Which of these is a method can generate a boolean output?
a) retbool()
b) getBool()
c) nextBool()
d) nextBoolean()
View Answer

Answer: d
Explanation: The nextBoolean() method of the Random class is used to generate a pseudorandom boolean value (true or false). The other options are not valid methods in Java’s standard library for generating boolean values.
Free 30-Day Java Certification Bootcamp is Live. Join Now!

6. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class LOCALE_CLASS
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Locale obj = new Locale("INDIA") ;
  7.             System.out.print(obj.getCountry());
  8.         }
  9.     }

a) India
b) INDIA
c) Compilation Error
d) Nothing is displayed
View Answer

Answer: d
Explanation: Since only a single argument is passed to the Locale() constructor, ‘INDIA’ is treated as the language code, and the country is not set. Therefore, when retrieving the country, it prints nothing.
Output:

$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS

7. What will be the output of the following Java program?

advertisement
  1.     import java.util.*;
  2.     class LOCALE_CLASS
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Locale obj = new Locale("HINDI", "INDIA") ;
  7.             System.out.print(obj.getCountry());
  8.         }
  9.     }

a) India
b) INDIA
c) Compilation Error
d) Nothing is displayed
View Answer

Answer: b
Explanation: Since the Locale() constructor has two parameters—the first representing the language and the second representing the country—’INDIA’ is set as the country and is therefore printed.
Output:

$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
INDIA

8. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class LOCALE_CLASS
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Locale obj = new Locale("HINDI") ;
  7.             System.out.print(obj.getDisplayLanguage());
  8.         }
  9.     }

a) India
b) INDIA
c) hindi
d) Nothing is displayed
View Answer

Answer: c
Explanation: Since only a single argument is passed to the Locale() constructor, ‘HINDI’ is treated as the language. Therefore, it prints ‘hindi’.
Output:

$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
hindi

9. What will be the output of the following Java program?

  1.     import java.util.*;
  2.     class LOCALE_CLASS
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Locale obj = new Locale("HINDI", "INDIA") ;
  7.             System.out.print(obj.getDisplayLanguage());
  8.         }
  9.     }

a) India
b) INDIA
c) hindi
d) Nothing is displayed
View Answer

Answer: c
Explanation: Since Locale() constructor has two parameters, the first one is language and the second one is the country. Hence the hindi is printed.
Output:

$ javac LOCALE_CLASS.java
$ java LOCALE_CLASS
hindi

Sanfoundry Global Education & Learning Series – Java Programming Language.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.