Core Java Concepts
-
What is Java?
-
Java is a high-level, object-oriented programming language known for its platform independence, security, and versatility. It's widely used for developing various applications, including web applications, desktop applications, and mobile apps.
-
-
Explain the difference between JDK, JRE, and JVM.
-
JDK (Java Development Kit): Contains everything you need to develop, compile, and run Java programs, including the JRE, compiler, debugger, and other tools.
-
JRE (Java Runtime Environment): Contains the JVM and the class libraries necessary to run Java programs.
-
JVM (Java Virtual Machine): The software that interprets and executes Java bytecode, making Java platform-independent.
-
-
What is the difference between a class and an object in Java?
-
A class is a blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have.
-
An object is an instance of a class. It represents a real-world entity and has its own values for the properties defined in the class.
-
-
What is inheritance in Java?
-
Inheritance is a mechanism in Java that allows a class (subclass) to inherit properties and methods from another class (superclass). This promotes code reusability and creates a hierarchical relationship between classes.
-
-
What are the different types of inheritance in Java?
-
Single inheritance: A class can inherit from only one superclass.
-
Multiple inheritance: A class can inherit from multiple superclasses (not directly supported in Java, but can be achieved using interfaces).
-
Hierarchical inheritance: Multiple subclasses can inherit from the same superclass.
-
Multilevel inheritance: A subclass can inherit from a subclass that inherits from a superclass.
-
-
What is polymorphism in Java?
-
Polymorphism means "many forms." In Java, it allows objects of different classes to be treated as if they were of the same type. This can be achieved through method overriding and method overloading.
-
-
What is method overriding in Java?
-
Method overriding occurs when a subclass provides its own implementation of a method that is declared in its superclass. The method signature (name, parameters) must be the same.
-
-
What is method overloading in Java?
-
Method overloading occurs when a class has multiple methods with the same name but different parameters. The compiler determines which method to call based on the arguments passed.
-
-
What is a constructor in Java?
-
A constructor is a special method that is automatically called when an object is created. It initializes the object's properties.
-
-
What are the different types of constructors in Java?
-
Default constructor: A constructor with no parameters.
-
Parameterized constructor: A constructor with parameters.
-
Copy constructor: A constructor that creates a new object as a copy of an existing object.
Data Types and Variables
-
What are the different data types in Java?
-
Primitive data types:
byte,short,int,long,float,double,char,boolean -
Reference data types: Classes, interfaces, arrays
-
What is the difference between a primitive data type and a reference data type?
-
Primitive data types: Store the actual values.
-
Reference data types: Store references to objects.
-
What is automatic type conversion in Java?
-
Automatic type conversion occurs when a smaller data type is automatically converted to a larger data type to prevent data loss.
-
What is type casting in Java?
-
Type casting is the process of explicitly converting a value from one data type to another.
-
What is the difference between
==andequals()in Java?
-
== compares the references of two objects.
-
equals()` compares the contents of two objects.
Control Flow
-
What are the different control flow statements in Java?
-
Conditional statements:
if,else,if-else,switch -
Looping statements:
for,while,do-while
-
What is a break statement in Java?
-
A
breakstatement is used to exit a loop or aswitchstatement.
-
What is a continue statement in Java?
-
A
continuestatement is used to skip the current iteration of a loop and proceed to the next iteration.
Arrays and Collections
-
What is an array in Java?
-
An array is a collection of elements of the same data type.
-
What are the different types of arrays in Java?
-
Single-dimensional array
-
Multi-dimensional array
-
What is a collection framework in Java?
-
The collection framework provides a set of interfaces and classes to represent and manipulate collections of objects.
-
What are the different types of collections in Java?
-
List: Ordered collection of elements that allow duplicates.
-
Set: Unordered collection of elements that do not allow duplicates.
-
Map: Associates keys with values.
Exception Handling
-
What is an exception in Java?
-
An exception is an unexpected event that occurs during program execution.
-
What is exception handling in Java?
-
Exception handling is the process of managing exceptions in a Java program using
try,catch,finally, andthrowkeywords.
-
What is a
try-catch-finallyblock in Java?
-
A
try-catch-finallyblock is used to handle exceptions in Java. -
The
tryblock contains the code that might throw an exception. -
The
catchblock handles the exception if it occurs. -
The
finallyblock is always executed, regardless of whether an exception occurs.
Multithreading
-
What is multithreading in Java?
-
Multithreading is the ability of a program to execute multiple tasks concurrently.
-
How do you create a thread in Java?
-
You can create a thread by extending the
Threadclass or implementing theRunnableinterface.
-
What is the difference between
start()andrun()methods in Java?
-
start() starts a new thread and calls the
run()method. -
run()` is a method that contains the code that the thread will execute.
-
What is thread synchronization in Java?
-
Thread synchronization is used to control access to shared resources between multiple threads.
-
What is a deadlock in Java?
-
A deadlock occurs when two or more threads are waiting for each other to release a resource, resulting in a situation where no thread can proceed.
Input/Output (I/O)
-
What is the difference between stream-based I/O and file-based I/O in Java?
-
Stream-based I/O: Reads and writes data in a sequential manner using streams.
-
File-based I/O: Reads and writes data to and from files directly.
-
What are the different types of streams in Java?
-
Byte streams: Read and write bytes of data.
-
Character streams: Read and write characters of data.
Generics
-
What are generics in Java?
-
Generics allow you to create type-safe collections and methods that can work with different data types.
-
What is type erasure in Java?
-
Type erasure is the process of removing type information from generic types at compile time.
Java 8 Features
-
What is a lambda expression in Java?
-
A lambda expression is an anonymous function that can be passed around like an object.
-
What is method referencing in Java?
-
Method referencing is a concise way to refer to a method without explicitly writing a lambda expression.
-
What is the
Optionalclass in Java?
-
The
Optionalclass represents a value that may or may not be present.
-
What is a stream in Java?
-
A stream is a sequence of elements that can be processed in parallel or sequentially.
-
What is the difference between
forEach()andmap()methods in Java streams?
-
forEach() performs an action on each element of the stream.
-
map() transforms each element of the stream into a new element.
Advanced Topics
-
What is reflection in Java?
-
Reflection allows you to inspect and modify the behavior of classes and objects at runtime.
-
What is serialization in Java?
-
Serialization is the process of converting an object into a sequence of bytes that can be stored or transmitted.