Menu
Question Index
...

What will be the output of the following program?

class Operations
{
    int v;

    public static void main(String[] args)
    {
        new Operations().add(5).print().sub(7).print().multiply(3).add(3).print().sub(4).print();
    }

    Operations add(int i) { v+=i; return this; }

    Operations sub(int i) { v=-i; return this; }

    Operations multiply(int j) { v*=j; return this; }

    Operations print() { System.out.println("v = " + v); return this; }
}


v = 5
v = -2
v = -3
v = 5
v = -2
v = -3
v = -7
v = 5
v = -7
v = -18
v = -4
Compilation 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