42.3k views
2 votes
To check if a string s contains the prefix "Java", you may write A. if (s.startsWith("Java")) ... B. if (s.indexOf("Java") == 0) ... C. if (s.substring(0, 4).equals("Java")) ... D. if (s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2) == 'v' && s.charAt(3) == 'a') ...

User Ashanta
by
4.8k points

2 Answers

3 votes

Answer:

D

Step-by-step explanation:

  • Option D is the correct answer
  • The Option checks each characters from index 0-3 and compares the character against the letters (J,A,V,A).
  • See an implementation of in the code snippet below. The output will be Yes since the condition is true

public class num5 {

public static void main(String[] args) {

//Create a string and give a value starting with Java

String s = "Java is cool";

if(s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2)

== 'v' && s.charAt(3) == 'a'){

System.out.println("Yes");

}

else{

System.out.println("No");

}

}

}

User Mark Fondy
by
4.9k points
5 votes

Answer:

D

Step-by-step explanation:

  • Option D is the correct answer
  • The Option checks each characters from index 0-3 and compares the character against the letters (J,A,V,A).
  • See an implementation of in the code snippet below. The output will be Yes since the condition is true

public class num5 {

public static void main(String[] args) {

//Create a string and give a value starting with Java

String s = "Java is cool";

if(s.charAt(0) == 'J' && s.charAt(1) == 'a' && s.charAt(2)

== 'v' && s.charAt(3) == 'a'){

System.out.println("Yes");

}

else{

System.out.println("No");

}

}

}

User Flamusdiu
by
5.1k points