| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice Test
Share
easy Solved by 10 students
Conditional Array Element Assignment
< Prev
Next >

Which of the following lines correctly replaces the missing line to safely assign “Welcome” to messages[0] only if it is currently null?

public class InitializeObjectArray {
    public static void main(String[] args) {
        String[] messages = new String[3];
        // Missing line: Safely assign "Welcome" to the first element if it is null.
        if(messages[0] == null){
            messages[0] = "Welcome";
        }
        System.out.println(messages[0]);
    }
}
A

if(messages[0] != null){ messages[0] = “Welcome”; }

B

messages[0].equals(“Welcome”);

C

if(messages[0] == null){ messages[0] = “Welcome”; }

D

messages[0] = new String(“Welcome”);

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1b.bao.o22 0m 00s 200
#2bommasam00033 2m 07s 173
#3singhris00011 0m 15s 85
#4sathilak00011 0m 17s 83
#5suhanakochhar00611 0m 39s 61
#6geethasailaja13 0m 52s 28
#7sailajavadlamani3312 2m 10s -40
#8y.seong202711 19m 36s -1,076
APFIVE © 2020.
Email: [email protected]|Privacy Policy