Menu
Topics Index
...
`

Classes834


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes834
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();
                
        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";
    }
}

A. Name = Yogesh, section = B, marks = 67
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = A, marks = 0
Name = null, section = , marks = 0
B. Name = Yogesh, section = B, marks = 67
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = A, marks = 0
Name = Kiran, section = , marks =
C. Name = Yogesh, section = B, marks = 67
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = A, marks = 0
Name = Kiran, section = , marks = 0
D. Compilation Error
E. 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.

a

Posted by Shubham Bansal    2015-01-30 12:20:56


Ans is C..here we have Student class with the name and marks and section as properties..
Execution:
here we create the instance for the class student with the name yogesh by passing the "yogesh" 67 'B' so the 3 arg constructor is invoked and values get assigned in the yogesh object
yogesh-->name="Yogesh"
yogesh-->marks=67
yogesh-->section=B
next we are creating the one more instnace with the name narayan by passing the "Naraya" 72 so the 2arg constructor is invoked in the we are  setting the misstion section variable as 'A'

narayan-->name="Narayan"
narayan-->marks=72
narayan-->section=A
one more instance with the name mahesh by passing the name as "Mahesh" so 1 arg constructor is invoked than there we are setting the remaining values to marks as 0 and section is 'A'
mahesh-->name="Mahesh"
mahesh-->marks=0
mahesh-->section=A
one more instance with the name kiran with no args so default constructor is invoked there we are setting the name="Kiran" and remaining values set default to 0 for int and empty for the char
kiran-->name="kiran"
kiran-->marks=0
kiran-->section=
next we are printing the data in the object so it will print the output as c

Posted by Uday Kumar    2015-01-30 12:43:24


y,67 b
nara 72 a
mahesh 0 a
kiran  0

ANS IS C

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

--Following 4 objects are created with following names:
1. yogesh-yogeah 67 B
2. narayan-Narayan 72
3. mahesh-Mahesh
4. kiran (default constructor is called)

--Student class is defined with 4 constructors i.e. constructor
overloading is used where constructor with different argument
list are defined.

--when yogesh object is created 1st constructor is called where
name,section and marks attributes are assigned values.

--for narayan,2nd constructor is called where name and marks
attribute are assigned and section is defined with default value "A".

--for mahesh,3rd constructor is called where name attribute is
assigned value and marks=0 and section-"A" are default values.

--for kiran,4th i.e. default constructor is called.Where only
name attribute is assigned value.for section and marks default values
of that data type will be used i.e. blank space(" ") for character
and 0 for integer is used.

Posted by Mânïshå Mùlchåndânï    2015-01-30 19:39:13


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Robi Parvez. 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-02-02 06:35:56


© meritcampus 2019

All Rights Reserved.

Open In App