Menu
Topics Index
...
`

Classes842


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes842
What will be the output of the program?
class ClassDataValidation
{
    public static void main(String arg[])
    {
        Student ashok = new Student("Ashok");

        ashok.setSection('B');
        ashok.setMarks(80);
        ashok.print("LINE A");
        
        ashok.setSection('Z');
        ashok.print("LINE B");
                
        ashok.setMarks(100);
        ashok.print("LINE C");

        ashok.setDetails(90, 'C');
        ashok.setSection('A');
        ashok.print("LINE D");    
    }
}

class Student
{
    String name;
    int marks;
    char section;
    
    Student(String name)
    {
        this.name = name;
    }
    
    void setMarks(int marks)
    {
        if( marks >= 0 && marks <= 100 )
        {
            this.marks = marks;
        }
    }
    
    void setSection(char section)
    {
        if( section == 'A' || section == 'B' || section == 'C' || section == 'D')
        {
            this.section = section;
        }
    }
    
    void setDetails(int marks, char section)
    {
        setMarks(marks);
        setSection(section);
    }

    void print(String LineA)
    {
        System.out.println(LineA + " : " + name + " - " + marks + " - " + section);
    }
}

A. LINE A : Ashok - 80 - B
LINE B : Ashok - 0 - B
LINE C : Ashok - 100 - B
LINE D : Ashok - 90 - A
B. LINE A : Ashok - 80 - B
LINE B : Ashok - 0 -  
LINE C : Ashok - 100 - 
LINE D : Ashok - 90 - C
C. LINEA : Ashok - 80 - B
LINEA : Ashok - 80 - B
LINEA : Ashok - 100 - B
LINEA : Ashok - 90 - A
D. LINE A : Ashok - 80 - B
LINE B : Ashok - 80 - B
LINE C : Ashok - 100 - B
LINE D : Ashok - 90 - A
E. Compilation Error
Topic: Creating A Class For Data Validation

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 D..here we have the Student class with name,marks,section as instance variables..and this class has Constrctor which can receive the String param..and this class methods for setting the values for the instance variables...print() for print purpose

Coming to the main class here we create the Instance for the Student class by passing the String as "Ashok"..
next we are calling the setSection() by passing 'B' using the above instance so that this method will set the value for section variable for the calling instance..

we are calling the setMarks() by passing 80 using the above instance so that this method will set the value for marks variable for the calling instance..
next we are calling print() by passing the "LineA"String in that method we are printing the passed String which is "LineA" and name in current object which is Ashok and marks which is 80 next section 'B'
so it will print "LINE A : Ashok - 80 - B"
so now

ashok objt will look like this
ashok--->name=Ashok
ashok--->section=B
ashok--->marks=80
next we are calling setSection() by passing the 'Z' with ashok object only...here previous value will get overided and
ashok--->section=Z
nw we are calling the print() by passing the "LineB"
here every thing will be same Bcz here we are checking if the recevied char must be 'A' or 'B' or 'C' if not we are not assigning
so it will print "LineB-ashok-B-80"

next we are calling setMarks() by passing the 100 so it will overide the previous value in marks and 100 will assgned in marks
calling print() by passing "LineC" so it will print "LineB-ashok-B-100"

next we are calling the setDetails() by passing the marks and section...from setDetails() method we are calling setMarks() and setSection() so it will Bcm
ashok---name=Ashok
ashok---marks=90
ashok---section=C

next we are calling the setSection() passing 'A' so
ashok---name=Ashok
ashok---marks=90
ashok---section=A
calling print() by passing "LineA" so it will print
LINE D : Ashok - 90 - A

Posted by Uday Kumar    2014-11-28 12:42:31


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 Gopinath Manchikanti. 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-11-30 01:30:19


© meritcampus 2019

All Rights Reserved.

Open In App