
ゆめさくが
例外処理の
ログインなしで
この 分野の小分野
- 例外の
種類と try-catch(7問) - try-with-resources、
カスタム例外、 multi-catch(7問)
例外処理の問題
14 問選ぶと、
例外の
種類と try-catch 問 1Throwable、
Error、 Exception、 RuntimeException の 継承の 関係と して 正しい ものは どれですか。 問 2次の
コードを コンパイルした ときの 結果と して 正しい ものは どれですか。 import java.io.IOException; public class Main { static void read() throws IOException { throw new IOException("x"); } static void a() throws Exception { read(); // (A) } static void b() { try { read(); // (B) } catch (RuntimeException e) { System.out.print("R"); } } static void c() { try { read(); // (C) } catch (Exception e) { System.out.print("E"); } } public static void main(String[] args) { c(); } }問 3次の
コードを コンパイルした ときの 結果と して 正しい ものは どれですか。 public class Main { public static void main(String[] args) { try { System.out.println("A"); } catch (Exception e) { // (A) } try { System.out.println("B"); } catch (RuntimeException e) { // (B) } try { System.out.println("C"); } catch (java.io.IOException e) { // (C) } try { System.out.println("D"); } catch (Throwable e) { // (D) } } }問 4次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { public static void main(String[] args) { try { try { System.out.print("1"); throw new IllegalStateException(); } catch (IllegalStateException e) { System.out.print("2"); throw new IllegalArgumentException(); } catch (IllegalArgumentException e) { System.out.print("3"); } finally { System.out.print("4"); } } catch (RuntimeException e) { System.out.print("5"); } System.out.print("6"); } }問 5次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static int f() { try { System.out.print("T"); throw new IllegalStateException(); } finally { System.out.print("F"); return 5; } } public static void main(String[] args) { try { System.out.print(f()); } catch (IllegalStateException e) { System.out.print("E"); } } }問 6次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static StringBuilder f() { StringBuilder sb = new StringBuilder("T"); try { return sb; } finally { sb.append("F"); sb = new StringBuilder("X"); } } public static void main(String[] args) { System.out.println(f()); } }問 7次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static void c() { System.out.print("c "); int[] a = new int[1]; a[1] = 5; System.out.print("c2 "); } static void b() { try { c(); System.out.print("b "); } finally { System.out.print("bF "); } } public static void main(String[] args) { try { b(); } catch (IllegalArgumentException e) { System.out.print("M "); } System.out.print("end"); } }try-with-resources、
カスタム 例外、 multi-catch 問 8次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Res implements AutoCloseable { private final String name; Res(String name) { if (name.equals("B")) { throw new IllegalStateException("B"); } this.name = name; System.out.print("open" + name + " "); } @Override public void close() { System.out.print("close" + name + " "); } } public class Main { public static void main(String[] args) { try (Res a = new Res("A"); Res b = new Res("B"); Res c = new Res("C")) { System.out.print("body "); } catch (IllegalStateException e) { System.out.print("catch" + e.getMessage()); } } }問 9次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Res implements AutoCloseable { @Override public void close() { System.out.print("C "); throw new IllegalStateException("close"); } } public class Main { public static void main(String[] args) { try (Res r = new Res()) { System.out.print("T "); } catch (IllegalStateException e) { System.out.print(e.getMessage() + " "); } finally { System.out.print("F"); } } }問 10次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Res implements AutoCloseable { private final String name; Res(String name) { this.name = name; } @Override public void close() { throw new IllegalStateException("close" + name); } } public class Main { public static void main(String[] args) { try (Res a = new Res("A"); Res b = new Res("B")) { throw new RuntimeException("body"); } catch (RuntimeException e) { System.out.print(e.getMessage()); for (Throwable s : e.getSuppressed()) { System.out.print(" " + s.getMessage()); } } } }問 11java.lang.AutoCloseable と
java.io.Closeable の 説明と して 正しい ものは どれですか。 問 12次の
コードを コンパイルした ときの 結果と して 正しい ものは どれですか。 class Res implements AutoCloseable { private final String name; Res(String name) { this.name = name; } @Override public void close() { System.out.print("close" + name + " "); } } public class Main { public static void main(String[] args) { Res r1 = new Res("1"); Res r2 = new Res("2"); r2 = new Res("3"); // (A) try (r1; r2) { // (B) System.out.print("body "); } } }問 13次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class AppException extends Exception { AppException(String message, Throwable cause) { super(message, cause); } } public class Main { static void load() throws AppException { try { Integer.parseInt("x"); } catch (NumberFormatException e) { throw new AppException("load", e); } } public static void main(String[] args) { try { load(); } catch (AppException e) { System.out.print(e.getMessage() + " " + e.getCause().getClass().getSimpleName()); } } }問 14次の
コードを コンパイルした ときの 結果と して 正しい ものは どれですか。 public class Main { public static void main(String[] args) { try { if (args.length == 0) { throw new IllegalStateException("s"); } Integer.parseInt(args[0]); } catch (NumberFormatException | IllegalStateException e) { // (A) System.out.print(e.getMessage()); e = new IllegalStateException("t"); // (B) } } }
14 問中 0 問に答えました
解説の一覧
解いた
例外の 種類と try-catch
問1Throwable、 Error、 Exception、 RuntimeException の 継承の 関係として 正しい ものは どれですか。
正解は
問2次の コードを コンパイルした ときの 結果として 正しい ものは どれですか。
import java.io.IOException;
public class Main {
static void read() throws IOException {
throw new IOException("x");
}
static void a() throws Exception {
read(); // (A)
}
static void b() {
try {
read(); // (B)
} catch (RuntimeException e) {
System.out.print("R");
}
}
static void c() {
try {
read(); // (C)
} catch (Exception e) {
System.out.print("E");
}
}
public static void main(String[] args) {
c();
}
}正解は
問3次の コードを コンパイルした ときの 結果として 正しい ものは どれですか。
public class Main {
public static void main(String[] args) {
try {
System.out.println("A");
} catch (Exception e) { // (A)
}
try {
System.out.println("B");
} catch (RuntimeException e) { // (B)
}
try {
System.out.println("C");
} catch (java.io.IOException e) { // (C)
}
try {
System.out.println("D");
} catch (Throwable e) { // (D)
}
}
}正解は
問4次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
public static void main(String[] args) {
try {
try {
System.out.print("1");
throw new IllegalStateException();
} catch (IllegalStateException e) {
System.out.print("2");
throw new IllegalArgumentException();
} catch (IllegalArgumentException e) {
System.out.print("3");
} finally {
System.out.print("4");
}
} catch (RuntimeException e) {
System.out.print("5");
}
System.out.print("6");
}
}正解は
問5次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static int f() {
try {
System.out.print("T");
throw new IllegalStateException();
} finally {
System.out.print("F");
return 5;
}
}
public static void main(String[] args) {
try {
System.out.print(f());
} catch (IllegalStateException e) {
System.out.print("E");
}
}
}正解は
問6次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static StringBuilder f() {
StringBuilder sb = new StringBuilder("T");
try {
return sb;
} finally {
sb.append("F");
sb = new StringBuilder("X");
}
}
public static void main(String[] args) {
System.out.println(f());
}
}正解は
問7次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static void c() {
System.out.print("c ");
int[] a = new int[1];
a[1] = 5;
System.out.print("c2 ");
}
static void b() {
try {
c();
System.out.print("b ");
} finally {
System.out.print("bF ");
}
}
public static void main(String[] args) {
try {
b();
} catch (IllegalArgumentException e) {
System.out.print("M ");
}
System.out.print("end");
}
}正解は
try-with-resources、 カスタム例外、 multi-catch
問8次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Res implements AutoCloseable {
private final String name;
Res(String name) {
if (name.equals("B")) {
throw new IllegalStateException("B");
}
this.name = name;
System.out.print("open" + name + " ");
}
@Override
public void close() {
System.out.print("close" + name + " ");
}
}
public class Main {
public static void main(String[] args) {
try (Res a = new Res("A"); Res b = new Res("B"); Res c = new Res("C")) {
System.out.print("body ");
} catch (IllegalStateException e) {
System.out.print("catch" + e.getMessage());
}
}
}正解は
問9次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Res implements AutoCloseable {
@Override
public void close() {
System.out.print("C ");
throw new IllegalStateException("close");
}
}
public class Main {
public static void main(String[] args) {
try (Res r = new Res()) {
System.out.print("T ");
} catch (IllegalStateException e) {
System.out.print(e.getMessage() + " ");
} finally {
System.out.print("F");
}
}
}正解は
問10次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Res implements AutoCloseable {
private final String name;
Res(String name) {
this.name = name;
}
@Override
public void close() {
throw new IllegalStateException("close" + name);
}
}
public class Main {
public static void main(String[] args) {
try (Res a = new Res("A"); Res b = new Res("B")) {
throw new RuntimeException("body");
} catch (RuntimeException e) {
System.out.print(e.getMessage());
for (Throwable s : e.getSuppressed()) {
System.out.print(" " + s.getMessage());
}
}
}
}正解は
問11java.lang.AutoCloseable と java.io.Closeable の 説明として 正しい ものは どれですか。
正解は
問12次の コードを コンパイルした ときの 結果として 正しい ものは どれですか。
class Res implements AutoCloseable {
private final String name;
Res(String name) {
this.name = name;
}
@Override
public void close() {
System.out.print("close" + name + " ");
}
}
public class Main {
public static void main(String[] args) {
Res r1 = new Res("1");
Res r2 = new Res("2");
r2 = new Res("3"); // (A)
try (r1; r2) { // (B)
System.out.print("body ");
}
}
}正解は
問13次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class AppException extends Exception {
AppException(String message, Throwable cause) {
super(message, cause);
}
}
public class Main {
static void load() throws AppException {
try {
Integer.parseInt("x");
} catch (NumberFormatException e) {
throw new AppException("load", e);
}
}
public static void main(String[] args) {
try {
load();
} catch (AppException e) {
System.out.print(e.getMessage() + " " + e.getCause().getClass().getSimpleName());
}
}
}正解は
問14次の コードを コンパイルした ときの 結果として 正しい ものは どれですか。
public class Main {
public static void main(String[] args) {
try {
if (args.length == 0) {
throw new IllegalStateException("s");
}
Integer.parseInt(args[0]);
} catch (NumberFormatException | IllegalStateException e) { // (A)
System.out.print(e.getMessage());
e = new IllegalStateException("t"); // (B)
}
}
}正解は
ほかの分野
Java の
概要と 簡単な Java プログラムの作成 main メソッド、
コンパイルと 実行、 パッケージ Javaの
基本 データ型と 文字列の操作 変数と
スコープと var と 文字列、 配列と ArrayList 演算子と
制御構造 演算子と
分岐と switch、 繰り返しと break と continue クラスの
定義と インスタンスの使用 クラス、
メソッド、 コンストラクタ、 オーバーロード、 static、 アクセス修飾子、 パターン・マッチング、レコード 継承と
インタフェースの使用 継承、
抽象クラス、 オーバーライド、 キャスト、 インタフェースと シール・クラス
Java Silverの頁に、
続きは ゆめさくで
試験の