Bird
0
0

What will be printed by this code?

medium📝 Predict Output Q5 of 15
Java - Strings and String Handling
What will be printed by this code?
String s = "Java Programming";
System.out.println(s.indexOf("gram"));
A5
B8
C10
D-1
Step-by-Step Solution
Solution:
  1. Step 1: Understand indexOf() method

    indexOf("gram") returns the starting index of substring "gram".
  2. Step 2: Find "gram" in "Java Programming"

    "Programming" starts at index 5, "gram" starts at index 8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    indexOf("gram") = 8 [OK]
Quick Trick: indexOf() returns first index of substring or -1 if missing [OK]
Common Mistakes:
  • Counting from 1 instead of 0
  • Confusing substring start index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes