Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Exception Handling and String Handling

Total questions: 49

Worksheet time: 29mins

Name
Class
Date
1.

What is the parent class of All Exceptions in JAVA

a)

Throwable

b)

Exception

c)

Error

d)

Bug

2.

What is the advantage of Exception Handling

a)

To avoid abnormal termination of a program

b)

To find out errors

c)

To Debug program

d)

None of these

3.

Which of the following key word is optional in Exception handling program

a)

try

b)

catch

c)

finally

d)

throw

4.

try block may or may not contains catch blocks

a)

True

b)

False

5.

What is the purpose of 'throw' keyword

a)

To Raise an exception explicityly

b)

To Raise an exception implicityly

c)

To create user defined exceptions

d)

To create custom exceptions

6.

What is an exception

a)

Error occurred during the execution of a program

b)

Bug occurred during the compile time of a program

c)

Simply an Error

d)

It is related to input values

7.

How to create our own exception

a)

using 'throw' keyword

b)

using 'throws' keyword

c)

using 'finally' keyword

d)

using 'throwable' keyword

8.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

9.

Which of these keywords is not a part of exception handling?

a)

try

b)

finally

c)

thrown

d)

catch

10.

java.lang.NullPointerException is a

a)

Error

b)

runtime exception

c)

Compile time exception

d)

None

11.

Which of these class is highest in hierarchy in java

a)

java.lang.Exception

b)

java.lang.Error

c)

java.lang.Throwable

d)

java.lang.Object

12.

Choose the CORRECT exception


String department="JTMK";

Integer no=Integer.parseInt(department);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundsException

13.

Choose an exception if attempt to divide number by zero


int number = 89 / 0;

System.out.println("The answer is " + number);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundException

14.

What will it print?

a)

1

2

3

30

b)

1

2

30

c)

1

2

3

11

d)

1

2

11

15.

Which line or lines should we get inside a try block to avoid errors?

a)

3

b)

4

c)

6

d)

2

16.

What will this print?

a)

There was an error

b)

1

c)

9

d)

0

17.

What will this program print?

a)

Nothing, there is an error

b)

There was a ValueError, please try again

c)

num1num2num1^{num2}

d)

num2num1num2^{num1}

18.

int len = "jump it!".length();


What is the value of len?

a)

8

b)

7

c)

9

d)

6

e)

This code does not compile.

19.

int x = "quarantine".indexOf("a");


What is the value of x?

a)

4

b)

3

c)

2

d)

This code does not compile.

e)

StringIndexOutOfBoundsException

20.

String s = "whatever".substring(1,4);


What is the value of s?

a)

"hate"

b)

"hat"

c)

"whate"

d)

"what"

e)

StringIndexOutOfBoundsException

21.

String s1 = "hello";

String s2 = new String("hello");

if (s1 == s2) System.out.print("Hey");

else System.out.print("Yo");


What is the output?

a)

Hey

b)

Yo

c)

There is no output.

d)

This code will not compile.

22.

int p = "Go Paw Patrol!".indexOf("p");


What is the value of p?

a)

0

b)

4

c)

3

d)

-1

e)

This code will not compile.

23.

String s = "cool cat".substring(6);


What is the value of s?

a)

"cat"

b)

"at"

c)

"cool c"

d)

This code does not compile.

e)

StringIndexOutOfBoundsException

24.

int x = "Mary".compareTo("Molly");

if (x>=0) System.out.print("Yes");

else System.out.print("No");


What is the output?

a)

Yes

b)

No

c)

There is no output.

d)

This code will not compile.

25.

String s1 = "goat";

String s2 = s1.substring(0,s1.length());

boolean z = s1.equals(s2);


What is the value of z?

a)

true

b)

false

c)

This code will not compile.

d)

StringIndexOutOfBoundsException

26.

String s = "good job".substring(3, 9);


What is the value of s?

a)

"d job"

b)

"od job"

c)

"d jo"

d)

"od jo"

e)

StringIndexOutOfBoundsException

27.

int x = "bob".compareTo("Bob");

if (x>=0) System.out.print("Good");

else System.out.print("Bad");


What is the output?

a)

Good

b)

Bad

c)

There is no output.

d)

This code will not compile.

28.

Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?

a)

str.length() -2

b)

str.length() -4

c)

str.length() - 3

d)

str.length() - 1

e)

str.length()

29.

Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?

a)

pos>0

b)

pos>=0

c)

pos<0

d)

pos<=0

e)

pos==0

30.

Is String a primitive data type?

a)

Yes

b)

No

c)

Both

31.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

32.

Which data type gets returned from length() method in String class?

a)

double

b)

String

c)

int

d)

number

33.

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


What is the correct way to find the length of "txt" string?

a)

int len = length(txt);

b)

float len = txt.length();

c)

int len = txt.length();

d)

double len = length(txt);

34.

Which is correct method to convert String into uppercase

a)

changeUpperCase()

b)

convertUpperCase()

c)

toUpper()

d)

toUpperCase()

35.

String txt = "Hello World";

System.out.println(txt.toUpperCase());


Choose the correct output.

a)

"HELLO WORLD"

b)

HELLO WORLD

c)

Hello world

d)

Hello World

36.

Choose correct purpose of indexOf() in String class.

a)

returns the index (the position) of the last occurrence of a specified text in a string

b)

returns the character of the first occurrence of a specified character in a string

c)

returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)

d)

returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)

37.

String x = "10";

String y = "20";

String z = x + y;

System.out.println(z);

Guess the output.

a)

1020

b)

10 20

c)

30

d)

"10 20"

38.

What will be the output of below code?


String statement = "Outfit of the day";

System.out.println(statement.Substring(3,6));

a)

it of

b)

tfit

c)

fit

d)

FIT

39.

Choose most appropriate purpose of trim() method in String?

a)

to remove white spaces

b)

to get a substring of the string

c)

to cut String at desired index

d)

to remove extra white spaces from start and end of String

40.

What is the return type of equalsIgnorecase() method?

a)

int

b)

String

c)

char

d)

boolean

41.

What is the most appropriate way to check if two Strings are identical?

a)

string1 == string2

b)

string1.equals(string2)

c)

string1.same(string2)

d)

string1.equalsIgnorecase(string2)

42.

(Multiple answer question)

Pick all NON-primitive datatypes from below :

a)

String

b)

int

c)

primitive

d)

Array

43.

What is the correct relation between length and last-index of array?

a)

last-index is always 1 more than the length

b)

last-index is 1 less than the length (but not always)

c)

last-index is always 1 less than the length

d)

last-index is always equal to the length

44.
String word = "A few good men";
What is word.length()?
a)
14
b)
15
c)
13
45.
String word = "A few good men";
What is word.charAt(7) ?
a)
o
b)
g
c)
7
d)
good men
46.
String word = "A few good men";
What is word.startsWith("A few") ?
a)
true
b)
false
47.
String word = "A few good men";
What is word.endsWith("man") ?
a)
true
b)
false
48.

String word = "A few good men";

What is word.indexOf(“f”)?

a)

2

b)

0

c)

1

d)

3

49.
String word = "A few good men";
What is word.substring(3, 7) ?
a)
ew g
b)
ew go
c)
few
d)
few[space]