Java OCA OCP Practice Question 3045

Question

Consider the following code segment:

try (BufferedReader inputFile = new BufferedReader(new FileReader(srcFile));
    BufferedWriter outputFile// w ww.  ja  va2  s.c o  m
        = new BufferedWriter(new FileWriter(dstFile))) {  // TRY-BLOCK
    int ch = 0;
    while( (ch = inputFile.read()) != -1) {              // COND-CHECK
        outputFile.write( (char)ch );
    }
} catch (Exception exception) {
    System.err.println("Error in opening or processing file "
        + exception.getMessage());
}

Assume that srcFile and dstFile are Strings. Choose the correct option.

a)this program will get into an infinite loop because the condition check for end-of-stream  (checking != -1) is incorrect
b)this program will get into an infinite loop because the variable ch is declared as int instead of char
c)this program will result in a compiler error in line marked with comment trY-BLoCk because you need to use , (comma) instead of ; (semi-colon) as separator for opening multiple resources
d)this program works fine and copies srcFile to dstFile


d)

Note

options a) and b) this program does not get into an infinite loop because the condition check for end-of-stream (checking != -1) is correct and the variable ch needs to be declared as int (and not char).

option c) You can use ; (semi-colon) as separator for opening multiple resources in try-with-resources statement.




PreviousNext

Related