Java Silverに関する問題を出題していきます!
問題1
次のプログラムをコンパイル、実行した結果として正しいものはどれか。
public class Sample { public static void main(String[] args) { SubSample obj = new SubSample(); int sum = obj.x + obj.y; System.out.println(sum); } } class SuperSample { int x = 10; } class SubSample extends SuperSample { int y = 20; }
- 「20」と表示
- 「30」と表示
- 何も表示されない
- コンパイルエラー
- 実行時エラー
問題2
次のプログラムをコンパイル、実行した結果として正しいものはどれか。
public class Sample { public static void main(String[] args) { SubSample obj = new SubSample(); System.out.println(obj.str); } } class SuperSample { String str = "スーパー"; } class SubSample extends SuperSample { String str = "サブ"; }
- 「スーパー」と表示
- 「サブ」と表示
- 何も表示されない
- コンパイルエラー
- 実行時エラー
問題3
次のプログラムをコンパイル、実行した結果として正しいものはどれか。
public class Sample { public static void main(String[] args) { SubSample obj = new SubSample(); int sum = obj.x + obj.y; System.out.println(sum); } } class SuperSample { int x = 10; } class SubSample extends SuperSample { int y = 20; x = 30; }
- 「30」と表示
- 「50」と表示
- 何も表示されない
- コンパイルエラー
- 実行時エラー
問題4
次のプログラムをコンパイル、実行した結果として正しいものはどれか。
public class Sample { public static void main(String[] args) { SubSample obj = new SubSample(); System.out.println(obj.a); } } class SuperSample { int a = 10; } class SubSample extends SuperSample { static int a = 20; }
- 「10」と表示
- 「20」と表示
- 何も表示されない
- コンパイルエラー
- 実行時エラー
問題5
次のプログラムをコンパイル、実行した結果として正しいものはどれか。
public class Sample { public static void main(String[] args) { System.out.println(SubSample.a); } } class SuperSample { static int a = 10; } class SubSample extends SuperSample { static int b = 20; }
- 「10」と表示
- 「20」と表示
- 何も表示されない
- コンパイルエラー
- 実行時エラー