Methods and constructors Flashcards

1
Q

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

A

b) To separate the code into reusable blocks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

c) To initialize an object when it is instantiated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

b) Handling invalid units of measure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

b) A plain old Java object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

b) To store and convert units of measurement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

b) Converts the input value into different units of measure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which method in the MeasurementValue class handles user input parsing?
a) runConversions()
b) convertImperialToMetric()
c) parseInput()
d) toString()

A

c) parseInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

b) Converts imperial units to metric units

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

c) Converts metric units to imperial units

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What exception is thrown if an invalid unit of measure is provided?
a) NullPointerException
b) InvalidUOMException
c) IllegalArgumentException
d) IOException

A

b) InvalidUOMException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

a) It marks a variable as immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

a) It formats the output of the measurement values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What rounding mode is applied in the toString() method?
a) RoundingMode.DOWN
b) RoundingMode.UP
c) RoundingMode.HALF_DOWN
d) RoundingMode.HALF_UP

A

b) RoundingMode.UP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

c) Formatting numeric values with two decimal points

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

a) To execute code based on the user’s input unit of measure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the primary input format expected by the MeasurementValue constructor?
a) A double followed by a comma
b) A string containing a numeric value and a unit of measure separated by a space
c) A single numeric value
d) An array of numbers

A

b) A string containing a numeric value and a unit of measure separated by a space

17
Q

What type of exception does the MeasurementValue class throw if the input is invalid?
a) IOException
b) RuntimeException
c) InvalidUOMException
d) ArithmeticException

A

c) InvalidUOMException

18
Q

What does the spacePos variable check in the MetricUnitConverter class?
a) If the user input is valid
b) If the user input contains a space between the value and the unit
c) If the input is numeric
d) If the conversion is possible

A

b) If the user input contains a space between the value and the unit

19
Q

Which Java class is imported for reading user input in the MetricUnitConverter class?
a) BufferedReader
b) Scanner
c) InputStreamReader
d) Console

A

b) Scanner

20
Q

What is the output of the program when the input ‘8 m’ is provided?
a) Error: Invalid input
b) The measurement in meters only
c) The measurement in both metric and imperial units
d) The measurement in kilometers only

A

c) The measurement in both metric and imperial units