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
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
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
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.
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
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
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.
6. What will be the output of the following Java program?
import java.util.*;
class LOCALE_CLASS
{
public static void main(String args[])
{
Locale obj = new Locale("INDIA") ;
System.out.print(obj.getCountry());
}
}
a) India
b) INDIA
c) Compilation Error
d) Nothing is displayed
View Answer
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?
import java.util.*;
class LOCALE_CLASS
{
public static void main(String args[])
{
Locale obj = new Locale("HINDI", "INDIA") ;
System.out.print(obj.getCountry());
}
}
a) India
b) INDIA
c) Compilation Error
d) Nothing is displayed
View Answer
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?
import java.util.*;
class LOCALE_CLASS
{
public static void main(String args[])
{
Locale obj = new Locale("HINDI") ;
System.out.print(obj.getDisplayLanguage());
}
}
a) India
b) INDIA
c) hindi
d) Nothing is displayed
View Answer
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?
import java.util.*;
class LOCALE_CLASS
{
public static void main(String args[])
{
Locale obj = new Locale("HINDI", "INDIA") ;
System.out.print(obj.getDisplayLanguage());
}
}
a) India
b) INDIA
c) hindi
d) Nothing is displayed
View Answer
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.
- Practice Programming MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Java Books
- Check Programming Books