250+ TOP MCQs on Environment Properties and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Environment Properties”.

1. Which object Java application uses to create a new process?
a) Process
b) Builder
c) ProcessBuilder
d) CreateBuilder

Answer: c
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.

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()

Answer: b
Clarification: 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

Answer: a
Clarification: 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

Answer: c
Clarification: 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

Answer: d
Clarification: 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

Answer: c
Clarification: 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

Answer: d
Clarification:

             @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;

Answer: a
Clarification: @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

Answer: b
Clarification: 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”);

Answer: c
Clarification: This method can be used to load files using relative path to the package of the class.

Leave a Reply

Your email address will not be published. Required fields are marked *