What is difference between break, continue and return statements ?

The break statement results in the termination of the loop, it will stop further iteration of loop and come out of the loop.

The continue statement stops the current execution of the iteration and proceeds to the next iteration.

The return statement is used to go back to the step from where it was called or to stop further execution.

What is the difference between while and do-while statements ?

A while loop will check the condition first before executing the content. Whereas the do while loop executes the content of the loop once before checking the condition of the while.

When does the compiler provides the default constructor ?

The default constructor is the no-argument constructor automatically provided if you do not define any other constructor.

Difference between C++ and Java ?

Java does not support pointers, templates, unions, operator overloading (except +), structures. C++ supports structures, unions, templates, operator overloading, pointers and pointer arithmetic.

Java support automatic garbage collection. C++ support destructor, which is automatically invoked when the object is destroyed.

Java does not support conditional compilation and inclusion. Conditional inclusion (#ifdef #ifndef type) is one of the main features of C/C++.

Java has built in support for threads. In Java, there is a Thread class that you inherit to create a new thread and override the run() method. C++ has no built in support for threads. C++ relies on non-standard third-party libraries for thread support.

Java does not support default arguments. There is no scope resolution operator (::) in Java. The method definitions must always occur within a class, so there is no need for scope resolution there either. C++ supports default arguments. C++ has scope resolution operator (::) which is used to to define a method outside a class and to access a global variable within from the scope where a local variable also exists with the same name.

There is no goto statement in Java. The keywords const and goto are reserved, even though they are not used. C++ has goto statement.

Java doesn’t provide multiple inheritance but supports multiple implementations of interfaces. C++ does support multiple inheritance. The keyword virtual is used to resolve ambiguities during multiple inheritance if there is any.

Exception handling in Java is different because there are no destructors. Also, in Java, try/catch must be defined if the function declares that it may throw an exception. While in C++, you may not include the try/catch even if the function throws an exception.

Java has built-in support for documentation comments (/** … */); therefore, Java source files can contain their own documentation, which is read by a separate tool usually javadoc and reformatted into HTML. This helps keeping documentation maintained in easy way. C++ does not support documentation comments.

Java is interpreted for the most part and hence platform independent. C++ generates object code and the same code may not run on different platforms.

Usages of Java packages ?

Packages in Java are simply a mechanism used to organize classes and prevent class name collisions. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time.

They can be used for:

separating projects
separating modules
separating application layers (business, web, dao)
further finer grained code separation

What is dynamic class loading ?

Dynamic Class Loading allows the loading of Java code that is not known about before a program starts. The Java model loads classes as needed and need not know the name of all classes in a collection before any one of its classes can be loaded and run.

Dynamic Java Class loading is an important feature of the Java Virtual Machine because it provides the Java platform with the ability to install software components at run-time.

One common example is trivial JDBC programming. Dynamic class loading is used to load the driver class. For example using Class.forName() we can load class dynamically as and when required.

What happens if you do not provide a constructor ?

A no-argument constructor will be provided.

Difference between shallow cloning and deep cloning of objects ?

Please read https://www.jeejava.com/shallow-copy-and-deep-copy-in-java/

Can we have interfaces with no defined methods in Java ?

Yes. Interface with no defined method is called a marker or tag interface. Some of marker interfaces are Serializable, Cloneable etc.

What is the difference between “==” and equals() method ?

The == operator compares if two objects are the same instance. The equals() operator compares the state of two objects (e.g. if all attributes are equal). You can even override the equals() method to define yourself when an object is equal to another.

How can you create an immutable class in Java ?

There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. We can also create immutable class by creating final class that have final data members.

What are access modifiers ?

Java provides a number of access modifiers to set level of access for classes, variables, methods and constructors.

The four access levels are:

default : visible to the package only.

private : visible to the class only.

public : visible to the everywhere.

protected : visible to package and all subclasses.

Can we have private constructor in Java ?

Yes. There are different uses of this.

To prevent instantiation outside of the object, in the following cases:

singleton
factory method
static-methods-only (utility) class
constants-only class

To prevent subclassing or extending.

Why do we need generics in Java ?

The goal of implementing Generics is finding bugs in compile-time, other than in run-time. Finding bugs in compile-time can save time for debugging Java program, because compile-time bugs are much easier to find and fix. Generic types only exist in compile-time.

In summary,

Stronger type checking at compile time.
Elimination of explicit cast.
Enabling better code reusability such as implementation of generic algorithms

What is POJO ?

Java Objects that store application data and do not implement or extend any framework specific interfaces or classes are commonly referred to as Plain Old Java Objects (POJOs).

What are the types of variables in Java ?

Java has three kinds of variables namely, the instance variable, the local variable and the class variable.

Local variables are used inside blocks as counters or in methods as temporary variables and are used to store information needed by a single method.

Instance variables are used to define attributes or the state of a particular object and are used to store information needed by multiple methods in the objects.

Class variables are global to a class and to all the instances of the class and are useful for communicating between different objects of all the same class or keeping track of global states.

What is Strategy Pattern?

The Strategy Pattern defines a family of algorithms, encapsulates each other and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Please have a look for more information Strategy Design Pattern

What is Observer Pattern?

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

Please have a look here Observer Design Pattern

What is Decorator Pattern?

The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

For more information go through Decorator Design Pattern

What is Factory Method pattern?

The Factory Method pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclass.

Please see the example here at Factory Design Pattern

You may also read all other design patterns

What is lambda expression in Java 8?

Please go through the tutorial here at Lambda Expression in Java 8

What changes were brought into Java 8 Date Time API?

Have a look here Date and Time API in Java 8

Can you please explain Stream API in Java 8?

Stream API in Java 8

Can we write static and default methods in interface?

Have a detailed look here Default and static methods in Java 8

Where to use filter of stream API?

Please look at the example here Stream filter in Java 8

How to use forEach loop in Java 8?

Please see the example ForEach example with Map