Python 3エンジニア認定基礎試験-例外処理(例外処理)-

1. 
次のコードの実行結果を選んでください。

try:
x = int("42")
y = int("hello")
print(x + y)
except ValueError as e:
print("Error:", e)

2. 
次のコードの実行結果を選んでください。

try:
d = {"key": "value"}
print(d["missing_key"])
except KeyError:
print("Key not found")

3. 
次のコードの実行結果を選んでください。

try:
result = "text" + 5
except TypeError as e:
print("TypeError occurred:", e)

4. 
次のコードの実行結果として正しいものを選んでください。

try:
result = 10 / 2
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Result:", result)

5. 
次のコードの実行結果を選んでください。

try:
x = 5 / 0
except ZeroDivisionError as e:
print("Error:", e)

6. 
次のコードの実行結果を選んでください。

try:
with open("nonexistent.txt", "r") as f:
content = f.read()
except FileNotFoundError as e:
print("Error:", e)

7. 
次のコードの実行結果を選んでください。

try:
x = int("10.5")
except ValueError:
print("Conversion failed")

8. 
次のコードの実行結果を選んでください。

try:
raise AssertionError("Assertion failed")
except AssertionError as e:
print("Caught assertion error:", e)

9. 
次のコードの実行結果を選んでください。

try:
result = "5" + 5
except TypeError as e:
print("Error type:", type(e))

10. 
次のコードの実行結果を選んでください。

try:
lst = [1, 2, 3]
print(lst[5])
except IndexError:
print("Index out of range")

11. 
次のコードの実行結果として正しいものを選んでください。

try:
open("nonexistent_file.txt", "r")
except FileNotFoundError:
print("File not found")
finally:
print("Execution complete")

12. 
次のコードの実行結果として正しいものを選んでください。

try:
raise KeyError("Key not found")
except KeyError as e:
print(e)

13. 
例外処理におけるtryブロックの役割として正しいものを選んでください。

14. 
次のコードの実行結果として正しいものを選んでください。

try:
x = int("123")
except ValueError:
print("Invalid value")
else:
print("Conversion successful:", x)
finally:
print("End of program")

15. 
次のコードの実行結果として正しいものを選んでください。

try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")

16. 
次のコードの実行結果を選んでください。

try:
raise RuntimeError("Unexpected error")
except RuntimeError as e:
print("Caught runtime error:", e)

17. 
次のコードの実行結果を選んでください。

try:
raise ValueError("Invalid input")
except ValueError as e:
print("Caught exception:", e)

18. 
次のコードで発生する例外の種類として正しいものを選んでください。

x = int("hello")

19. 
次のコードの実行結果として正しいものを選んでください。

try:
x = 10 / 0
except ZeroDivisionError:
print("Division by zero")
except Exception:
print("General exception")

20. 
次のコードの実行結果を選んでください。

try:
x = 10 / 2
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Result:", x)
finally:
print("Execution complete")

コメントを残すにはログインしてください。