Showing posts with label exception. Show all posts
Showing posts with label exception. Show all posts

Saturday, August 17, 2019

what will happen when you run the following code?

Problem statement:
Given the java code. what will be the output?
  1. public class Test {
  2. static void test() throws RuntimeException {
  3. try {
  4. System.out.println("test");
  5. throw new RuntimeException();
  6. } catch (Exception ex) {
  7. System.out.println("exception");
  8. }
  9. }
  10. public static void main(String[] args) {
  11. try {
  12. test();
  13. throw new RuntimeException();
  14. } catch (RuntimeException ex) {
  15. System.out.println("runtime");
  16. System.out.println("end");
  17. }
  18. }
  19. }
Output:
test
exception
runtime
end

Friday, August 16, 2019

What is the output of the following code?

Problem statement:
Given the code of java. Find out the output.
  1. public class Test {
  2. static void test() throws RuntimeException {
  3. try {
  4. System.out.println("test");
  5. throw new RuntimeException();
  6. } catch (Exception ex) {
  7. System.out.println("exception");
  8. }
  9. }
  10. public static void main(String[] args) {
  11. try {
  12. test();
  13. } catch (RuntimeException ex) {
  14. System.out.println("runtime");
  15. }
  16.     System.out.println("end");
  17. }
  18. }
Output:
test
exception
end

Saturday, May 11, 2019

what will happen when you run the following code?

Problem statement:
what is the output for the following code?
  1. class ExecutionTen {
  2.     public void talk() {}
  3. }
  4. public class ExecutionNine {
  5.     public static void main(String[] args) {
  6.         ExecutionTen t = null;
  7.         try {
  8.             t.talk();
  9.         } catch (NullPointerException e) {
  10.             System.out.println("This is NullPointerException !");
  11.         } catch (Exception e) {
  12.             System.out.println("This is an Exception !");
  13.         }
  14.         System.out.println("Everything went fine !");
  15.     }
  16. }
Output:
This is NullPointerException !
Everything went fine !

what will be the console output when you run the following code?

Problem statement:
what will be the output of the following exception handling code?
  1. public class ExecutionSix {
  2.     public static void throwIt() 
  3.     {
  4.         throw new RuntimeException();
  5.     }
  6.     public static void unsafe() 
  7.     {
  8.         try 
  9.         {
  10.             System.out.println("Unsafe try start !");
  11.             throwIt();
  12.             System.out.println("Unsafe try end !");
  13.         } finally 
  14.         {
  15.             System.out.println("Finally executing !");
  16.         }
  17.     }
  18.     public static void main(String[] args) 
  19.     {
  20.         try 
  21.         {
  22.             unsafe();
  23.         } catch (RuntimeException e) 
  24.         {
  25.             System.out.println("Exception caught !");
  26.         }
  27.     }
  28. }
Output:
Unsafe try start !
Finally executing !
Exception caught !

Saturday, July 14, 2018

Exception piece of code output ?

Problem statement: what will be the output of the below code?
import java.io.FileNotFoundException;
import java.io.IOException;
class A {
    public void run() throws FileNotFoundException {
        System.out.println("A");
    }
}
class B extends A {
    public void run() throws IOException {
        System.out.println("B");
    }
}
public class ExceptionFileNotFound {
    public static void main(String args[]) {
        B a = new B();
        a.run();
    }
}
Output:
compilation error
run() method in A class clashes with run() method in B class.
Overriden method does not throw Exception

Exception piece of code output ?

Problem statement: what will be the output of the below code?

class A {
public void run() throws ArithmeticException {
System.out.println("A");
}
}
class B extends A {
public void run() throws Exception {
System.out.println("B");
}
}
public class ExceptionClass {
public static void main(String args[]) {
A a = new B();
a.run();
}
}

Output:
compilation error
run() method in A class clashes with run() method in B class.
Overriden method does not have subclass of ArithmaticException class. 

so there is no visibility of Exception class from the parent class.


Monday, June 4, 2018

what are the LoopHoles of null & NullPointerException

Problem statement: what do you mean by null & NullPointerException?
  • null:
  1. null is reserved literal but not keyword in java [literal mean any constant value that can be assigned to a variable]
  2. we can use null for any object reference variable
  3. if the value of reference variable null means it is not pointing to any object
  4. null is applicable to only for Object types but not for Primitive Type if we are trying to use for the primitive data types then we will get Compile Time Error
  5. null is default value for reference variable [note: make sure you should remember default values concept is applicable only for Instance & Static Variables but not for Local Variable]
  6. System.out.println(null); Compile error - reference to println is ambiguous
String s = null;
System.out.println(s); // valid
System.out.println((String)null); // valid
  • NullPointerException:
7.If we are trying to perform any Operation on the null then we will get Runtime Exception - NullPointerException
8. For any object reference r,
r == null is always false
but null == null is always true
9. for any object reference r,
r.equals(null) is always false
10. null vs NULL vs String s ="";
null & empty both are not same.
there is no concept like NULL in java
11. If an object is no longer required then we can make that object eligible for Garbage Collector by assigning null to all its reference variables.
Student s1 = new Student();
s1 = null;
Now object is eligible for GC

12. In java there are total 53 reserved word out of them 50's are keyword and 3's are reserved literal[true, false, null]

[a]-if reserved words are associated with functionality then it will keyword. e.g. if()...
[b]-if reserved words are just to represent the value then such reserved words are called reserved literals like true, false, null

Tuesday, April 24, 2018

What is the output of the program ? [based on exception]

  1. class SampleExe {
  2. void run() throws Exception {
  3. System.out.println("run before x");
  4. int x = 10 / 0;
  5. System.out.println("run after x");
  6. }
  7. }

  8. public class SmapleExe1 {
  9. public static void main(String[] args) {
  10. SampleExe exe = new SampleExe();
  11. exe.run();
  12. }
  13. }
Output: Unhandled type exception at compile time at line 12
--------------------------------------------------------------------
  1. class SampleExe {
  2. void run() throws Exception {
  3. System.out.println("run before x");
  4. int x = 10 / 0;
  5. System.out.println("run after x");
  6. }
  7. }

  8. public class SmapleExe1 {
  9. public static void main(String[] args) throws Exception {
  10. SampleExe exe = new SampleExe();
  11. exe.run();
  12. }
  13. }
Output: 
run before x
java.lang.ArithmeticException: / by zero at line 4

--------------------------------------------------------------------
  1. class SampleExe {
  2. void run() throws Exception {
  3. System.out.println("run before x");
  4. int x = 10 / 0;
  5. System.out.println("run after x");
  6. }
  7. }

  8. public class SmapleExe1 {
  9. public static void main(String[] args) {
  10. SampleExe exe = new SampleExe();
  11. try {
  12. exe.run();
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
Output: 
run before x
java.lang.ArithmeticException: / by zero at line 4

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...