<%@ include file="../../ga4.jsp" %> Character Sets Readers[Java] - Quiz Explanation

Character Sets and Readers - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.
 
1. How many bytes are there in a Java char?
Please select the best answer.
  A. 1
  B. 2
  C. 3
  D. 4
 

The correct answer is B.

Every Java char is a 2-byte, Unicode character. ASCII and ISO Latin-1 characters are 1 byte long, but the char data type is not ASCII or ISO Latin-1 (unlike C). The UTF-8 format used to encode strings in .class files has some 3-byte characters (and some 1-byte characters, and some 2-byte characters) but this is not used for raw chars. UCS uses four-byte characters, but Java does not use UCS.

2. What's Java's basic character set?
Please select the best answer.
  A. ASCII
  B. ISO Latin-1
  C. Unicode
  D. UTF-8
 

The correct answer is C.

Unicode is Java's basic character set. Source-code files compiled by javac and other existing Java compilers are allegedly written in ASCII and use an escaped form of Unicode. However, in practice many of these compilers really accept ISO Latin-1 as well. Internally, in class files and data input and output streams, Java uses UTF-8 to encode Unicode strings. However, as far as Java is concerned these are all just convenient variants on Unicode. Unicode is the fundamental Java character set.

3. What's the difference between a reader and an input stream?
Please select the best answer.
  A. An input stream can be buffered; a reader cannot be
  B. A reader reads characters; an input stream reads bytes
  C. A reader returns characters; an input stream returns bytes
  D. A reader returns characters; an input stream returns numbers
 

The correct answer is D.

This is a very picky question. Answer A is clearly wrong. Readers can be buffered, just like input streams. However, Answers B, C, and D are very similar. Answer B is wrong because a reader normally reads bytes, just like an input stream does. (The single exception is CharArrayReader.) All input and output in Java is done with bytes. However, the bytes read are converted into characters before being returned by the read() method. Answer C is closer but still wrong because an input stream does not just return bytes. Many read methods in the different input stream classes return ints, doubles, floats, and other numeric data types. The best answer, therefore, is D. Even this is a little flaky, because some deprecated methods like readLine() in DataInputStream do return Strings, but Answer D is still the best available answer.