Menu
Topics Index
...
`

Classes841


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes841
What will be the output of the following program?
class OutPut
{
    public static void main(String arg[])
    {
        Student narayan = new Student();
        Student mahesh = new Student("Mahesh", 98, 'C');
        Student kiran = new Student("Kiran", 'A');
        
        kiran.print("Kiran", 'A');
        mahesh.print("Mahesh", 'B', 65);
        narayan.print();
    }
}

class Student
{
    String name;
    int marks;
    char section;

    Student(String name, int marks, char section)
    {
        this.name = name;
        this.section = section;
        this.marks = marks;
    }

    Student(String name, char section)
    {    
        this(name, 0, section);
    }

    Student()
    {
        this("Narayan", 95, 'A');
    }

    void print()
    {
        System.out.println("Name = " + name + ", marks = " + marks);
    }

    void print(String name, char section)
    {
        System.out.println("Name = " + name + ", section = " + section);
    }
    
    void print(String name, char section, int marks)
    {
        System.out.println("Name = " + name + ", section = " + section + ", marks = " + marks);
    }
}

A. Name = Kiran, section = A, marks = 0
Name = Mahesh, section = B, marks = 65
Name = Narayan, section = A, marks = 95
B. Name = Kiran, section = A
Name = Mahesh, section = B, marks = 65
Name = Narayan, marks = 95
C. Name = Kiran, section = A, marks = 0
Name = Mahesh, section = B, marks = 65
Name = Narayan, marks = 95
D. Name = Kiran, section = A
Name = Mahesh, section = B, marks = 65
Name = Narayan, section = A, marks = 95
E. Compilation 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 is B..here we have the student class with the name,marks and section as properties coming to the program we create the 3 instance for the clss
Student...we create narayan instance with out passing the any arguments so the default constructor in the student class is invoked in that constructor we
called this() by passing the "narayan" 95 and 'A' so from the default cinstructor it will invoke the 3 param constructor in the student class so in that
constrctor we are assigning the values into object..
next we create the mahesh as instance by passing the "mahesh" 98 and 'A' so here also in invokes the 3arg constructor and we are assigning vales into the
object..
next we are creating the kiran instance by passing the "kiran" and A so here 2 arg constructor will invoked and from there we are calling the 3 arg
constructor by adding marks as 0...
this() is used to invoke the constructor in the same class
this keyword is used to differentiate the difference between the local variables and instance variables..
next we are calling the print() with kiran object by passing the "kiran " and 'A' so print method invokes and print the received values as "Name = Kiran, section = A"

next we are calling the print() with mahesh instance by passing the "Mahesh", 'B', 65 so there it will print Name = Mahesh, section = B, marks = 65
next we are calling the print() with the instance narayan so here it will cal print() with no args..in print() we are printing the name and marks which are present the object..we have name as "narayan" and marks as 95 so it will print Name = Narayan, marks = 95

Posted by Uday Kumar    2015-01-27 10:01:19


b

Posted by Shubham Bansal    2015-01-27 10:05:02


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-28 03:47:43


© meritcampus 2019

All Rights Reserved.

Open In App