wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Pre Placement Test (Java: Exception_Strings)

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What is the output of this code?

public class Test1

{ public static void main(String[] args)

{

try

{ System.out.println("Try block");

System.exit(0); }

catch (Exception e)

{ System.out.println("Catch block"); }

finally

{ System.out.println("Finally block"); } } }

a)

Try block; Finally block

b)

Try block

c)

Try block; Catch block

d)

Compilation error

2.

What is the output of the code?

public class Test2

{ public static void main(String[] args)

{ try {throw null;}

catch (NullPointerException e)

{ System.out.println("Caught NPE"); } } }

a)

Compilation error

b)

Runtime NullPointerException

c)

Caught NPE

d)

No output

3.

Predict the output of the code.

public class Test3

{ public static void main(String[] args)

{ try

{ System.out.println("A");

throw new Exception("B"); }

catch (Exception e)

{ System.out.println(e.getMessage());}

finally

{ System.out.println("C");

} } }

a)

A B C

b)

A C

c)

Compilation error

d)

A B C in a single line

4.

Predict the output.

public class Test4

{ static int test() {try { return 1; }

catch (Exception e) { return 2; }

finally { return 3; } }

public static void main(String[] args)

{ System.out.println(test()); } }

What value is printed??

a)

1

b)

2

c)

3

d)

Compilation error

5.

What is the output of the following code?

public class Test5

{ public static void main(String[] args)

{ try {

int x = 0;

int y = 5 / x; }

catch (ArithmeticException e)

{ throw new RuntimeException("Runtime from catch"); }

finally

{ System.out.println("Finally runs"); } } }

Choose the correct behavior/output.

a)

Finally runs; RuntimeException

b)

RuntimeException only

c)

Compilation error

d)

Program terminates silently

6.

Predict the output of the code.

public class Test6

{ public static void main(String[] args)

{ try { try { throw new Exception("Inner");}

catch (Exception e)

{ System.out.println("Inner catch"); throw e; }

finally

{ System.out.println("Inner finally"); } }

catch (Exception e) { System.out.println("Outer catch"); } } }

Select the correct sequence printed.

a)

Inner catch; Inner finally; Outer catch

b)

Inner finally; Inner catch; Outer catch

c)

Inner catch; Outer catch; Inner finally

d)

Compilation error

7.

Which of the following is a checked exception?

a)

NullPointerException

b)

ArithmeticException

c)

FileNotFoundException

d)

ArrayIndexOutOfBoundsException

8.

Which of the following is the superclass of all exceptions in Java?

a)

Throwable

b)

Exception

c)

RuntimeException

d)

Object

9.

predict the output of the code?

public class Test {

public static void main(String[] args)

{ try

{ return; }

finally { System.out.println("Finally block executed"); } } }

a)

Nothing is printed

b)

Finally block executed

c)

Compile-time error

d)

Program crashes

10.

Which of the following is true about finally block?

a)

It is not executed if JVM shuts down

b)

It overrides return statements from try and catch

c)

It executes only if an exception occurs

d)

It can be skipped if exception is caught

11.

predict the output of the code

public class Test1 { public static void main(String[] args) {

String a = "Hello";

String b = "Hel" + "lo";

System.out.println(a == b); } }

a)

true

b)

false

c)

Compile-time error

d)

Runtime exception

12.

predict the output of the code?

public class Test2

{ public static void main(String[] args)

{ String str1 = new String("Java");

String str2 = "Java";

System.out.println(str1 == str2); } }

a)

true

b)

false

c)

Compile error

d)

Depends on JVM

13.

predict the output of the code

public class Test4

{ public static void main(String[] args)

{ String s1 = "Java";

String s2 = s1.replace('a', 'e');

System.out.println(s2); } }

a)

Jeve

b)

Jeva

c)

compilation error

d)

Exception raised

14.

predict the output of the code?

public class Test3

{ public static void main(String[] args)

{ String s = "abc";

s.concat("def");

System.out.println(s); } }

a)

abc

b)

abcdef

c)

def

d)

Compile error

15.

predict the output of the code?

public class Test5

{ public static void main(String[] args)

{ String s1 = "abc";

String s2 = new String("abc");

String s3 = s2.intern();

System.out.println(s1 == s3); } }

a)

true

b)

false

c)

Depends on JVM

d)

Compile-time error

16.

predict the output of the code?

public class Test6

{ public static void main(String[] args)

{ String s = "abcde";

System.out.println(s.substring(1, 4)); } }

a)

abc

b)

bcd

c)

bcde

d)

cde

17.

Predict the output of the code:

public class Test8

{ public static void main(String[] args)

{ String s = "hello";

String t = s.toUpperCase();

s.toUpperCase();

System.out.println(s + " " + t); } }

a)

HELLO HELLO

b)

hello hello

c)

hello HELLO

d)

Compile error

18.

Predict the output of the code:

public class Test1

{ public static void main(String[] args)

{ StringBuffer sb = new StringBuffer("Hello");

sb.append(" World");

System.out.println(sb); } }

a)

Hello

b)

HelloWorld

c)

Hello World

d)

Compile-time error

19.

Predict the output of the code:

public class Test2

{ public static void main(String[] args)

{ StringBuffer sb = new StringBuffer("ABCDE");

sb.delete(1, 3);

System.out.println(sb); } }

a)

ABCDE

b)

ACD

c)

ADE

d)

AE

20.

What is the initial capacity of a StringBuffer created with no arguments?

a)

16

b)

0

c)

10

d)

32