Menu
Topics Index
...
`

Creating Classes with multiple methods


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Classes with multiple methods
What will be the output of the following program?
import java.io.*;
public class AddAppendConcat {
    public static void main(String[] args) throws IOException {
        new A().append("Mat").append("Bet").append("Spirit", 3, 2).print().append(' ').append("Campuit", 0, 3).append("replusit").append('s').print();
    }
}
class A implements Appendable {
    static int count;
    String output = "";
    public A append(CharSequence arg0) throws IOException {
        output += arg0.charAt(count++);
        return this;
    }
    public A append(char arg0) throws IOException {
        output += arg0;
        return new A();
    }
    public A append(CharSequence arg0, int arg1, int arg2) throws IOException {
        output += arg0.subSequence(arg1, arg1 + ++count);
        return this;
    }
    A print() {
        System.out.print(output);
        return this;
    }
}

A. (EMPTY)
B. Merit
C. MeritCampus
D. MatBetriCpreplusits
E. Some other output
F. Compilation Error or Runtime Error
Topic: Unknown

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is B...

Posted by Sai Abhishek    2014-03-10 13:28:14


output B.Merit. 'Mat' is appended to the string output, and next Bet. next append() method appends character sequence from 3 to 4 and so on..

Posted by Nikhil Sulibhavi    2014-03-10 13:37:40


Output A because preincrement so,this will be empty

Posted by Prudviraj Regumani    2014-03-10 13:58:46


ans is B M from Met e from Bet rit from Spirit are appended den append(' ') returns new object Ce is appended to new object but again call to append('s') returns new object whose output is initialised to "" therfore prints empty string

Posted by Teja Potlapally    2014-03-10 14:04:52


Congratulations Teja Potlapally. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-11 04:23:43


I said  same output..   n first one to answer ..Merit Campus Admin .

Posted by Sai Abhishek    2014-03-11 13:06:23


Hello Sai Abhishek Gogu, we need correct and detailed explanation along with the answer. Please see http://java.meritcampus.com/dailydose for details. Better luck next time.

Posted by Merit Campus    2014-03-12 14:46:17


© meritcampus 2019

All Rights Reserved.

Open In App