Menu
Topics Index
...
`

Account Test


If you need explanation Read this topic

If you need Answer Take test on this topic

Account Test
What will be the output of the following program?
public class AccountTest {
    public static void main(String[] args) {
    Account account = new Account();
    account.credit(200, "15-Oct-2013", "Hyderabad").print();
    account.debit(50, "16-Oct-2013", "Warangal").print();
    account.debit(75, "18-Oct-2013", "Guntur").print();
    account.credit(15, "18-Oct-2013", "Khammam").print();
    }
}
class Account {
    double amount;
    String lastTransactionDate, lastLocation;
    int numberOfTranscations;
    Account credit(double amount, String date, String location) {
        numberOfTranscations++;
        lastTransactionDate = date;
        lastLocation = location;
        amount += amount;
        return this;
    }
    Account debit(double amount, String date, String location) {
        if (this.amount >= amount) {
            numberOfTranscations++;
            lastTransactionDate = date;
            lastLocation = location;
            amount -= amount;
            return this;
        }
        return null;
    }
    void print() {
        System.out.println(numberOfTranscations + ". " + lastTransactionDate + " at " + lastLocation + " : " + amount);
    }
}

A. 1. 15-Oct-2013 at Hyderabad : 200.0
2. 16-Oct-2013 at Warangal : 150.0
3. 18-Oct-2013 at Guntur : 75.0
4. 18-Oct-2013 at Khammam : 90.0
B. 1. 15-Oct-2013 at Hyderabad : 200.0
2. 16-Oct-2013 at Warangal : 150.0
3. 18-Oct-2013 at Guntur : 75.0
4. 18-Oct-2013 at Khammam : 60.0
C. 1. 15-Oct-2013 at Hyderabad : 200.0
2. 16-Oct-2013 at Warangal : 50.0
3. 18-Oct-2013 at Guntur : 75.0
4. 18-Oct-2013 at Khammam : 15.0
D. Some other output
E. Compilation Error or Runtime Error
Topic: Behavior Of Java Classes Using Methods

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 D,when there is a name conflict for the variables always local variables will have precedence over instance variables,here we did't used the concept of this keyword for assigning amount to the instance variables,so instance variables holds their default values..

Posted by Raviteja Daggupati    2014-04-30 12:25:48


E Runtime Error will occur as  null pointer is returned

Posted by Akash Kolariya    2014-04-30 12:41:51


Ans is E. Runtime error occurs. After the first call with amount printed as 0.0 due to name conflict, for the second call nullpointer exception arises.

Posted by Nikhil Sulibhavi    2014-04-30 12:56:56


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

Posted by Merit Campus    2014-05-01 06:59:52


© meritcampus 2019

All Rights Reserved.

Open In App