NEW
Font size
WorksheetsPre Placement Test (Java: Exception_Strings)
Total questions: 20
Worksheet time: 10mins
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"); } } }
Try block; Finally block
Try block
Try block; Catch block
Compilation error
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"); } } }
Compilation error
Runtime NullPointerException
Caught NPE
No output
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 B C
A C
Compilation error
A B C in a single line
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??
1
2
3
Compilation error
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.
Finally runs; RuntimeException
RuntimeException only
Compilation error
Program terminates silently
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.
Inner catch; Inner finally; Outer catch
Inner finally; Inner catch; Outer catch
Inner catch; Outer catch; Inner finally
Compilation error
Which of the following is a checked exception?
NullPointerException
ArithmeticException
FileNotFoundException
ArrayIndexOutOfBoundsException
Which of the following is the superclass of all exceptions in Java?
Throwable
Exception
RuntimeException
Object
predict the output of the code?
public class Test {
public static void main(String[] args)
{ try
{ return; }
finally { System.out.println("Finally block executed"); } } }
Nothing is printed
Finally block executed
Compile-time error
Program crashes
Which of the following is true about finally block?
It is not executed if JVM shuts down
It overrides return statements from try and catch
It executes only if an exception occurs
It can be skipped if exception is caught
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); } }
true
false
Compile-time error
Runtime exception
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); } }
true
false
Compile error
Depends on JVM
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); } }
Jeve
Jeva
compilation error
Exception raised
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); } }
abc
abcdef
def
Compile error
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); } }
true
false
Depends on JVM
Compile-time error
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)); } }
abc
bcd
bcde
cde
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); } }
HELLO HELLO
hello hello
hello HELLO
Compile error
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); } }
Hello
HelloWorld
Hello World
Compile-time error
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); } }
ABCDE
ACD
ADE
AE
What is the initial capacity of a StringBuffer created with no arguments?
16
0
10
32
