Exceptions & Flow Control In Java MCQ


Hello friends in this post we are going to discuss about Exceptions MCQ with answer | Exceptions Multiple choice question with answer | Exceptions in Java Objective type question | Flow Control MCQ with Answers | Flow Control Multiple choice question | Flow Control Objective type question

If you are unable to find related question answer visit this Post first

If you are looking for more Dumps for MYSKILLZ Visit Here

135.Which statement is TRUE about catch{} blocks?

a) There can only be one catch{} block in a try/catch structure.

b) The catch{} block for a child exception class must PRECEDE that of a parent exception class.

c) The catch{} block for a child exception class must FOLLOW that of a parent exception class.

d) A catch{} block need not be present even if there is no finally{} block.

Ans: b

136.Both class Error and class Exception are children of this parent:
a)Throwable
b) Catchable
c) Runnable
d) Problem

Ans: a

137.What type of exception is thrown by parseInt() if it gets illegal data?
a) ArithmeticException
b) RunTimeException
c) NumberFormatException
d) NumberError

Ans: c

138.Which of the following lists exception types from MOST specific to LEAST specific?
a) Error, Exception
b) Exception, RunTimeException
c) Throwable, RunTimeException
d) ArithmeticException, RunTimeException

Ans: d

139.Which of these statement is true ?
a) finally block gets executed only when there are exceptions.
b) Finally gets always executed irrespective of the flow in try catch block.
c) finally block can be present only when a catch block is present.
d) finally block gets executed only when there are no exceptions.

Ans: b

140.On occurrence of which of the following is it possible for a program to recover?
a) Errors
b) Exceptions
c) Both errors and exceptions
d) Neither

Ans: b

141.Which statement is true?
a) If an exception is uncaught in a method, the method will terminate and normal execution will resume.
b) An overriding method must declare that it throws the same exception classes as the method it overrides.
c) The main() method of a program cannot declare that it throws checked exceptions.
d) A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class.

Ans: d

142.class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {C() {}} // 3
Which one of the following statements is true?
a) Compile-time error at 1.

b) Compile-time error at 2.
c) Compile-time error at 3.
d) No compile-time errors.

Ans: c

143.When is a finally{} block executed?
a) Only when an unhandled exception is thrown in a try{} block.
b) Only when any exception is thrown in a try{} block.
c) Always after execution has left a try catch{} block, no matter for what reason
d) Always just as a method is about to finish.

Ans: c

144.Which statement is TRUE about the try{} block?
a) It is mandatory for statements in a try{} block to throw at least one exception type.
b) The statements in a try{} block can only throw one exception type and not several types.
c) The try{} block can contain loops or branches.
d) The try{} block can appear after the catch{} blocks.

Ans: c

145.class A {
public static void main (String[] args) {
Object error = new Error();
Object runtimeException = new RuntimeException();
System.out.print((error instanceof Exception) + “,”);
System.out.print(runtimeException instanceof Exception);
}}
What is the result of attempting to compile and run the program?
a) Prints: false,false
b) Prints: false,true
c) Prints: true,false
d) Prints: true,true

Ans: b


146.What is the result of compiling and executing the below code with the mentioned arguments ?
java TestInvocation Welcome Year 2009
public class TestInvocation
{
public static void main(String… args)
{
String arg1 = args[1];
String arg2 = args[2];
String arg3 = args[3];
}
}
a) Compilation succeeds
b) Throws exception at runtime
c) Compilation fails
d) None of the above.

Ans: b

147.Given the following,

  1. public class MyProgram {
  1. public static void main(String args[]){
  2. try {
  3. System.out.print(“Hello world “);
  4. }
  5. finally {
  6. System.out.println(“Finally executing “);
  7. }
  8. }
  9. }
    What is the result?
    a) Nothing. The program will not compile because no exceptions are specified.
    b) Nothing. The program will not compile because no catch clauses are specified.
    c) Hello world.
    d) Hello world Finally executing

Ans: d

148.What is the result of compiling and executing the below code ?
public class TryTest {
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println(“Finally”);
}
}
}
a) Outputs nothing
b) Finally
c) Compilation Error
d) Runtime Error

Ans: b

149.class A {
public static void main (String[] args) {
Error error = new Error();
Exception exception = new Exception();
System.out.print((exception instanceof Throwable) + “,”);
System.out.print(error instanceof Throwable);
}}
What is the result of attempting to compile and run the program?
a) Prints: false,false
b) Prints: false,true
c) Prints: true,false
d) Prints: true,true

Ans: d

150.public class MyClass {
public static void main(String[] args) {
RuntimeException re = null;
throw re;

}
}
What will be the result of attempting to compile and run the above program?
a) The code will fail to compile, since the main() method does not declare that it throws RuntimeException in its declaration.
b) The program will compile without error and will throw java.lang.RuntimeException when run.
c) The program will compile without error and will throw java.lang.NullpointerException when run.
d) The program will compile without error and will run and terminate without any output.

Ans: c

151.Given the following program, which one of the statements is true?
public class Exceptions {
public static void main(String[] args) {
try {
if (args.length == 0) return;
System.out.println(args[0]);
} finally {
System.out.println(“The end”);
}
}
}
a) If run with one argument, the program will produce no output.
b) If run with one argument, the program will simply print the given argument.
c) If run with one argument, the program will print the given argument followed by “The end”.
d) The program will throw an ArrayIndexOutOfBoundsException.

Ans: c

152.Given the following:
public class TestDivide {
public static void main(String[] args) {
int value=0;
try {
int result = 10/value;
} finally {
System.out.println(“f”);
}
}
}
What is the result ?
a) Compilation fails since a catch block is not present.
b) Prints only “f” in the output.
c) Only a runtime error is displayed.
d)Prints an “f” in the output and a runtime error is also displayed.

Ans: d

153.Given the following code in the 3 java files:
NewException.java

class NewException extends Exception {
}
Welcome.java
class Welcome {
public String displayWelcome(String name) throws NewException {
if(name == null) {
throw new NewException();
}
return “Welcome “+ name;
}
10.}
TestNewException.java
11.class TestNewException {
public static void main(String… args) {
Welcome w = new Welcome();
System.out.println(w.displayWelcome(“Ram”));
}
16.}
What is the result on compiling and executing it ?
a) Compiles successfully and displays Ram when TestNewException is executed.
b) Runtime exception occurs on executing the class TestNewException.
c) Compilation of Welcome.java fails.
d)Compilation of TestNewException.java fails

Ans: d

154.Given the following code:
public class ArithmeticTest {
public static void main(String[] args){
try
{
int x=0;
int y=5/x;
System.out.println(y);
}
catch (Exception e)
{
System.out.println(“Exception”);
}
catch (ArithmeticException ae)
{
System.out.println(“ArithmeticException”);
}
}
}
What is the output?
a) Exception
b) ArithmeticException
c) NaN
d) Compilation Error

Ans: d

155.Given the following,
1.import java.io.*;
2.public class MyProgram {

3.public static void main(String args[]){
4.FileOutputStream out = null;
5.try {
6.out = new FileOutputStream(“test.txt”);
7.out.write(122);
8.}
9.catch(IOException io) {
10.System.out.println(“IO Error.”);
11.}
12.finally {
13.out.close();unhandled exception
14.}
15.}
16.}
and given that all methods of class FileOutputStream, including close(), throw an IOException, which one of these is true?
a) This program will compile successfully.
b) This program fails to compile due to an error at line 13.
c) This program fails to compile due to an error at line 9.
d) This program fails to compile due to an error at line 6.

Ans: b

156.Given the following:
1.class Base {
2.void display() throws Exception { throw new Exception(); }
3.}
4.public class Derived extends Base {
5.void display() { System.out.println(“Derived”); }
6.public static void main(String[] args) {
7.new Derived().display();
8.}
9.}
What is the result ?
a) Derived
b) The code runs with no output.
c) Compilation fails because of an error in line 2.
d) Compilation fails because of an error in line 7.

Ans: a

157.Given the following,
1.public class RTExcept {
2.public static void throwit () {
3.System.out.print(“throwit “);
4.throw new RuntimeException();
5.}
6.public static void main(String [] args) {
7.try {
8.System.out.print(“hello “);
9.throwit();
10.}
11.catch (Exception re ) {
System.out.print(“caught “);

13.}
14.finally {
15.System.out.print(“finally “);
16.}
17.System.out.println(“after “);
18.}
19.}
What is the output ?
a) hello throwit caught
b) hello throwit RuntimeException caught after
c) hello throwit caught finally after
d) hello throwit caught finally after RuntimeException

Ans: c

158.public class ExceptionTest {
public static void main(String[] args)
{
try
{
ExceptionTest a = new ExceptionTest();
a.badMethod();
System.out.println(“A”);
}
catch (Exception e)
{
System.out.println(“B”);
}
finally
{
System.out.println(“C”);
}
}

void badMethod()
{
    throw new Error();
}

}
What is the output?
a) BC followed by Error exception
b) Error exception followed by BC
c) C followed by Error exception
d) Error exception followed by C

Ans: c

159.Given the following,
public class MyProgram {
public static void throwit() {
throw new RuntimeException();
}
public static void main(String args[]){
try {
System.out.println(“Hello 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?
a) The program will not compile.
b) The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing.
c) The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.
d) The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.

Ans: d

160.Given the following,

  1. System.out.print(“Start “);
  2. try {
  3. System.out.print(“Hello world”);
  4. throw new FileNotFoundException();
  5. }
  6. System.out.print(” Catch Here “);
  7. catch(EOFException e) {
  8. System.out.print(“End of file exception”);
  9. }
  10. catch(FileNotFoundException e) {
  11. System.out.print(“File not found”);
  12. }
    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?
    a) The code will not compile.
    b) Code output: Start Hello world File Not Found.
    c) Code output: Start Hello world End of file exception.
    d) Code output: Start Hello world Catch Here File not found.

Ans: a

161.Given the following code:

  1. import java.io.IOException;
    1.2. public class ExceptionTest
    2.{
    3.public static void main(String[] args)
    4.{
    5.try
    6.{
    7.methodA();
    8.}
    9.catch(IOException e)
    10.{
    11.System.out.println(“Caught IO Exception”);

12.}
13.catch(Exception e)
14.{
15.System.out.println(“Caught Exception”);
16.}
17.}
18.static public void methodA()
19.{
20.throw new IOException();
21.}
22.}
What is the output ?
a) The output is “Caught Exception”.
b) The output is “Caught IO Exception”.
c) Code will not compile.
d) Program executes normally without printing a message.

Ans:c

162.Given:
public class TestException {
public static void main(String… args) {
try {
// some piece of code
} catch (NullPointerException e1) {
System.out.print(“n”);
} catch (RuntimeException e2) {
System.out.print(“r”);
} finally {
System.out.print(“f”);
}
}
}
What is the output if NullPointerException occurs when executing the code in the try block ?
a) f
b)nf
c) rf
d) nrf

Ans: b

163.Given the following:

  1. class ShapeException extends Exception {}
    2.
  2. class CircleException extends ShapeException {}
    1. public class Circle2 {
  3. void m1() throws ShapeException {throw new CircleException();}
    1. public static void main (String[] args) {
  4. Circle2 circle2 = new Circle2();
  5. int a=0, b=0;
    1. try {circle2.m1(); a++;} catch (ShapeException e) {b++;}

System.out.printf(“a=%d, b=%d”, a, b);


  1. }
    16.}
    What is the expected output ?
    a) a=0, b=0
    b) a=1, b=0
    c)a=0, b=1
    d) Compile time error at line 6.

Ans: c

164.Given the following:

  1. class ShapeException extends Exception {}
    2.
  2. class CircleException extends ShapeException {}
    1. public class Circle1 {
  3. void m1() throws CircleException {throw new ShapeException();}
    1. public static void main (String[] args) {
  4. Circle1 circle1 = new Circle1();
  5. int a=1, b=1;
    1. try {circle1.m1(); a–;} catch (CircleException e) {b–;}
    1. System.out.printf(“a=%d, b=%d”, a, b);
  6. }
    16.}
    What is the expected output ?

a) a=1, b=1
b) a=0, b=1
c) a=1, b=0
d)Compile time error at line 6.

Ans: d

165.Suppose your code needs to traverse through an array named array1. Which code would you use to do this ?
a) for(int i = 0; i <= array1.length; i++)
b)for(int i = 0; i < array1.length; i++)
c) for(int i = 0; i <= array1.length(); i++)
d) for(int i = 0; i < array1.length(); i++)

Ans: b

166.Which of the following is legal?
a)for (int i=0, j=1; i<10; i++, j++) { }
b) for (int i=0, j=1; i<10; i++; j++) { }
c) for (int i=0, j=1; i<10,j<10; i++, j++) { }
d) for (int i=0, float j=1.0; ; i++, j++) { }

Ans: a

167.Which option completes the code to print the message as long as number is greater than 20?
int number = 100 ;

MISSING CODE {
System.out.println(“The number = ” + number);
number –;
}
a) do while (number > 20)
b) for (number > 20)
c)while (number > 20)
d) if (number >20)

Ans: c

168.Suppose you are writing code for a for loop that must execute three times.Which is the correct declaration for the loop?
a) for (int i < 4; i = 1; i++) b) for (int i = 0; i < 4; i++) c) for (int i = 1; i++; i < 4) d) for (int i = 3; i >=1; i–)

Ans: d

169.Which flow control mechanism determines when a block of code should run more than once?
a) iteration
b) sequence
c) selection
d) exceptions

Ans: a

170.Which of the following is a legal loop definition?
a) while (int a == 0) { /* whatever / } b) do { / whatever / } while (int a = 0); c) do { / whatever / } while (int a == 0); d) for (int a=0; a<100; a++) { / whatever */ }

Ans: d

171.Given the following code:
public class SwitchTest {
public static void main(String [] args) {
int i=5, j=0;
switch(i){
case 2: j+=3;
case 4: j+=5;
default : j+=1;
case 0: j+=7;
}
System.out.println(“j value ” + j);
}
}
What is the result?
a) j value 16
b) j value 8
c) j value 7
d) Compilation error ( “default” should be at the last of the switch statement)

Ans: b

172.Given the following code:
public class TestBreak {
public static void main(String [] args) {
int i = 2;
if (i < 2) {

i++;
break printAndExit;
}
i++;
printAndExit:
System.out.print(i);
}
}
What will be the result of the above code?
a) 2
b) 3
c) 4
d) Compilation error

Ans: d

173.Given the following:
public class DoWhileTest {
public static void main(String [] args) {
int i=2, j=5;
do
{
if(i++ > –j) continue;
}while(i < 3);
System.out.printf(“i=%d, j=%d”,i,j);
}
}
After execution, what are the values of i and j?
a) i=4, j=4
b) i=3, j=4
c) i=2, j=4
d) i=2, j=5

Ans: b

174.Which statement is true about the following code fragment?

  1. int j = 2;
  2. switch (j) {
  3. case 2:
  4. System.out.println(“value is two”);
  5. case 2 + 1:
  6. System.out.println(“value is three”);
  7. break;
  8. default:
  9. System.out.println(“value is ” + j);
  10. break;
  11. }
    The code is illegal because of the expression at line 5.
    a) The output would be
    value is two
    b) The output would be
    value is two
    value is three
    c) The output would be
    value is two
    value is three
    d) value is 2

Ans: b

175.Given the following:
public class TestLoop
{
public static void main(String… args)
{
int index = 2;
while( –index > 0 )
System.out.println( index );
}
}
What is printed to standard output?
a)1
0
b) 1
2
c) 1
d) Nothing is printed

Ans: a

176.Given the following,

  1. int i = 0;
  2. label:
  3. if (i < 2) {
  4. System.out.print(” i is ” + i);
  5. i++;
  6. continue label;
  7. }
    What is the result?
    a) Compilation fails.
    b) Produces no output
    c) i is 0
    d) i is 0 i is 1

Ans: a

177.Given the following:
public class TestLoop {
public static void main(String… args) {
outer: for( int i = 0; i <2; i++ )
{ inner: for( int j = 0; j < 2; j++ )
{ if( j==1 )
continue outer;
System.out.printf( “i=%d, j=%d\n”,i,j);
}
}
}
}
What is printed to standard output?
a) i=0, j=0
b) i=0, j=0
i=1, j=0
c) i=0, j=0
i=0, j=1
d) i=0, j=0
i=1, j=1

Ans: b

178.Given the following code:
public class PESTest {

public static void main (String[] args) {
int j = 0;
do for (int i = 0; i++ < 2;)
System.out.print(i);
while (j++ < 1);
}
}
What is the result of attempting to compile and run the program?
a) Prints: 12
b)Prints: 1212
c) Prints: 121212
d) Compile-time error

Ans: b

179.Given:
switch( i)
{
default :
System.out.println(“Hello”);
}
What is the acceptable type for the variable i?

a) byte
b) float
c) double
d) Object

Ans: a

180.Given the following code:
public class TestSwitch {
public static void main(String args[]) {
byte b = -1;
switch(b) {
case -1: System.out.print(“-1”); break;
case 127: System.out.print(“127”); break;
case 128: System.out.print(“128”); break;
default: System.out.print(“Default “);
}}}
What is the result of attempting to compile and run the program?
a) Prints: -1
b) Prints: 128
c) Prints: Default
d) Compile-time error

Ans: a

181.Given the following code:
public class TestFor {
static int i;
public static void main(String args[]) {
for (i=1; i<2; i++) {System.out.print(i);} // Line 1
for (int i=1; i<2; i++) {System.out.print(i);} // Line 2
int i; // Line 3
for (i=0; i<1; i++) {System.out.print(i);} // Line 4

System.out.print(TestFor.i);
}}
What is the result of attempting to compile and run the program?
a) Prints: 1100
b) Prints: 1102
c) Compile-time error at Line 1
d) Compile-time error at Line 4

Ans: b

182.For the code snippet:
int m = 0;
while( ++m < 2 )
System.out.println( m );
What is printed to standard output?
a) 0
b)1
c) 2
d) Nothing is printed

Ans: b

183.Given the following code:
public class TestForSwitch {
public static void main (String[] args) {
for (int i = 0; i < 3; i++) {
switch (i) {
default: System.out.print(“D”);
case 0: System.out.print(“0”);
case 1: System.out.print(“1”);
}}}}
What is the result of attempting to compile and run the program?
a) Prints: DDD
b) Prints: 01D
c) Prints: 01D01
d)Prints 011D01

Ans: d

184.Given the following code:
class SwitchTest {
public static void main(String args[]) {
int x = 3; int success = 0;
do {
switch(x) {
case 0: System.out.print(“0”); x += 5; break;
case 1: System.out.print(“1”); success++; break;
case 2: System.out.print(“2”); x += 1; break;
case 3: System.out.print(“3”); x -= 2; break;
default: break;
}
} while ((x != 1) || (success < 2));
}}
What is the result of attempting to compile and run the program?
a) Prints: 3631
b) Prints: 3621
c) Prints: 311

Ans: c

185.Given the following,

  1. public class Test {
  2. public static void main(String [] args) {
  3. int i = 1;
  4. do while ( i < 1 )
  5. System.out.print(” i is ” + i);
  6. while ( i > 1 ) ;
  7. }
  8. }
    What is the result?
    a) i is 1
    b) i is 1 i is 1
    c)No output is produced.
    d) i is 1 i is 1 … in an infinite loop.

Ans: c

186.Given the following code:
public class JavaRunTest {
public static void main (String[] args) {
int i = 0, j = 8;
do {
if (j < 4) {break;} else if (j– < 7) {continue;}
i++;
} while (i++ < 5);
System.out.print(i + “,” + j);
}
}
What is the result of attempting to compile and run the program?
a) Prints: 5,4
b) Prints: 6,5
c) Prints: 6,4
d) Prints: 5,7

Ans: c

187.Given the following:
public class DoTest
{
public static void main(String[] args)
{
boolean flag; int index=3;
do
{
flag = false;
System.out.print(index);
index–;
flag = (index>0);
continue;
} while ((flag) ? true : false);
}
}
What will be the output of above code?
a) 3210
b)321
c) Will go into an infinite loop

Ans: b

188.Given the following code:

  1. public class Test1
  2. {
  3. public static void main(String[] args)
  4. {
  5. int i=0;
  6. while(i)
  7. {
  8. if(i==4) break;
  9. i++;
  10. }
  11. }
  12. }
    What will be the value of i at line 11?
    a) 0
    b)4
    c)5
    d)The code will not compile.

Ans: d

189.Given the following code what is the effect of the parameter “num” passed a value of 1.
public class LoopTest {
public static void process(int num) {
loop: for (int i = 1; i < 2; i++){ for (int j = 1; j < 2; j++) { if (num > j) {
break loop;
}
System.out.println(i * j);
}
}
}
public static void main (String[] args) {
process(1);
}
}
a) Generates a runtime error
b) 3
c) 2
d) 1

Ans: d

190.Given the following code:
public class TestIf {

public static void main(String[] args) {
boolean bFlag = true;
if (bFlag = false) {System.out.print(“X”);
} else if (bFlag) {System.out.print(“Y”);
} else {System.out.print(“Z”);}
}}
What is the result of attempting to compile and run the program?

a) Prints: X
b) Prints: Y
c) Prints: Z
d) Compile-time error

Ans: c

192.Given the following:
public class TestLoop2
{
public static void main(String… args)
{
int count = 10;
while( count++ < 11 )
System.out.println( count );
}
}
What is the output ?
a) 10
11
b) 10
c) 11
d) Nothing is printed

Ans: c

193.Given the following:
public class TestDoWhile
{
public static void main(String… args)
{
int count = 20;
do {
System.out.println( count );
} while ( count++ < 21 );
}
}
What is the output ?
a) 20
21
b) 20
c) 21
d) Nothing is printed

Ans: a

194.Given the following:
public class TestIfBoolean {
public static void main(String[] args) {
Boolean bFlag=null;
if (bFlag) {
System.out.print(“A”);
} else if (bFlag == false) {
System.out.print(“B”);
} else {
System.out.print(“C”);
}
}
}
What is the expected output ?
a) A

b) B
c) C

d) java.lang.NullPointerException is thrown at runtime

Ans: d

195.Given the following,

  1. int j = 7;
  2. label:
  3. if (j > 5) {
  4. System.out.print(” j is ” + j);
  5. j–;
  6. continue label;
  7. }
    What is the result?
    a) j is 7
    b) j is 7 j is 6
    c) Compilation fails
    d) Produces no output

Ans: c


Leave a Reply

Your email address will not be published. Required fields are marked *