Menu
Question Index
...

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;
    }
}


-5#-7#-45#-4#
-5#-7#18#-4#-20#
5#-7#-39#-4#-40#
-5#-7#-45#-4#40#
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java Multiple Methods In One Class

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App