What all gets printed when the following gets compiled and run. Select the three correct answers.
package net.dharmaraj;
public class test{ public static void main(String args[]) { int i = 1, j = 1; try { i++; j--; if (i / j > 1) i++; } catch (ArithmeticException e) { System.out.println(0); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch (Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); }}
a) 0b) 1 c) 2 d) 3 e) 4
Out Put:--034
Given the following code
What code would be most likely for the body of the ioCall method
package net.dharmaraj;
import java.io.*;
public class Th{ public static void main(String argv[]) { Th t = new Th(); t.amethod(); }
public void amethod() { try { ioCall(); } catch (IOException ioe) { } }}
1) public void ioCall ()throws IOException{ DataInputStream din = new DataInputStream(System.in); din.readChar(); }2) public void ioCall ()throw IOException{ DataInputStream din = new DataInputStream(System.in); din.readChar(); }3) public void ioCall (){ DataInputStream din = new DataInputStream(System.in); din.readChar(); }4) public void ioCall throws IOException(){ DataInputStream din = new DataInputStream(System.in); din.readChar(); }
Compile Out Put:--
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method ioCall() is undefined for the type Th
at net.dharmaraj.Th.amethod(Th.java:17) at net.dharmaraj.Th.main(Th.java:10)
What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current directory?
package net.dharmaraj;
import java.io.*;
public class Mine{ public static void main(String argv[]) { Mine m = new Mine(); System.out.println(m.amethod()); }
public int amethod() { try { FileInputStream dis = new FileInputStream("Hello.txt"); } catch (FileNotFoundException fne) { System.out.println("No such file found"); return -1; }
finally { System.out.println("Doing finally"); } return 0; }}
1) No such file found 2 No such file found ,-1 3) No such file found, Doing finally, -1 4) 0
Compile Out Put:--No such file foundDoing finally-1
Please select the correct answer from the following?
package net.dharmaraj;
public class ThrowsDemo{ static void throwMethod() throws Exception { System.out.println("Inside throwMethod."); throw new IllegalAccessException("demo"); }
public static void main(String args[]) { try { throwMethod(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); }
}}
A) Compilation error B) Runtime error C) Compile successfully, nothing is printed. D) inside throwMethod. followed by caught: java.lang.IllegalAccessException: demo
The thread run() method has the following code, what is the result when the thread runs? try { sleep( 200 ); System.out.println( "Printing from thread run() method" ); } catch ( IOException ie) { }
Code :--
public class dev{ public static void main(String a[]) { try { sleep( 200 ); System.out.println( "Printing from thread run() method" ); } catch ( IOException ie) { } }}
A) Compile time error B) Prints on the console Printing from thread run() method C) At line 2 the thread will be stop running and resumes after 200 milliseconds and prints "Printing from thread run() method"D) At line 2 the thread will be stop running and resumes exactly 200 milliseconds elapsed
What is the result from the following code when you run?
package net.dharmaraj;
import java.io.*;
class A{ A() { System.out.println("Executing class A1 constructor"); }
A(int a) throws Exception { System.out.println("Executing class A2 constructor"); throw new IOException("Dunu"); }}
public class B extends A{ B() { System.out.println("Executing class B constructor"); }
public static void main(String args[]) { try { A a = new B(); // A a1=new A(2);
} catch (Exception e) { System.out.println(e.getMessage()); } }}
A) Executing class A1 constructor followed by Executing class B constructor B) No output C) Runtime error D) Compile time error
Compile Out pur :--
Executing class A1 constructorExecuting class B constructor
What is the result from the following code when you run?
package net.dharmaraj;
import java.io.*;
class A{ A() throws Exception { System.out.println("Executing class A constructor"); throw new IOException("Dunu"); }}
public class B extends A{ B() { /* * try{ super(); System.out.println ("Executing class B constructor"); }catch(Exception e){} */ System.out.println("Executing class B constructor"); }
public static void main(String args[]) { try { A a = new B(); } catch (Exception e) { System.out.println(e.getMessage()); } }}
A) Executing class A constructor B) Executing class B constructor C) Runtime error D) Compile time error
Answer -- d inside constructor we can not give try catch block because inside constructor super() or this() must be first statement implicitly or explicitly
Compaile Error:--
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type Exception
at net.dharmaraj.B.<init>(B.java:16) at net.dharmaraj.B.main(B.java:28)