Menu
Topics Index
...
`

Classes839


If you need explanation Read this topic

If you need Answer Take test on this topic

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

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

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

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

A. Name = null, section = 0, marks = 0
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = C, marks = 98
B. Name = , section = 0, marks = 0
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = C, marks = 98
C. Name = null, section = null, marks = 0
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = C, marks = 98
D. Name = , section = , marks = 0
Name = Narayan, section = A, marks = 72
Name = Mahesh, section = C, marks = 98
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 A.
It stores the values passed in the constructor while creating an object for that and prints the data according to that.

Posted by Gopinath Manchikanti    2014-12-25 04:11:10


Ans A.
Here 'this' statement will allow  print the number as it is. So there will be no modifications in the print statement

Posted by Fuzail Ahmed    2014-12-25 05:06:57


ANS IS A
--Three Objects of "Student" class are created i.e. narayan,mahesh n kiran.
--Creation of these objects makes the parametrized constructor of student class to execute in which actual parameters are assigned to formal ones and then to "INSTANCE VARIABLES" of class using "This" keyword.
--After that print() method is called with each object which displays the appropriate information of that object.
--"null" will get printed if String is assigned with "null".

Posted by Mânïshå Mùlchåndânï    2014-12-25 09:47:00


This dose is now closed and the winners are Gopinath Manchikanti,  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 Bolo Raven. 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-26 02:32:17


© meritcampus 2019

All Rights Reserved.

Open In App