ProgrammingEasyTCS NQT · Infosys pattern
Which statement about Java String objects is correct?
- A. They are primitive types
- B. They are immutable: methods like toUpperCase return a new String
- C. They are mutable but thread-unsafe
- D. They can be changed in place with setCharAt
Correct answer: B. They are immutable: methods like toUpperCase return a new String
A String's characters never change after creation; every modifying-looking method returns a new object. setCharAt belongs to StringBuilder, the mutable alternative.
More OOP with C++ and Java questions
- What is printed? java String s = "ab"; s.concat("cd"); s.toUpperCase(); System.out.print(s);
- Which pairing is correct?
- Which of these operators cannot be overloaded in C++?
- A variable declared inside a Java interface is implicitly:
- What is printed? cpp class A { public: A() { cout << "A"; } ~A() { cout << "a"; } }; class B : public A { public: B() { cout << "B"; }…
- What is printed? java class C { static int n = 0; C() { n++; } } // in main: for (int i = 0; i < 3; i++) new C(); C c = new C()…