Ch16 Exceptions, Assertions, and Localization Flashcards

1
Q

What is an assertion?

A

An assertion is a boolean expression that you can place at a point in your code where you expect something to be true.

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

What is an assert statement?

A

An assert statement is used to declare an expected boolean condition in a program. If the program is running with assertions enabled, then the condition is checked at runtime. If the condition is false, the Java runtime system throws an AssertionError .

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

What is the syntax of an assert statement?

A

assert keyword
boolean expression
optional message
example:

assert 1 == age;
assert(2 == height);
assert 100.0 == length : “Problem with length”;

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

Is the following assert statement valid?

assert (“Cecilia”.equals(name)): “Failed to verify user data”;

A

Yes.

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

Is the following assert statement valid?

assert (1);

A

No. it expects a boolean value.

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

Is the following assert statement valid?

assert x -> true;

A

No. it expects a boolean value.

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

Is the following assert statement valid?

assert.test(5 > age);

A

No. The syntax is invalid

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

What happens when assertions are enabled and the boolean expression of an assertion is false?

A

Java will throw an AssertionError.

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

How can we enable assertions using the java command?

A

// single-file source-code

java -ea SomeFile.java
java -enableassertions SomeFile.java
// normal

java -ea SomeFile
java -enableassertions SomeFile

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

Given the package com.demos, how do we run the application and only enable assertions for that package?

A
  • java -ea:com.demos… MyMainClass
  • With one argument ending in “…”, assertions are enabled in the specified package and any subpackages by default.
  • With one argument not ending in “…”, assertions are enabled in the specified class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What flag can we use to exclude a package/class from enabling assertions?

A

-da or -disableassertions

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

Given the class Test in package com.demos, how do we run the application and enable assertions for that package, but exclude the class Test?

A

java -ea:com.demos… -da:com.demos.Test MyMainClass

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

What is the difference between:

java.time.LocalDate
java.time.LocalTime
java.time.LocalDateTime
java.time.ZonedDateTime

A
  • java.time.LocalDate: Date with day, month, year
  • java.time.LocalTime: Time of day
  • java.time.LocalDateTime: Date and time with no time zone
  • java.time.ZonedDateTime: Date and time with a specific time zone
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What static method exists to get the current value for:

java.time.LocalDate
java.time.LocalTime
java.time.LocalDateTime
java.time.ZonedDateTime

A

now()

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

What static method can we use to get a specific date?

A

of()

example:
LocalDate d1 = LocalDate.of(2020, 10, 20);
LocalTime t1 = LocalTime.of(6, 15, 30);

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

Create a LocalTime object of 6 hours and 15 minutes.

A

LocalTime time = LocalTime.of(6, 15);

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

Create a LocalTime object of 6 hours, 15 minutes, 0 seconds and 200 nanoseconds

A

LocalTime time = LocalTime.of(6, 15, 0, 200);

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

Given the following objects, create a LocalDateTime object.

LocalDate d1 = LocalDate.of(2020, 10, 20);
LocalTime t1 = LocalTime.of(6, 15);

A

LocalDateTime dt1 = LocalDateTime.of(d1, t1);

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

What class can we use to format a date/time object to display standard formats?

A

DateTimeFormatter

example:
DateTimeFormatter.ISO_LOCAL_DATE // 2020-10-20
DateTimeFormatter.ISO_LOCAL_TIME // 11:22:34
DateTimeFormatter.ISO_LOCAL_DATE_TIME // 2011-12-03T10:15:30

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

How can we print a date/time object in a custom format?

example: October 20, 2020 at 11:12

A

using DateTimeFormatter.ofPattern() and use it with the format() method.

example:

var f = DateTimeFormatter.ofPattern(“MMMM dd, yyyy ‘at’ hh:mm”);
somedateobject.format(f);

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

What does the date/time symbol ‘M’ print?

A

‘M’ = month and prints the index of the month, starting with 1.

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

What does the date/time symbol ‘MM’ print?

A

‘M’ = month and prints the index of the month, starting with 01.

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

What does the date/time symbol ‘MMM’ print?

A

‘MMM’ = month and prints the first three letters of the month (example: JUL).

24
Q

What does the date/time symbol ‘MMMM’ print?

A

‘MMMM’ = month and prints the full month name.

25
Q

What does the date/time symbol ‘a’ print?

A

‘a’ = a.m./p.m. and prints either AM or PM

26
Q

What does the date/time symbol ‘z’ print?

A

‘z’ = Time Zone Name and prints the full time zone name.

example: Eastern Standard Time, EST

27
Q

What does the date/time symbol ‘Z’ print?

A

‘Z’ = Time Zone Name and prints the relative timezone.

example: -0400

28
Q

Given a LocalDateTime dt, why does the following code throw a runtime exception?

var f = DateTimeFormatter.ofPattern(“h:mm z”);
dt.format(f);

A

LocalDateTime does not have any information of time zones (z).

29
Q

Given a LocalDate dt, why does the following code throw a runtime exception?

var f = DateTimeFormatter.ofPattern(“h:mm”);
dt.format(f);

A

LocalDate does not have any information of time (h:mm)

30
Q

What other statement does the exact same as the last statement of this code?

var dt = LocalDateTime.of(2020, Month.OCTOBER, 20, 6, 15, 30);
var f = DateTimeFormatter.ofPattern(“MM/dd/yyyy hh:mm:ss”);
dt.format(f)

A

f.format(dt);

31
Q

Given the following LocalDate, create a formatter to print out: Party’s at October 20, 2020.

LocalDate d1 = LocalDate.of(2020, 10, 20);

A

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“‘Party’’s at’ MMMM d, yyyy”);

32
Q

How do we get the current locale?

A

Locale locale = Locale.getDefault();

33
Q

What does Locale.getDefault() return?

A

A Locale object containing the locale language (required) and the locale country (optional).

34
Q

Which Locale formats is correct/incorrect?

  • US
  • en_US
  • US_en
  • EN
  • enUS
A

All but en_US is incorrect.

35
Q

What are three ways to get a Locale other than the default one?

A

// 1
Locale.GERMAN // -> de
Locale.GERMANY // -> de_DE

//2
new Locale(“fr”) // fr
new Locale(“hi”, “IN”) //hi_IN
//3
new Locale.Builder()
.setLanguage(“en”)
.setRegion(“US”)
.build()

36
Q

How can you change the default Locale of your Java program? (for testing etc)

A

Locale.setDefault(someLocaleObjectHere);

37
Q

How do we format the following code to german currency?

double price = 11;

A

NumberFormat myLocale = NumberFormat.getCurrencyInstance(Locale.GERMANY);

myLocale.format(price);

38
Q

What package does NumberFormat belong to?

A

java.text

39
Q

How can we format a number to the default locale?

A

NumberFormat.getInstance()
or
NumberFormat.getNumberInstance()

40
Q

How can we format decimal values to the default locale before displaying?

A

NumberFormat.getIntegerInstance();

41
Q

How can we format data to a structured object / primitive value, taking into account the Locale?

A

using NumberFormat.parse();

42
Q

What does the following code return?

var cf = NumberFormat.getCurrencyInstance();
cf.parse(value); //returns what?

A

A Number object.

43
Q

What are two Locale.Category values?

A
  • DISPLAY
  • FORMAT
44
Q

What is a ResourceBundle?

A

A ResourceBundle contains the locale specific objects to be used by a program. It is like a map with keys and values.

45
Q

Given the ResourceBundle file Zoo_fr

How do you get this Resource Bundle from java code?

A

ResourceBundle.getBundle(“Zoo”);
or
ResourceBundle.getBundle(“Zoo”, locale);

46
Q

Given a ResourceBundle ‘Zoo’ with the default locale, what happens when requesting a ResourceBundle ‘Zoo’ of a locale that does not exist?

A

It will return the ResourceBundle with the default locale.

47
Q

What happens when requesting a ResourceBundle of which none exists?

A

Java will throw a MissingResourceException

48
Q

What is the maximum number of files Java would need to consider to find the appropriate resource bundle with the following code?

Locale.setDefault(new Locale(“hi”));
ResourceBundle rb = ResourceBundle.getBundle(“Zoo”, new Locale(“en”));

A

three

  1. Zoo_en.properties
  2. Zoo_hi.properties
  3. Zoo.properties
49
Q

What happens when Java cannot find a key in the requested (and existing) resource bundle?

Use the example: Zoo_fr_FR.properties

A

After checking Zoo_fr_FR.properties, it will go to Zoo_fr.properties. If it doesn’t exist there, it will go to Zoo.properties.

If it also does not exist there it will throw a MissingResourceException.

50
Q

Why should you use the getProperty()/setProperty() over get()/set() methods when working with the Properties class?

A

With getProperty() you can supply a default value.
With setProperty() you can assure the data can be read

51
Q

Which of the following classes contain at least one compiler error? (Choose all that apply.)

class Danger extends RuntimeException {
public Danger(String message) {
super();
}
public Danger(int value) {
super((String) null);
}
class Catastrophe extends Exception {
public Catastrophe (Throwable c) throws RuntimeException {
super(new Exception());
c.printStackTrace();
}
}
class Emergency extends Danger (
public Emergency () {}
public Emergency (String message) {
super (message);
}
}
A. Danger
B. Catastrophe
C. Emergency
D. All of these classes compile correctly. E. The answer cannot be determined from the information given.

A

C. Exception and RuntimeException, along with many other exceptions in the Java API, define a no-argument constructor, a constructor that takes a String, and a constructor that takes a Throwable. For this reason, Danger compiles without issue. Catastrophe also compiles without issue. Just creating a new checked exception, without throwing it, does not require it to be handled or declared. Finally, Emergency does not compile. The no-argument constructor in Emergency must explicitly call a parent constructor, since Danger does not define a no-argument constructor,

52
Q

What is the output of the following code?

LocalDate date = LocalDate.parse(“2020-04-30”, DateTimeFormatter. ISO_LOCAL_DATE_TIME);

System.out.println(date.getYear () + “ “
+ date.getMonth() + “ “ + date.getDayOfMonth());

A. 2020 APRIL 2
B. 2020 APRIL 30
C. 2020 MAY 2
D. The code does not compile.
E. A runtime exception is thrown.

A

E.

A LocalDate does not have a time element. Therefore, a Date/Time formatter is not appropriate. The code compiles but throws an exception at runtime. If ISO_LOCAL_DATE was used, then the code would compile and option B would be the correct answer.

53
Q

What is the output of the following code?

LocalDate date = LocalDate.parse(“2020-04-30”, DateTimeFormatter. ISO_LOCAL_DATE);

System.out.println (date.getYear () + “ “
+ date.getMonth()+””+ date.getDayOfMonth());

A. 2020 APRIL 2
B. 2020 APRIL 30
C. 2020 MAY 2
D. The code does not compile.
E. A runtime exception is thrown.

A

B.

The code compiles and option B is the correct answer.

54
Q

Assume that all of the files mentioned in the answer choices exist and define the same keys. Which one will be used to find the key in line 8?

6: Locale.setDefault(new Locale(“en”, “US”));
7: var b = ResourceBundle.getBundle (“Dolphins”);
8: System.out.println(b.getString(“name”));

A. Dolphins.properties
B. Dolphins_US.properties
C. Dolphins_en.properties
D. Whales.properties
E. Whales_en_US.properties
F. The code does not compile.

A

C.

Java will first look for the most specific matches it can find, starting with Dolphins_en_US. properties. Since that is not an answer choice, it drops the country and looks for Dolphins_en.properties, making option C correct. Option B is incorrect because a country without a language is not a valid locale.

55
Q

What is the output of the following code?

LocalDateTime ldt = LocalDateTime.of (2020, 5, 10, 11, 22, 33);
var f = DateTimeFormatter.ofLocalizedTime(FormatStyle. SHORT); System.out.println(ldt.format(f));
A. 3/7/19 11:22 AM
B. 5/10/20 11:22 AM
C. 3/7/19
D. 5/10/20
E. 11:22 AM
F. The code does not compile.
G. A runtime exception is thrown.

A

E.

Even though ldt has both a date and time, the formatter outputs only time.

56
Q

Identify the correct statement about i18n.

A. I18N class allows you to port your code for multiple regions and/or languages.

B. You should use Locale and formatter objects such as NumberFormat and DateFormat to generate locale specific output.

C. The i18n method of NumberFormat and DateFormat allows you to generate locale specific output.

D. Using default locale for NumberFormat and DateFormat automatically ensures that the formatted text will be localized to the location setting of the machine on which the code is run. (Assuming that default locale hasn’t been explicitly changed by any means.)

E. i18n stands for Internationalization and it is handled automatically by Java.

A

B, D

A: There is no class named I18N.
E: While i18n indeed stands for Internationalization, it is not done automatically. You have to write code to internationalize your output.