Chapter 16: 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

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

A

Nothing happens and Java will continue execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
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
11
Q

By default, assertions are enabled or disabled?

A

disabled.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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

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

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

A

java -ea:com.demos.Test MyMainClass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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
15
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
16
Q

Why should assertions never alter outcomes?

example:
int x = 10;
assert ++x > 10;

A

This is not a good use of assertions because the outcome of the code will be different depending on wether assertions are turned on.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
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
18
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
19
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
20
Q

Create a LocalTime object of 6 hours and 15 minutes.

A

LocalTime time = LocalTime(6, 15);

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

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

A

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

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

Given the following objects, create a LocalDateTime object.

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

A

LocalDateTime dt1 = new LocalDateTime(d1, t1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
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
25
Q

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

A

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

26
Q

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

A

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

27
Q

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

A

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

28
Q

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

A

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

29
Q

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

A

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

30
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

31
Q

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

A

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

example: -0400

32
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).

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

34
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);

35
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”);

36
Q

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

DateTimeFormatter.ofPattern(“‘Time is: hh:mm: “);

A

There is an incomplete escape sequence (‘).

37
Q

What is Internationalization?

A

It is the process of designing your program so it can be adapted. This involves placing strings in a properties file and ensuring the proper data formatters are used.

38
Q

What is Localization?

A

supporting multiple locales or geographic regions and includes translating string to different languages and outputting dates and numbers in the correct format for that locale.

39
Q

How do we get the current locale?

A

Locale locale = Locale.getDefault();

40
Q

What does Locale.getDefault() return?

A

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

41
Q

Which Locale formats is correct/incorrect?

  • US
  • en_US
  • US_en
  • EN
  • enUS
A

All but en_US is incorrect.

42
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()
43
Q

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

A

Locale.setDefault(someLocaleObjectHere);

44
Q

How do we format the following code to german currency?

double price = 11;

A

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

myLocale.format(price);

45
Q

What package does NumberFormat belong to?

A

java.text

46
Q

How can we format a number to the default locale?

A

NumberFormat.getInstance()
or
NumberFormat.getNumberInstance()

47
Q

How can we format a percentage to the default locale?

A

NumberFormat.getPercentInstance()

48
Q

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

A

NumberFormat.getIntegerInstance();

49
Q

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

A

using NumberFormat.parse();

50
Q

What does the following code return?

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

A Number object.

51
Q

What are two Locale.Category values?

A
  • DISPLAY

- FORMAT

52
Q

What is the Locale.Category value DISPLAY?

A

Category used for displaying data about the locale.

53
Q

What is the Locale.Category value FORMAT?

A

Category used for formatting dates, numbers, or currencies.

54
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.

55
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);

56
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.

57
Q

What happens when requesting a ResourceBundle of which none exists?

A

Java will throw a MissingResourceException

58
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

(page 783)

59
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.

60
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