Menu
Topics Index
...
`

Marks validation


If you need explanation Read this topic

If you need Answer Take test on this topic

Marks validation
What will be the output of the following program?
public class MarksValidation {
    public static void main(String[] args) throws Exception {
        int marks = 102;
        if (marks < 100) {
            System.out.println("You marks are Valid");
        } else {
            try {
                throw new MyException("You marks are not Valid");
            } catch (MyException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
class MyException extends Exception {
    public MyException(String string) {
        super(string);
    }
}

A. You data are Valid
B. You marks are not Valid
C. Compilation Error
D. Runtime Error
Topic: User Defined Exception 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.

answer b...    in this program variable m is assigned with 102. if condition chck m value with 100...m value greater than 100 so if case failed and control goes to else part of if condition..    here try catch mechanism is used for handling an exception.   Myexeception class extends Exception..it inherits all the features of exception...exceptions are run time error so to handle these error Exception class and method are used..    here try block throw exception.  catch block catches exception and prints error msg.   so your marks are not valid get printd bcz exception object hving these string.

Posted by Ananda Kumar    2014-10-22 10:05:13


b

Posted by Shubham Bansal    2014-10-22 10:16:21


Ans is B..
In this program marks is assigned with 102 which fails the if condition as 102 is greater than 100.. so it will enter into else part and it throws a user defined exception "MyException" with a message in it... and as catch block catching the exception, it prints the error message sent.. So it will print "You marks are not valid".

Posted by Gopinath Manchikanti    2014-10-22 13:36:06


Ans is B...

here we are dealing with the concept of user defined Exceptions...in java we have in built exceptions for few topics only if we want throw Excpetion for your own requirements then u can create an Exception class by extending the Class Extend Throwble or Excpetion..
if you want throw an exceptions if Cmd line args is more than 2 than we can create that

coming to the example here we have  2 classes MyException,MarksValidation

coming to the main class here we create marks variable assigned with value 102
here the given condtion get failed

throw--> it is an keyword which is used throw an Exception by the programmer manually

here we throwing Object of class MyException by passing the String to it..in MyException class Constrctor we are calling Super class Constructor means here we are calling Exception class Constructor...

here we thrown Exeception but we put it in try catch to avoid the abnormal termination of the program..
so in catch it wil printed the message

Posted by Uday Kumar    2014-10-22 14:12:48


This dose is now closed and the winners are Ananda Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Vikas Tiyagi. 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-10-23 01:24:38


© meritcampus 2019

All Rights Reserved.

Open In App