You are on page 1of 10

6/1/2018 Core Java Interview Questions List: Part I - DZone Java

Core Java Interview Questions List:


Part I
by Sam Atkinson   · Dec. 15, 14 · Java Zone

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at
Scale. Brought to you in partnership with Lightbend.

As I’ve discussed several times on this site, I’m not a fan of learn by wrote Java questions. However, a
lot of people continue to ask them during their interview process so it means you need to know how to
answer them.

The name of the game is core java interview questions and I will be your quiz master. Each week I will
publish 10 new quick fire questions and answers.

1. What is a static variable?


A static variable is a value that remains the same through all the instances and can be modified by all
the instances. It is a shared value.

2. If I change the order of a methods modifiers will it still


work? Is public static void main the same as public void
static main? What about void static public main?
Modifiers can be moved around freely. The visibility modifier (if there is one)must be at the start.

3. Why would you pick an ArrayList or LinkedList?


ArrayList is great if you need fast access to objects but can cope with slower writes. Conversely, if you
need fast writes (moving, removing or adding) but can cope with slower direct access then choose a
LinkedList (read our in depth article on collections here).

4. What are the different access modifiers in Java and what


do they mean?
public- accessible to everyone in the JVM

private- only accessible from inside the class.

protected- accessible from by any class in the same package or any subclass in any package

default-when no visibility modifier is present. accessible from any class in the same package only.

5 If you wanted to prevent your methods or classes from


https://dzone.com/articles/core-java-interview-questions-0 1/10
6/1/2018 Core Java Interview Questions List: Part I - DZone Java
5. If you wanted to prevent your methods or classes from
being overridden, how would you do this?
By declaring something as final you prevent it from being overridden. Nothing is perfect though, as
crafty developers can always use Reflection to get around this, or alternatively just copy and paste
your code into their own version of the class. It is rarely a good idea to prevent your methods or
classes being overridden, and you should code defensively to reflect this.

6. What is an immutable object?


The state of an immutable object cannot be changed after construction. Immutable objects can play
an important roll in threading. (read more on threading here). A good example of an immutable
objeect is String. Any modification to an immutable object will result in the creation of a new object.

7. What is the difference between overloading and


overriding?
Method overloading is having multiple methods with the same name. They can be differentiated as
they will have a different signature, e.g. take different method parameters. Overriding is used when
creating a subclass of a class and specifying your own functionality for the method by copying the
method signature identically.

8. What does it mean when we say java does not support


multiple inheritance? Is this a good thing?
Java cannot extend functionality from more than one concrete or abstract class. If both parent classes
had a jump() method, it would be unclear which functionality the caller would need to use. We can
implement multiple interfaces however as the the implementation occurs in our actual class so this
problem does not occur.

9. What is an abstract class?


Similar to an interface, an abstract class cannot actually be instantiated. Unlike an interface, an
abstract class can have method implementations. Any method without implementation will have the
modifier “abstract” to indicate to classes which extend it that they must provide the implementation.

10. What does “write once run anywhere” mean in relation


to Java?
Java is a cross platform language; java compiles down to byte code which can be run on a Java Virtual
Machine (JVM). JVMs are available on many platforms including the major operating systems. This
means that any Java application can in theory run on any platform where a JVM is available, hence
write once (for the JVM) and run anywhere (there is a JVM).

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic
application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Lik Thi A ti l ? R dM
https://dzone.com/articles/core-java-interview-questions-0 F DZ 2/10
6/1/2018 Core Java Interview Questions List: Part I - DZone Java

Like This Article? Read More From DZone


Top 10 Reasons You Don't Need DDD Strategic Patterns: How to
Service Virtualization Define Bounded Contexts

Exploratory Testing: Testing in the Free DZone Refcard


Wild Getting Started With Scala

Topics: JAVA , CAREERS , INTERVIEWS

Published at DZone with permission of Sam Atkinson, DZone MVB. See the original article here. 
Opinions expressed by DZone contributors are their own.

Java Partner Resources


Level up your code with a Pro IDE
JetBrains
×

Deep insight into your code with IntelliJ IDEA.
JetBrains

jQuery UI and Auto-Complete Address Entry
Melissa Data
 The 2017 Guide to Java: Development and Evolution
Advanced Linux Commands The BestSheet]
[Cheat Gang of Four Design Patterns for Microservices
Red Hat Developer Program The Benefits of Using the Future API in Kotlin
 Java 9: Jigsaw Capabilities, jlink, and More

7 Things to Know Getting


Download for Free Started
With Spring Boot
by Ranga Karanam  · Jan 06, 18 · Java Zone

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers.
Brought to you in partnership with Red Hat.

Spring Boot is a tricky framework to understand. In this tutorial for beginnners with Spring Boot, we
will look at the basics of Spring Boot and help you understand the important concepts — Starter
Projects, Auto Configuration, and Starter Parents.

Understanding Spring Boot's Architecture


The building blocks of Spring Boot are:

Spring Boot Starter Projects


Spring Boot Starter Parent
https://dzone.com/articles/core-java-interview-questions-0 3/10
6/1/2018 Core Java Interview Questions List: Part I - DZone Java

Auto Configuration

We will start with understanding what Spring Boot wants to achieve by comparing it with Spring and
Spring MVC. Once you understand that, you should be in a good position to start with the building
blocks of Spring Boot.

Spring Boot vs. Spring MVC vs. Spring


The most important thing to understand is:

Spring Boot does not compete with Spring or Spring MVC. It


makes it easy to use them.

Spring Framework

The most important feature of Spring Framework is


Dependency Injection. At the core of all Spring Modules is
Dependency Injection or IOC — Inversion of Control.

When DI or IOC is used properly, we can develop loosely coupled applications. And loosely coupled
applications can be easily unit tested.

Spring MVC

Spring MVC provides a decoupled way of developing web


applications. With simple concepts like Dispatcher Servlet,
ModelAndView, and View Resolver, it makes it easy to develop
web applications.

Spring Boot
The problem with Spring and Spring MVC is the amount of configuration that is needed:

1 <bean

2 class="org.springframework.web.servlet.view.InternalResourceViewResolver">

3 <property name="prefix">

4 <value>/WEB-INF/views/</value>

5 </property>

6 <property name="suffix">

7 <value>.jsp</value>
https://dzone.com/articles/core-java-interview-questions-0 4/10
6/1/2018 Core Java Interview Questions List: Part I - DZone Java
7 j p

8 </property>

9 </bean>

10
11 <mvc:resources mapping="/webjars/**" location="/webjars/"/>

Spring Boot solves this problem through a combination of Auto Configuration and Starter Projects.
Spring Boot also provides a few features to make building production-ready applications faster.

The following article digs deeper and gives you a full-blown comparison between Spring, Spring MVC,
and Spring Boot.

TITLE CATEGORY URL GITHUB

Spring Boot vs. Spring MVC vs. Spring - How do they compare? Spring Boot Basics URL

Spring Boot Auto Configuration


Spring and Spring MVC applications have a lot of XML or Java Bean Configuration.

Spring Boot brings in a new thought process around this.

Can we bring more intelligence into this? When a Spring MVC


JAR is added into an application, can we auto configure some
beans?

How about auto configuring a Data Source if a Hibernate JAR is on the classpath?
How about auto configuring a Dispatcher Servlet if a Spring MVC JAR is on the classpath?

There would be provisions to override the default auto configuration.

Spring Boot looks at a) Frameworks available on the


CLASSPATH b) Existing configurations for the application.
Based on these, Spring Boot provides the basic configuration
needed to configure the application with these frameworks.
This is called Auto Configuration .

The following article explores Auto Configuration in depth.

TITLE CATEGORY URL GITHUB

What is Spring Boot Auto Configuration? Spring Boot Basics URL

Spring Boot Starter Projects


https://dzone.com/articles/core-java-interview-questions-0 5/10
Spring Boot Starter Projects
6/1/2018 Core Java Interview Questions List: Part I - DZone Java

Here’s what the Spring Boot documentation says about starters.

Starters are a set of convenient dependency descriptors that


you can include in your application. You get a one-stop-shop
for all the Spring and related technology that you need, without
having to hunt through sample code and copy paste loads of
dependency descriptors. For example, if you want to get
started using Spring and JPA for database access, just include
the spring-boot-starter-data-jpa dependency in your project,
and you are good to go.

Let’s consider an example starter — Spring Boot Starter Web.

If you want to develop a web application or an application to expose RESTful services, Spring Boot
Start Web is the starter to pick.

The following screenshot shows the different dependencies that are added into our application when
you add Spring Boot Start Web into the dependencies of your project.

This is a wide variety of components that are typically used to develop web applications. Spring Boot
Starter Web brings them together and provides a simple approach to use them.

Spring — core, beans, context, AOP


Web MVC — (Spring MVC)
Jackson — for JSON Binding
Validation — Hibernate Validator, Validation API
Embedded Servlet Container — Tomcat
Logging — logback, slf4j

Any typical web application would use all these dependencies.


Spring Boot Starter Web comes pre packaged with these. As a
developer, I would not need to worry about these dependencies
or their compatible versions.

Spring Boot provides a wide range of starter projects. Spring Initializr suppports all of them and more.
Among the varied range of starter projects and options supported are:

spring-boot-starter-web-services: Build applications exposing SOAP web services


spring-boot-starter-web: Build Web applications and RESTful applications
b
https://dzone.com/articles/core-java-interview-questions-0 d 6/10
6/1/2018 Core Java Interview Questions List: Part I - DZone Java
spring-boot-starter-test: Write great unit and integration tests
spring-boot-starter-jdbc: Traditional JDBC applications
spring-boot-starter-hateoas: Make your services more RESTful by adding HATEOAS features
spring-boot-starter-security: Authentication and authorization using Spring Security
spring-boot-starter-data-jpa: Spring Data JPA with Hibernate
spring-boot-starter-cache: Enabling Spring Framework’s caching support
spring-boot-starter-data-rest: Expose simple REST services using Spring Data REST

You can explore starter projects in depth with the article below.

TITLE CATEGORY URL GITHUB

Initializing Projects with Spring Boot Starters - Web and JPA Spring Boot Basics URL

Spring Boot Starter Parent


All Spring Boot projects typically use spring-boot-starter-parent as the parent in the pom.xml.

1 <parent>

2 <groupId>org.springframework.boot</groupId>

3 <artifactId>spring-boot-starter-parent</artifactId>

4 <version>2.0.0.RELEASE</version>

5 </parent>

Parent POMs allow you to manage the following things for multiple child projects and modules:

Configuration: Java version and other properties


Depedency Management: Version of dependencies
Default plugin configuration

You can read more about Spring Boot Starter Parent at the article below.

TITLE CATEGORY URL GITHUB

Introduction to Spring Boot Starter Parent Spring Boot Basics URL

Spring Initializr: Create Spring Boot Projects at F1


Speed
Spring Initializr is great tool to bootstrap your Spring Boot projects.

It allows you to create a varied range of Spring Boot-based applications from a very simple UI. Some
of the types of applications you can bootstrap are:

Web applications
Restful applications
https://dzone.com/articles/core-java-interview-questions-0 7/10
6/1/2018 Core Java Interview Questions List: Part I - DZone Java

Batch applications

Let’s take the example of creating a web application with Spring Initializr.

As shown in the image above, the following steps have to be taken:

Launch Spring Initializr (http://start.spring.io/) and choose the following


Choose com.in28minutes.springboot as the Group
Choose student-services as the Artifact
Choose following dependency
Web

Click the Generate Project button at the bottom of the page.


Import the project into Eclipse.

The following article delves more into Spring Initializr.

TITLE CATEGORY URL GITHUB

Spring Initializr - Bootstrap Your Spring Boot Applications at F1 Spring Boot


URL
speed! Basics

Spring Boot and Embedded Servers


When we create a deployable application, we can embed the server (for example, Tomcat) inside the
deployable.

For example, for a Spring Boot Application, you can generate


an application JAR that contains Embedded Tomcat. You can
run a web application as a normal Java application!

An embedded server implies that our deployable unit contains the binaries for the server (for example,
tomcat.jar).

Let’s take a quick look at the dependencies for spring-boot-starter-web

1 <dependency>

2 <groupId>org.springframework.boot</groupId>

3 <artifactId>spring-boot-starter-tomcat</artifactId>

4 <version>2.0.0.M6</version>

5 <scope>compile</scope>

6 </dependency>

You can see that, by default, Starter Web includes a dependency on the Starter Tomcat.

https://dzone.com/articles/core-java-interview-questions-0 8/10
6/1/2018 Core Java Interview Questions List: Part I - DZone Java

Tomcat is the default embedded server for Spring Boot.


Spring Boot also supports Jetty and Undertow.

The following article explores more about embedded servers:

TITLE CATEGORY URL GITHUB

Spring Boot and Embedded Servers - Tomcat, Jetty, and Spring Boot Project Code on
URL
Undertow Basics Github

Spring Data
From http://projects.spring.io/spring-data/:

Spring Data’s mission is to provide a familiar and consistent,


Spring-based programming model for data access while still
retaining the special traits of the underlying data store. It
makes it easy to use data access technologies, relational and
non-relational databases, map-reduce frameworks, and cloud-
based data services.

To make it simpler, Spring Data provides abstractions (interfaces) you can use irrespective of
underlying data source.

Some of the sub modules in Spring Data are:

Spring Data JPA — relational databases


Spring Data MongoDB
Spring Data REST — Expose awesome REST APIs around Spring Data Repositories

The following articles digs deeper into Spring Data.

TITLE CATEGORY URL GITHUB

Introduction to Spring Data - Spring Data JPA, Spring Data REST, and
Spring Data URL
MongoDB

Other References
Spring MVC: https://www.youtube.com/watch?v=BjNhGaZDr0Y
Spring Boot: https://www.youtube.com/watch?v=PSP1-2cN7vM
Eclipse: https://www.youtube.com/watch?v=s4ShbtOHMCA
Maven: https://www.youtube.com/watch?v=0CFWeVgzsqY
JUnit: https://www.youtube.com/watch?v=o5k9NOR9lrI
Mockito: https://www youtube com/watch?v=d2KwvXQgQx4
https://dzone.com/articles/core-java-interview-questions-0 9/10
6/1/2018 Core Java Interview Questions List: Part I - DZone Java
Mockito: https://www.youtube.com/watch?v=d2KwvXQgQx4

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application


Design. Brought to you in partnership with Red Hat.

Like This Article? Read More From DZone


What Is Spring Boot Auto Creating a Spring Boot Project
Configuration? With Eclipse and Maven

Fixing My Own Spring Boot Starter Free DZone Refcard


Demo Getting Started With Scala

Topics: SPRING MVC, SPRING BOOT, JAVA, AUTO CONFIGURATION, SPRING INITIALIZR, TUTORIAL

Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here. 
Opinions expressed by DZone contributors are their own.

https://dzone.com/articles/core-java-interview-questions-0 10/10

You might also like