Computer Applications
A shopping website offers a special discount if the order ID has the sequence 555 anywhere in it. For example, 158545553031, 198555267140, …. .
Fill in the blanks (a) and (b) in the given Java Method to convert the order ID (a long integer) into a string and check if the sequence 555 is present in it.
void checkOrder(long oid)
{
String str = _______(a)_________;
if(______(b)_______)
{
System.out.println("Special Discount Eligible: " + oid);
}
}
Answer
(a) String str = String.valueOf(oid);
(b) if (str.contains("555"))
Reason
To check if "555" appears in the order ID, we need to convert the long
number into a String
. String.valueOf(oid)
correctly converts a long
to a String
. contains("555")
checks if the substring "555" is present in str.
Related Questions
The output of the statement "CONCENTRATION".indexOf('T') is:
- 9
- 7
- 6
- (-1)
Which of the following returns a value greater than or equal to 0?
Two strings,
city1
andcity2
, are compared usingcity1.compareTo(city2)
, and the result is less than zero. What does this indicate?Consider the following program segment in which the statements are jumbled, choose the correct order of statements to check if a given word is Palindrome or not.
boolean palin(String w) { boolean isPalin; w = w.toUpperCase(); int l = w.length(); isPalin = false; // Stmt (1) for (int i = 0; i < l / 2; i++) { char c1 = w.charAt(i), c2 = w.charAt(l - 1 - i); // Stmt (2) if (c1 != c2) { break; // Stmt (3) isPalin = true; // Stmt (4) } } return isPalin; }