This set of Java Multiple Choice Questions & Answers (MCQs) on “Environment Properties”.
1. Which object Java application uses to create a new process? Answer: c 2. Which of the following is true about Java system properties? Answer: b 3. Java system properties can be set at runtime. Answer: a 4. Which system property stores installation directory of JRE? Answer: c 5. What does System.getProperty(“variable”) return? Answer: d 6. What is true about the setProperties method? Answer: c 7. How to use environment properties in the class? This is how environment variables are injected in the class where they can be used. 8. How to assign values to variable using property? b) c) d) Answer: a 9. Which environment variable is used to set java path? Answer: b 10. How to read a classpath file? Answer: c
a) Process
b) Builder
c) ProcessBuilder
d) CreateBuilder
Clarification: 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.
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()
Clarification: Java system properties are only used and accessible by the processes they are added.
a) True
b) False
Clarification: Java system properties can be set at runtime using System.setProperty(name, value) or using System.getProperties().load() methods.
a) user.home
b) java.class.path
c) java.home
d) user.dir
Clarification: java.home is the installation directory of Java Runtime Environment.
a) compilation error
b) value stored in variable
c) runtime error
d) null
Clarification: System.getProperty(“variable”) returns null value. Because, variable is not a property and if property does not exist, this method returns null value.
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
Clarification: The changes made by the setProperties method are not persistent. Hence, it does not affect future invocation.
a) @Environment
b) @Variable
c) @Property
d) @Autowired
Clarification:
@Autowired
private Environment env;
a) @Value("${my.property}")
private String prop;
@Property("${my.property}")
private String prop;
@Environment("${my.property}")
private String prop;
@Env("${my.property}")
private String prop;
Clarification: @Value are used to inject the properties and assign them to variables.
a) JAVA
b) JAVA_HOME
c) CLASSPATH
d) MAVEN_HOME
Clarification: JAVA_HOME is used to store a path to the java installation.
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”);
Clarification: This method can be used to load files using relative path to the package of the class.