Menu
Topics Index
...
`

Classes833


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes833
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()
    {

    }
}

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 = , section = , 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.

Ans is A..here we have the Student class with the Properties name,marks and section...using the Different Constructors we are assigning the values to these properties..here we have the Concept of the Constructor overloading which means Having more constructors with different type,order,num of parameters..
Coming to the main() here create the Instance for the class student by passing the values yogesh,67 and b..so 3-arg constructor will Invoke
yogesh-->name=yogesh
yogesh-->marks=67
yogesh-->section=B
next we are printing the values using the object in the Sop()

here we create the another instance as narayan by passing the Narayan,72 so 2 arg Construcotr will invoked..and for section variable we are setting manually 'A'
Narayan-->name=Narayan
Narayan-->marks=80
Narayan-->section=A
next we are printing the values using the object in the Sop()

creating mahesh object by passing only name as mahesh and there we are setting other 2 properties(0,A) manually in the 1-arg constructor..
mahesh-->name=Mahesh
mahesh-->marks=0
mahesh-->section=A

next we are printing the values using the object in the Sop()

creating student object without anyvalues so JVM give default values..for String null,for int 0

Posted by Uday Kumar    2014-12-09 12:35:47


ans is a,,,,it won't lead to compilation or runtime error when a object referecning to an data type which is not present ,,it should print empty for strings it is null for inteers it is 0 for chars it is ' ' empty type this a a guess but reason  might  not be correct,,,,,

Posted by Sai Veerendra    2014-12-09 12:59:40


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 Sai Ram. 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    2014-12-10 04:50:35


© meritcampus 2019

All Rights Reserved.

Open In App