Topics Index

Class with multiple methods1888

If you need explanation Read this topic

If you need Answer Take test on this topic

Class with multiple methods1888
What will be the output of the following program?
public class TomAndJerry {
    int tomAge = 5;
    String name = "Tommy";
    public TomAndJerry(String name) {
        System.out.println("Name is : " + name);
    public void getAge(int age) {
        tomAge = age;
    public int getAge() {
        System.out.println("Tom's age is : " + tomAge);
        return tomAge;
    public static void main(String[] args) {
        TomAndJerry tom = new TomAndJerry("Tom");
        System.out.println("Value : " + tom.tomAge);

A. Name is : Tommy
Tom's age is : 2
Value : 5
B. Name is : Tom
Tom's age is : 5
Value : 5
C. Name is : Tom
Tom's age is : 2
Value : 2
D. Some other output
E. 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.

ans must be C as we can overload functions wth difernt set of parameters.. constructor gets executed.. so prints 1st line of output as Tom(local variable gets printed) .. nxt getage function wth int parameter is tomage is set to 2 .. and on call of getage() 2nd line of output gets printed... wth 2 as ouput and the final value reamins 2 ..

Posted by Pramod Jain    2013-10-29 09:00:02

C is the correct answer as name is tom will print while creatiing constructor variable tomage is modified with 2 wwhen we call tomage(2) after that we are calling tomage() which prints the value of tomage and same in case of next statement also

Posted by Ram Reddy Bitla    2013-10-30 00:20:05

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

Posted by Merit Campus    2013-10-30 03:39:01

thank u mc :) got rc :) :)

Posted by Pramod Jain    2013-12-03 04:38:14

© meritcampus 2019

All Rights Reserved.

Open In App