【Java SE17 Silver 模擬試験】B-44

Java SE17 Silver 模擬試験
Java SE17 Silver 模擬試験

問題

次のコード断片と等しい処理はどれですか。(1つ選択)

int rgb = 0xFF0000;
String color = "";
switch (rgb) {
	case 0x0000FF:
		color = "BULE";
		break;
	case 0x008000:
		color = "GREEN";
		break;
	case 0xFF0000:
		color = "red";
		break;
	case 0xFFFF00:
		color = "YELLOW";
		break;
	case 0xFFFFFF:
		color = "WHITE";
		break;
	default:
		color = "UNKNOWN";
}

選択肢

A)
String color = "UNKNOWN";
switch (rgb) {
	case 0x0000FF:
		yield "BULE";
	case 0x008000:
		yield "GREEN";
	case 0xFF0000:
		yield "RED";
	case 0xFFFF00:
		yield "YELLOW";
	case 0xFFFFFF:
		yield "WHITE";
};
B)
String color = switch (rgb) {
	case 0x0000FF:
		yield "BULE";
	case 0x008000:
		yield "GREEN";
	case 0xFF0000:
		yield "RED";
	case 0xFFFF00:
		yield "YELLOW";
	case 0xFFFFFF:
		yield "WHITE";
	default := "UNKNOWN";
};
C)
String color = switch (rgb) {
	case 0x0000FF -> "BULE";
	case 0x008000 -> "GREEN";
	case 0xFF0000 -> "RED";
	case 0xFFFF00 -> "YELLOW";
	case 0xFFFFFF -> "WHITE";
	default:yield "UNKNOWN";
};
D)
String color = switch (rgb) {
	case 0x0000FF -> "BULE"; break;
	case 0x008000 -> "GREEN"; break;
	case 0xFF0000 -> "RED"; break;
	case 0xFFFF00 -> "YELLOW"; break;
	case 0xFFFFFF -> "WHITE"; break;
	default -> "UNKNOWN"; break;
};
E)
String color = switch (rgb) {
	case 0x0000FF -> "BULE";
	case 0x008000 -> "GREEN";
	case 0xFF0000 -> "RED";
	case 0xFFFF00 -> "YELLOW";
	case 0xFFFFFF -> "WHITE";
	default -> "UNKNOWN";
};
F)
String color = "UNKNOWN";
switch (rgb) {
	case 0x0000FF -> "BULE";
	case 0x008000 -> "GREEN";
	case 0xFF0000 -> "RED";
	case 0xFFFF00 -> "YELLOW";
	case 0xFFFFFF -> "WHITE";
};
G)
String color = switch (rgb) {
	case 0x0000FF: clor = "BULE";
	case 0x008000: clor = "GREEN";
	case 0xFF0000: clor = "RED";
	case 0xFFFF00: clor = "YELLOW";
	case 0xFFFFFF: clor = "WHITE";
	default: clor = "UNKNOWN";
};

解答

A)
String color = "UNKNOWN";
switch (rgb) {
case 0x0000FF:
yield "BULE";
case 0x008000:
yield "GREEN";
case 0xFF0000:
yield "RED";
case 0xFFFF00:
yield "YELLOW";
case 0xFFFFFF:
yield "WHITE";
};
B)
String color = switch (rgb) {
	case 0x0000FF:
		yield "BULE";
	case 0x008000:
		yield "GREEN";
	case 0xFF0000:
		yield "RED";
	case 0xFFFF00:
		yield "YELLOW";
	case 0xFFFFFF:
		yield "WHITE";
	default := "UNKNOWN";
};
C)
String color = switch (rgb) {
	case 0x0000FF -> "BULE";
	case 0x008000 -> "GREEN";
	case 0xFF0000 -> "RED";
	case 0xFFFF00 -> "YELLOW";
	case 0xFFFFFF -> "WHITE";
	default:yield "UNKNOWN";
};
D)
String color = switch (rgb) {
	case 0x0000FF -> "BULE"; break;
	case 0x008000 -> "GREEN"; break;
	case 0xFF0000 -> "RED"; break;
	case 0xFFFF00 -> "YELLOW"; break;
	case 0xFFFFFF -> "WHITE"; break;
	default -> "UNKNOWN"; break;
};
E)
String color = switch (rgb) {
	case 0x0000FF -> "BULE";
	case 0x008000 -> "GREEN";
	case 0xFF0000 -> "RED";
	case 0xFFFF00 -> "YELLOW";
	case 0xFFFFFF -> "WHITE";
	default -> "UNKNOWN";
};
F)
String color = "UNKNOWN";
switch (rgb) {
	case 0x0000FF -> "BULE";
	case 0x008000 -> "GREEN";
	case 0xFF0000 -> "RED";
	case 0xFFFF00 -> "YELLOW";
	case 0xFFFFFF -> "WHITE";
};
G)
String color = switch (rgb) {
case 0x0000FF: clor = "BULE";
case 0x008000: clor = "GREEN";
case 0xFF0000: clor = "RED";
case 0xFFFF00: clor = "YELLOW";
case 0xFFFFFF: clor = "WHITE";
default: clor = "UNKNOWN";
};

前の問題へ SE17_B-43

次の問題へ SE17_B-45

対象資格:Java SE17 認定資格