ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
System.out.print("apple".compareTo("apricot") + " "
+ "app".compareTo("apple") + " "
+ "b".compareTo("a"));- A. -1 -2 1
- B. -1 -1 1
- C. -2 -1 1
- D. -2 -2 1
Correct answer: D. -2 -2 1
compareTo returns the difference of the first differing characters: 'p' - 'r' = -2. If one string is a prefix of the other, it returns the length difference: 3 - 5 = -2. 'b' - 'a' = 1. It is not limited to -1, 0, 1.
More OOP with C++ and Java questions
- What is printed? java class P { static void sm() { System.out.print("P"); } void im() { System.out.print("P"); } } class C extends P {…
- What is printed? java class P { int x = 10; int getX() { return x; } } class C extends P { int x = 20; int getX() { return x; } } // in…
- What is printed (default JVM settings)? java Integer x = 127, y = 127; Integer p = 128, q = 128; System.out.print((x == y) + " " + (p ==…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("Object"); } static void f(String s)…
- What is printed? java // static methods of the main class: static void f(long x) { System.out.print("long"); } static void f(Integer x) {…
- What is printed? java class P { P() { show(); } void show() { System.out.print("P "); } } class C extends P { int x = 10; void show() {…