Java Silverに関する問題を出題していきます!
問題1
「30」と表示させるためには①にどのようなコードを記述すればよいか、選択肢の中から全て選べ。
public class Sample { public static void main(String[] args) { Number n = new Number(); int sum = /* ① */; System.out.println(sum); } } class Number { static int a = 10; int b = 20; }
- Number.a + Number.b
- n.a + Number.b
- Number.a + n.b
- n.a + n.b
- 当てはまるものはない
問題2
次のプログラムをコンパイル、実行した結果として正しいものはどれか。
public class Sample { public static void main(String[] args) { int sum = sum(10, 20); System.out.println(sum); } int sum(int x, int y) { return x + y; } }
- 「30」と表示
- 「0」と表示
- 何も表示されない
- コンパイルエラー
- 実行時エラー
問題3
次のプログラムをコンパイル、実行した結果として正しいものはどれか。
public class Sample { public static void main(String[] args) { Sample s = new Sample(); s.show("Hello"); } void show(String str) { System.out.println(str); } }
- 「Hello」と表示
- 何も表示されない
- コンパイルエラー
- 実行時エラー
- 1~4以外
問題4
「ポチ」と表示させるためには①にどのようなコードを記述すればよいか、選択肢の中から選べ。
public class Sample { public static void main(String[] args) { System.out.println(/* ① */); } } class Dog { String name = "ポチ"; }
- name
- Dog.name
- dog.name
- new Dog().name
- 当てはまるものはない
問題5
次のプログラムをコンパイル、実行した結果として正しいものはどれか。
public class Sample { public static void main(String[] args) { Cat cat1 = new Cat(); Cat cat2 = new Cat(); System.out.println(cat1.name == cat2.name); } } class Cat { String name = "タマ"; }
- 「ture」と表示
- 「false」と表示
- 何も表示されない
- コンパイルエラー
- 実行時エラー