ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
class Converter<T, R> {
R convert(T input, java.util.function.Function<T, R> fn) {
return fn.apply(input);
}
}
// in main:
Converter<String, Integer> c = new Converter<>();
Integer result = c.convert("123", Integer::parseInt);
System.out.println(result + 1);- A. 124
- B. 1231
- C. Compilation error: Integer::parseInt is not a valid Function<String, Integer>
- D. 123
Correct answer: A. 124
Integer::parseInt is a static method reference matching Function<String, Integer>'s functional signature (String -> int, autoboxed to Integer); convert() applies it to "123", yielding 123, and result + 1 (auto-unboxed for the arithmetic) is 124.
More OOP with C++ and Java questions
- What is printed? cpp template<typename T> T maxVal(T a, T b) { return a > b ? a : b; } class Money { int cents; public: Money(int c)…
- What is printed? cpp class Shape { public: virtual void draw() const = 0; }; class Group : public Shape { vector<Shape> children; public…
- What is printed? java class Base { public int hashCode() { return 1; } } class Derived extends Base { @Override public int hashCode() {…
- What is printed? java class Base { Base self() { return this; } } class Derived extends Base { @Override Derived self() { return this; }…
- What is printed? java class Account { private double balance; Account(double b) { balance = b; } void withdraw(double amt) { if (amt >…
- What is printed? java class Task { void execute() throws Exception { try { riskyStep(); } catch (RuntimeException e) { throw new…