Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Mastering Java Concepts

Total questions: 30

Worksheet time: 30mins

Name
Class
Date
1.

What are the four types of inheritance in Java?

a)

Static Inheritance

b)

Single Inheritance, Multilevel Inheritance, Hierarchical Inheritance, Multiple Inheritance (via interfaces)

c)

Dynamic Inheritance

d)

Circular Inheritance

2.

Explain the difference between an interface and an abstract class.

a)

An abstract class is a contract with no implementation, while an interface can have some implemented methods.

b)

An interface can have implemented methods, while an abstract class cannot.

c)

An interface is a contract with no implementation, while an abstract class can have some implemented methods and state.

d)

An interface can contain state, while an abstract class cannot.

3.

Which method is used to compare two strings for equality in Java?

a)

isEqual()

b)

stringCompare()

c)

compareTo()

d)

equals()

4.

What is the purpose of the 'throws' keyword in Java?

a)

To declare that a method can throw exceptions.

b)

To indicate a method's return type.

c)

To specify the method's access level.

d)

To define a method's parameters.

5.

How do you create a custom exception in Java?

a)

public class MyCustomException extends Exception { public MyCustomException(String message) { super(message); } public MyCustomException(String message, Throwable cause) { super(message, cause); }}

b)

public class MyCustomException extends Exception { public MyCustomException(String message) { } }

c)

public class MyCustomException { public MyCustomException() { } }

d)

public class MyCustomException extends RuntimeException { }

6.

What is single inheritance in Java?

a)

Single inheritance in Java allows a class to inherit from multiple superclasses.

b)

Single inheritance allows multiple classes to inherit from one superclass.

c)

Single inheritance in Java allows a class to inherit from one superclass only.

d)

Single inheritance means a class cannot inherit any properties from a superclass.

7.

Can a class implement multiple interfaces? If yes, how?

a)

No, interfaces cannot be implemented by classes.

b)

Yes, a class can implement multiple interfaces.

c)

Yes, but only if the interfaces are related.

d)

No, a class can only implement one interface.

8.

What method would you use to convert a string to an integer in Java?

a)

Integer.valueOfString()

b)

convertStringToInt()

c)

Integer.parseInt()

d)

String.toInteger()

9.

What is the significance of the 'finally' block in exception handling?

a)

The 'finally' block can only be used with a try block that has no catch.

b)

The 'finally' block ensures that code runs after try/catch, regardless of exceptions.

c)

The 'finally' block is optional and does not affect program execution.

d)

The 'finally' block is used to catch exceptions before they propagate.

10.

How do you handle multiple exceptions in a single catch block?

a)

Ignore exceptions and let them propagate.

b)

Use multiple catch blocks for each exception type.

c)

Use a single catch block with multiple exception types separated by a pipe (|).

d)

Only catch the most specific exception type.

11.

What is the difference between String, StringBuilder, and StringBuffer?

a)

String is synchronized; StringBuilder is immutable; StringBuffer is mutable.

b)

String is mutable; StringBuilder is immutable; StringBuffer is synchronized.

c)

String is mutable; StringBuilder is synchronized; StringBuffer is immutable.

d)

String is immutable; StringBuilder is mutable and not synchronized; StringBuffer is mutable and synchronized.

12.

What is the purpose of the 'try-catch' block in Java?

a)

To optimize memory usage.

b)

To handle exceptions and prevent program crashes.

c)

To improve code readability.

d)

To define variables in a method.

13.

How can you create an instance of a custom exception?

a)

raise Exception('MyCustomException')

b)

class MyException(Exception): pass

c)

class MyCustomException(Exception): pass raise MyCustomException('An error occurred')

d)

throw MyCustomException('Error')

14.

What is the role of the 'super' keyword in inheritance?

a)

The 'super' keyword is used to create new classes.

b)

The 'super' keyword prevents access to the parent class's methods.

c)

The 'super' keyword allows access to the parent class's methods and properties in inheritance.

d)

The 'super' keyword is only applicable in static methods.

15.

What method would you use to find the length of a string in Java?

a)

Use the size() method of the String class.

b)

Call the getLength() function on the string.

c)

Use the count() method of the String class.

d)

Use the length() method of the String class.

16.

What is the difference between checked and unchecked exceptions?

a)

Unchecked exceptions require explicit handling; checked exceptions do not.

b)

Checked exceptions require handling; unchecked exceptions do not.

c)

Checked exceptions are always fatal; unchecked exceptions are not.

d)

Both checked and unchecked exceptions must be handled in the same way.

17.

How do you throw an exception manually in Java?

a)

raise new Exception("Your error message");

b)

throw new Error("Your error message");

c)

throw new Exception("Your error message");

d)

throw Exception("Your error message");

18.

What is method overriding in Java inheritance?

a)

Method overriding is used to define static methods in subclasses.

b)

Method overriding is a way to create new methods in Java.

c)

Method overriding in Java inheritance allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

d)

Method overriding allows a superclass to change its own methods.

19.

How can you ensure that a resource is closed after use in Java?

a)

Ignore closing the resource if it's not needed.

b)

Use a finalizer method to close the resource.

c)

Use try-with-resources statement.

d)

Manually call the close() method after use.

20.

What is the use of the 'instanceof' operator in Java?

a)

The 'instanceof' operator is used to create new instances of a class.

b)

The 'instanceof' operator checks the type of a variable at compile time.

c)

The 'instanceof' operator is used to compare two primitive data types.

d)

The 'instanceof' operator checks if an object is an instance of a specified class or subclass.

21.

What is the purpose of the 'static' keyword in Java?

a)

The 'static' keyword restricts access to a method or variable to only within the class.

b)

The 'static' keyword is used to create abstract classes.

c)

The 'static' keyword allows a method or variable to be accessed without creating an instance of the class.

d)

The 'static' keyword is used to define instance variables.

22.

What is polymorphism in Java?

a)

Polymorphism is a way to restrict method access in subclasses.

b)

Polymorphism allows a single method to operate on different types of objects.

c)

Polymorphism allows methods to be defined in multiple classes.

d)

Polymorphism is the ability to create multiple instances of a class.

23.

class A {

void show() { System.out.println("Class A"); }

}

class B extends A {

void show() { System.out.println("Class B"); }

}

public class Test {

public static void main(String[] args) {

A obj = new B();

obj.show();

}

}

a)

Class A

b)

Class B

c)

Compilation Error

d)

Runtime Error

24.

class Parent {

int x = 10;

}

class Child extends Parent {

int x = 20;

}

public class Test {

public static void main(String[] args) {

Child c = new Child();

System.out.println(c.x);

}

}

a)

10

b)

20

c)

Compilation Error

d)

Garbage Value

25.

interface Animal {

void sound();

}

class Dog implements Animal {

public void sound() { System.out.println("Bark"); }

}

public class Test {

public static void main(String[] args) {

Animal a = new Dog();

a.sound();

}

}

a)

Bark

b)

sound

c)

Compilation Error

d)

Nothing

26.

interface A {

int VALUE = 10;

}

class B implements A {

public static void main(String[] args) {

System.out.println(VALUE);

}

}

a)

0

b)

10

c)

Compilation Error

d)

null

27.

public class Test {

public static void main(String[] args) {

int arr[] = {10, 20, 30};

System.out.println(arr[1]);

}

}

Output?

a)

10

b)

20

c)

30

d)

ArrayIndexOutOfBoundsException

28.

public class Test {

public static void main(String[] args) {

String s1 = "Hello";

String s2 = new String("Hello");

System.out.println(s1 == s2);

}

}

Output?

a)

true

b)

false

c)

Hello

d)

Compilation Error

29.

public class Test {

public static void main(String[] args) {

try {

int a = 5 / 0;

} catch (ArithmeticException e) {

System.out.println("Error");

}

}

}

a)

5/0

b)

Error

c)

Compilation Error

d)

Runtime Error

30.

public class Test {

public static void main(String[] args) {

String s = "JavaProgramming";

System.out.println(s.substring(4, 11));

}

}

Output?

a)

Java

b)

Progra

c)

ogrammi

d)

Program