ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
System.out.print(Optional.ofNullable(null).map(Object::toString).orElse("none") + " ");
System.out.print(Optional.of(null).isPresent());- A. none false
- B. "none " is printed, then a NullPointerException is thrown
- C. null false
- D. A NullPointerException is thrown before anything is printed
Correct answer: B. "none " is printed, then a NullPointerException is thrown
ofNullable(null) gives an empty Optional, map is skipped and orElse supplies "none". Optional.of requires a non-null value and throws NullPointerException immediately.
More OOP with C++ and Java questions
- What happens when this is compiled? java interface I { void a(); void b(); } abstract class P implements I { public void a() { } } class C…
- Which line fails to compile? cpp struct P { int x = 3; ostream& operator<<(ostream& os) { return os << x; } }; int main() { P p; cout <<…
- What is wrong with this program? cpp struct Buf { int p; Buf() : p(new int[4]) { } ~Buf() { delete[] p; } }; int main() { Buf a; Buf b =…
- What happens when this is compiled? cpp struct A { int v = 0; int& get() const { return v; } };
- A global object g1 defined in file1.cpp uses the global object g2 defined in file2.cpp inside g1's constructor. Both have dynamic…
- What is printed? cpp void f(int) { cout << "I"; } void f(char) { cout << "P"; } int main() { f(0); f(nullptr); }