
ゆめさくが
クラスの
ログインなしで
この 分野の小分野
- クラス、
メソッド、 コンストラクタ、 オーバーロード(11問) - static、
アクセス修飾子、 パターン・マッチング、レコード(11問)
クラスの 定義と インスタンスの 使用の問題
22 問選ぶと、
クラス、
メソッド、 コンストラクタ、 オーバーロード 問 1次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Point { int x; } public class Main { public static void main(String[] args) { Point p = new Point(); Point q = p; q.x = 5; p = new Point(); System.out.println(p.x + " " + q.x); } }問 2次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { double rate; boolean done; Integer count; public static void main(String[] args) { Main m = new Main(); System.out.println(m.rate + " " + m.done + " " + m.count); } }問 3次の
コードの (X) の 時点で、 ガベージ・ コレクションの 対象に なれる StringBuilder の オブジェクトは どれですか。 public class Main { public static void main(String[] args) { StringBuilder a = new StringBuilder("A"); StringBuilder b = new StringBuilder("B"); StringBuilder c = new StringBuilder("C"); a = b; c = a; b = null; // (X) System.out.println(a + " " + c); } }問 4次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static String grade(int score) { if (score >= 80) { return "A"; } else if (score >= 50) { return "B"; } } // (A) public static void main(String[] args) { System.out.println(grade(70)); } }問 5次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static void edit(StringBuilder sb, String s) { sb.append("!"); s = s + "!"; sb = new StringBuilder("new"); sb.append("?"); } public static void main(String[] args) { StringBuilder sb = new StringBuilder("hi"); String s = "hi"; edit(sb, s); System.out.println(sb + " " + s); } }問 6次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static int a(int... n) { return n.length; } // (A) static int b(String s, int... n) { return n.length; } // (B) static int c(int... n, String s) { return n.length; } // (C) public static void main(String[] args) { System.out.println("" + a() + b("x", 7)); } }問 7次の
2 つの クラスは どちらも コンストラクタを 書いていません。 コンパイラが 自動で 足す既定の コンストラクタの アクセス 修飾子の 組み合わせと して 正しい ものは どれですか。 Report と Memo は 同じ パッケージの 別の ファイルに あります。 // Report.java public class Report { } // Memo.java class Memo { }問 8次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Timer { int sec; Timer() { this(60); } Timer(int sec) { this(); this.sec = sec; } } public class Main { public static void main(String[] args) { System.out.println(new Timer().sec); } }問 9次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Gauge { int a = 5; { a += 1; } Gauge() { a *= 10; } { a -= 2; } } public class Main { public static void main(String[] args) { System.out.println(new Gauge().a); } }問 10次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static void show(Long x) { System.out.println("Long"); } static void show(Object x) { System.out.println("Object"); } static void show(int... x) { System.out.println("int..."); } public static void main(String[] args) { show(5); } }問 11次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static void show(int a, long b) { System.out.println("int,long"); } static void show(long a, int b) { System.out.println("long,int"); } public static void main(String[] args) { show(1, 2); // (A) } }static、
アクセス 修飾子、 パターン・ マッチング、 レコード 問 12次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Visit { static int total; Visit() { total++; } } public class Main { public static void main(String[] args) { new Visit(); new Visit(); Visit v = null; System.out.println(v.total); } }問 13次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static final int LIMIT; // (A) static final String NAME = "box"; static { LIMIT = NAME.length(); // (B) } public static void main(String[] args) { System.out.println(LIMIT + " " + NAME); } }問 14次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Config { static int level = 5; static { System.out.print("S"); } } public class Main { public static void main(String[] args) { System.out.print("A"); Config c = null; System.out.print("B"); System.out.print(Config.level); } }問 15Tool と
Main は 同じファイルに あり、 同じ パッケージに 属します。 次の コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 class Tool { public int a = 1; protected int b = 2; int c = 3; private int d = 4; } public class Main { public static void main(String[] args) { Tool t = new Tool(); int sum = 0; sum += t.a; // (A) sum += t.b; // (B) sum += t.c; // (C) sum += t.d; // (D) System.out.println(sum); } }問 16次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 public class Main { static String label(Object o) { if (!(o instanceof Integer n)) { return "other"; } return "num" + (n + 1); } public static void main(String[] args) { System.out.println(label(41) + " " + label("41")); } }問 17次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 record Range(int low, int high) { Range { if (low > high) { int t = low; low = high; high = t; } } } public class Main { public static void main(String[] args) { Range r = new Range(9, 3); System.out.println(r.low() + "-" + r.high()); } }問 18次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 record Book(String title, int pages) { } public class Main { public static void main(String[] args) { Book b = new Book("Java", 300); System.out.println(b.title() + " / " + b); } }問 19次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 record Point(int x, int y) { } public class Main { public static void main(String[] args) { Point a = new Point(1, 2); Point b = new Point(1, 2); System.out.println((a == b) + " " + a.equals(b) + " " + (a.hashCode() == b.hashCode())); } }問 20次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 record Counter(int value) { void reset() { value = 0; // (A) } int next() { return value + 1; // (B) } } public class Main { public static void main(String[] args) { Counter c = new Counter(5); c.reset(); System.out.println(c.next()); } }問 21次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 record Item(String name, int price) { static int created; // (A) static Item free(String n) { return new Item(n, 0); } // (B) int withTax() { return price * 110 / 100; } // (C) int stock; // (D) } public class Main { public static void main(String[] args) { System.out.println(Item.free("pen").withTax()); } }問 22次の
コードを コンパイルして 実行した ときの 結果と して 正しい ものは どれですか。 record Size(int w, int h) { } class Square extends Size { // (A) Square(int s) { super(s, s); } // (B) } public class Main { public static void main(String[] args) { System.out.println(new Square(2).w()); } }
22 問中 0 問に答えました
解説の一覧
解いた
クラス、 メソッド、 コンストラクタ、 オーバーロード
問1次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Point {
int x;
}
public class Main {
public static void main(String[] args) {
Point p = new Point();
Point q = p;
q.x = 5;
p = new Point();
System.out.println(p.x + " " + q.x);
}
}正解は
問2次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
double rate;
boolean done;
Integer count;
public static void main(String[] args) {
Main m = new Main();
System.out.println(m.rate + " " + m.done + " " + m.count);
}
}正解は
問3次の コードの (X) の 時点で、 ガベージ・コレクションの 対象に なれる StringBuilder の オブジェクトは どれですか。
public class Main {
public static void main(String[] args) {
StringBuilder a = new StringBuilder("A");
StringBuilder b = new StringBuilder("B");
StringBuilder c = new StringBuilder("C");
a = b;
c = a;
b = null;
// (X)
System.out.println(a + " " + c);
}
}正解は
問4次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static String grade(int score) {
if (score >= 80) {
return "A";
} else if (score >= 50) {
return "B";
}
} // (A)
public static void main(String[] args) {
System.out.println(grade(70));
}
}正解は
問5次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static void edit(StringBuilder sb, String s) {
sb.append("!");
s = s + "!";
sb = new StringBuilder("new");
sb.append("?");
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("hi");
String s = "hi";
edit(sb, s);
System.out.println(sb + " " + s);
}
}正解は
問6次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static int a(int... n) { return n.length; } // (A)
static int b(String s, int... n) { return n.length; } // (B)
static int c(int... n, String s) { return n.length; } // (C)
public static void main(String[] args) {
System.out.println("" + a() + b("x", 7));
}
}正解は
問7次の 2 つの クラスは どちらも コンストラクタを 書いていません。 コンパイラが 自動で 足す既定の コンストラクタの アクセス修飾子の 組み合わせとして 正しい ものは どれですか。 Report と Memo は 同じパッケージの 別の ファイルに あります。
// Report.java
public class Report {
}
// Memo.java
class Memo {
}正解は
問8次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Timer {
int sec;
Timer() {
this(60);
}
Timer(int sec) {
this();
this.sec = sec;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Timer().sec);
}
}正解は
問9次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Gauge {
int a = 5;
{
a += 1;
}
Gauge() {
a *= 10;
}
{
a -= 2;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Gauge().a);
}
}正解は
問10次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static void show(Long x) { System.out.println("Long"); }
static void show(Object x) { System.out.println("Object"); }
static void show(int... x) { System.out.println("int..."); }
public static void main(String[] args) {
show(5);
}
}正解は
問11次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static void show(int a, long b) { System.out.println("int,long"); }
static void show(long a, int b) { System.out.println("long,int"); }
public static void main(String[] args) {
show(1, 2); // (A)
}
}正解は
static、 アクセス修飾子、 パターン・マッチング、レコード
問12次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Visit {
static int total;
Visit() {
total++;
}
}
public class Main {
public static void main(String[] args) {
new Visit();
new Visit();
Visit v = null;
System.out.println(v.total);
}
}正解は
問13次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static final int LIMIT; // (A)
static final String NAME = "box";
static {
LIMIT = NAME.length(); // (B)
}
public static void main(String[] args) {
System.out.println(LIMIT + " " + NAME);
}
}正解は
問14次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Config {
static int level = 5;
static {
System.out.print("S");
}
}
public class Main {
public static void main(String[] args) {
System.out.print("A");
Config c = null;
System.out.print("B");
System.out.print(Config.level);
}
}正解は
問15Tool と Main は 同じファイルに あり、 同じ パッケージに 属します。 次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
class Tool {
public int a = 1;
protected int b = 2;
int c = 3;
private int d = 4;
}
public class Main {
public static void main(String[] args) {
Tool t = new Tool();
int sum = 0;
sum += t.a; // (A)
sum += t.b; // (B)
sum += t.c; // (C)
sum += t.d; // (D)
System.out.println(sum);
}
}正解は
問16次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
public class Main {
static String label(Object o) {
if (!(o instanceof Integer n)) {
return "other";
}
return "num" + (n + 1);
}
public static void main(String[] args) {
System.out.println(label(41) + " " + label("41"));
}
}正解は
問17次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
record Range(int low, int high) {
Range {
if (low > high) {
int t = low;
low = high;
high = t;
}
}
}
public class Main {
public static void main(String[] args) {
Range r = new Range(9, 3);
System.out.println(r.low() + "-" + r.high());
}
}正解は
問18次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
record Book(String title, int pages) { }
public class Main {
public static void main(String[] args) {
Book b = new Book("Java", 300);
System.out.println(b.title() + " / " + b);
}
}正解は
問19次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
record Point(int x, int y) { }
public class Main {
public static void main(String[] args) {
Point a = new Point(1, 2);
Point b = new Point(1, 2);
System.out.println((a == b) + " " + a.equals(b) + " " + (a.hashCode() == b.hashCode()));
}
}正解は
問20次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
record Counter(int value) {
void reset() {
value = 0; // (A)
}
int next() {
return value + 1; // (B)
}
}
public class Main {
public static void main(String[] args) {
Counter c = new Counter(5);
c.reset();
System.out.println(c.next());
}
}正解は
問21次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
record Item(String name, int price) {
static int created; // (A)
static Item free(String n) { return new Item(n, 0); } // (B)
int withTax() { return price * 110 / 100; } // (C)
int stock; // (D)
}
public class Main {
public static void main(String[] args) {
System.out.println(Item.free("pen").withTax());
}
}正解は
問22次の コードを コンパイルして 実行した ときの 結果として 正しい ものは どれですか。
record Size(int w, int h) { }
class Square extends Size { // (A)
Square(int s) { super(s, s); } // (B)
}
public class Main {
public static void main(String[] args) {
System.out.println(new Square(2).w());
}
}正解は
ほかの分野
Java の
概要と 簡単な Java プログラムの作成 main メソッド、
コンパイルと 実行、 パッケージ Javaの
基本 データ型と 文字列の操作 変数と
スコープと var と 文字列、 配列と ArrayList 演算子と
制御構造 演算子と
分岐と switch、 繰り返しと break と continue 継承と
インタフェースの使用 継承、
抽象クラス、 オーバーライド、 キャスト、 インタフェースと シール・クラス 例外処理
例外の
種類と try-catch、 try-with-resources、 カスタム例外、 multi-catch
Java Silverの頁に、
続きは ゆめさくで
試験の