Java Flashcards

1
Q

What is the main purpose of Java’s garbage collector?

A

To automatically manage memory by reclaiming memory used by objects that are no longer referenced.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: Java supports multiple inheritance through classes.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What keyword is used to inherit a class in Java?

A

extends

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Fill in the blank: In Java, the _______ keyword is used to declare a constant.

A

final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the default value of a boolean variable in Java?

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does JVM stand for?

A

Java Virtual Machine

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False: An interface can contain method implementations in Java.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of the ‘synchronized’ keyword in Java?

A

To control access to a method or block by multiple threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the output of the following code: System.out.println(1 + 1 + ‘1’);? (A) 21 (B) 11 (C) 2

A

21

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which collection class allows duplicate elements in Java?

A

ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the primary function of the ‘try-catch’ block?

A

To handle exceptions that may occur during program execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Fill in the blank: A _______ is a blueprint for creating objects in Java.

A

class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is method overloading?

A

Defining multiple methods with the same name but different parameters in the same class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

True or False: Java provides a mechanism called ‘autoboxing’ for converting primitive types to their corresponding wrapper classes.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the ‘this’ keyword in Java?

A

To refer to the current object instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which of the following is NOT a primitive data type in Java? (A) int (B) String (C) boolean

A

B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does the ‘transient’ keyword indicate in Java?

A

That a field should not be serialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a ‘static’ method?

A

A method that belongs to the class rather than any specific instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Fill in the blank: Java uses _______ to achieve platform independence.

A

bytecode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the difference between ‘== ‘ and ‘.equals()’ in Java?

A

’==’ checks reference equality, while ‘.equals()’ checks for value equality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

True or False: A Java interface can extend multiple other interfaces.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the purpose of the ‘finalize()’ method in Java?

A

To perform cleanup before an object is garbage collected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the output of the following code: System.out.println(5 / 2);?

A

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Which exception is thrown when a program attempts to access an array with an illegal index?

A

ArrayIndexOutOfBoundsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is a ‘constructor’ in Java?

A

A special method used to initialize objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Fill in the blank: The _______ block is used to execute code regardless of whether an exception is thrown or not.

A

finally

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What does the ‘volatile’ keyword do in Java?

A

It indicates that a variable may be changed by different threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

True or False: Java supports operator overloading.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the difference between ‘List’ and ‘Set’ in Java Collections Framework?

A

‘List’ allows duplicates and maintains order, while ‘Set’ does not allow duplicates and does not guarantee order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the purpose of the ‘instanceof’ operator?

A

To test whether an object is an instance of a specific class or interface.

32
Q

What is the default access modifier for class members in Java?

A

Package-private

33
Q

Which keyword is used to create an anonymous inner class in Java?

A

new

34
Q

Fill in the blank: The Java Development Kit (JDK) includes a _______ for compiling Java source code.

A

compiler

35
Q

What is a ‘Java Bean’?

A

A reusable software component that follows specific conventions for property management.

36
Q

True or False: Java supports multiple inheritance through interfaces.

A

True

37
Q

What is the purpose of the ‘super’ keyword?

A

To refer to the superclass of the current object.

38
Q

Which of the following is a feature of Java? (A) Platform independence (B) Automatic memory management (C) Both A and B

A

C

39
Q

What does ‘JAR’ stand for in Java?

A

Java Archive

40
Q

What is the significance of the ‘main’ method in Java?

A

It serves as the entry point for any Java application.

41
Q

Fill in the blank: The _______ package contains the Java Collections Framework.

A

java.util

42
Q

What is the purpose of ‘Java Reflection’?

A

To inspect classes, interfaces, fields, and methods at runtime without knowing the names of the classes.

43
Q

True or False: The ‘String’ class in Java is mutable.

A

False

44
Q

What is the role of a ‘Comparator’ in Java?

A

To define a custom sorting order for objects.

45
Q

What is the purpose of the ‘assert’ statement in Java?

A

To provide a means for debugging by testing assumptions in the code.

46
Q

What is a ‘static block’ in Java?

A

A block of code that runs when the class is loaded into memory.

47
Q

Fill in the blank: Java uses _______ to manage access control for classes and members.

A

access modifiers

48
Q

What is the primary use of ‘Java Annotations’?

A

To provide metadata about a program element.

49
Q

What is a ‘Checked Exception’?

A

An exception that must be either caught or declared in the method signature.

50
Q

True or False: The ‘StringBuilder’ class is synchronized.

A

False

51
Q

What is the purpose of the ‘volatile’ keyword?

A

To ensure visibility of changes to variables across threads.

52
Q

Which Java keyword is used to create a new thread?

A

Thread

53
Q

Fill in the blank: A _______ is used to handle a group of related exceptions in Java.

A

catch block

54
Q

What is the difference between ‘HashMap’ and ‘TreeMap’?

A

‘HashMap’ is unordered and allows null keys, while ‘TreeMap’ is ordered and does not allow null keys.

55
Q

What does ‘JVM’ do when it encounters a ‘ClassNotFoundException’?

A

It indicates that the Java Virtual Machine cannot find a class that is being referenced.

56
Q

What is the output of the expression ‘10 % 3’ in Java?

A

1

57
Q

Fill in the blank: The _______ interface is used to implement a callback mechanism in Java.

A

Runnable

58
Q

What is the purpose of the ‘default’ keyword in interfaces?

A

To provide a default implementation for a method in an interface.

59
Q

True or False: The ‘main’ method must always be declared as public.

A

True

60
Q

What is the function of the ‘Thread.sleep()’ method?

A

To pause the execution of the current thread for a specified time.

61
Q

What does the ‘final’ keyword indicate when used with a class?

A

That the class cannot be subclassed.

62
Q

Fill in the blank: An _______ is a template for creating objects and contains methods and variables.

A

object

63
Q

What is the role of ‘JavaFX’?

A

To provide a platform for creating rich internet applications.

64
Q

What is the purpose of the ‘volatile’ keyword in concurrent programming?

A

To ensure that updates to a variable are visible to other threads.

65
Q

What happens when you try to access an index of an array that is out of bounds?

A

ArrayIndexOutOfBoundsException is thrown.

66
Q

Fill in the blank: Java uses _______ to manage the lifecycle of an application.

A

threads

67
Q

What is the difference between ‘abstract class’ and ‘interface’?

A

‘Abstract class’ can have both abstract and concrete methods, while ‘interface’ can only have abstract methods (until Java 8).

68
Q

What is ‘Java Streams’?

A

A feature that allows functional-style operations on collections of objects.

69
Q

True or False: A constructor can have a return type.

A

False

70
Q

What is the role of the ‘java.lang’ package?

A

It contains fundamental classes that are essential for Java programming.

71
Q

What is the output of the following code: System.out.println(3 + 4 + ‘7’);?

A

74

72
Q

Fill in the blank: The _______ method is called when an exception is not caught.

A

printStackTrace

73
Q

What is the main purpose of the ‘synchronized’ keyword?

A

To prevent thread interference by allowing only one thread to access a block of code at a time.

74
Q

What does the term ‘boxing’ refer to in Java?

A

The conversion of a primitive type to its corresponding wrapper class.

75
Q

True or False: A synchronized block is less flexible than synchronized methods.

A

True