Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 78
Question ID: UKOCP78832
Given code:
- package com.udayankhattry.ocp;
- import java.io.*;
- public class Test {
- public static void main(String[] args) throws IOException {
- var file = new File("F:\\temp.dat");
- try(
- var os = new DataOutputStream(new FileOutputStream(file));
- var is = new DataInputStream((new FileInputStream(file)))
- ) {
- os.writeChars("JAVA");
- System.out.println(is.readChar());
- }
- }
- }
F: is accessible for reading/writing and currently doesn't contain any directories/files.
What is the result?
- A J
- B A
- C V
- D None of the other options
Reveal correct answer
Correct answer: A
Explanation
UKOCP78832:
writeChars(String) is a convenient method to write multiple characters, behind the scenes it writes 4 characters as 8 bytes of data.
readChar() method returns 2 bytes of data, interpreted as char. Return type is char and not int.
'J' is displayed in the output.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
