Consider this Unity C# code snippet that checks if a save slot is empty or not. What will be printed to the console?
string slotKey = "SaveSlot1"; if (PlayerPrefs.HasKey(slotKey)) { Debug.Log("Slot occupied"); } else { Debug.Log("Slot empty"); }
Think about what PlayerPrefs.HasKey returns if the key does not exist.
Since the key "SaveSlot1" is not set, PlayerPrefs.HasKey returns false, so "Slot empty" is printed.
This code saves a player's score in slot 2 and then reads it back. What will be printed?
int slot = 2; string key = $"SaveSlot{slot}_Score"; PlayerPrefs.SetInt(key, 150); int score = PlayerPrefs.GetInt(key, 0); Debug.Log(score);
Remember that SetInt stores the value and GetInt retrieves it.
The score 150 is saved with the key "SaveSlot2_Score" and then retrieved, so 150 is printed.
Look at this code that tries to load a player's name from a save slot. It throws a NullReferenceException. Why?
string slotKey = "SaveSlot3_Name"; string playerName = PlayerPrefs.GetString(slotKey, null); int nameLength = playerName.Length; Debug.Log(nameLength);
What happens if you call GetString with a key that does not exist?
PlayerPrefs.GetString(slotKey, null) returns null if the key does not exist, causing a NullReferenceException when accessing .Length.
Unity's PlayerPrefs does not have a direct method for booleans. Which code correctly saves and loads a boolean value?
PlayerPrefs supports int, float, and string but not bool directly.
Option D uses int 1 and 0 to represent true and false, which is the common pattern in Unity. Options B and C use non-existent methods or parsing that can fail. Option D uses float which is less common and can cause precision issues.
This code saves player data for multiple slots. How many distinct save slots are created?
for (int i = 0; i < 3; i++) { PlayerPrefs.SetInt($"Slot{i}_Level", i + 1); PlayerPrefs.SetString($"Slot{i}_Name", $"Player{i}"); } int count = 0; for (int i = 0; i < 5; i++) { if (PlayerPrefs.HasKey($"Slot{i}_Level")) count++; } Debug.Log(count);
Check how many keys are set and how many are checked.
The first loop sets keys for slots 0, 1, and 2 only. The second loop checks slots 0 to 4. Only slots 0, 1, and 2 have keys, so count is 3.