wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

java

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

3.When executing the expression int b = a++; with int a = 5;, what are the values of a and b after this statement?

a)

A. a is 6 and b is 5.

b)

B. a is 6 and b is 6.

c)

C. a is 5 and b is 6.

d)

D. a is 5 and b is 5.

2.

2.What is the fundamental difference between JDK and JRE?

a)

A. JDK is for running Java programs, while JRE is for developing them.

b)

B. JRE is used to compile code, while JDK is used to interpret bytecode.

c)

C. JRE contains the JVM and libraries, while JDK contains JRE and development tools.

d)

D. JDK and JRE are two different names for the same set of tools.

3.

4.In Java, which of the following variables is correctly declared as a constant?

a)

A. static float PI = 3.14f;

b)

B. const float PI = 3.14f;

c)

C. final float PI = 3.14f;

d)

D. static final float PI = 3.14f;

4.

5.Which characteristic is NOT true for ArrayList in Java?

a)

A. Its size can change dynamically.

b)

B. It allows fast element access via index.

c)

C. It allows storing duplicate elements.

d)

D. It can store both primitive data types and objects.

5.

6.Why is the String class in Java called "immutable"?

a)

A. Because it can only store characters in the ASCII table.

b)

B. Because all of its methods are synchronized.

c)

C. Because once a String object is created, its content (value) cannot be changed.

d)

D. Because it cannot be inherited by other classes.

6.

7.What is the main difference between StringBuilder and StringBuffer?

a)

A. StringBuilder is immutable, while StringBuffer is mutable.

b)

B. StringBuilder belongs to java.util package, while StringBuffer belongs to java.lang.

c)

C. StringBuffer provides more string manipulation methods than StringBuilder.

d)

D. StringBuilder is faster but not thread-safe, whereas StringBuffer is the opposite.

7.

8.How do the == operator and the .equals() method differ when comparing two String objects?

a)

A. .equals() can only be used for strings created from String literals, while == is for String objects created with new.

b)

B. == compares whether two variables point to the same object in memory, while .equals() compares the character content of the string.

c)

C. == compares the value of the string, while .equals() compares the memory reference.

d)

D. They are completely identical and can be used interchangeably.

8.

9.What is the purpose of the finally block in a try-catch-finally structure?

a)

A. It only executes when an exception is thrown in the try block.

b)

B. It is always executed after the try block (and catch if present), regardless of whether an exception occurs or not.

c)

C. It only executes when no exception occurs.

d)

D. It is used to throw a new exception after handling the original one.

9.

10.What is the difference between an Error and a Checked Exception in Java?

a)

A. Checked Exception must be declared with the throws keyword, while Error does not.

b)

B. Both are the same, but Error is used in system classes and Checked Exception in application code.

c)

C. Error represents serious system problems that the application should not try to handle, while Checked Exception represents foreseeable error conditions that must be handled.

d)

D. Error is a syntax error detected at compile time, while Checked Exception is a logic error at runtime.

10.

11.In Java naming conventions, which style is recommended for variable names?

a)

A. kebab-case, e.g., number-of-students

b)

B. camelCase, e.g., numberOfStudents

c)

C. PascalCase, e.g., NumberOfStudents

d)

D. snake_case, e.g., number_of_students

11.

12.What distinguishes a static variable from an instance (non-static) variable?

a)

A. A static variable is shared among all objects of a class, whereas each object has its own copy of an instance variable.

b)

B. A static variable can only be accessed inside static methods.

c)

C. A static variable is stored on the Stack, while an instance variable is stored on the Heap.

d)

D. The value of a static variable cannot be changed after initialization, while an instance variable can.

12.

13.When is a static block in a Java class executed?

a)

A. Only once, when the class is first loaded into the JVM.

b)

B. Every time a new object of that class is created.

c)

C. After the class's constructor has finished executing.

d)

D. Only when the main method of that class is called.

13.

1.In Java, what is the main role of the Java Virtual Machine (JVM)?

a)

A. It only includes development tools like the compiler and debugger.

b)

B. It compiles .java source code directly into machine code.

c)

C. It provides an environment to execute bytecode and converts it into machine code.

d)

D. It manages Java class libraries and provides them to the compiler.

14.

14.Java is said to be "pass-by-value". What does this mean when passing an object into a method?

a)

A. The method can change the external reference variable to point to a different object.

b)

B. The value of the reference variable (memory address) is copied and passed into the method.

c)

C. A complete copy of the object (deep copy) is created and passed into the method.

d)

D. Any changes to the object's properties inside the method will not affect the original object.

15.

15.What is the fundamental difference between an interface and an abstract class in Java (before Java 8)?

a)

A. A class can inherit multiple abstract classes but can only implement one interface.

b)

B. interface is used to define "IS-A" relationships, while abstract class is for "HAS-A" relationships.

c)

C. All variables in an abstract class are public static final by default.

d)

D. abstract class can have both abstract and concrete methods, while interface can only have abstract methods.

16.

16.When should LinkedList be used instead of ArrayList?

a)

A. When maximum memory saving is desired for element storage.

b)

B. When a thread-safe data structure is needed by default.

c)

C. When the application involves many element insertion or deletion operations, especially at the beginning or middle of the list.

d)

D. When frequent and efficient random access to elements is required.

17.

17.In the conditional expression (a > b) && (c < d), if (a > b) is false, what happens next?

a)

A. The program will wait to evaluate (c < d) in another thread.

b)

B. The expression (c < d) is not evaluated, and the entire expression returns false.

c)

C. The expression (c < d) is still evaluated to determine the final result.

d)

D. A RuntimeException will be thrown.

18.

18.What is the purpose of the hashCode() method in Java?

a)

A. To provide a globally unique identifier for every object.

b)

B. To encrypt object data for security purposes.

c)

C. To compare two objects to see if they are logically equal.

d)

D. To effectively support storing and retrieving objects in hash-based data structures like HashMap and HashSet.

19.

19.What is the main difference between a Set and a List in the Java Collections Framework?

a)

A. Set does not allow duplicate elements and usually does not guarantee order, while List allows duplicates and maintains insertion order.

b)

B. Set can only store String objects, while List can store any type of object.

c)

C. List does not allow duplicate elements, while Set does.

d)

D. All methods in Set are synchronized, while in List they are not.

20.

20.Polymorphism in object-oriented programming is demonstrated through which mechanism in Java?

a)

A. Method Overloading and Method Overriding.

b)

B. Variable declaration and object initialization.

c)

C. Encapsulation and Abstraction.

d)

D. Using static and final keywords.

21.

21.What is the purpose of the finalize() method?

a)

A. It is called to initialize an object immediately after the constructor runs.

b)

B. This is a synonym for the finally block and is executed after try-catch.

c)

C. It is called by the JVM immediately before an object is cleaned up by the Garbage Collector.

d)

D. It is used to terminate a Java program safely.

22.

22.Which of the following conditions about Method Overriding is TRUE?

a)

A. private and final methods in the parent class can be overridden in the child class.

b)

B. The overriding method can throw any Checked Exception, even if the original method does not.

c)

C. The overriding method must have the same name, same return type, and same parameter list.

d)

D. The overriding method can have a more restrictive access modifier than the overridden method.

23.

23.What is the difference between creating a thread by extending the Thread class and implementing the Runnable interface?

a)

A. Extending Thread has higher performance than implementing Runnable.

b)

B. Implementing Runnable allows sharing the same Runnable object across multiple threads, whereas extending Thread creates a unique object for each thread.

c)

C. Only threads created by Runnable can be managed in a ThreadPool.

d)

D. Extending Thread allows multiple inheritance, while implementing Runnable does not.

24.

24.The ternary operator in Java is a shorthand for which structure?

a)

A. try-catch block

b)

B. switch-case structure

c)

C. for loop

d)

D. if-else structure

25.

25.Which of the following data types CANNOT be used in a switch statement in Java?

a)

A. enum

b)

B. int

c)

C. String

d)

D. double

26.

26.What is the difference between while and do-while loops?

a)

A. Only do-while can use break and continue statements.

b)

B. do-while always runs faster than while.

c)

C. while checks the condition at the start of the loop, whereas do-while checks it at the end.

d)

D. while loops can cause infinite loops, while do-while loops cannot.

27.

27.What is the purpose of calling System.gc()?

a)

A. To force the Garbage Collector to run immediately and clean up all unused objects.

b)

B. To give a hint to the JVM that this might be a good time to run the Garbage Collector.

c)

C. To check if there is a memory leak in the application.

d)

D. To release Stack memory currently being used by the current thread.

28.

28.In the array declaration syntax int[][] numbers = new int[3][5];, what is the value of numbers.length?

a)

A. 3

b)

B. 5

c)

C. 15

d)

D. It will cause a compilation error because the syntax is incorrect.

29.

29.What happens if you try to convert an array to a List using Arrays.asList() and then call add() or remove() on that list?

a)

A. The original array will automatically resize to reflect the change in the list.

b)

B. The operation will succeed and the list will be resized.

c)

C. An ArrayIndexOutOfBoundsException will be thrown.

d)

D. An UnsupportedOperationException will be thrown.

30.

30.What is Serialization in Java?

a)

A. Splitting a string into multiple parts based on a delimiter.

b)

B. Sorting elements of a collection in a specific order.

c)

C. Ensuring that only one thread can access an object at a time.

d)

D. Converting an object into a byte stream for storage or transmission.