Core Java Tutorial with Top 10 Interview Questions

Core Java Tutorial with Top 10 Interview Questions

Introduction

This tutorial will help you to understand the basics of Core Java. The content has been designed to keep students in mind. A separate link is provided for each basic Core Java sub-topic. We highly recommend you to take the quizzes at the end of the tutorial to assess your understanding of the subject.


Followed by the quiz are the Top 10 Core Java Interview Questions You’ll Most Likely Be Asked to ensure your readiness for an Interview. These questions will help you get an idea of the most common questions that interviewers ask and the answers you’re expected to give. Follow the below links to access the tutorial of each topic:

 

 

 

Tutorial:

  1. Flow Control and Assertion
  2. Wrapper Classes, Garbage Collection, and Exception Handling
  3. Threads
  4. Object Oriented Programming Concepts
  5. Declarations and Access Controls
  6. Java Assignments
  7. Java Operators
  8. Inner Classes and String Handling
  9. Streams
  10. Collections

 

 

Quiz:

Take these Java quizzes to assess your understanding of the subject.
  1. Quiz 1
  2. Quiz 2

 

 

 

Top 10 Core Java Interview Questions:

These Interview Questions are answered in a crisp and concise way requiring minimum time to understand the most important concepts in CORE JAVA. The Questions and Answers have been crafted by professionals around the globe with years of industry experience and are on quite a few interview panels.

 

 

 

1) What is an assertion in Java? How is it different from if - else conditions?

 

An assertion is a Boolean expression that is assumed by the programme. If true it executes the next statement and if false, the program throws an exception and ends. If–else conditions are different from assertions— if the if condition is false then the else part takes over, but in the case of an assertion if the assert expression is false, the program ends up throwing an error but will execute the second expression in the assert statement.

 

 

Example:

 

 

{

 

int var = 0;

 

System.out.println(“Enter a value bigger than 20 : ”);

 

var = in.nextLine();

 

assert var / 2 >5 : System.out.println(“Please try again and enter the

 

correct value”); System.out.println(“Good Choice!”);

 

}

 

 

Case 1:

Enter a value bigger than 20: 10 Exception in thread “main” java.lang.AssertionError: Please try again and enter the correct value

 

 

Case 2:

Enter a value bigger than 20: 30 Good Choice!

 

 

When Case 1 executes, the output will be – Exception in thread “main” java.lang.AssertionError: Please try again and enter the correct value.
Case 2 will not throw any exception as the assumption or assertion is true and hence the next line in program will execute and the output is Good Choice!

 

 

 

2) What is a Wrapper class in Java? What are the special properties of Wrapper class objects?​

 

Java is completely Object-oriented programming. So, everything in Java should correspond to an object. But the primitives in Java are not objects. So, the wrapper classes convert Java Primitives to corresponding objects so that they can be utilized in the programs. Each primitive has a corresponding wrapper class and you can create an object of that type. In the same way, you can convert a wrapper object to the primitive type too. If you have an integer variable in Java, you can convert it into an Integer object using a wrapper class. Similarly, you can convert an Integer object to an int primitive type also. All wrapper objects are final. So, you cannot extend the wrapper classes.

 

 

 

3) What are the different types of Thread? Explain?

Threads are of 2 types – Daemon Threads and Non-Daemon Threads.

 

All user-defined threads are Non-Daemon Threads unless they are explicitly set to be a Daemon thread. Daemon threads are usually used for background processes such as Garbage Collection. As soon as all Non-Daemon threads stop running, the JVM stops running and does not wait for the Daemon threads to stop.

 

 

 

4) What is an Object in software and what are the benefits of Objects?​

 

Software objects consist of state and their behavior. State of an object is stored in its fields and the behavior is exposed through the methods. Object-oriented programming helps to hide the internal characteristics of an object and this is called encapsulation. The main advantages of Objects are:

 

  • Independent: An Object’s definition can be kept separate from other objects and passed on to other programs.
  • Encapsulation: The object’s fields and characteristics are hidden from the outer world. The only way to access the object’s characteristics are through the methods they provide. So the programmer can decide what to hide and what to expose.
  • Reuse Code: Existing objects can be reused in many programs by creating an instance of extending its functionality.
  • Easy to debug: Problematic objects can be traced out and debugged easily without affecting or touching any other portion of the code.

 

 

 

5) What is the benefit of declaring a class as final?

By declaring a class as final, we ensure that the class is secured thereby no one can change its existing implementation. Also, the final class is thread-safe thereby restricting interaction between multiple threads.

 

Example:

 

 

final class MyClass {}

 

 

By making the class ‘MyClass’ as final, it is not possible for us to extend this class and use its implementation.Also, inter-thread communication is not possible when we declare the class as final.

 

 

 

6) Give an example for implicit cast?

The below line of code is an example for implicit cast:

 

 

int iValue = 250;

 

long lValue = iValue;

 

 

The above code explains that an int value can always be assigned to a long variable without casting and the conversion happens by default.

 

 

 

7) State the use of instance of operator. Give an example?

The use of instanceof operator is to check if an object belongs to a particular type. So, it is used only with object reference variables.

 

Example:

 

 

 

String name = “This is Java”;

 

if (name instanceof String)

 

{

 

  System.out.println(name);

 

}

 

 

The above code checks if the variable ‘name’ is of type String. Since the condition is true, it will print the result “This is Java” when you execute the above code.

 

 

 

8) Explain about anonymous inner class?

The anonymous inner class can be better explained with the below example.

 

Example:

 

 

 

Runnable runnable = new

 

Runnable() { //Line 1

 

public void run() { } //Line 2

 

}; //Line 3

 

 

  • Anonymous inner class can either extend a subclass or implement one interface but not both. In the above code, we have used the Runnable interface as an anonymous inner class.
  • This inner class has a closed curly brace followed by a semicolon as coded in Line 3. This is the syntax of declaring an anonymous inner class.
  • This inner class can have a method which is from its reference sub-class or interface. In the above code, we have used the run() method which belongs to the Runnable interface.

 

 

 

9) What is the difference between streams and collections?

Though Streams and Collections look similar, their functionality is very different. The major differences between Streams and Collections are:

 

  • Collection holds an array of values whereas streams do not hold any value. They only help us carry the source value through a pipeline of computations.
  • As they do not hold any value, they perform the function and do not change the value at the source.
  • Collections are finite whereas strings are infinite.
  • Streams perform mapping and other computations lazily which is more efficient.

 

 

 

10) What are the four interfaces of Java collections?​

Collection is the root of the collection hierarchy. A collection incorporates a multitude of objects cited as its elements. The Java platform doesn’t give any direct implementations of this interface.

 

Set can be a collection that contains distinctive elements. This interface is used to represent data similar to the deck of cards.

 

List is a collection that is ordered and it allows duplicate elements too. You may be able to access any part from its index. List is more like an array with variable length.

 

Queue is a collection in Java that works on the First In First Out principle. The elements that are added first will be removed first from the queue.

 

Map matches keys to values. A map will not contain duplicate keys: every key can map to at the most one value.

 

 

End note:

In this blog, we took a look at the different types of questions asked in Java interviews. We also briefly glanced at various Java concepts that are commonly tested in interviews. 

 

 

Get one step closer to your dream job!

 

Prepare for your Java programming interview with Core Java Questions You’ll Most Likely Be Asked. This book has a whopping 290 technical interview questions on Java Collections: List, queues, stacks, and Declarations and access control in Java, Exceptional Handling, Wrapper class, Java Operators, Java 8 and Java 9, etc. and 77 behavioral interview questions crafted by industry experts and interviewers from various interview panels.

 

 

We’ve updated our blogs to reflect the latest changes in Java technologies.
This blog was previously uploaded on March 24th, 2020, and has been updated on January 3rd, 2022.