Java Flashcards

1
Q

What is JDK?

A

JDK stands for Java Development Kit. It is a software development kit required to develop applications in Java and includes JRE and development tools.

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

What is JRE?

A

JRE stands for Java Runtime Environment. It provides the libraries, Java Virtual Machine (JVM), and other components to run applications written in Java.

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

What is JVM?

A

JVM stands for Java Virtual Machine. It is an abstract machine that provides a runtime environment in which Java bytecode can be executed.

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

Explain public static void main(String[] args) in Java.

A

This is the main method in Java which is the entry point of any Java application.

public: The method is accessible from anywhere.
static: It can be called without creating an instance of the class.
void: The method does not return any value.
main: The name of the method which is searched by JVM as the starting point of the program.
String[] args: Parameter passed to the main method.

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

Why is Java platform-independent?

A

Java is platform-independent because of its bytecode which can run on any system equipped with a JVM, irrespective of the underlying operating system.

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

Why is Java not 100% Object-oriented?

A

Java is not 100% Object-oriented because it uses eight primitive data types (boolean, byte, char, short, int, float, double, long) that are not objects.

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

What are wrapper classes in Java?

A

Wrapper classes convert primitive data types into objects. Examples include Integer for int, Character for char, and Double for double.

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

What are constructors in Java?

A

Constructors are blocks of code used to initialize objects. They have the same name as the class and do not have a return type.

Default Constructor: No arguments and initializes instance variables with default values.

Parameterized Constructor: Takes arguments and initializes instance variables with provided values.

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

What is a singleton class in Java?

A

A singleton class is a class that can have only one instance at a time. It is created by making its constructor private.

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

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

A

equals(): Method to check equality of two objects based on their content.

== operator: Compares references, checking if both references point to the same object in memory.

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

When can you use the super keyword?

A

The super keyword in Java is used to refer to the immediate parent class object. It can be used to:

Call the parent class constructor.
Access the parent class methods.
Access the parent class variables.

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

What are the differences between HashMap and HashTable in Java?

A

HashMap: Not synchronized, permits one null key and multiple null values, faster, and traversed through an iterator.

HashTable: Synchronized, does not permit any null key or value, slower, and traversed through an enumerator and iterator.

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

Can you call a constructor of a class inside another constructor?

A

Yes, this is called constructor chaining. It can be done using this() for calling constructors within the same class and super() for calling the constructor from the parent class.

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

How is the creation of a String using new() different from that of a literal?

A

Using new(), a new String object is created in the heap. Using a string literal, it may return an existing object from the string pool if the string already exists.

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

What are the differences between Heap and Stack Memory in Java?

A

Stack Memory: Used for static memory allocation, holds local variables, and is LIFO (Last In First Out).

Heap Memory: Used for dynamic memory allocation, holds objects, and is accessible globally.

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

What is a package in Java?

A

A package is a collection of related classes and interfaces bundled together. It helps in modularizing the code, avoiding name conflicts, and controlling access.

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

What are access modifiers in Java?

A

Access modifiers control the visibility of classes, methods, and variables. Types include:

Default: Accessible within the same package.

Private: Accessible only within the declared class.

Protected: Accessible within the same package and subclasses.

Public: Accessible from any other class.

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

Define a Java Class.

A

A class in Java is a blueprint that includes fields (variables) and methods to describe the behavior and state of objects.

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

What is an object in Java and how is it created?

A

An object is an instance of a class having state and behavior. It is created using the new keyword, e.g., ClassName obj = new ClassName();.

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

What is Object-Oriented Programming (OOP)?

A

OOP is a programming paradigm organized around objects rather than actions, focusing on data rather than logic. It includes concepts like inheritance, encapsulation, abstraction, and polymorphism.

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

What is inheritance in Java?

A

Inheritance is a mechanism where one class acquires the properties and behaviors of another class. It promotes code reuse.

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

What is encapsulation in Java?

A

Encapsulation is the wrapping up of data and methods into a single unit called a class. It restricts direct access to some of an object’s components and can prevent the accidental modification of data.

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

What is abstraction in Java?

A

Abstraction is the concept of hiding the implementation details and showing only the functionality to the user. It is achieved using abstract classes and interfaces.

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

What is polymorphism in Java?

A

Polymorphism is the ability of an object to take on many forms. It allows one interface to be used for a general class of actions, typically achieved through method overriding and overloading.

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

What is the difference between a local variable and an instance variable?

A

Local Variable: Declared within a method, constructor, or block, and only accessible within it.

Instance Variable: Declared within a class but outside any method, constructor, or block, and accessible by all methods, constructors, and blocks in the class.

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

What is the final keyword in Java?

A

The final keyword can be used with variables, methods, and classes to restrict their modification.

final variable: Cannot be changed once assigned.

final method: Cannot be overridden by subclasses.

final class: Cannot be subclassed.

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

What is the difference between break and continue statements?

A

break: Terminates the loop or switch statement.

continue: Skips the current iteration of the loop and proceeds to the next iteration.

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

What is an infinite loop in Java?

A

An infinite loop is a loop that runs indefinitely because the loop condition always evaluates to true or the termination condition is never met.

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

What is the difference between this() and super() in Java?

A

this(): Calls the constructor of the same class.

super(): Calls the constructor of the parent class.

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

What is Java String Pool?

A

Java String Pool is a collection of strings stored in heap memory. When a new string is created, the pool checks if the string already exists and returns the reference if it does, otherwise, it creates a new string in the pool.

31
Q

What is constructor chaining in Java?

A

Constructor chaining is the process of calling one constructor from another within the same class or parent class using this() or super() respectively.

32
Q

What are the differences between String, StringBuilder, and StringBuffer?

A

String: Immutable and stored in the String pool.

StringBuilder: Mutable and not thread-safe, better performance for single-threaded operations.

StringBuffer: Mutable and thread-safe, used in multi-threaded environments.

33
Q

What is the difference between an array and an ArrayList in Java?

A

Array: Fixed size, can store primitive data types and objects.

ArrayList: Dynamic size, can only store objects, part of the Java Collections Framework.

34
Q

What is a Map in Java?

A

A Map is a collection that maps unique keys to values, cannot contain duplicate keys, and each key can map to only one value.

35
Q

What is the Java Collections Framework?

A

The Java Collections Framework is a set of classes and interfaces that implement commonly reusable collection data structures, such as lists, sets, queues, and maps.

36
Q

What is abstraction in Java?

A

Abstraction in Java is the process of hiding the implementation details from the user and revealing only the functionality. It can be achieved through abstract classes (0-100% abstraction) and interfaces (100% abstraction).

37
Q

What is an interface in Java?

A

An interface in Java is a blueprint of a class consisting of abstract methods and static constants. All methods in an interface are public and abstract, and it does not contain constructors.

38
Q

What is the difference between abstract classes and interfaces?

A

Abstract classes can provide default code and may have non-abstract methods, instance variables, and constructors. Interfaces can only have abstract methods and cannot contain instance variables or constructors. A class can implement multiple interfaces but can extend only one abstract class.

39
Q

What is inheritance in Java?

A

Inheritance in Java allows one class to inherit the properties of another, facilitating code reuse and relationship establishment between classes. It involves parent (super) and child (sub) classes.

40
Q

What are the types of inheritance in Java?

A

The types of inheritance in Java are Single, Multilevel, Hierarchical, and Hybrid inheritance.

41
Q

What is method overloading?

A

Method overloading occurs when multiple methods in the same class have the same name but different parameters.

42
Q

What is method overriding?

A

Method overriding occurs when a subclass provides a specific implementation for a method already defined in its superclass.

43
Q

What is the finalize() method in Java?

A

The finalize() method is invoked by the garbage collector before an object is destroyed to perform cleanup activities.

44
Q

What is serialization in Java?

A

Serialization is the process of converting an object into a byte stream, enabling the object to be saved to a file or transmitted over a network.

45
Q

What is a transient variable?

A

A transient variable is a variable that is not serialized during the serialization process.

46
Q

What does the synchronized keyword do?

A

The synchronized keyword ensures that a method or block of code can only be accessed by one thread at a time, providing thread safety.

47
Q

Describe the thread lifecycle in Java.

A

The thread lifecycle includes the states: New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated.

48
Q

What is garbage collection in Java?

A

Garbage collection is the process of automatically freeing memory by deleting objects that are no longer reachable in the heap.

49
Q

What are Java Generics?

A

Java Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces, and methods, providing stronger type checks at compile-time and eliminating the need for typecasting.

50
Q

What is the difference between checked and unchecked exceptions?

A

Checked exceptions are checked at compile-time, and must be declared or handled. Unchecked exceptions are checked at runtime and do not require explicit handling.

51
Q

What part of memory is cleaned in the garbage collection process?

A

The Heap memory is cleaned during the garbage collection process.

52
Q

What is the difference between constructors and methods in a class?

A

Constructors initialize the state of an object and do not have a return type. Methods perform specific operations and must have a return type.

53
Q

What happens if there are multiple main methods inside one class in Java?

A

The JVM only calls the main method with the signature public static void main(String[] args). Other main methods with different signatures are ignored.

54
Q

How do exceptions affect the program if they are not handled?

A

If exceptions are not handled, the program terminates abruptly and subsequent code does not execute.

55
Q

Is it mandatory for a catch block to follow a try block?

A

No, a catch block is not mandatory after a try block, but a finally block or another catch block should follow it.

56
Q

Can you call a constructor of a class inside another constructor?

A

Yes, constructors can call other constructors within the same class using this() and from the superclass using super().

57
Q

What is the difference between JDK, JRE, and JVM?

A

JDK (Java Development Kit) includes tools for developing Java applications. JRE (Java Runtime Environment) provides the libraries and JVM for running Java applications. JVM (Java Virtual Machine) executes Java bytecode.

58
Q

Can a constructor return a value?

A

A constructor implicitly returns the current instance of the class but cannot explicitly return a value.

59
Q

How many types of constructors are used in Java?

A

There are two types of constructors: Parameterized constructors and Default constructors.

60
Q

What is the difference between ‘throw’ and ‘throws’ keywords in Java?

A

The ‘throw’ keyword is used to explicitly throw an exception, while ‘throws’ is used in method signatures to declare that the method may throw exceptions.

61
Q

What is the final keyword in Java?

A

The final keyword can be used with variables to make them constants, with methods to prevent overriding, and with classes to prevent inheritance.

62
Q

What is an infinite loop in Java?

A

An infinite loop is a loop that runs indefinitely because its exit condition is never met.

63
Q

What is Java String Pool?

A

Java String Pool is a collection of Strings stored in heap memory. When a new String is created, the pool is checked first to see if the String already exists. If it does, the existing reference is returned.

64
Q

Differentiate between static and non-static methods.

A

Static methods belong to the class and can be called without creating an instance, whereas non-static methods belong to instances of the class.

65
Q

What is constructor chaining?

A

Constructor chaining is the process of calling one constructor from another within the same class or a superclass using this() or super().

66
Q

Why are Java Strings immutable?

A

Java Strings are immutable to enhance security, synchronization, performance, and because they are cached in the String pool.

67
Q

What is the difference between an array and an ArrayList in Java?

A

An array has a fixed size, while an ArrayList can dynamically resize. An array can hold both primitives and objects, while an ArrayList can only hold objects.

68
Q

What are access modifiers in Java?

A

Access modifiers in Java are keywords that set the accessibility of classes, methods, and other members. They include Default, Private, Protected, and Public.

69
Q

What is the difference between this() and super() in Java?

A

this() calls a constructor from the same class, while super() calls a constructor from the superclass.

70
Q

What is the difference between HashMap and Hashtable?

A

HashMap is non-synchronized and allows one null key and multiple null values. Hashtable is synchronized, thread-safe, and does not allow null keys or values.

71
Q

What is the difference between Heap and Stack Memory in Java?

A

Stack memory is used for static memory allocation and is thread-specific, while Heap memory is used for dynamic memory allocation and is accessible by all threads.

72
Q

What is a package in Java?

A

A package is a namespace that organizes a set of related classes and interfaces, helping to avoid name conflicts and control access.

73
Q

What is the purpose of the synchronized keyword?

A

The synchronized keyword ensures that a method or block of code is accessed by only one thread at a time, providing thread safety.

74
Q

What is the difference between throw and throws in Java?

A

The throw keyword is used to explicitly throw an exception, while the throws keyword is used to declare exceptions that a method might throw.