MCQs on Reading Properties File
Which Java class is used to read properties from a properties file?
a) java.util.Properties
b) java.io.FileReader
c) java.util.Map
d) java.io.BufferedReader
Answer: a) java.util.Properties
Which method of the Properties class is used to load properties from a file?
a) load()
b) read()
c) import()
d) initialize()
Answer: a) load()
What is the correct way to create a Properties object and load properties from a file named config.properties?
a)
Properties properties = new Properties();
properties.load(new FileInputStream("config.properties"));
b)
Properties properties = new Properties();
properties.read(new FileInputStream("config.properties"));
c)
Properties properties = new Properties();
properties.load(new File("config.properties"));
d)
Properties properties = new Properties();
properties.load(new FileReader("config.properties"));
Answer: a)
Properties properties = new Properties();
properties.load(new FileInputStream("config.properties"));
How do you access a property value from a Properties object using a key?
a) properties.get(key)
b) properties.getProperty(key)
c) properties.find(key)
d) properties.getValue(key)
Answer: b) properties.getProperty(key)
Which of the following is a correct way to handle exceptions when loading properties from a file?
a) try { properties.load(new FileInputStream("config.properties")); } catch (Exception e) { e.printStackTrace(); }
b) try { properties.load(new File("config.properties")); } catch (IOException e) { e.printStackTrace(); }
c) try { properties.load(new FileReader("config.properties")); } catch (IOException e) { e.printStackTrace(); }
d) try { properties.read(new FileInputStream("config.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); }
Answer: a) try { properties.load(new FileInputStream("config.properties")); } catch (Exception e) { e.printStackTrace(); }
What will the properties.getProperty("key") method return if the key does not exist in the properties file?
a) null
b) "" (empty string)
c) 0
d) default value
Answer: a) null
Which of the following is the correct way to set a property value in a Properties object?
a) properties.setProperty("key", "value");
b) properties.addProperty("key", "value");
c) properties.put("key", "value");
d) properties.putProperty("key", "value");
Answer: a) properties.setProperty("key", "value");
How can you check if a Properties object contains a specific key?
a) properties.containsKey("key")
b) properties.hasKey("key")
c) properties.keyExists("key")
d) properties.keyPresent("key")
Answer: a) properties.containsKey("key")
Which method will list all the property names in a Properties object?
a) properties.list()
b) properties.propertyNames()
c) properties.keys()
d) properties.getAllKeys()
Answer: b) properties.propertyNames()
How can you load properties from a file located in a directory relative to the project's root directory?
a) properties.load(new FileInputStream("relative/path/to/config.properties"));
b) properties.load(new FileInputStream("config.properties"));
c) properties.load(new File("relative/path/to/config.properties"));
d) properties.load(new ClassPathResource("relative/path/to/config.properties").getInputStream());
Answer: a) properties.load(new FileInputStream("relative/path/to/config.properties"));
Which class provides a utility method for reading and writing properties in a file?
a) java.util.Properties
b) java.io.File
c) java.nio.file.Files
d) java.util.Map
Answer: a) java.util.Properties
No comments:
Post a Comment