0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Java研修】新入社員向けコーディング問題① 変数・データ型・演算子 編

0
Posted at

はじめに

4月から始まるJava研修に向けた コーディング問題集 です。

この記事では 変数・データ型・演算子 の基礎を、実際にコードを書いて身につけます。

難易度の見方

マーク 難易度 目安
基本 研修1週目レベル
⭐⭐ 応用 少し考える必要あり
⭐⭐⭐ チャレンジ 複数の知識を組み合わせる

まずは自分で考えてから、模範解答を見てください!


問題1:変数の宣言と出力 ⭐

問題

以下の変数を宣言し、値を代入して出力してください。

  • 名前(String型):"田中太郎"
  • 年齢(int型):23
  • 身長(double型):170.5
  • Javaが好きか(boolean型):true

期待する出力

名前: 田中太郎
年齢: 23
身長: 170.5cm
Javaが好き: true
模範解答
public class Exercise01 {
    public static void main(String[] args) {
        String name = "田中太郎";
        int age = 23;
        double height = 170.5;
        boolean likeJava = true;

        System.out.println("名前: " + name);
        System.out.println("年齢: " + age);
        System.out.println("身長: " + height + "cm");
        System.out.println("Javaが好き: " + likeJava);
    }
}

ポイント: 各データ型に適した型を使い分けましょう。文字列はString、整数はint、小数はdouble、真偽値はbooleanです。


問題2:型変換(キャスト)⭐

問題

以下の処理を行うプログラムを作成してください。

  1. int型の変数a10 を代入
  2. int型の変数b3 を代入
  3. a ÷ b の結果を int型で出力(整数除算)
  4. a ÷ b の結果を double型で出力(小数あり)

期待する出力

整数の除算: 3
小数の除算: 3.3333333333333335

ヒント

  • int同士の除算は小数点以下が切り捨てられます
  • doubleで結果を得るには、どちらかをdoubleにキャストする必要があります
模範解答
public class Exercise02 {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;

        // 整数の除算(小数点以下切り捨て)
        int intResult = a / b;
        System.out.println("整数の除算: " + intResult);

        // 小数の除算(キャスト)
        double doubleResult = (double) a / b;
        System.out.println("小数の除算: " + doubleResult);
    }
}

ポイント: (double) a / b とキャストすると、adoubleに変換されてから除算が行われるため、小数の結果が得られます。


問題3:算術演算子 ⭐

問題

変数 x = 17y = 5 を使って、以下の演算結果を出力してください。

  • 加算(+)
  • 減算(-)
  • 乗算(*)
  • 除算(/)
  • 剰余(%)

期待する出力

17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 % 5 = 2
模範解答
public class Exercise03 {
    public static void main(String[] args) {
        int x = 17;
        int y = 5;

        System.out.println(x + " + " + y + " = " + (x + y));
        System.out.println(x + " - " + y + " = " + (x - y));
        System.out.println(x + " * " + y + " = " + (x * y));
        System.out.println(x + " / " + y + " = " + (x / y));
        System.out.println(x + " % " + y + " = " + (x % y));
    }
}

ポイント: + は文字列結合にも使われるため、計算部分は () で囲みましょう。(x + y) としないと文字列結合として扱われます。


問題4:定数(final)⭐

問題

消費税率を定数(final)として定義し、税込価格を計算するプログラムを作成してください。

  • 商品名:"ノートPC"
  • 税抜価格:98000(int型)
  • 消費税率:0.10(double型、finalで定義)

期待する出力

商品名: ノートPC
税抜価格: 98000円
消費税: 9800円
税込価格: 107800円

ヒント

  • finalをつけた変数は再代入できません
  • 消費税は (int)(price * TAX_RATE) で整数に変換できます
模範解答
public class Exercise04 {
    public static void main(String[] args) {
        final double TAX_RATE = 0.10;

        String productName = "ノートPC";
        int price = 98000;
        int tax = (int) (price * TAX_RATE);
        int totalPrice = price + tax;

        System.out.println("商品名: " + productName);
        System.out.println("税抜価格: " + price + "円");
        System.out.println("消費税: " + tax + "円");
        System.out.println("税込価格: " + totalPrice + "円");
    }
}

ポイント: 定数名は慣習として 大文字のスネークケースTAX_RATE)で書きます。finalをつけると値の変更を防げるため、固定値には積極的に使いましょう。


問題5:インクリメント・デクリメント ⭐⭐

問題

以下のコードの出力結果を 実行せずに 予想してください。予想した後にコードを実行して答え合わせしましょう。

public class Exercise05 {
    public static void main(String[] args) {
        int a = 5;
        int b = a++;
        int c = ++a;

        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);

        int x = 10;
        System.out.println("x = " + x++);
        System.out.println("x = " + x);
    }
}
解答と解説
a = 7
b = 5
c = 7
x = 10
x = 11

解説:

動作
a++(後置) 先に値を使ってから 1を加算。bに5が入った後、aが6になる
++a(前置) 先に1を加算してから 値を使う。aが7になった後、cに7が入る
x++(後置) printlnで10が出力された後、xが11になる

ポイント: 前置(++a)と後置(a++)の違いは研修でよく出題されます。代入や出力と組み合わさったとき に違いが現れます。


問題6:複合代入演算子 ⭐⭐

問題

変数 score = 100 に対して、以下の操作を 順番に 行い、各操作後の値を出力してください。

  1. 20を加算(+=
  2. 30を減算(-=
  3. 2を乗算(*=
  4. 3で除算(/=
  5. 7で割った余り(%=

期待する出力

初期値: 100
+= 20 → 120
-= 30 → 90
*= 2 → 180
/= 3 → 60
%= 7 → 4
模範解答
public class Exercise06 {
    public static void main(String[] args) {
        int score = 100;
        System.out.println("初期値: " + score);

        score += 20;
        System.out.println("+= 20 → " + score);

        score -= 30;
        System.out.println("-= 30 → " + score);

        score *= 2;
        System.out.println("*= 2 → " + score);

        score /= 3;
        System.out.println("/= 3 → " + score);

        score %= 7;
        System.out.println("%= 7 → " + score);
    }
}

ポイント: score += 20score = score + 20 と同じ意味です。コードが簡潔になるため、実務でも頻繁に使います。


問題7:データ型の範囲 ⭐⭐

問題

以下のデータ型の最大値と最小値を出力するプログラムを作成してください。

  • byte
  • short
  • int
  • long

ヒント

  • 各型のラッパークラスに MAX_VALUEMIN_VALUE という定数があります
  • 例:Integer.MAX_VALUE

期待する出力(例)

byte:  -128 〜 127
short: -32768 〜 32767
int:   -2147483648 〜 2147483647
long:  -9223372036854775808 〜 9223372036854775807
模範解答
public class Exercise07 {
    public static void main(String[] args) {
        System.out.println("byte:  " + Byte.MIN_VALUE + " 〜 " + Byte.MAX_VALUE);
        System.out.println("short: " + Short.MIN_VALUE + " 〜 " + Short.MAX_VALUE);
        System.out.println("int:   " + Integer.MIN_VALUE + " 〜 " + Integer.MAX_VALUE);
        System.out.println("long:  " + Long.MIN_VALUE + " 〜 " + Long.MAX_VALUE);
    }
}

ポイント: Javaの整数型はすべて 符号付き です。各型のサイズ(byte=8bit, short=16bit, int=32bit, long=64bit)によって範囲が決まります。実務では基本的にintを使い、大きな数値が必要な場合にlongを使います。


問題8:BMI計算 ⭐⭐

問題

身長と体重からBMIを計算するプログラムを作成してください。

  • 身長:170.0 cm
  • 体重:65.0 kg
  • BMI = 体重(kg) ÷ (身長(m) × 身長(m))
  • 結果は小数第1位まで表示(String.formatを使用)

期待する出力

身長: 170.0cm
体重: 65.0kg
BMI: 22.5

ヒント

  • 身長をcmからmに変換する必要があります(÷100)
  • String.format("%.1f", value) で小数第1位までの表示ができます
模範解答
public class Exercise08 {
    public static void main(String[] args) {
        double heightCm = 170.0;
        double weightKg = 65.0;

        double heightM = heightCm / 100.0;
        double bmi = weightKg / (heightM * heightM);

        System.out.println("身長: " + heightCm + "cm");
        System.out.println("体重: " + weightKg + "kg");
        System.out.println("BMI: " + String.format("%.1f", bmi));
    }
}

ポイント: String.format("%.1f", value) は書式指定で小数点以下の桁数を制御できます。%.2fなら小数第2位まで表示されます。


問題9:変数のスワップ ⭐⭐

問題

2つの変数 a = 10b = 20 の値を 入れ替えて 出力してください。

期待する出力

入れ替え前: a=10, b=20
入れ替え後: a=20, b=10

ヒント

  • 一時変数(temp)を使う方法が基本です
模範解答
public class Exercise09 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        System.out.println("入れ替え前: a=" + a + ", b=" + b);

        // 一時変数を使ったスワップ
        int temp = a;
        a = b;
        b = temp;

        System.out.println("入れ替え後: a=" + a + ", b=" + b);
    }
}

ポイント: スワップは基本中の基本のアルゴリズムです。一時変数を使わない方法(XOR演算や加減算)もありますが、可読性を考えて一時変数を使うのが実務では推奨されます。


問題10:給与計算 ⭐⭐⭐

問題

以下の条件で月給の手取りを計算するプログラムを作成してください。

  • 基本給:250000
  • 残業手当:時給1500円 × 残業時間20時間
  • 通勤手当:15000
  • 総支給額 = 基本給 + 残業手当 + 通勤手当
  • 健康保険料:総支給額の 5%
  • 厚生年金:総支給額の 9%
  • 所得税:(総支給額 - 健康保険料 - 厚生年金)の 5%
  • 手取り = 総支給額 - 健康保険料 - 厚生年金 - 所得税

すべて int 型で計算してください(小数点以下切り捨て)。

期待する出力

=== 給与明細 ===
基本給:     250000円
残業手当:    30000円
通勤手当:    15000円
---
総支給額:   295000円
---
健康保険料:  14750円
厚生年金:    26550円
所得税:     12685円
---
手取り額:   241015円
模範解答
public class Exercise10 {
    public static void main(String[] args) {
        // 支給項目
        int baseSalary = 250000;
        int overtimeRate = 1500;
        int overtimeHours = 20;
        int overtimePay = overtimeRate * overtimeHours;
        int commutingAllowance = 15000;

        // 総支給額
        int grossPay = baseSalary + overtimePay + commutingAllowance;

        // 控除項目
        int healthInsurance = (int) (grossPay * 0.05);
        int pension = (int) (grossPay * 0.09);
        int incomeTax = (int) ((grossPay - healthInsurance - pension) * 0.05);

        // 手取り
        int netPay = grossPay - healthInsurance - pension - incomeTax;

        // 出力
        System.out.println("=== 給与明細 ===");
        System.out.println("基本給:     " + baseSalary + "円");
        System.out.println("残業手当:    " + overtimePay + "円");
        System.out.println("通勤手当:    " + commutingAllowance + "円");
        System.out.println("---");
        System.out.println("総支給額:   " + grossPay + "円");
        System.out.println("---");
        System.out.println("健康保険料:  " + healthInsurance + "円");
        System.out.println("厚生年金:    " + pension + "円");
        System.out.println("所得税:     " + incomeTax + "円");
        System.out.println("---");
        System.out.println("手取り額:   " + netPay + "円");
    }
}

ポイント: 実務では金額計算にdoubleを使うと丸め誤差が発生するため、int(円単位)やBigDecimalを使います。複数の変数を段階的に計算する流れは、実務のビジネスロジックでも頻出するパターンです。


まとめ

問題 テーマ 難易度
問題1 変数の宣言と出力
問題2 型変換(キャスト)
問題3 算術演算子
問題4 定数(final)
問題5 インクリメント・デクリメント ⭐⭐
問題6 複合代入演算子 ⭐⭐
問題7 データ型の範囲 ⭐⭐
問題8 BMI計算 ⭐⭐
問題9 変数のスワップ ⭐⭐
問題10 給与計算(総合問題) ⭐⭐⭐

次回は 条件分岐(if / switch) のコーディング問題です!


シリーズ一覧:Java研修コーディング問題集

  1. 👉 変数・データ型・演算子 編(本記事)
  2. 条件分岐(if / switch)編
  3. 繰り返し処理(for / while)編
  4. 配列 編
  5. メソッド 編
  6. 文字列操作(String)編
  7. クラスとオブジェクト 編
  8. 継承とインターフェース 編
  9. 例外処理 編
  10. コレクションと Stream API 編

著者: @kotaro_ai_lab
AI駆動開発やテック情報を毎日発信しています。フォローお気軽にどうぞ!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?