ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
String r = Stream.of(3, 1, 3, 2).distinct().sorted()
.map(String::valueOf).collect(Collectors.joining("-"));
System.out.print(r);- A. 3-1-2
- B. 1-2-3-3
- C. 3-2-1
- D. 1-2-3
Correct answer: D. 1-2-3
distinct keeps the first occurrence of each value (3, 1, 2), sorted orders them naturally, and joining concatenates with the separator.
More OOP with C++ and Java questions
- What is printed? java System.out.print(Optional.ofNullable(null).map(Object::toString).orElse("none") + " ")…
- 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…