Explore topic-wise MCQs in Java Programming.

This section includes 61 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

and given that all methods of class , including , throw an , which of these is true?

A. This program will compile successfully.
B. This program fails to compile due to an error at line 4.
C. This program fails to compile due to an error at line 6.
D. This program fails to compile due to an error at line 18.
Answer» E.
2.

Which four can be thrown using the throw statement?

A. 1, 2, 3 and 4
B. 2, 3, 4 and 5
C. 1, 4, 5 and 6
D. 2, 4, 5 and 6
Answer» D. 2, 4, 5 and 6
3.

and given that and are both subclasses of , 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.
Answer» B. Code output: Start Hello world File Not Found.
4.

What is the output of this program?
public class Exec_Handling_Example 
{
public static void main(String args[])
{
try
{
int k, n;
n = 5;
for (k = -2; k < 2 ;++k)
{
n = (n / k);
System.out.print(k);
}
}
catch(ArithmeticException e)
{
System.out.print("0");
}
}
}

A. -1 0
B. 0 -1
C. 0
D. 1
E. None of these
Answer» B. 0 -1
5.

What is the output of this program?
public class Exep_Handling_Example
{
public static void main(String args[])
{
try
{
int p, q;
p = 0;
q = 15/p;
}
catch(ArithmeticException e)
{
System.out.print("Welcome to ");
}
finally
{
System.out.print("Interview Mania");
}
}
}

A. Welcome to
B. Interview Mania
C. Interview
D. Mania
E. Welcome to Interview Mania
Answer» F.
6.

What is the output of this program?
public class Null_Pointer_Exception
{
public static void main(String args[])
{
try
{
System.out.print("First");
throw new NullPointerException ("Interview Mania");
}
catch(ArithmeticException e)
{
System.out.print("Second");
}
}
}

A. First
B. Second
C. Interview Mania
D. Compilation Error
E. Runtime Error
Answer» F.
7.

What is the output of this program?
public class Array_Exception 
{
public static void main(String args[])
{
try
{
int p = args.length;
int q = 12 / p;
System.out.print(p);
try
{
if (p == 1)
p = p / p - p;
if (p == 2)
{
int []s = {1};
s[10] = 8;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("First");
}
}
catch (ArithmeticException e)
{
System.out.println("Second");
}
}
}

A. Compilation Error
B. Runtime Error
C. First
D. Second
E. None of these
Answer» C. First
8.

What is the output of this program?
public class Result 
{
public static void main(String args[])
{
try
{
int p = 2;
int q = 9;
int s = p / q;
System.out.print("First");
}
catch(Exception e)
{
System.out.print("Second");
}
}
}

A. First
B. Second
C. Compilation Error
D. Runtime Error
E. None of these
Answer» B. Second
9.

What is the output of this program?
public class Without_Catch
{
public static void main(String args[])
{
try
{
System.out.print("Interview Mania ");
}
finally
{
System.out.println("Finally executed.. ");
}
}
}

A. Interview Mania
B. Finally Executed
C. Runtime Error
D. Compilation Error
E. Interview Mania Finally executed..
Answer» F.
10.

What is the output of this program?
public class Finally_Example
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally block executed..." );
}
}
}

A. Compilation Error
B. Runtime Error
C. Finally block executed...
D. All of above
E. None of these
Answer» D. All of above
11.

What is the output of this program?
public class Arithmetic_Exception
{
public static void main(String args[])
{
try
{
int p = args.length;
int q = 11 / p;
System.out.print(p);
}
catch (ArithmeticException e)
{
System.out.println("First");
}
}
}

A. Compilation Error
B. Runtime Error
C. First
D. 11
E. None of these
Answer» D. 11
12.

What is the output of this program?
public class Result 
{
public static void main(String args[])
{
try
{
int num = 0;
int num0 = 23;
int num1 = num0 / num;
System.out.print(+num1);
}
catch(Exception e)
{
System.out.print("Interview Mania");
}
finally
{
System.out.print(" Finally executed...");
}
}
}

A. Interview Mania
B. Finally executed...
C. Interview Mania Finally executed...
D. All of above
E. None of these
Answer» D. All of above
13.

What is the output of this program?
public class Result 
{
public static void main(String args[])
{
try
{
int p = 0;
int q = 11;
int s = q / p;
System.out.print("Hello");
}
catch(Exception e)
{
System.out.print("Java");
}
}
}

A. Compilation Error
B. Runtime Error
C. Hello
D. Java
E. None of these
Answer» E. None of these
14.

What is the output of this program?
public class exceptionHandling_Example
{
public static void main(String args[])
{
try
{
int n = args.length;
int n1 = 9 / n;
System.out.print(n);
try
{
if (n == 1)
n = n / n - n;
if (n == 2)
{
int s = {1};
s[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("Hello");
}
catch (ArithmeticException e)
{
System.out.println("Java");
}
}
}
}

A. Runtime Error
B. Compilation Error
C. First
D. Second
E. None of these
Answer» C. First
15.

What is the output of this program?
public class exception_handlingExample 
{
public static void main(String args[])
{
try
{
int num = 10;
for (int k = -1; k < 3 ;++k)
num = (num / k);
}
catch(ArithmeticException e)
{
System.out.print("0");
}
System.out.print(num);
}
}

A. 10
B. 3
C. Compilation Error
D. Runtime Error
E. None of these
Answer» D. Runtime Error
16.

What is the output of this program?
class Newexception extends Exception 
{
int Execution;
Newexception(int p)
{
Execution = p;
}
public String toString()
{
return "Execution";
}
}

public class Output
{
static void compute (int p) throws Newexception
{
throw new Newexception(p);
}
public static void main(String args[])
{
try
{
compute(13);
}
catch(Exception e)
{
System.out.print("Newexception working proper...");
}
}
}

A. Newexception
B. Execution
C. Newexception working proper...
D. Runtime Error
E. None of these
Answer» D. Runtime Error
17.

What is the output of this program?
public class exceptionHandling_Example
{
public static void main(String args[])
{
try
{
throw new NullPointerException ("Hello");
System.out.print("First");
}
catch(ArithmeticException e)
{
System.out.print("Second");
}
}
}

A. NullPointerException
B. ArithmeticException
C. Second
D. First
E. Runtime Error
Answer» F.
18.

What is the output of this program?
class Myexception extends Exception 
{
int Execution;
Myexception(int p)
{
Execution = p;
}
public String toString()
{
return "Execution";
}
}

public class Result
{
static void compute (int p) throws Myexception
{
throw new Myexception(p);
}
public static void main(String args[])
{
try
{
compute(6);
}
catch(DevideByZeroException e)
{
System.out.print("Catch block Executed...");
}
}
}

A. 6
B. Compilation Error
C. Runtime Error
D. Catch block Executed...
E. None of these
Answer» D. Catch block Executed...
19.

What is the output of this program?
class Myexception extends Exception 
{
int Execution;
Myexception(int n)
{
Execution = n;
}
public String toString()
{
return "Execution";
}
}

public class UserDefine_Exception
{
static void compute (int n) throws Myexception
{
throw new Myexception(n);
}
public static void main(String args[])
{
try
{
compute(5);
}
catch(Myexception e)
{
System.out.print("User define Exception executed...");
}
}
}

A. User define Exception executed...
B. Execution
C. Compilation Error
D. Runtime Error
E. None of these
Answer» B. Execution
20.

What is the output of this program?
 public class I_LOVE_YOU
{
public static void main(String args[])
{
try
{
int You, Me, You_Me;
You = 0;
Me = 5;
You_Me = Me/You;
System.out.print("I MISS U ALWAYS...");
}
catch(ArithmeticException e)
{
System.out.print("I LOVE YOU");
}
finally
{
System.out.print(" FOREVER...");
}
}
}

A. I MISS U ALWAYS...
B. I LOVE YOU
C. FOREVER...
D. I LOVE YOU FOREVER...
E. None of these
Answer» E. None of these
21.

What is the output of this program?
 public class exception_Example
{
public static void main(String args[])
{
try
{
int p, q;
q = 0;
p = 13 / q;
System.out.print("welcome to");
}
catch(ArithmeticException e)
{
System.out.print("Interview");
}
finally
{
System.out.print(" Mania");
}
}
}

A. welcome to
B. Interview Mania
C. Interview
D. Mania
E. None of these
Answer» C. Interview
22.

What is the output of this program?
public class ExceptionHandling_Example
{
public static void main(String args[])
{
try
{
int array[] = {10, 20,30 , 40, 50};
for (int K = 0; K < 5; ++K)
System.out.print(array[K]);
int p = 1/0;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("L");
}
catch(ArithmeticException e)
{
System.out.print("U");
}
}
}

A. 1020304050
B. 10203050
C. 1020304050U
D. 20304050U
E. 304050U
Answer» D. 20304050U
23.

What is the output of this program?
public class ExceptionHandling_Example
{
public static void main(String args[])
{
try
{
int array[] = {5,6,7,8};
for (int K = 0; K < 7; ++K)
System.out.print(array[K]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("9");
}
}
}

A. Compilation Error
B. 56789
C. Runtime Error
D. 12345
E. 56780
Answer» C. Runtime Error
24.

What is the output of this program?
public class ExceptionHandling_Example
{
public static void main(String args[])
{
try
{
int p = args.length;
int q = 10 / p;
System.out.print(p);
try
{
if (p == 1)
{
p = p / p - p;
}
if (p == 2)
{
int r = {1};
r[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("TypeA");
}
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
}

A. Compilation Error
B. Rutime Error
C. TypeA
D. TypeB
E. None of these
Answer» B. Rutime Error
25.

What is the output of this program?
public class ExceptionHandling_Example
{
public static void main(String args[])
{
try
{
int p = 1;
int q = 10 / p;
try
{
if (q == 1)
p = p / p - p;
if (p == 2)
{
int r[] = {1};
r[8] = 9;
}

}
catch (NullPointerException e)
{
System.out.println("*****");
}
}
finally
{
System.out.print("***");
}
}
}

A. *****
B. ***
C. Compilation Error
D. Runtime Error
E. None of these
Answer» C. Compilation Error
26.

What is the output of this program?
public class ExceptionHandling_Example
{
static void throwexception() throws ArithmeticException
{
System.out.print("10");
throw new ArithmeticException ("Exception");
}
public static void main(String args[])
{
try
{
throwexception();
}
catch (ArithmeticException e)
{
System.out.println("U");
}
}
}

A. 10U
B. 10
C. U
D. U10
E. None of these
Answer» B. 10
27.

What is the output of this program?
public class exception_Example
{
static void throwexception() throws ArithmeticException
{
System.out.print("50");
throw new ArithmeticException ("Exception");
}
public static void main(String args[])
{
try
{
throwexception();
}
catch (ArithmeticException e)
{
System.out.println("M");
}
}
}

A. 50
B. M
C. 50M
D. M50
E. None of these
Answer» D. M50
28.

What is the output of this program?
public class exception_Example 
{
public static void main(String args[])
{
try
{
int array[] = {11, 21,31 , 41, 51};
for (int k = 0; k < 5; ++k)
System.out.print(array[k]);
int p = 1/0;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("I");
}
catch(ArithmeticException e)
{
System.out.print("L");
}
}
}

A. 1121314151L
B. 112131
C. 1121314151U
D. 112131451
E. L1121314151
Answer» B. 112131
29.

What is the output of this program?
public class exception_Example 
{
public static void main(String args[])
{
try
{
int array[] = {1, 2,3 , 4, 5};
for (int k = 0; k < 7; ++k)
System.out.print(array[k]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("5");
}
}
}

A. 123455
B. 123
C. 12345
D. 123450
E. 120345
Answer» B. 123
30.

What is the output of this program?
public class exception_handling 
{
public static void main(String args[])
{
try
{
int p, q;
q = 0;
p = 5 / q;
System.out.print("Love");
}
catch(ArithmeticException e)
{
System.out.print("You");
}
}
}

A. Love You
B. You Love
C. Love
D. You
E. None of these
Answer» E. None of these
31.

What is the output of this program?
public class exception_Example
{
public static void main(String args[])
{
try
{
System.out.print("Interview" + " " + 15 / 0);
}
catch(ArithmeticException e)
{
System.out.print("Mania");
}
}
}

A. Interveiw
B. Mania
C. Compilation Error
D. Runtime Error
E. None of these
Answer» C. Compilation Error
32.

What will be the result after the class Test execution?class A{public void doA(){B b = new B();b.dobB();System.out.print("doA");}}class B{public void dobB(){C c = new C();c.doC();System.out.print("doB");}}class C{public void doC(){if(true)throw new NullPointerException();System.out.print("doC");}}public class Test{public static void main(String args[]){try{A a = new A();a.doA();}catch(Exception ex){System.out.print("error");}}}

A. doCdoBdoA" is printed
B. doAdoBdoC" is printed
C. doBdoAerror" is printed
D. error" is printed
E. othing is printed
Answer» E. othing is printed
33.

Given the code. What is the result when this program is executed?public class Test{static int x[];static{x[0] = 1;}public static void main(String args[]){}}

A. rrayIndexOutOfBoundsException is thrown
B. xceptionInInitializerError is thrown
C. llegalStateException is thrown
D. tackOverflowException is thrown
E. one of these
Answer» C. llegalStateException is thrown
34.

Predict the output:public class Test{public static void main(String args[]){try{String arr[] = new String[10];arr = null;arr[0] = "one";System.out.print(arr[0]);}catch(Exception ex){System.out.print("exception");}catch(NullPointerException nex){System.out.print("null pointer exception");}}}

A. one" is printed.
B. exception" is printed.
C. null pointer exception" is printed.
D. ompilation fails saying NullPointerException has already been caught.
E. one of these
Answer» E. one of these
35.

Which of the below statement is/are true about Error?A. An Error is a subclass of Throwable.B. An Error is a subclass of Exception.C. Error indicates serious problems that a reasonable application should not try to catch.D. An Error is a subclass of IOException.

A. and D
B. and B
C. and C
D. and C
E. and D
Answer» C. and C
36.

What will be the result of executing the following code?public class Test{public void divide(int a, int b){try{int c = a / b;}catch(Exception e){System.out.print("Exception ");}finally{System.out.println("Finally");}public static void main(String args[]){Test t = new Test();t.divide(0,3);}}

A. rints out: Exception
B. rints out: Exception Finally
C. ompile with error
D. rints out: Finally
E. one of these
Answer» E. one of these
37.

What is the output for the below code ?import java.io.FileNotFoundException;class A{public void printName() throws FileNotFoundException{System.out.println("Value-A");}}class B extends A{public void printName() throws NullPointerException{System.out.println("Name-B");}}public class Test{public static void main (String[] args) throws Exception{A a = new B();a.printName();}}

A. alue-A
B. ompilation fails-Exception NullPointerException is not compatible with throws clause in A.printName()
C. ame-B
D. ompilation succeed but no output
E. one of these
Answer» D. ompilation succeed but no output
38.

What will be the result after compiling this code?class SuperClass{public int doIt(String str, Integer... data)throws Exception{String signature = "(String, Integer[])";System.out.println(str + " " + signature);return 1;}}public class Test extends SuperClass{public int doIt(String str, Integer... data){String signature = "(String, Integer[])";System.out.println("Overridden: " + str + " " +signature);return 0;}public static void main(String... args){SuperClass sb = new Test();sb.doIt("hello", 3);}}

A. verridden: hello (String, Integer[])
B. ello (String, Integer[])
C. ompilation fails
D. one of these
Answer» D. one of these
39.

What will be the output?class MyClass{public String test(){try{System.out.print("One");return ";}finally{System.out.print("Two");}}}public class Test{public static void main(String args[]){MyClass m =new MyClass();m.test();}}

A. ne
B. wo
C. ne Two
D. ompilation Error
E. one of these
Answer» D. ompilation Error
40.

public class Test{public static void main(String args[]){try{int a = Integer.parseInt("four");}}}Which exception could be handled by the catch block for above?

A. llegalStateException
B. umberFormatException
C. lassCastException
D. rrayIndexOutOfBoundsException
E. one of these
Answer» C. lassCastException
41.

What will be the output of the following piece of code:class Person{public void talk() {}}public class Test{public static void main(String args[]){Person p = null;try{p.talk();}catch(NullPointerException e){System.out.print("There is a NullPointerException. ");}catch(Exception e){System.out.print("There is an Exception. ");}System.out.print("Everything went fine. ");}}

A. here is a NullPointerException. Everything went fine.
B. here is a NullPointerException.
C. here is a NullPointerException. There is an Exception.
D. his code will not compile, because in Java there are no pointers.
Answer» B. here is a NullPointerException.
42.

Given the following piece of code:class SalaryCalculationException extends Exception{}class Person{public void calculateSalary() throws SalaryCalculationException{//...throw new SalaryCalculationException();//...}}class Company{public void paySalaries(){new Person().calculateSalary();}}Which of the following statements is correct?1. This code will compile without any problems.2. This code will compile if in method paySalaries() we return a boolean in stead of void.3. This code will compile if we add a try-catch block in paySalaries().4. This code will compile if we add throws SalaryCalculationException in the signature of method paySalaries().

A. and 4
B. and 3
C. and 4
D. and 4
E. and 2
Answer» E. and 2
43.

What is the output of the following program code?public class Test{public static void main(String args[]){try{int i;return;}catch(Exception e){System.out.print("inCatchBlock");}finally{System.out.println("inFinallyBlock");}}}

A. nCatchBlock
B. nCatchBlock inFinallyBlock
C. nFinallyBlock
D. he program will return without printing anything
Answer» D. he program will return without printing anything
44.

Which exception is thrown when an array element is accessed beyond the array size?

A. rrayElementOutOfBounds
B. rrayIndexOutOfBoundsException
C. rrayIndexOutOfBounds
D. one of these
Answer» C. rrayIndexOutOfBounds
45.

What happen in case of multiple catch blocks?

A. ither super or subclass can be caught first.
B. he superclass exception must be caught first.
C. he superclass exception cannot caught first.
D. one of these
Answer» D. one of these
46.

Which of the following blocks execute compulsorily whether exception is caught or not.

A. inally
B. atch
C. hrows
D. hrow
Answer» B. atch
47.

System.out.print("Start "); try { System.out.print("Hello world"); throw new FileNotFoundException(); } System.out.print(" Catch Here "); /* Line 7 */ catch(EOFException e) { System.out.print("End of file 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?

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.
Answer» B. Code output: Start Hello world File Not Found.
48.

What will be the output of the program? class Exc0 extends Exception { } class Exc1 extends Exc0 { } /* Line 2 */ public class Test { public static void main(String args[]) { try { throw new Exc1(); /* Line 9 */ } catch (Exc0 e0) /* Line 11 */ { System.out.println("Ex0 caught"); } catch (Exception e) { System.out.println("exception caught"); } } }

A. Ex0 caught
B. exception caught
C. Compilation fails because of an error at line 2.
D. Compilation fails because of an error at line 9.
Answer» B. exception caught
49.

public class ExceptionTest { class TestException extends Exception {} public void runTest() throws TestException {} public void test() /* Point X */ { runTest(); } } At Point X on line 5, which code is necessary to make the code compile?

A. No code is necessary.
B. throws Exception
C. catch ( Exception e )
D. throws RuntimeException
Answer» C. catch ( Exception e )
50.

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.
Answer» E.