Menu
Topics Index
...
`

Classes845


If you need explanation Read this topic

If you need Answer Take test on this topic

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

        ashok.print("LINE A");

        ashok.marks += 20;
        ashok.section = 'B';
        ashok.print("LINE B");

        ashok.marks += 20;                
        ashok.section = 'C';
        ashok.print("LINE C");

        ashok.marks += 20;
        ashok.section = 'D';
        ashok.print("LINE D");    
    }
}

class Student
{
    String name;
    int marks = 20;
    char section = 'A';

    Student(String name)
    {
        this.name = name;
    }
    
    void setMarks(int marks)
    {
        if( marks >= 0 && marks <= 100)
        {
            this.marks = 35;
        }
    }
    
    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 - 20 - A.
LINE B : Ashok - 40 - B.
LINE C : Ashok - 40 - C.
LINE D : Ashok - 40 - D.
B. LINE A : Ashok - 20 - A.
LINE B : Ashok - 40 - A.
LINE C : Ashok - 80 - A.
LINE D : Ashok - 160 - A.
C. <cw>LINE A : Ashok - 20 - A.
LINE B : Ashok - 40 - A.
LINE C : Ashok - 60 - A.
LINE D : Ashok - 80 - A.
D. LINE A : Ashok - 20 - A.
LINE B : Ashok - 40 - B.
LINE C : Ashok - 60 - C.
LINE D : Ashok - 80 - D.
E. LINE A : Ashok - 20 - A.
LINE B : Ashok - 40 - B.
LINE C : Ashok - 80 - C.
LINE D : Ashok - 160 - D.
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.

Answer is D....Because the variables name,marks and section all have the default access which  means they can be accessed any where  within the same package as that of the class....As setter methods were never called in the program,print statements in the program straight way prints the assigned values in the main method

Posted by Shashanka Mogaliraju    2015-02-25 13:32:08


ANS IS D

Here concept of classes and objects is used.

First of all, class student is defined with the three attributes name, marks and section and it has many method for manipulating and assigning values to them.

Print() method is used to print the value of name passed , marks and section.

In student class, marks are initialized to 20 and section to A.

--constructor of class student is called with name Ashok and print method is called by passing ashok.

So,
Line A: ashok-20-A.

After that, ashok object is used to call Print() method and every time marks are incremented by 20 and section is changed from A to B and B to C and so on...

OUTPUT:

Line A: ashok-20-A
Line A: ashok-40-B
Line A: ashok-60-C
Line A: ashok-80-D

Posted by Mânïshå Mùlchåndânï    2015-02-25 14:41:30


Ans is A..here we have the Student class with the properties name,marks,section and marks and section is assigned with the 20 and 'A'
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" and reamining attributes is assigned with the 20 and 'A' next we are callin the print() by passing the LineA so there we are printing the
received String and name and marks and section of the calling object
so "LINE A : Ashok - 20 - A" get printed
next we are adding 20 marks to the ashok object than the marks become 40 and changing the section to the 'B' next we are calling the print() by passing the "LineB" so there we are printing the received String and name and marks and section of the calling object
"LINE B: Ashok - 40 - B" get printed

next we are adding 20 marks to the ashok object than the marks become 60 and changing the section to the 'C' next we are calling the print() by passing the "LineC" so there we are printing the received String and name and marks and section of the calling object
"LINE C: Ashok - 60 - C" get printed

next we are adding 20 marks to the ashok object than the marks become 80 and changing the section to the 'D' next we are calling the print() by passing the "LineD" so there we are printing the received String and name and marks and section of the calling object
"LINE D: Ashok - 80 - D" get printed

we have some more methods in the class but we did not use any of those methods

Posted by Uday Kumar    2015-02-25 15:54:53


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  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 Mânïshå Mùlchåndânï. 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-26 02:38:57


© meritcampus 2019

All Rights Reserved.

Open In App