This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Environment Properties”.
1. Which object Java application uses to create a new process?
a) Process
b) Builder
c) ProcessBuilder
d) CreateBuilder
View Answer
Explanation: Java application uses ProcessBuilder object to create a new process. By default, same set of environment variables passed which are set in application’s virtual machine process.
2. Which of the following is true about Java system properties?
a) Java system properties are accessible by any process
b) Java system properties are accessible by processes they are added to
c) Java system properties are retrieved by System.getenv()
d) Java system properties are set by System.setenv()
View Answer
Explanation: Java system properties are only used and accessible by the processes they are added.
3. Java system properties can be set at runtime.
a) True
b) False
View Answer
Explanation: Java system properties can be set at runtime using System.setProperty(name, value) or using System.getProperties().load() methods.
4. Which system property stores installation directory of JRE?
a) user.home
b) java.class.path
c) java.home
d) user.dir
View Answer
Explanation: java.home is the installation directory of Java Runtime Environment.
5. What does System.getProperty(“variable”) return?
a) compilation error
b) value stored in variable
c) runtime error
d) null
View Answer
Explanation: System.getProperty(“variable”) returns null value. Because, variable is not a property and if property does not exist, this method returns null value.
6. What is true about the setProperties method?
a) setProperties method changes the set of Java Properties which are persistent
b) Changing the system properties within an application will affect future invocations
c) setProperties method changes the set of Java Properties which are not persistent
d) setProperties writes the values directly into the file which stores all the properties
View Answer
Explanation: The changes made by the setProperties method are not persistent. Hence, it does not affect future invocation.
7. How to use environment properties in the class?
a) @Environment
b) @Variable
c) @Property
d) @Autowired
View Answer
Explanation:
@Autowired private Environment env;
This is how environment variables are injected in the class where they can be used.
8. How to assign values to variable using property?
a)
@Value("${my.property}") private String prop;
b)
@Property("${my.property}") private String prop;
c)
@Environment("${my.property}") private String prop;
d)
@Env("${my.property}") private String prop;View Answer
Explanation: @Value are used to inject the properties and assign them to variables.
9. Which environment variable is used to set java path?
a) JAVA
b) JAVA_HOME
c) CLASSPATH
d) MAVEN_HOME
View Answer
Explanation: JAVA_HOME is used to store a path to the java installation.
10. How to read a classpath file?
a) InputStream in = this.getClass().getResource(“SomeTextFile.txt”);
b) InputStream in = this.getClass().getResourceClasspath(“SomeTextFile.txt”);
c) InputStream in = this.getClass().getResourceAsStream(“SomeTextFile.txt”);
d) InputStream in = this.getClass().getResource(“classpath:/SomeTextFile.txt”);
View Answer
Explanation: This method can be used to load files using relative path to the package of the class.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Information Technology MCQs
- Practice Programming MCQs
- Check Java Books
- Apply for Computer Science Internship
- Check Programming Books