Menu
Question Index
...

What will be the output of the following program?

public class Cards {
    public static void main(String[] args) {
        final Card[] allCards = new Card[3];
        allCards[0] = new WeddingCard();
        allCards[1] = new GreetingCard();
        allCards[2] = new Card() {
            String getDateAndLocation() {
                date = allCards[1].location;
                location = allCards[0].date;
                return super.getDateAndLocation();
            }
        };
        for (Card card : allCards) {
            card.date = "30/02/2015";
            card.location = "Bangalore";
            System.out.print(card.getDateAndLocation() + " ");
        }
    }
}
abstract class Card {
    String date = "06/11/1990";
    String location = "Anantapuram";
    String getDateAndLocation() {
        return date + "~" + location;
    }
}
class WeddingCard extends Card {
    String date = "23/04/2015";
    String getDateAndLocation() {
        return date + "~" + location;
    }
}
class GreetingCard extends Card {
    String location = "Hyderabad";
    String getDateAndLocation() {
        return date + "~" + location;
    }
}


23/04/2015~Bangalore 30/02/2015~Bangalore Bangalore~30/02/2015
30/02/2015~Bangalore 30/02/2015~Bangalore Bangalore~30/02/2015
30/02/2015~Bangalore 30/02/2015~Hyderabad Bangalore~30/02/2015
23/04/2015~Bangalore 30/02/2015~Hyderabad Bangalore~30/02/2015
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Advanced Java Programming Concepts

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App