Menu
Topics Index
...
`

Classes835


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes835
What will be the output of the following program?
class OutPut
{
    public static void main(String arg[])
    {
        Student yogesh = new Student("Yogesh", 67, 'B');
        Student narayan = new Student("Narayan", 72);
        Student mahesh = new Student("Mahesh");

        Student kiran = new Student("Kiran");
                
        System.out.println("Name = " + yogesh.name + ", section = " + yogesh.section + ", marks = " + yogesh.marks);

        System.out.println("Name = " + narayan.name + ", section = " + narayan.section + ", marks = " + narayan.marks);

        System.out.println("Name = " + mahesh.name + ", section = " + mahesh.section + ", marks = " + mahesh.marks);

        System.out.println("Name = " + kiran.name + ", section = " + kiran.section + ", marks = " + kiran.marks);

    }
}

class Student
{
    String name;
    int marks;
    char section;
    
    Student(String nameParam, int marksParam, char sectionParam)
    {
        name = nameParam;
        marks = marksParam;
        section = sectionParam;
    }
    
    Student(String nameParam, int marksParam)
    {
        name = nameParam;
        marks = marksParam;
        section = 'A';
    }
    
    Student(String nameParam)
    {
        name = nameParam;
        marks = 0;
        section = 'A';
    }

    Student()
    {
        name = "Kiran";
        section = 'C';
    }
}

A. Name = Yogesh, section = B, marks = 67
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = A, marks = 0
Name = Kiran, section = A, marks = 0
B. Name = Yogesh, section = B, marks = 67
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = A, marks = 0
Name = Kiran, section = C, marks =
C. Compilation Error
D. Runtime Error
Topic: Class With Multiple Constructors In Java

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.

Answer is A....Student("Yoigesh,67,'B'") will invoke the 3 args constructor and prints name = yogesh section = B and marks =67....Student ("Narayan",72) will invoke the two args constructor for which section 'A' is intialised in the constructor and name and marks are assigned with the passed in arguments to constructor....Student("Mahesh") willinvoke the 1 arg contrustor which defaults marks as 0 and section as 'A'.....Finally Student("Kiran") also invokes the 1 arg constructor and pritns kiran as name,section as 'a' and marks with 0

Posted by Shashanka Mogaliraju    2015-03-02 09:08:23


ANS IS A

--Here,the concept of classes and objects is used.

--Four objects of student class are created in output class
1.yogesh
2.narayan
3.mahesh
4.kiran

--After that name,marks and grade of these four objects are ptinted systematically.

--In student class,three attributes are defined:
1.Name
2.marks
3.section

And three constructor are defined:
1.constructor with 3 parameters
2.constructor with 2 parameters
3.constructor with 1 parameter
4.constructor with no parameter

--Concept of "CONSTRUCTOR OVERLOADING" is used, i.e. depending on the parameters passed appropriate constructor will be called.
1.for yogesh object constructor with 3 parameters wil be called.
1.for narayan object constructor with 2 parameters wil be called.
1.for mahesh object constructor with 1 parameters wil be called.
1.for kiran object constructor with 1 parameters wil be called.

OUTPUT:
Name=yogesh section=B marks=67
Name=Narayan section=A marks=72
Name=Mahesh section=A marks=0
Name=Kiran section=A marks=0

Posted by Mânïshå Mùlchåndânï    2015-03-02 10:13:16


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram 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-03-03 03:37:42


© meritcampus 2019

All Rights Reserved.

Open In App