Interview Prep Flashcards

1
Q

What is Java platform independence?

A

Java platform independence means that this programming language can run on different platforms or OS without any changes, which becomes possible with bytecode. The original code of Java is converted into bytecode to adapt it for a specific machine. Bytecode is transmitted to Java Virtual Machine (or simply JYM) that is located in the RAM of any OS. JYM detects the bytecodes and transforms them into the native machine code.

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

What is an RDBMS?

A

RDBMS stands for Relational Data Base Management System, which is based on the relational model of the database system. It includes numerous tables that have their own primary key and are used to store the data using columns and rows. Typical examples of the RDBMS are systems like SQL, MS SQL Server, IBM DB2, ORACLE, My-SQL, Microsoft Access, and others.

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

What are the basic access specifiers for Java classes?

A

Access specifiers (also known as access modifiers) are the specific keywords used before a class name to determine the access scope. They’re mostly used for defining the visibility of classes, variables, interfaces, constructors, methods, etc. In Java, there are 4 types of access modifiers:

Private - variables that can be accessed only from the same class they belong to;
Public - methods and variables that can be accessed by any other classes in the project;
Protected - variables that can be accessed within the same set of classes and subclasses of any other packages;
Default - methods and variables that can be accessed only from the same package (not from the native one).

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

What are loops in Java and what are the 3 types of loops?

A

Java loop repeats a sequence of instructions until a particular condition is met. There are three types of loops in Java: for, while, and do-while.

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

What is the difference between fail-fast and fail-safe iterators?

A

Fail-fast iterators perform directly on the collection, which means they fail as soon as the definition of the collection has been changed during iteration.

Unlike the previous ones, fail-safe iterators function directly on a collection’s copy, thus they do not throw any exception if the collection has been changed during iteration.

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

What is the MVC design pattern?

A

MVC (also known as Model-View-Controller) is an app design tool that divides the application into 3 parts:

Model - stands for the app data
View - data representation on a screen
Controller - operates the list of processes and responds to the actions, that sometimes can change the model

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

What is Core Java?

A

Core Java is a general term used by Sun Microsystems to define a basic edition of Java (J2SE), used for the other editions created. Core Java is mainly used for building desktop and server-based apps, mostly because of its benefits as the fast and a highly-secure scripting language.

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

What are the 4 types of Java platforms?

A

Being a particular environment for the Java programming language, there are 4 different platform types for Java:

Java SE or Java Standard Edition
Java EE or Java Enterprise Edition
Java ME or Java Micro Edition
Java FX

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

What is the difference between transient and volatile variables in Java?

A

The main difference between transient vs volatile variables is that transient variables are utilized to prevent serialization while volatile variables are utilized to provide alternative synchronization in Java.

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

What’s the difference between a ClassNotFoundException and NoClassDefFoundError?

A

ClassNotFoundException is an exception that occurs when the class file for a requested class can’t be found on the classpath.

NoClassDefFoundError is an error that occurs when the class file existed at runtime but can’t be turned into a Class definition for some reason.

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

What’s the difference between ‘sleep ( )’ and ‘wait ( )’ methods in Java?

A

‘sleep ( )’ is a blocking operation that keeps either hold on the monitor or the lock on the shared object for a set amount of milliseconds and is commonly used for polling or certain results tracking at a set time.

‘wait ( )’ pauses the thread for the set amount of time or enables it after receiving approval from the other thread without locking the shared object or holding on to the monitor.

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

When ‘finally’ block is executed in Java?

A

The ‘finally’ block is mostly applied for putting the important codes like the clean-up code, for example when the file or connection needs closing. The ‘finally’ block is executed when an exception is thrown from a ‘try’ block that does not have a ‘catch’ block. It is executed even in cases when an exception is thrown or propagated to the calling code block.

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

What is the Java ClassLoader?

A

ClassLoader is the “deepest” mechanism in Java, which allows you to intervene practically into the core of the Java Virtual Machine (JVM) while remaining within the framework of Java programming. ClassLoader provides loading the classes from the local file system, remote file system, or web.

There are 3 types of ClassLoader in Java:

Bootstrap ClassLoader: Responsible for loading standard Java API files rt.jar from folder
Extensions ClassLoader: Manages loading jar files from the folder.
System Class Loader: Operates loading jar files from the specific path in the CLASSPATH environment

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

Can you use == on enum?

A

Yes, as the enums represent a set of constants like unchangeable variables or final variables, which ensure that you’ll have only one case of the constants used in the JVM. They have close instance controls which allow using == for the instances comparison. Furthermore, the ‘==’ operator provides compile-time and run-time safety.

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

What is composition in Java?

A

Composition in Java stands for code reusing. Basically, the composition is used as a part-of or ‘has-a’ relationship that helps to ensure the code reusability in the program. In other words, that’s a technique that assists in describing the reference between two or more classes. The composition between two entities is marked as done when an object contains a composed object, and the last one can exist within another entity.

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

What is a static import?

A

A static import is a method used in the Java development process, which allows various members (like fields and methods), scoped within their container classes as public static, to be employed in Java code without indicating the exact class in which the field has been defined. This method enhances the code readability and gets direct access to the static members.

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

What is method overloading in Java?

A

Method overloading in Java stands for a feature that enables a class to have more than one method with the same name if their argument lists are different. It has some similar points to the constructor overloading. There are 3 different ways in which you can overload a method:

by number of parameters
by the data type of parameters
by the sequence of the data type of parameters

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

What is method overriding in Java?

A

Method overriding in Java stands for a feature that allows a subclass or child class to complete a specific implementation of a method that has been done by one of its superclasses or parent classes. One of the benefits of this method is that it assists in defining a specific class type behavior so that a subclass can use a parent class method based on its type.

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

What does the Final Keyword in Java mean?

A

The final keyword defines the entity which can be assigned only once and can’t be modified in the future. Once it’s been set, it can’t change its value anymore, for instance in the functions. The entity defined by the final keyword can be a variable, a class, a method, etc.

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

What are OOPs concepts in Java?

A

The OOPs concepts stand for Object-Oriented Programming in Java and cover the following elements: class, object, encapsulation, inheritance, polymorphism, and abstraction. All these assist in ensuring the application’s reusability, security, and flexibility, and allows developers to create the working methods and variables, then reuse them all or part of them without compromising the system security.

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

Does Java have a static class?

A

1 Declare the class final to prevent the class extension and make it static

You can’t build the advanced class static but you can enhance its performance in the next steps:

#2 Set up the private constructor
#3 Configure the members and functions of the class static as well

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

What does the GetMethod of HashMap do in Java?

A

HashMap is constructed using the hash table data structure. Basically, it employs the ‘hashCode ( )’ method to compute hash code to find the bucket location on the underlying array and the ‘equals ( )’ method to detect the object in the same bucket in case of a collision.

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

What is the Difference between JDK and JRE?

A

Java Runtime Environment (also known as JRE) stands for a Java Virtual Machine (or JVM), the programming environment where the Java programs are being executed. It also covers the browser plugins for the advanced applet execution.

Java Development Kit (also known as JDK) is a much more complex, full-stack Software Development Kit for Java programming language which includes the JRE, various compilers, and a list of tools (for instance, JavaDoc or Java Debugger). JDK is targeted at developing, compiling, and executing different Java apps.

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

What is the meaning of Spring beans?

A

The Spring beans are the objects that form the keystone of your application and are controlled by the Spring IoC container. A so-called “bean” is an object that is instantiated, assembled, and managed in any possible option by a Spring IoC container. The Spring beans are formed with the configuration metadata that is supplied to the container.

For instance, you can always meet their definitions in the XML form:

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

What is the Application Context definition?

A

In general, the application context has a similar definition to the bean factory. It loads bean definitions, wires beans together, and dispenses them if required. Also, it provides a generic way to load file resources, specific events to the beans defined as listeners, as well as means for resolving text messages using various languages.

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

What is JDBC?

A

JDBC (also known as Java Database Connectivity) stands for the Java API, responsible for the connection to a database, queries, and commands issuing as well as receiving the result sets processed by the database. Basically, it enables developers to switch between the databases without having to underlie the specific features of a particular database.

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

What is Controller in the Spring framework?

A

The controllers of the Spring MVC framework enable access to the app behavior programmers typically define via the service interface. They analyze the user’s query and convert it into the specific model user can perceive by the view component. The controllers in Spring are implemented only in the abstract way, thus allowing developers to create a wide range of different controllers.

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

What is the function of the Continuous Integration (CI) server?

A

CI is a DevOps software development practice that is used to merge the code changes made by different developers in the central repository in order to run automated builds and tests. CI should build code multiple times a day in order to timely detect any issue and identify its cause.

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

How is Java different from C++?

A

Java and C++ are both object-oriented programming languages, but they have some key differences.

Platform independence: Java is a platform-independent language, while C++ is a platform-dependent language. This means that Java code can run on any platform that has a Java virtual machine (JVM), while C++ code can only run on the platform that it was compiled for.

Memory management: Java uses automatic memory management, while C++ requires manual memory management. This means that Java programmers do not need to worry about allocating and freeing memory, while C++ programmers need to be careful to manage memory correctly to avoid memory leaks.

Safety: Java is a safer language than C++. This is because Java has built-in security features that help to protect applications from malicious code. For example, Java does not allow direct access to the operating system, which makes it more difficult for attackers to exploit security vulnerabilities.

Performance: Java is typically not as fast as C++. This is because Java uses a virtual machine, which adds an extra layer of abstraction between the code and the hardware. However, Java applications are typically more portable and secure than C++ applications.

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

JDK

A

It stands for Java Development Kit.

It is the tool necessary to compile, document and package Java programs.

It contains JRE + development tools.

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

JRE

A

It stands for Java Runtime Environment.
JRE refers to a runtime environment in which Java bytecode can be executed.
It’s an implementation of the JVM which physically exists.

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

JVM

A

It stands for Java Virtual Machine.
It is an abstract machine. It is a specification that provides a run-time environment in which Java bytecode can be executed.
JVM follows three notations: Specification, Implementation, and Runtime Instance.

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

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

A

main() in Java is the entry point for any Java program. It is always written as public static void main(String[] args).

public: Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.

static: It is a keyword in java which identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class.

void: It is the return type of the method. Void defines the method which will not return any value.

main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
String args[]: It is the parameter passed to the main method.

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

What is classLoader in Java?

A

The Java ClassLoader subset of JVM loads class files. The classloader loads Java programs first. Three classloaders are built into Java:

Bootstrap ClassLoader
Extension ClassLoader
System/Application ClassLoader

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

Why Java is not 100% Object-oriented?

A

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

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

8 primitive data types

A

boolean, byte, char, double, int, float,
long, short

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

What are wrapper classes in Java?

A

Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument.

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

What are constructors in Java?

A

In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.

There are two types of constructors:

Default Constructor: In Java, a default constructor is the one which does not take any inputs. In other words, default constructors are the no argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation.

Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.

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

What is singleton class in Java and how can we make a class singleton?

A

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

40
Q

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

A

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.

“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

41
Q

When can you use the super keyword?

A

In Java, the super keyword is a reference variable that refers to an immediate parent class object.

When you create a subclass instance, you’re also creating an instance of the parent class, which is referenced to by the super reference variable.

The uses of the Java super Keyword are-

To refer to an immediate parent class instance variable, use super.
The keyword super can be used to call the method of an immediate parent class.
Super() can be used to call the constructor of the immediate parent class.

42
Q

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

A

When we create a string using new(), a new object is created. Whereas, if we create a string using the string literal syntax, it may return an already existing object with the same name.

43
Q

Stack memory

A

Stack memory is used only by one thread of execution.
Stack memory can’t be accessed by other threads.
Exists until the end of execution of the thread.
Stack memory only contains local primitive and reference variables to objects in heap space.

44
Q

Heap memory

A

Heap memory is used by all the parts of the application.
Objects stored in the heap are globally accessible.
Memory management is based on the generation associated with each object.
Heap memory lives from the start till the end of application execution.
Whenever an object is created, it’s always stored in the Heap space.

45
Q

What is a package in Java? List down various advantages of packages.

A

Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. Also, the code within the packages can be imported by other classes and reused. Below I have listed down a few of its advantages:

Packages help in avoiding name clashes
They provide easier access control on the code
Packages can also contain hidden classes which are not visible to the outer classes and only used within the package
Creates a proper hierarchical structure which makes it easier to locate the related classes

46
Q

What is JIT compiler in Java?

A

JIT stands for Just-In-Time compiler in Java. It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it “just in time” to execute. Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it. This is why it is often responsible for the performance optimization of Java applications at the run time.

47
Q

What are access modifiers in Java?

A

In Java, access modifiers are special keywords which are used to restrict the access of a class, constructor, data member and method in another class. Java supports four types of access modifiers:

Default
Private
Protected
Public

48
Q

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

A

An object is a real-world entity that has a state and behavior. An object has three characteristics:

State
Behavior
Identity
An object is created using the ‘new’ keyword. For example:

ClassName obj = new ClassName();

49
Q

What are the main concepts of OOPs in Java?

A

Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:

Inheritance: Inheritance is a process where one class acquires the properties of another.

Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.

Abstraction: Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.

Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple forms.

50
Q

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

A

In Java, a local variable is typically used inside a method, constructor, or a block and has only local scope. Thus, this variable can be used only within the scope of a block. The best benefit of having a local variable is that other methods in the class won’t be even aware of that variable.

Whereas, an instance variable in Java, is a variable which is bounded to its object itself. These variables are declared within a class, but outside a method. Every object of that class will create it’s own copy of the variable while using it. Thus, any changes made to the variable won’t reflect in any other instances of that class and will be bound to that particular instance only.

51
Q

Constructor characteristics

A

Used to initialize the state of an object
Do not have any return type
Is invoked implicitly
A default constructor is provided by the compiler if the class has none
Constructor name must always be the same as the class name

52
Q

What is final keyword in Java?

A

final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:

final variable
When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

final method
When a method is declared final then it can’t be overridden by the inheriting class.

final class
When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class

53
Q

this() in java

A

this() represents the current instance of a class
Used to call the default constructor of the same class
Used to access methods of the current class
Used for pointing the current class instance
Must be the first line of a block

54
Q

super() in java

A

super() represents the current instance of a parent/base class
Used to call the default constructor of the parent/base class
Used to access methods of the base class
Used for pointing the superclass instance
Must be the first line of a block

55
Q

Why Java Strings are immutable in nature?

A

In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object.

Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.

56
Q

What is the difference between an array and an array list?

A

Arrays can contain primitive data types as well as objects.
Arrays Cannot contain values of different data types.

Arraylists can contain only objects, no primitive data types are allowed
Arraylists Can contain values of different data types.

57
Q

What is a Map in Java?

A

In Java, Map is an interface of Util package which maps unique keys to values. The Map interface is not a subset of the main Collection interface and thus it behaves little different from the other collection types. Below are a few of the characteristics of Map interface:

Map doesn’t contain duplicate keys.
Each key can map at max one value.

58
Q

What is collection class in Java? List down its methods and interfaces.

A

In Java, the collection is a framework that acts as an architecture for storing and manipulating a group of objects. Using Collections you can perform various tasks like searching, sorting, insertion, manipulation, deletion, etc. Java collection framework includes the following:

Interfaces
Classes
Methods

59
Q

What is Polymorphism?

A

Polymorphism is briefly described as “one interface, many implementations”. Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are two types of polymorphism:

Compile time polymorphism
Run time polymorphism

Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface.

60
Q

What is abstraction in Java?

A

Abstraction refers to the quality of dealing with ideas rather than events. It basically deals with hiding the details and showing the essential things to the user. Thus you can say that abstraction in Java is the process of hiding the implementation details from the user and revealing only the functionality to them. Abstraction can be achieved in two ways:

Abstract Classes (0-100% of abstraction can be achieved)
Interfaces (100% of abstraction can be achieved)

61
Q

What do you mean by an interface in Java?

A

An interface in Java is a blueprint of a class or you can say it is a collection of abstract methods and static constants. In an interface, each method is public and abstract but it does not contain any constructor. Thus, interface basically is a group of related methods with empty bodies. Example:

62
Q

Interfaces

A

An interface cannot provide any code at all, just the signature
A Class may implement several interfaces
All methods of an Interface are abstract
An Interface cannot have instance variables
An Interface visibility must be public (or) none
f we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method
An Interface cannot contain constructors

63
Q

What is inheritance in Java?

A

Inheritance in Java is the concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish a relationship between different classes. Inheritance is performed between two types of classes:

Parent class (Super or Base class)
Child class (Subclass or Derived class)
A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.

64
Q

What are the different types of inheritance in Java?

A

Java supports four types of inheritance which are:

Single Inheritance: In single inheritance, one class inherits the properties of another i.e there will be only one parent as well as one child class.

Multilevel Inheritance: When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent class but at different levels, such type of inheritance is called Multilevel Inheritance.

Hierarchical Inheritance: When a class has more than one child classes (subclasses) or in other words, more than one child classes have the same parent class, then such kind of inheritance is known as hierarchical.

Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance.

65
Q

Method Overloading :

A

In Method Overloading, Methods of the same class shares the same name but each method must have a different number of parameters or parameters having different types and order.
Method Overloading is to “add” or “extend” more to the method’s behavior.
It is a compile-time polymorphism.
The methods must have a different signature.
It may or may not need inheritance in Method Overloading.

66
Q

Method Overriding:

A

In Method Overriding, the subclass has the same method with the same name and exactly the same number and type of parameters and same return type as a superclass.
Method Overriding is to “Change” existing behavior of the method.
It is a run time polymorphism.
The methods must have the same signature.
It always requires inheritance in Method Overriding.

67
Q

Can you override a private or static method in Java?

A

You cannot override a private or static method in Java. If you create a similar method with the same return type and same method arguments in child class then it will hide the superclass method; this is known as method hiding. Similarly, you cannot override a private method in subclass because it’s not accessible there. What you can do is create another private method with the same name in the child class.

68
Q

What is multiple inheritance? Is it supported by Java?

A

If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes.

The problem with multiple inheritance is that if multiple parent classes have the same method name, then at runtime it becomes difficult for the compiler to decide which method to execute from the child class.

Therefore, Java doesn’t support multiple inheritance. The problem is commonly referred to as Diamond Problem.

69
Q

What is encapsulation in Java?

A

Encapsulation is a mechanism where you bind your data(variables) and code(methods) together as a single unit. Here, the data is hidden from the outer world and can be accessed only via current class methods. This helps in protecting the data from any unnecessary modification. We can achieve encapsulation in Java by:

Declaring the variables of a class as private.
Providing public setter and getter methods to modify and view the values of the variables.

70
Q

What is an association?

A

Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take the example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationships can be one to one, one to many, many to one and many to many.

71
Q

What do you mean by aggregation?

A

An aggregation is a specialized form of Association where all object has their own lifecycle but there is ownership and child object can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department teacher object will not destroy.

72
Q

What is composition in Java?

A

Composition is again a specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of a relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different houses if we delete the house room will automatically delete.

73
Q

What is a marker interface?

A

In Java, a marker interface is an interface that does not declare any methods or fields. Its sole purpose is to mark or tag a class as having a certain characteristic or capability. By implementing a marker interface, a class indicates that it possesses specific behavior or qualifies for a particular treatment.

Marker interfaces are purely a convention and serve as a form of metadata. They provide a way for developers or frameworks to identify classes that meet certain criteria without requiring any additional methods or fields.

74
Q

What is object cloning in Java?

A

Object cloning in Java is the process of creating an exact copy of an object. It basically means the ability to create an object with a similar state as the original object. To achieve this, Java provides a method clone() to make use of this functionality. This method creates a new instance of the class of the current object and then initializes all its fields with the exact same contents of corresponding fields. To object clone(), the marker interface java.lang.Cloneable must be implemented to avoid any runtime exceptions. One thing you must note is Object clone() is a protected method, thus you need to override it.

75
Q

What is a copy constructor in Java?

A

Copy constructor is a member function that is used to initialize an object using another object of the same class. Though there is no need for copy constructor in Java since all objects are passed by reference. Moreover, Java does not even support automatic pass-by-value.

76
Q

What is a constructor overloading in Java?

A

In Java, constructor overloading is a technique of adding any number of constructors to a class each having a different parameter list. The compiler uses the number of parameters and their types in the list to differentiate the overloaded constructors.

77
Q

What is a servlet?

A

Java Servlet is server-side technologies to extend the capability of web servers by providing support for dynamic response and data persistence.

The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing our own servlets.

All servlets must implement the javax.servlet.Servlet interface, which defines servlet lifecycle methods. When implementing a generic service, we can extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.

Most of the times, web applications are accessed using HTTP protocol and thats why we mostly extend HttpServlet class. Servlet API hierarchy is shown in below image.

78
Q

What is Request Dispatcher?

A

RequestDispatcher interface is used to forward the request to another resource that can be HTML, JSP or another servlet in same application. We can also use this to include the content of another resource to the response.

There are two methods defined in this interface:

1.void forward()

2.void include()

79
Q

What is the life-cycle of a servlet?

A

Servlet is loaded
Servlet is instantiated
Servlet is initialized
Service the request
Servlet is destroyed

80
Q

What is the role of JDBC DriverManager class?

A

The DriverManager class manages the registered drivers. It can be used to register and unregister drivers. It provides factory method that returns the instance of Connection.

81
Q

What is JDBC Connection interface?

A

The Connection interface maintains a session with the database. It can be used for transaction management. It provides factory methods that returns the instance of Statement, PreparedStatement, CallableStatement and DatabaseMetaData.

82
Q

List some of the important annotations in annotation-based Spring configuration.

A

he important annotations are:

@Required
@Autowired
@Qualifier
@Resource
@PostConstruct
@PreDestroy

83
Q

Explain Bean in Spring and List the different Scopes of Spring bean.

A

Beans are objects that form the backbone of a Spring application. They are managed by the Spring IoC container. In other words, a bean is an object that is instantiated, assembled, and managed by a Spring IoC container.

There are five Scopes defined in Spring beans.

84
Q

There are five Scopes defined in Spring beans.

A

Singleton: Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure spring bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues because it’s not thread-safe.

Prototype: A new instance will be created every time the bean is requested.

Request: This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.

Session: A new bean will be created for each HTTP session by the container.

Global-session: This is used to create global session beans for Portlet applications.

85
Q

How we can set the spring bean scope? And what supported scopes does it have?

A

n Spring framework, the scope of a bean determines the lifecycle and visibility of the bean instance within the container. You can set the scope of a Spring bean using the @Scope annotation or the XML configuration.

To set the scope using the @Scope annotation, you can specify it on the bean class or the bean definition method within a configuration class. Here’s an example:

86
Q

What are some of the important Spring annotations which you have used?

A

@Controller – for controller classes in Spring MVC project.

@RequestMapping – for configuring URI mapping in controller handler methods. This is a very important annotation, so you should go through Spring MVC RequestMapping Annotation Examples

@ResponseBody – for sending Object as response, usually for sending XML or JSON data as response.

@PathVariable – for mapping dynamic values from the URI to handler method arguments.

@Autowired – for autowiring dependencies in spring beans.

@Qualifier – with @Autowired annotation to avoid confusion when multiple instances of bean type is present.

@Service – for service classes.

@Scope – for configuring the scope of the spring bean.

@Configuration, @ComponentScan and @Bean – for java based configuration

87
Q

What is Hibernate Framework?

A

Object-relational mapping or ORM is the programming technique to map application domain model objects to the relational database tables. Hibernate is Java-based ORM tool that provides a framework for mapping application domain objects to the relational database tables and vice versa.

Hibernate provides a reference implementation of Java Persistence API, that makes it a great choice as ORM tool with benefits of loose coupling. We can use the Hibernate persistence API for CRUD operations. Hibernate framework provide option to map plain old java objects to traditional database tables with the use of JPA annotations as well as XML based configuration.

88
Q

Explain Hibernate architecture.

A

Hibernate has a layered architecture which helps the user to operate without having to know the underlying APIs. Hibernate makes use of the database and configuration data to provide persistence services (and persistent objects) to the application. It includes many objects such as persistent object, session factory, transaction factory, connection factory, session, transaction etc.

89
Q

What is the Difference Between ArrayList and HashSet in Java?

A

Implementation
ArrayList: Implements List interface.
HashSet: Implements Set interface.

Duplicates
ArrayList: Allows duplicates values.
HashSet: Doesn’t allow duplicates values.

Constructor
ArrayList: Have three constructor which are ArrayList (), ArrayList (int capacity), ArrayList (int Collection c).
HashSet: Have four constructor which are HashSet (), HashSet (int capacity), HashSet (Collection c), and HashSet (int capacity, float loadFactor).

Order
ArrayList: Maintains the order of the object in which they are inserted.
HashSet: Doesn’t maintain any order.

Null Object
ArrayList: It’s possible to add any number of null value.
HashSet: Allow one null value.

90
Q

Java Compilation and execution

A

For those who don’t know Java is both the compiler and interpreter language. When you compile a Java program creates a .class file which is a collection of byte code, these byte codes are not machine instructions instead they are instructions which Java a virtual machine can understand.

Since every Java program runs on Java virtual machine, the same byte code can be run on any platform. the key is byte code is not machine instruction they are platform-independent instruction to JVM.

On another hand, JVM or Java virtual machine is platform-dependent because it converts byte code into machine level instruction which is platform-specific and that’s why you have a different version of JDK and JRE for windows and Linux because both JDK and JRE come with Java virtual machines. if you are confused between JVM, JRE and JDK then read my post on the difference between JDK, JRE, and JVM in Java.

91
Q

How Java is independent

A

Byte code is created when you compile Java program using Java compiler “javac” and byte code runs on JVM which is created by running the “java” command. In detail when you run “java” command it creates Java virtual machine, loads Main class specified in the command line and calls the standard main method in java.

In summary combination of bytecode and JVM makes the Java program platform-independent. Write once run everywhere was Java’s mantra when it started ruling the programming world in the mid and late ’90s.

Always remember, Java programs are platform independent but JVM is not. That’s why you have different JVM and JRE installations for different platforms like Mac, Windows, Linux, or Solaris. Similarly, there are different JVM for 32-bit and 64-bit machines.

Java has always been famous for its platform independent and write once run everywhere (WORA) feature and that’s why it is still popular after 25 years of its creation. There are many more programming language and platform created after Java which uses same feature like .NET CLR (Common Language Runtime) which also offer platform independency for .NET application.

92
Q

What is ClassLoader in Java

A

ClassLoader in Java is a class that is used to load class files in Java. Java code is compiled into a class file by javac compiler and JVM executes the Java program, by executing byte codes written in the class file. ClassLoader is responsible for loading class files from file systems, networks, or any other source.

There is three default class loader used in Java, Bootstrap, Extension, and System or Application class loader.

In short here is the location from which Bootstrap, Extension, and Application ClassLoader load Class files.

1) Bootstrap ClassLoader - JRE/lib/rt.jar

2) Extension ClassLoader - JRE/lib/ext or any directory denoted by java.ext.dirs

3) Application ClassLoader - CLASSPATH environment variable, -classpath or -cp option, Class-Path attribute of Manifest inside JAR file.

93
Q

What is Autoboxing in Java

A

Autoboxing and unboxing are introduced in Java 1.5 to automatically convert the primitive type into boxed primitive( Object or Wrapper class). autoboxing allows you to use primitive and object types interchangeably in Java in many places like an assignment, method invocation, etc. If you have been using Collections like HashMap or ArrayList before Java 1.5 then you are familiar with the issues like you can not directly put primitives into Collections, instead, you first need to convert them into Object only then only you can put them into Collections.

Wrapper classes like Integer, Double and Boolean help for converting primitive to Object but that clutter the code. With the introduction of autoboxing and unboxing in Java, this primitive to object conversion happens automatically by the Java compiler which makes the code more readable.

94
Q

Difference between method overloading and overriding in Java?

A

Overriding happens at subclass while Overloading happens in the same class. Also, overriding is a runtime activity while overloading is resolved at compile time.

95
Q

Difference between StringBuilder and StringBuffer in Java? And String

A

StringBuilder is not synchronized while StringBuffer is synchronized.

One of the most notable differences between StringBuilder, StringBuffer, and String in Java is that both StringBuffer and StringBuilder are Mutable classes but String is Immutable in Java.

What this means is, you can add, remove or replace characters from StringBuffer and StringBuilder object but any change on the String object like converting uppercase to lowercase or appending a new character using String concatenation will always result in a new String object

Another key difference between them is that both StringBuffer and String are thread-safe but StringBuilder is not thread-safe in Java.

String achieves its thread-safety from Immutability but StringBuffer achieves it via synchronization, which is also the main difference between the StringBuffer and StringBuilder in Java.

96
Q
A