Defining Exceptions,Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher
Defining Exceptions

Here our working the basically  a develop the java code and the flow of our programs and also exception in functional java code.its mainly the alerts the normal program flow.Exception in its inheritance hierarchy. In other words, exceptions are always some subclass of java.lang.Exception.

try {
// some code here
}
catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}


In this example, e is an instance of a class with the tersely named array Index Out Of Bounds Exception. As with any other object, We can call its methods.

More interview questions and answers

Given the following, class Test { public static void main(String [] args) { int b= 0; int b= 0; for (int c = 0; c < 5; c++) { if (( ++a > 2 ) && (++b > 2)) { a++; } } System.out.println(a + " " + b); } } What is the result?

Given the following, class SSBool { public static void main(String [] args) { boolean p1 = true; boolean p2 = false; boolean p3 = true; if ( p1 & p2 | p2 & p3 | p2 ) System.out.print("ok "); if ( p1 & p2 | p2 & p3 | p2 | p1 ) System.out.println("dokey"); } } what is the result?

Given the following, class BoolArray { boolean [] b = new boolean[3]; int count = 0; void set(boolean [] x, int i) { x[i] = true; ++count; } public static void main(String[] args) { BoolArray ba = new BoolArray(); ba.set(ba.b, 0); ba.set(ba.b, 2); ba.test(); } } 16. 17. void test() { 18. if ( b[0] && b[1] | b[2] ) 19. count++; 20. if ( b[1] && b[(++count - 2)] ) count += 7; 2System.out.println("count = " + count); } } what is the result?

Given the following, class Test { static int s; public static void main(String [] args) { Test p = new Test(); p.start(); System.out.println(s); } void start() { int x = 7; twice(x); System.out.print(x + " "); } void twice(int x) { = x*2; s = x; } } what is the result

What result come when we compile and run following code: public class R4RSCJPTest { final static short i = 4; public static int j = 0; public static void main(String [] args) { for (int k=0; k <= 4; k++) { switch (k) { case j: System.out.print(" 0 "); case i-3: System.out.print(" 1 "); case i-2: System.out.print(" 2 "); case i-1: System.out.print(" 3 "); case i: System.out.print(" 4 "); } } } }

What result come when we compile and run following code: public class R4RSCJPTest { final static short i = 3; public static int j = 1; public static void main(String [] args) { for (int k=1; k <= 4; k++) { switch (k) { case i: System.out.print(" 0 "); case i-1: System.out.print(" 1 "); case i-2: System.out.print(" 2 "); case i-3: System.out.print(" 3 "); } } } }

What result come when we compile and following code: public class R4RSCJPTest { static boolean boo; public static void main(String [] args) { short st = 24; if ( st < 30 & !boo ) st++; if ( st > 30 ) ; else if ( st > 38 ) { st -= 6; st++; } else ++st; System.out.println(st); } }

Given the following, class Test { public static void main(String [] args) { Test p = new Test(); p.start(); } void start() { boolean p1 = false; boolean p2 = fix(p1); System.out.println(p1 + " " + p2); } boolean fix(boolean b1) { b1 = true; return b1; } } what is the result?

Given the following, class PassS { public static void main(String [] args) { PassS p = new PassS(); p.start(); } void start() { String s1 = "slip"; String s2 = fix(s1); System.out.println(s1 + " " + s2); } String fix(String s1) { s1 = s1 + "stream"; .System.out.print(s1 + " "); return "stream"; } } what is the result

Given the following, class SC2 { public static void main(String [] args) { SC2 s = new SC2(); s.start(); } void start() { int a = 3; int b = 4; System.out.print(" " + 7 + 2 + " "); System.out.print(a + b); System.out.print(" " + a + b + " "); System.out.print(foo() + a + b + " "); 14. System.out.println(a + b + foo()); } String foo() { return "foo"; } } what is the result?

Given the following, class PassA { public static void main(String [] args) { PassA p = new PassA(); p.start(); } void start() { long [] a1 = {3,4,5}; long [] a2 = fix(a1); System.out.print(a1[0] + a1[1] + a1[2] + " "); System.out.println(a2[0] + a2[1] + a2[2]); } long [] fix(long [] a3) { a3[1] = 7; return a3; } } what is the result?

Given the following, class Two { byte x; } class PassO { public static void main(String [] args) { PassO p = new PassO(); p.start(); } void start() { Two t = new Two(); System.out.print(t.x + " "); Two t2 = fix(t); System.out.println(t.x + " " t2.x); } Two fix(Two tt) { tt.x = 42; return tt; } } what is the result?

What result come when we compile and run following code: public class R4RSCJPTest { final static short i = 3; public static int j = 1; public static void main(String [] args) { for (int j=1; j <= 4; j++) { switch (j) { case i: System.out.print(" 0 "); case i-1: System.out.print(" 1 "); default: System.out.print("def "); case i-2: System.out.print("2 ");break; case i-3: System.out.print("3 "); } } } }

What result come when we compile and run following code: public class R4RSCJPTest { public void loop() { int i= 3; while ( 1 ) { System.out.print(+ (i + 1)); } } }

What result come when we compile and run following code: public class R4RSCJPTest { public static void main(String args[]) { int i = 3; do while ( i < 3 ) System.out.print("Value of i is " + i); while ( i > 3 ) ; } }

What result come when we compile and run following code: class R4RSCJPTest { static int i; public static void main(String [] args) { R4RSCJPTest test = new R4RSCJPTest(); test.start(); System.out.println(i); } void start() { int j = 3; foo(j); j = j^2; System.out.print(j + " "); } void foo(int j) { j = j*2; i = j; } }

What result come when we compile and run following code: public class R4RSCJPTest { static boolean boo1, boo2; public static void main(String [] args) { int i = 0; if ( !boo1 ) { if ( !boo2 ) { boo1 = false; i++; if ( 0 < i ) { i++; } boo2 = !boo2; if ( boo1 ) i = i + 10; else if ( boo2 == false ) i = i + 100; else if ( boo1 | boo2 ) i = i + 1000; } } System.out.println(i); } }

Writing Code Using if statement

legal arguments for if Statements

Writing Code Using Switch Statement

Legal Arguments to switch and case

default, break, and fall-through in switch blocks

The Default Case

creating a switch-case statement.

Select correct data types that we used in switch statements.

Select two correct for loops when we insert those in following code output will be like that: i = 0 i = 1 class R4RSCJPTest { public static void main(String args[]) { //insert both for loop here { System.out.println("i = "+ i); } } }

What result come when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { int i = 2; boolean boo = false; outer: while (!boo) { i++; inner: for (int j = 1; j < 7; j++) { i += j; if (j == 4) continue inner; break outer; } continue outer; } System.out.println("Value of i is " +i); } }

What result come when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { int i = 9; label: if (i > 3) { i--; continue label; } System.out.println("i is " + i); } }

What result come when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { int i = 9; label: while (i > 3) { i--; continue label; } System.out.print("i is " + i); } }

Writing code using loops.

Using while loops

Using do-while Loops

Using for Loops

Declaration and Initialization

Conditional (boolean) Expression

What result come when we compile and run following code: public class R4RSCJPTest { public static void main(String args[]) { try { System.out.print("Welcome"); } finally { System.out.println("Finally executing "); } } }

Iteration Expression

for Loop Issues

Using break and continue in for Loops

Unlabeled Statements

Labeled Statements

Creating a Labeled while Loop

What result come when we compile and run following code: import java.io.*; public class R4RSCJPTest { public static void main(String args[]) { FileOutputStream out = null; try { out = new FileOutputStream("R4R.txt"); out.write(100); } catch(IOException io) { System.out.println("IOException occur"); } finally { out.close(); } } }

What result come when we compile and following code: public class R4RSCJPTest { public static void throwit() { throw new RuntimeException(); } public static void main(String args[]) { try { System.out.println("Welcome "); throwit(); System.out.println("Execute statement under try block "); } finally { System.out.println("Finally executing "); } } }

What result come when we compile and run following code: public class R4RSCJPTest { public static void throwit () { System.out.print("throwit "); throw new RuntimeException(); } public static void main(String [] args) { try { System.out.print("Welcome "); throwit(); } catch (Exception e ) { System.out.print("exception caught "); } finally { System.out.print("finally executed "); } System.out.println("user "); } }

Handling Exceptions

Catching an Exception Using try and catch

Using finally

Propagating Uncaught Exceptions

Propagating and Catching an Exception

Defining Exceptions

Exception Hierarchy

Handling an Entire Class Hierarchy of Exceptions

Exception Matching

Exception Declaration and the Public Interface

Rethrowing the Same Exception

Creating an Exception

Working with the Assertion Mechanism

Assertions Overview

Assertion Expression Rules

Enabling Assertions

Compiling with Assertions

Compiling Assertion-Aware Code

Compiling with Code That Uses Assert as an Identifier

Running with Assertions

Enabling Assertions at Runtime

Disabling Assertions at Runtime

Selective Enabling and Disabling

Using Assertions Appropriately

Do not use assertions to validate arguments to a public method.

Do use assertions to validate arguments to a private method.

Do not use assertions to validate command-line arguments.

Do use assertions, even in public methods, to check for cases that you know are never, ever supposed to happen

Do not use assert expressions that can cause side effects!

Select correct option for the following code: public class Vehicle { // Write here Vehicle code } public class Bike extends Vehicle { // Write here Cool Bike code } public class Hayabusa extends Bike { // Write here some specific information of Hayabusa // Keep in mind Hayabusa inherits accessible Bike members }

Which is the correct object-oriented relationship between a Building and a Apartment.

Select the three correct option in case of Method overriding.

Select two correct options from the followings:

Select three correct options from the followings:

What result comes when we compile and run following code: class Vehicle { } class Bike extends Vehicle { } class TestVehicles { public void ABC(Vehicle v) { System.out.println("Vehicle"); } public void ABC(Bike b) { System.out.println("Bike"); } public static void main (String [] args) { TestVehicles uvobj = new TestVehicles(); Vehicle vObj = new Vehicle(); Bike bObj = new Bike(); uvobj.ABC(vObj); uvobj.ABC(bObj); } }

What result comes when we compile and run following code: class Vehicle { } class Bike extends Vehicle { } class TestVehicles { public void ABC(Vehicle v) { System.out.print("Vehicle"); } public void ABC(Bike b) { System.out.println(" Bike"); } public static void main (String [] args) { TestVehicles uvobj = new TestVehicles(); Vehicle vObj = new Vehicle(); Vehicle VRTB = new Bike(); //VRTB(Vehicle refer to Bike) uvobj.ABC(VRTB); } }

Given the following, public class myprog { final static short a = 2; public static in b = 0; public static void main(String [] args) { for (int c=0; c < 3; c++) { switch (c) { case b: System.out.print("0 "); case a-1: System.out.print("1 "); case a: System.out.print("2 "); } } } } what is the result ?

Given the following, public class myprog { final static short a = 2; public static int b = 0; public static void main(String [] args) { for (int c=0; c < 3; c++) { switch (c) { case a: System.out.print("0 "); case a-1: System.out.print("1 "); case a-2: System.out.print("2 "); } } } what is the result?

Select three corerct options from the following:

Given the following, public class If1 { static boolean b; public static void main(String [] args) { short hand = 42; if ( hand < 50 & !b ) hand++; if ( hand > 50 ) ; else if ( hand > 40 ) { hand += 7; hand++; } lse -hand; System.out.println(hand); . } what is the result

Select two correct options incase of default constructor.

Given the following, public class myprog { final static short a = 2; public static int b = 0; public static void main(String [] args) { for (int c=0; c < 4; c++) { switch (c) { case a: System.out.print("0 "); default: System.out.print("def "); case a-1: System.out.print("1 "); break; case a-2: System.out.print("2 "); } } } } what is the result?

Given the following, public class myprog { static boolean x1, x2; public static void main(String [] args) { int a = 0; if ( !x1 ) { if ( !x2 ) { x1 = true; a++; if ( 5 > 6 ) { ++; if ( !x1 ) a = a + 10; else if ( x2 = true ) a = a + 100; else if ( x1 | x2 ) a = a + 1000; } } System.out.println(a); } what is the result?

Given the following, public class myprog { public void loop() { int a= 0; while ( 1 ) { System.out.print("a plus one is " + (a + 1)); } } } Which statement is true?

Given the following, class myprog { public void test() { ---------------------------- System.out.println("a = "+ a); } } } and the following output, a = 0 a = 1 which two lines of code (inserted independently) will cause this output?

Given the following, public class Test { public static void main(String [] args) { int A = 1; do while ( A < 1 ) System.out.print("A is " + A); while ( A > 1 ) ; } } what is the result?

Given the following, int A = 0; uter: while (true) { A++; inner: for (int b = ; b < 10; b++) { Self Test 67 A += b; if (b == 3) continue inner; break outer; } continue outer; } System.out.println(I); what is the result?

Given the following, int x = 0; label: if (x < 2) { System.out.print("x is " + x); x++; continue label; } what is the result?

Given the following, System.out.print("Open "); try { System.out.print("Hello This is my first Exception programe "); throw new FileNotFoundException(); } System.out.print(" Catch Here "); catch(EOFException e) { System.out.print("EOF exception"); } catch(FileNotFoundException e) { System.out.print("File not found"); } And given that EOFException and FileNotFoundException are both subclasses of IOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code?

What result come when we compile and run following code: class Test { public void foo() { System.out.println("Basic Info"); } } public class R4RSCJPTest extends Test { public static void main(String args[]) { Test t = new R4RSCJPTest(); t.foo(); } public void foo() { System.out.println("Check your skill with R4RSCJPTest"); } }

What result come when we compile and run following code: class Parent { public int foo(int l) { return l * 5; } } public class Child extends Parent { public static void main(String args[] ) { Child c = new Child(); long l = c.foo(6); System.out.println("l is " + l); } public long foo(int l) { return l * 4; } }

Given the following, public class MyProg { public static void main(String args[]){ try { System.out.print("WELCOME TO MY WORLD "); } finally { System.out.println("Finally executing "); } } } what is the result?

Given the following, import java.io.*; public class MyProgram { public static void main(String args[]){ FileOutputStream out = null; try { out = new FileOutputStream("test.txt"); out.write(122); } catch(IOException io) { System.out.println("IO Error."); } finally { out.close(); } } } and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?

Given the following, public class MyProg{ public static void throwit() { throw new RuntimeExcep(); } public static void main(String args[]){ try { System.out.println("Hello welcome to my world "); throwit(); System.out.println("Done with try block "); } finally { System.out.println("Finally executing "); } } } which answer most closely indicates the behavior of the program?

Given the following, public class RTExcept { public static void throwit () { System.out.print("throwit "); throw new RuntimeException(); } public static void main(String [] args) { try { System.out.print("hello "); throwit(); } atch (Exception re ) { System.out.print("caught "); } finally { System.out.print("finally "); } System.out.println("after "); } } what is the result

R4R provides free online tutolrails ,examples for Freshers and experts .We cover core java, tutorials, spring, Hibernate, python, struts, struts 2,c language, spring boot,PHP,MYSQL,C#,ASP.NET,WPF,WCF,Photoshop , JSP, programming, example, Interview questions and answer