Java Flashcards
What is the main purpose of Java’s garbage collector?
To automatically manage memory by reclaiming memory used by objects that are no longer referenced.
True or False: Java supports multiple inheritance through classes.
False
What keyword is used to inherit a class in Java?
extends
Fill in the blank: In Java, the _______ keyword is used to declare a constant.
final
Which of the following is a valid way to declare an array in Java? (A) int arr[]; (B) int[] arr; (C) Both A and B
C
What is the default value of a boolean variable in Java?
false
What does JVM stand for?
Java Virtual Machine
True or False: An interface can contain method implementations in Java.
False
What is the purpose of the ‘synchronized’ keyword in Java?
To control access to a method or block by multiple threads.
What is the output of the following code: System.out.println(1 + 1 + ‘1’);? (A) 21 (B) 11 (C) 2
21
Which collection class allows duplicate elements in Java?
ArrayList
What is the primary function of the ‘try-catch’ block?
To handle exceptions that may occur during program execution.
Fill in the blank: A _______ is a blueprint for creating objects in Java.
class
What is method overloading?
Defining multiple methods with the same name but different parameters in the same class.
True or False: Java provides a mechanism called ‘autoboxing’ for converting primitive types to their corresponding wrapper classes.
True
What is the purpose of the ‘this’ keyword in Java?
To refer to the current object instance.
Which of the following is NOT a primitive data type in Java? (A) int (B) String (C) boolean
B
What does the ‘transient’ keyword indicate in Java?
That a field should not be serialized.
What is a ‘static’ method?
A method that belongs to the class rather than any specific instance.
Fill in the blank: Java uses _______ to achieve platform independence.
bytecode
What is the difference between ‘== ‘ and ‘.equals()’ in Java?
’==’ checks reference equality, while ‘.equals()’ checks for value equality.
True or False: A Java interface can extend multiple other interfaces.
True
What is the purpose of the ‘finalize()’ method in Java?
To perform cleanup before an object is garbage collected.
What is the output of the following code: System.out.println(5 / 2);?
2
Which exception is thrown when a program attempts to access an array with an illegal index?
ArrayIndexOutOfBoundsException
What is a ‘constructor’ in Java?
A special method used to initialize objects.
Fill in the blank: The _______ block is used to execute code regardless of whether an exception is thrown or not.
finally
What does the ‘volatile’ keyword do in Java?
It indicates that a variable may be changed by different threads.
True or False: Java supports operator overloading.
False
What is the difference between ‘List’ and ‘Set’ in Java Collections Framework?
‘List’ allows duplicates and maintains order, while ‘Set’ does not allow duplicates and does not guarantee order.