ProgrammingModerateProduct-company placement · GATE CS
What is printed?
What is printed?
static void m(StringBuilder a, StringBuilder b) {
a.append("1");
b = a;
b.append("2");
}
// in main:
StringBuilder x = new StringBuilder("x"), y = new StringBuilder("y");
m(x, y);
System.out.print(x + " " + y);- A. x1 y2
- B. x12 x12
- C. x12 y
- D. x1 y
Correct answer: C. x12 y
Java passes references by value. Assigning b = a only changes the local copy, so both appends go to x's object (x12) and y is untouched.
More OOP with C++ and Java questions
- What is printed? java char c = 'a'; c += 1; System.out.print(c + 1); System.out.print(" " + c + 1);
- Which statement about this code is correct? java Object[] arr = new String[2]; // L1 arr[0] = "a"; // L2 arr[1] = Integer.valueOf(1); //…
- What is printed? java int i = 5; i = i++ + ++i; System.out.print(i);
- What is printed? java class A { Object get() { return "A"; } void set(String s) { System.out.print("A.set "); } } class B extends A {…
- What is printed? java Long a = 1L; System.out.print(a.equals(1) + " " + a.equals(1L) + " " + (a == 1));
- What is printed? cpp struct B { B() { f(); } virtual ~B() { f(); } virtual void f() { cout << "B"; } }; struct D : B { D() { f(); } ~D() {…