
ゆめさくが
継承と
ログインなしで
この 分野の小分野
- 継承、
抽象クラス、 オーバーライド、キャスト(12問) - インタフェースと
シール・クラス(12問)
継承と インタフェースの 使用の問題
24 問選ぶと、
継承、
抽象クラス、 オーバーライド、 キャスト 問 1Java の
クラスの 継承に ついて 正しい 説明は どれですか。 問 2次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class A { A() { System.out.print("a"); } A(int n) { System.out.print("A" + n); } } class B extends A { B() { this(2); System.out.print("b"); } B(int n) { super(n); System.out.print("B"); } } class C extends B { C() { System.out.print("C"); } } public class Main { public static void main(String[] args) { new C(); System.out.println(); } }問 3次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class A { String name() { return "A"; } } class B extends A { String name() { return "B" + super.name(); } } class C extends B { String name() { return "C" + super.name(); } } public class Main { public static void main(String[] args) { A a = new C(); System.out.println(a.name()); } }問 4次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 abstract class Shape { abstract double area(); } public class Main { public static void main(String[] args) { Shape[] shapes = new Shape[3]; // (A) System.out.print(shapes.length); // (B) Shape s = new Shape(); // (C) System.out.print(s.area()); } }問 5次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 abstract class Animal { abstract String sound(); abstract String name(); } abstract class Pet extends Animal { // (A) String name() { return "pet"; } } class Dog extends Pet { // (B) String name() { return "dog"; } } public class Main { public static void main(String[] args) { Animal a = new Dog(); // (C) System.out.println(a.name()); } }問 6次の
コードで、 コンパイルエラーに なる 行を すべて 挙げた ものは どれですか。 class Box { Number value() { return 1; } long count() { return 1L; } protected void open() { } } class SmallBox extends Box { Integer value() { return 2; } // (A) int count() { return 2; } // (B) public void open() { } // (C) } public class Main { public static void main(String[] args) { Box b = new SmallBox(); System.out.println(b.value()); } }問 7スーパークラスに
void load() throws IOException と いう メソッドが あります。 サブクラスで この メソッドを オーバーライドする 宣言と して、 コンパイルできる ものは どれですか。 問 8次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { private final int id; Main(int id) { this.id = id; } @Override // (A) public boolean equals(Main other) { // (B) return other != null && id == other.id; } public static void main(String[] args) { Object a = new Main(1); Object b = new Main(1); System.out.println(a.equals(b)); // (C) } }問 9次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Parent { String tag = "p"; static String kind() { return "P"; } String show() { return tag + kind(); } } class Child extends Parent { String tag = "c"; static String kind() { return "C"; } } public class Main { public static void main(String[] args) { Child c = new Child(); System.out.println(c.tag + c.kind() + c.show()); } }問 10次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Base { Base() { print(); } void print() { System.out.print("Base/"); } } class Derived extends Base { private String msg = "hello"; Derived() { print(); } @Override void print() { System.out.print(msg + "/"); } } public class Main { public static void main(String[] args) { new Derived(); System.out.println(); } }問 11次の
コードを コンパイルした ときの 結果と して 正しい ものは どれですか。 interface Printable { } class Paper { } final class Stone { } public class Main { public static void main(String[] args) { Paper paper = new Paper(); Stone stone = new Stone(); Object o = stone; Printable a = (Printable) paper; // (A) Printable b = (Printable) o; // (B) Printable c = (Printable) stone; // (C) String d = (String) o; // (D) } }問 12次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Account { final String close() { return "closed"; } } class Savings extends Account { String close(int code) { return "saved" + code; } // (A) } public class Main { public static void main(String[] args) { Savings s = new Savings(); System.out.println(s.close() + " " + s.close(1)); } }インタフェースと
シール・ クラス 問 13インタフェースの
中に 書いた フィールドに ついて 正しい 説明は どれですか。 問 14次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 interface Task { void run(); } class Base { void run() { System.out.print("base"); } } class Job extends Base implements Task { } // (A) public class Main { public static void main(String[] args) { Task t = new Job(); // (B) t.run(); } }問 15次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 interface Named { String name(); } interface Aged { int age(); } interface Person extends Named, Aged { default String label() { return name() + age(); } } class Student implements Person { public String name() { return "cat"; } public int age() { return 3; } } public class Main { public static void main(String[] args) { Named n = new Student(); Aged a = (Aged) n; System.out.println(((Person) a).label()); } }問 16次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 interface Left { default String side() { return "L"; } } interface Right { default String side() { return "R"; } } class Both implements Left, Right { public String side() { return Right.super.side() + Left.super.side(); } } public class Main { public static void main(String[] args) { System.out.println(new Both().side()); } }問 17次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 interface Greeter { default String greet() { return "I"; } } interface Polite extends Greeter { default String greet() { return "P"; } } class Base { public String greet() { return "B"; } } class A implements Greeter, Polite { } class B extends Base implements Polite { } public class Main { public static void main(String[] args) { System.out.println(new A().greet() + new B().greet()); } }問 18次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 interface Formatter { static String twice(String s) { return bracket(s) + bracket(s); // (A) } default String wrap(String s) { return bracket(s) + count(); // (B) } private String bracket(String s) { // (C) return "[" + s + "]"; } private static int count() { return 1; } } public class Main implements Formatter { public static void main(String[] args) { System.out.println(new Main().wrap("x")); } }問 19インタフェースの
デフォルト・ メソッドと して 宣言した とき、 コンパイルエラーに なる ものは どれですか。 問 20抽象クラスでは
できるが、 インタフェースでは できない ことは どれですか。 問 21sealed を
付けた クラスで permits を 省いた とき、 許可される サブクラスに ついて 正しい 説明は どれですか。 問 22sealed クラス Shape が
permits Square で Square を 許可しています。 Square の 宣言の うち、 コンパイルエラーに なる ものは どれですか。 問 23次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 sealed abstract class Pay permits Cash, Card { } // (A) final class Cash extends Pay { } // (B) final class Card { } // (C) public class Main { public static void main(String[] args) { Pay p = new Cash(); System.out.println(p instanceof Cash); } }問 24次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 sealed interface Animal permits Dog, Wild { default String sound() { return "?"; } } final class Dog implements Animal { public String sound() { return "wan"; } } non-sealed interface Wild extends Animal { } class Wolf implements Wild { } public class Main { public static void main(String[] args) { Animal[] list = { new Dog(), new Wolf() }; for (Animal a : list) { System.out.print(a.sound()); } System.out.println(); } }
24 問中 0 問に答えました
解説の一覧
解いた
継承、 抽象クラス、 オーバーライド、キャスト
問1Java の クラスの 継承に ついて 正しい 説明は どれですか。
正解は
問2次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class A {
A() { System.out.print("a"); }
A(int n) { System.out.print("A" + n); }
}
class B extends A {
B() { this(2); System.out.print("b"); }
B(int n) { super(n); System.out.print("B"); }
}
class C extends B {
C() { System.out.print("C"); }
}
public class Main {
public static void main(String[] args) {
new C();
System.out.println();
}
}正解は
問3次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class A {
String name() { return "A"; }
}
class B extends A {
String name() { return "B" + super.name(); }
}
class C extends B {
String name() { return "C" + super.name(); }
}
public class Main {
public static void main(String[] args) {
A a = new C();
System.out.println(a.name());
}
}正解は
問4次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
abstract class Shape {
abstract double area();
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = new Shape[3]; // (A)
System.out.print(shapes.length); // (B)
Shape s = new Shape(); // (C)
System.out.print(s.area());
}
}正解は
問5次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
abstract class Animal {
abstract String sound();
abstract String name();
}
abstract class Pet extends Animal { // (A)
String name() { return "pet"; }
}
class Dog extends Pet { // (B)
String name() { return "dog"; }
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog(); // (C)
System.out.println(a.name());
}
}正解は
問6次の コードで、 コンパイルエラーに なる 行を すべて 挙げた ものは どれですか。
class Box {
Number value() { return 1; }
long count() { return 1L; }
protected void open() { }
}
class SmallBox extends Box {
Integer value() { return 2; } // (A)
int count() { return 2; } // (B)
public void open() { } // (C)
}
public class Main {
public static void main(String[] args) {
Box b = new SmallBox();
System.out.println(b.value());
}
}正解は
問7スーパークラスに void load() throws IOException という メソッドが あります。 サブクラスで この メソッドを オーバーライドする 宣言として、 コンパイルできる ものは どれですか。
正解は
問8次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
private final int id;
Main(int id) { this.id = id; }
@Override // (A)
public boolean equals(Main other) { // (B)
return other != null && id == other.id;
}
public static void main(String[] args) {
Object a = new Main(1);
Object b = new Main(1);
System.out.println(a.equals(b)); // (C)
}
}正解は
問9次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Parent {
String tag = "p";
static String kind() { return "P"; }
String show() { return tag + kind(); }
}
class Child extends Parent {
String tag = "c";
static String kind() { return "C"; }
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
System.out.println(c.tag + c.kind() + c.show());
}
}正解は
問10次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Base {
Base() { print(); }
void print() { System.out.print("Base/"); }
}
class Derived extends Base {
private String msg = "hello";
Derived() { print(); }
@Override
void print() { System.out.print(msg + "/"); }
}
public class Main {
public static void main(String[] args) {
new Derived();
System.out.println();
}
}正解は
問11次の コードを コンパイルした ときの 結果として 正しい ものは どれですか。
interface Printable { }
class Paper { }
final class Stone { }
public class Main {
public static void main(String[] args) {
Paper paper = new Paper();
Stone stone = new Stone();
Object o = stone;
Printable a = (Printable) paper; // (A)
Printable b = (Printable) o; // (B)
Printable c = (Printable) stone; // (C)
String d = (String) o; // (D)
}
}正解は
問12次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Account {
final String close() { return "closed"; }
}
class Savings extends Account {
String close(int code) { return "saved" + code; } // (A)
}
public class Main {
public static void main(String[] args) {
Savings s = new Savings();
System.out.println(s.close() + " " + s.close(1));
}
}正解は
インタフェースと シール・クラス
問13インタフェースの 中に 書いた フィールドに ついて 正しい 説明は どれですか。
正解は
問14次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
interface Task {
void run();
}
class Base {
void run() { System.out.print("base"); }
}
class Job extends Base implements Task { } // (A)
public class Main {
public static void main(String[] args) {
Task t = new Job(); // (B)
t.run();
}
}正解は
問15次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
interface Named { String name(); }
interface Aged { int age(); }
interface Person extends Named, Aged {
default String label() { return name() + age(); }
}
class Student implements Person {
public String name() { return "cat"; }
public int age() { return 3; }
}
public class Main {
public static void main(String[] args) {
Named n = new Student();
Aged a = (Aged) n;
System.out.println(((Person) a).label());
}
}正解は
問16次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
interface Left {
default String side() { return "L"; }
}
interface Right {
default String side() { return "R"; }
}
class Both implements Left, Right {
public String side() {
return Right.super.side() + Left.super.side();
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Both().side());
}
}正解は
問17次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
interface Greeter {
default String greet() { return "I"; }
}
interface Polite extends Greeter {
default String greet() { return "P"; }
}
class Base {
public String greet() { return "B"; }
}
class A implements Greeter, Polite { }
class B extends Base implements Polite { }
public class Main {
public static void main(String[] args) {
System.out.println(new A().greet() + new B().greet());
}
}正解は
問18次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
interface Formatter {
static String twice(String s) {
return bracket(s) + bracket(s); // (A)
}
default String wrap(String s) {
return bracket(s) + count(); // (B)
}
private String bracket(String s) { // (C)
return "[" + s + "]";
}
private static int count() {
return 1;
}
}
public class Main implements Formatter {
public static void main(String[] args) {
System.out.println(new Main().wrap("x"));
}
}正解は
問19インタフェースの デフォルト・メソッドとして 宣言した とき、 コンパイルエラーに なる ものは どれですか。
正解は
問20抽象クラスでは できるが、 インタフェースでは できない ことは どれですか。
正解は
問21sealed を 付けた クラスで permits を 省いた とき、 許可される サブクラスに ついて 正しい 説明は どれですか。
正解は
問22sealed クラス Shape が permits Square で Square を 許可しています。 Square の 宣言の うち、 コンパイルエラーに なる ものは どれですか。
正解は
問23次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
sealed abstract class Pay permits Cash, Card { } // (A)
final class Cash extends Pay { } // (B)
final class Card { } // (C)
public class Main {
public static void main(String[] args) {
Pay p = new Cash();
System.out.println(p instanceof Cash);
}
}正解は
問24次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
sealed interface Animal permits Dog, Wild {
default String sound() { return "?"; }
}
final class Dog implements Animal {
public String sound() { return "wan"; }
}
non-sealed interface Wild extends Animal { }
class Wolf implements Wild { }
public class Main {
public static void main(String[] args) {
Animal[] list = { new Dog(), new Wolf() };
for (Animal a : list) {
System.out.print(a.sound());
}
System.out.println();
}
}正解は
ほかの分野
Java の
概要と 簡単な Java プログラムの作成 main メソッド、
コンパイルと 実行、 パッケージ Javaの
基本 データ型と 文字列の操作 変数と
スコープと var と 文字列、 配列と ArrayList 演算子と
制御構造 演算子と
分岐と switch、 繰り返しと break と continue クラスの
定義と インスタンスの使用 クラス、
メソッド、 コンストラクタ、 オーバーロード、 static、 アクセス修飾子、 パターン・マッチング、レコード 例外処理
例外の
種類と try-catch、 try-with-resources、 カスタム例外、 multi-catch
Java Silverの頁に、
続きは ゆめさくで
試験の