What will happen when you try to compile and run the following code? It is assumed that the outstream file is already created.

import java.io.*;

public class Main {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("outstream");
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            try {
                do {
                    String str = br.readLine();
                    System.out.println(str);
                } while (str != null);             
            }
            finally {
                fis.close();
                isr.close();
                br.close();
            }
        } catch(IOException e){
            e.printStackTrace();
        }
    }
}
Explanation
Compilation error will occur due to the fact that the str variable is declared inside the do .. while() loop.
Of course, it is allowed to declare variables inside loops. But then they should be used inside the loop as well. In the given example the str variable is used outside of the block in which it was declared - in a loop continuation.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести