Menu
Topics Index
...
`

Complex Output


If you need explanation Read this topic

If you need Answer Take test on this topic

Complex Output
What will be the output of the following program?
public class ICreated {
    int ab;
    public static void main(String[] args) {
        new ICreated().add(5).print().sub(7).
            print().multiply(3).add(3).print().
            sub(4).print().multiply(5).print();
    }
    ICreated add(int i) {
        ab += i;
        return this.sub(ab);
    }
    ICreated sub(int i) {
        ab = -i;
        return this;
    }
    ICreated multiply(int j) {
        ab *= j;
        return this.add(ab);
    }
    ICreated print() {
        System.out.print(ab + "#");
        return this;
    }
}

A. -5#-7#-45#-4#
B. -5#-7#18#-4#-20#
C. 5#-7#-39#-4#-40#
D. -5#-7#-45#-4#40#
E. Some other output
F. Compilation Error or Runtime Error
Topic: Java Multiple Methods In One Class

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.

ng

Posted by Aruna Dantu    2014-01-29 08:25:11


f because the printing statement is wrong

Posted by Aruna Dantu    2014-01-29 08:25:34


c

Posted by Kick Bhasha    2014-01-29 08:26:00


answer is d because  add(5) returns -5 and then print() prints -5# sub(-7).print=-7#  multiply(3)=-45 as ab=-7  sub(4)=-4  multiply(5)=40 as ab=4

Posted by Akash Kolariya    2014-01-29 08:37:45


Answer is 'C'
explanation: considering ab to be 0 initially for the first step of print 5 is added separated by# is followed by subtraction of 7 then 5*7+4(.sub) followed by -(.sub) that is -4# and then -40#

Posted by Jigar Mehta    2014-01-29 08:43:16


f because there is compilation error in system.out.print

Posted by Pranavi Prakash    2014-01-29 09:05:06


E because in add function ab +=I that means ab value will be 6 and in sub function ab value is -6 so the ans should start with -6 so ans is e

Posted by Lavanya Kategaru    2014-01-29 11:31:16


ans is F becz ab is nt initilized..

Posted by Uday Kumar    2014-01-29 12:51:56


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

Posted by Merit Campus    2014-01-30 04:26:44


© meritcampus 2019

All Rights Reserved.

Open In App