The read() method returns the ASCII value of the first character, but when assigned to a char and printed with Serial.println(char), it prints the character itself.
Step 2: Check code carefully
Since c is a char and Serial.println(c) prints the character, the output is 'H'.
Final Answer:
H -> Option A
Quick Check:
file.read() returns char 'H' printed as H [OK]
Hint: file.read() returns one char; Serial.println prints char [OK]
Common Mistakes:
Thinking read() returns whole string
Expecting ASCII number printed
Confusing file.read() with readStringUntil()
4. Identify the error in this Arduino code snippet that reads a line from an SD card file:
File file = SD.open("log.txt", FILE_READ);
if (file) {
String line = file.readStringUntil('\n');
Serial.println(line);
}
file.close();
medium
A. readStringUntil() is not a valid File method
B. file.close() is called outside the if block
C. Missing SD.begin() before opening file
D. Using single quotes instead of double quotes in readStringUntil
Solution
Step 1: Check file.close() placement
file.close() should be called only if file opened successfully, so it must be inside the if block.
Step 2: Understand consequences
If file failed to open, calling file.close() causes error because file is invalid.
Final Answer:
file.close() is called outside the if block -> Option B
Quick Check:
Close file only if opened [OK]
Hint: Call file.close() only inside if (file) block [OK]
Common Mistakes:
Calling file.close() unconditionally
Assuming readStringUntil() is invalid
Forgetting SD.begin() but not shown here
Confusing single vs double quotes in char argument
5. You want to read all lines from a file "config.txt" on an SD card and print each line to Serial. Which code snippet correctly does this?
hard
A. File file = SD.open("config.txt", FILE_READ);
while (file) {
Serial.println(file.readStringUntil('\n'));
}
file.close();
B. File file = SD.open("config.txt", FILE_READ);
while (!file.available()) {
Serial.println(file.readStringUntil('\n'));
}
file.close();
C. File file = SD.open("config.txt", FILE_READ);
while (file.read()) {
Serial.println(file.readStringUntil('\n'));
}
file.close();
D. File file = SD.open("config.txt", FILE_READ);
while (file.available()) {
Serial.println(file.readStringUntil('\n'));
}
file.close();
Solution
Step 1: Use file.available() to check for data
file.available() returns true if there is data left to read, so loop while available() is true.
Step 2: Read each line with readStringUntil('\n')
This method reads until newline, perfect for line-by-line reading.
Step 3: Close file after reading
Call file.close() after finishing reading to free resources.
Final Answer:
Use while (file.available()) with readStringUntil('\n') -> Option D
Quick Check:
Loop with file.available() to read lines [OK]
Hint: Loop while file.available() to read all lines [OK]
Common Mistakes:
Using while (!file.available()) which loops incorrectly
Using while (file.read()) which reads bytes, not lines
Using while (file) which doesn't check data availability
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.