Java Flashcards
What is JDK?
JDK stands for Java Development Kit. It is a software development kit required to develop applications in Java and includes JRE and development tools.
What is JRE?
JRE stands for Java Runtime Environment. It provides the libraries, Java Virtual Machine (JVM), and other components to run applications written in Java.
What is JVM?
JVM stands for Java Virtual Machine. It is an abstract machine that provides a runtime environment in which Java bytecode can be executed.
Explain public static void main(String[] args) in Java.
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.
Why is Java platform-independent?
Java is platform-independent because of its bytecode which can run on any system equipped with a JVM, irrespective of the underlying operating system.
Why is Java not 100% Object-oriented?
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.
What are wrapper classes in Java?
Wrapper classes convert primitive data types into objects. Examples include Integer for int, Character for char, and Double for double.
What are constructors in Java?
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.
What is a singleton class in Java?
A singleton class is a class that can have only one instance at a time. It is created by making its constructor private.
What is the difference between equals() and == in Java?
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.
When can you use the super keyword?
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.
What are the differences between HashMap and HashTable in Java?
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.
Can you call a constructor of a class inside another constructor?
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 is the creation of a String using new() different from that of a literal?
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.
What are the differences between Heap and Stack Memory in Java?
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.
What is a package in Java?
A package is a collection of related classes and interfaces bundled together. It helps in modularizing the code, avoiding name conflicts, and controlling access.
What are access modifiers in Java?
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.
Define a Java Class.
A class in Java is a blueprint that includes fields (variables) and methods to describe the behavior and state of objects.
What is an object in Java and how is it created?
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();.
What is Object-Oriented Programming (OOP)?
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.
What is inheritance in Java?
Inheritance is a mechanism where one class acquires the properties and behaviors of another class. It promotes code reuse.
What is encapsulation in Java?
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.
What is abstraction in Java?
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.
What is polymorphism in Java?
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.
What is the difference between a local variable and an instance variable?
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.
What is the final keyword in Java?
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.
What is the difference between break and continue statements?
break: Terminates the loop or switch statement.
continue: Skips the current iteration of the loop and proceeds to the next iteration.
What is an infinite loop in Java?
An infinite loop is a loop that runs indefinitely because the loop condition always evaluates to true or the termination condition is never met.
What is the difference between this() and super() in Java?
this(): Calls the constructor of the same class.
super(): Calls the constructor of the parent class.