Computer Applications
Raj wanted to count the number of digits in a given number without using a loop. Which of the following statements is correct to perform the above?
- String.valueOf(n).length()
- Integer.parseInt(n).length()
- n.length()
- All the above
Answer
String.valueOf(n).length()
Reason — The expression String.valueOf(n).length() converts the number n into a string using String.valueOf(n), and then calculates the number of characters (digits) in that string using .length(). This is a valid and loop-free way to count digits in an integer.
Related Questions
Mr. Sanjay is an event manager, he plans and allots duties to each of his subordinates to handle different events. In a program, a task is divided into simple methods. Name the feature used in these contexts.
- Complexity
- Modularity
- Reusability
- Monolithic
Raj was asked to accept the phone number which has 10 digits, using the appropriate method of Scanner class. Which of the following statement he must choose?
- Ob.nextInt()
- Ob.nextDouble()
- Ob.nextLong()
- Ob.next().chatAt(0)
Sham was asked to encode a string S, by replacing the letter E with #. Select the appropriate statement:
- S.replace('#','E')
- S.replace('E')
- S.replace('E', '#')
- S.replace('#')
When the String
mis compared ton, the result obtained is greater than zero. Which of the following is true?mcomes before n in the dictionary.ncomes beforemin the dictionary.mandnare equal.mandnhave the same length.