Menu
Topics Index
...
`

Classes843


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes843
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 name)
    {
        System.out.println(name + " : " + name + " - " + marks + " - " + section);
    }
}

A. LINE A : LINE A - 80 - B
LINE B : LINE B - 80 - B
LINE C : LINE C - 100 - B
LINE D : LINE D - 90 - A
B. LINE A : Ashok - 80 - B
LINE B : Ashok - 80 - B
LINE C : Ashok - 100 - B
LINE D : Ashok - 90 - A
C. Ashok : Ashok - 80 - B
Ashok : Ashok - 80 - B
Ashok : Ashok - 100 - B
Ashok : Ashok - 90 - A
D. Compilation Error
E. Runtime 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 A..here we have the Student class with the properties name,marks,section
Execution:

here we create the instance for the Student class by passing the "Ashok" as the Param than Constructor is invoked and name in the instance is assigned with the "Ashok"
next we are calling the setSection() by passing the 'B' than in that method we are checking if is valid than we are assigning to the section variable
next we are calling the setMarks() by passing the 80 as Param and checking if it is valid(>=0 and <=100) than we are assigning
calling the print() by passing the "LineA" as param than in the method we are printing the name and marsk and section here section and marks is printed which are in the instance and name is printed which is there in the local variable here name is local variable if we want print data in the object than we use the this keyword

next we are calling the setSection() by passing the 'Z' than in that method we are checking if is valid than we are assigning to the section variable.here 'Z' is not valid so we are not assignign anything so previous will be same(B)
next  we are calling the print by passing the "LineB" so here it will print the same values with the name as "Line b"
similar to setMarks method also
another method we are calling the setDetails() by passing the 'C' and 90 so in that method we are calling the setMarks() and setSection same thing will happen their

Posted by Uday Kumar    2015-02-11 14:42:26


a

Posted by Shubham Bansal    2015-02-11 16:25:28


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 Uday Kumar. 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-12 04:53:20


© meritcampus 2019

All Rights Reserved.

Open In App