Methods and constructors Flashcards
What is the main purpose of a method in Java?
a) To handle exceptions in the program
b) To separate the code into reusable blocks
c) To compile the code faster
d) To enhance security
b) To separate the code into reusable blocks
What is the role of a constructor in Java?
a) To create a new method
b) To execute code only once
c) To initialize an object when it is instantiated
d) To define the properties of a class
c) To initialize an object when it is instantiated
What is the InvalidUOMException class used for?
a) Handling invalid user inputs
b) Handling invalid units of measure
c) Handling invalid conversion rates
d) Handling null pointer exceptions
b) Handling invalid units of measure
What is a POJO in Java?
a) A Java object used to perform calculations
b) A plain old Java object
c) A special type of method
d) A utility class for handling strings
b) A plain old Java object
What is the purpose of the MeasurementValue class?
a) To handle exceptions in the main method
b) To store and convert units of measurement
c) To format strings in the output
d) To read user input from the console
b) To store and convert units of measurement
What does the runConversions() method do in the MeasurementValue class?
a) Parses user input and prints results
b) Converts the input value into different units of measure
c) Initializes the input value
d) Reads data from a file
b) Converts the input value into different units of measure
Which method in the MeasurementValue class handles user input parsing?
a) runConversions()
b) convertImperialToMetric()
c) parseInput()
d) toString()
c) parseInput()
What does the convertImperialToMetric() method do?
a) Converts metric units to imperial units
b) Converts imperial units to metric units
c) Handles invalid conversions
d) Formats the output
b) Converts imperial units to metric units
What does the convertMetricToImperial() method do?
a) Converts imperial units to metric units
b) Formats the output
c) Converts metric units to imperial units
d) Parses user input
c) Converts metric units to imperial units
What exception is thrown if an invalid unit of measure is provided?
a) NullPointerException
b) InvalidUOMException
c) IllegalArgumentException
d) IOException
b) InvalidUOMException
What is the significance of the final keyword in the MeasurementValue class?
a) It marks a variable as immutable
b) It initializes the object
c) It throws an exception
d) It indicates a static variable
a) It marks a variable as immutable
What does the toString() method do in the MeasurementValue class?
a) It formats the output of the measurement values
b) It converts the values into different units
c) It parses user input
d) It throws an exception
a) It formats the output of the measurement values
What rounding mode is applied in the toString() method?
a) RoundingMode.DOWN
b) RoundingMode.UP
c) RoundingMode.HALF_DOWN
d) RoundingMode.HALF_UP
b) RoundingMode.UP
In the MeasurementValue class, what is the dFormat object used for?
a) Parsing the user input
b) Converting metric to imperial units
c) Formatting numeric values with two decimal points
d) Calculating conversions
c) Formatting numeric values with two decimal points
What is the purpose of the switch/case statement in the runConversions() method?
a) To execute code based on the user’s input unit of measure
b) To handle exceptions
c) To format the input value
d) To print the output in a formatted way
a) To execute code based on the user’s input unit of measure