Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday, 31 August 2017

Interview Questions on Final Keyword in Java

Final Keyword Interview Questions in Java

Interview Questions on Final Keyword In Java

Here we are going to cover all the interview questions on final keyword in java. This is the most important article on final keyword which is mostly asked in any core java interviews.

In last post we have learned some interview questions on Exception Handling in java you can check this post.

There are many java keywords interview questions asked in core java interviews e.g static keyword in java interview questions, throw keyword interview questions, this keyword, etc. But here we will learn only java final keyword interview questions.


(1) What is final in Java?

Java final is a keyword or non-access modifier which is used to restrict the user.

(2) Where you can use final keyword in java?

There are many areas where we can use final keyword.
  • It can be used with the class.
  • It can be used with variables.
  • It can be used with methods.

(3) Can we inherit final class?

No, We cannot extend or inherit final class. If we will extend the final class, it will throw compile time error.


(4) Can we change the value of final variables?

No, After initializing final variable we cannot change the value of final variable because it becomes constant.

(5) Can we override final method?

No, We can't override final method in java programming language.

(6) Can we apply final keyword with main() method?

Yes, we can use final keyword with main() method in java.


(7) What is blank final variable?

A final variable which is not initialized at the time of declaration is known as blank final variable e.g...

final int a;//blank final variable

(8) What is static blank final variable?

A variable which is declared with static and final keyword without any initialization is called static blank final variable e.g...

static final int a;//static blank final variable

(9) How we can initialize blank final variables?

We can initialize blank final variables only in a constructor.

(10) How we can initialize static blank final variables?

We can initialize static blank final variable in static block only.


(11) Can we inherit final method in sub-class?

Yes, we can inherit final method in sub-class or child class.

(12) Can we declare java constructor as final?

No, we can't apply final keyword with the constructor.

(13) Can we create object for final class?

Yes, we can create object of final class but we cannot extend final class.

(14) Can we make interface as final?

No, we can't make interface as final because interface must be implemented in other classes.


(15) Difference between abstract method and final method in java?

Abstract method must be implemented or overridden in child class but we cannot override final method in child class or subclass.

(16) What is the difference between final, finally and finalize?

There are many differences between final, finally and finalize in java.

(17) What is the most common predefined final class object you used in a program?

Java String is a predefined final class object.

(18) Can we use non-final local variables inside a local inner class?

No, only final local variables can be used inside a local inner class.

Visit links:

Java Static Keyword Interview Questions.
Java Constructor Interview Questions.
Java Super Keyword Interview Questions.
Java Multithreading Interview Questions.
Java Interface Interview Questions.
Some Java Programs Using Final Keyword.

Here we discussed interview questions and answers on final keyword in java which will increase your concept skill for java questions on final keyword.

Share:

Difference Between HashSet and HashMap in Java

HashSet vs HashMap

HashSet vs HashMap

Here we will discuss, what is the difference between HashSet and HashMap in java. This is really very nice and useful and mostly asked java interview question.

In the last post, we have discussed that what is the difference between HashMap and Hashtable class in java collection framework but here we will learn only what is the difference between HashMap and HashSet in java with example.


Java HashSet

  • HashSet class implements the Set interface.
  • HashSet internally uses HashMap to store it elements.
  • HashSet class doesn't contain duplicate elements i.e it stores only unique elements.
  • In HashSet, There is no key-value combination for storing data. We can insert only elements or values.
  • According to some developers, Java HashSet performance is slower than HashMap.
  • HashSet uses add(object) method to store the elements.
  • There are only one null value allow in hash set.


HashSet Example in Java

This is the simple java HashSet example where we will insert some elements and iterate all the elements one by one.

import java.util.*;
class Demo
{
public static void main(String args[])
{
//declaring hash set
Set<String> hs = new HashSet<String>();
hs.add("java");//inserting elements
hs.add("c++");
hs.add("c#");
hs.add("html");
hs.add("css");
hs.add("java");
hs.add(null);

//iterating one by one
Iterator i = hs.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

Output: c#
             null
             c++
             html
             java
             css


Java HashMap

  • HashMap class implements Map interface.
  • HashMap class internally uses array of Entry<k,v> to store the elements.
  • HashMap class doesn't allow duplicate key but it allows duplicate values.
  • HashMap uses key-value combination for storing the data.
  • HashMap performance is faster than HashSet in java.
  • HashMap uses put(key, value) method to store the data.
  • There can be one null key and multiple null values in hash map.


HashMap Example in Java


This is the second example in HashSet vs HashMap java. Java HashMap store the data on the basis of key and value pairs.


import java.util.*;
class Simple
{
public static void main(String args[])
{
//creating HashMap

HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(101, "Ram");
hm.put(102, "Ram");
hm.put(103, "Suman");
hm.put(103, "Kiran");
hm.put(104, "Sekhar");
hm.put(105, "Vimal");

for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

Output:  101 Ram
              102 Ram
              103 Kiran
              104 Sekhar
              105 Vimal

In the above example, There can be duplicate values in hash map but there cannot be duplicate key in hash map class in java. It overrides duplicate keys.


Similarities Between HashSet and HashMap class in Java

There are some similarities between these two classes in collection framework. These are...

HashSet Class: - HashSet class are not synchronized i.e it is not thread-safe. There is no guarantee to maintain the insertion order of an element. It provides fast performance like HashMap but HashMap is much faster than hash set.

HashMap Class: HashMap class are not synchronized like hash set class.
HashMap also does not maintain insertion order of an element like hash set class. Both hash set and hash map provide fast performance.

Read More:

Difference Between ArrayList and LinkedList in Java.
Difference Between ArrayList and Vector in Java.
Difference Between HashMap and Hashtable.
Difference Between Abstract Class and Interface in Java.
Difference Between Array and Collection.
Java Collection Interview Questions.

I hope, the difference between HashSet and HashMap in java post will help you to handle java related interviews specially in HashSet vs HashMap.


Share:

Tuesday, 29 August 2017

Interview Questions on Exception Handling in Java

Java Exception Handling Interview Questions

java exception handling interview questions

Now, Here we are going to cover the most important topic from java interview point of view is the interview questions on exception handling in java.

There are many exception handling interview questions in java which is mostly asked in any java interviews.

So without any delay, let's start.


(1) What is an exception?

Exception means "abnormal condition", Exception is an abnormal condition that arises during the execution of the program and it disrupts the normal flow of the program. In other words, Exceptions are the objects and represents the logical errors that occur at run-time.


(2)  What is exception handling in java?

8 Exception handling means handle the exception by using some handler and maintain the normal flow of the programs. Exception handling is mainly used to handle checked exception.

(3) How can you handle an exception in java?

There are 3 ways we can handle the exception. We can use three handler - try, catch and finally block to handle the java exception.

(4) What is try-catch in exception handling?

In java try and catch is a most important block or handler which is used to handle the exceptions and finally block also used.

try Block: - Doubted statements or code i.e that can throw an exception, kept in this block.

catch Block: - This block is used to catch or handle the exception which is thrown from the try block.


(5) In which java package, Exceptions are defined?

Exception definition is defined in java.lang package.

(6) Which are the top most or super class for exception and error?

Throwable is the super or top most class for exception and error.


(7) What is finally in java?

Finally is a block in java which always executes whether the exception occurs or not and whether exception handled or not.

(8) Can we use try block without a catch or finally block?

No, We can't use try block without a catch or finally block.

(9) Can we use try block without catch block?

Yes, We can use try block without catch block but with finally block.

(10) Can we use multiple catch block with single try block?

Yes, We can.

(11) Can we use try, catch and finally together?

Yes, We can use try, catch and finally together in an exception handling program but finally block must be the last block.


(12) Can we use try block within another try block?

Yes, We can use try block into another try block. It is called nested try block.

(13) How many types of exception are there in exception handling?

There are two types of exception (1) Checked Exception and (2) Unchecked Exception.

(14) What is the difference between checked and unchecked exception?

Checked Exception: - Checked exception is an exception which is caught by the compiler at compile time is called checked exception e.g IOException, SQLException, etc.

Unchecked Exception: - Unchecked exception is a exception which occurs at run-time e.g ArithmeticException, NullPointerException and ArrayIndexOutOfBoundsException.


(15) What is the difference between exception and error?

Error: - This is a system issue and it happens at run-time.

Exception: - This is a wrong logical error in programs and can occur at compile-time or run-time.

(16) Can we keep other statements in between try, catch and finally block?


No, we can't.

For example:

class Demo
{
public static void main(String args[])
{
try
{
int i = 44/0;
System.out.println(i);
}
System.out.println("first");//compile-time error
catch(Exception e)
{
System.out.println(e);
}
System.out.println("second");//ct error
finally
{
System.out.println("finally block");
}
}
}


(17) Can we write multiple exceptions in single catch block?

Yes, It is 100% possible.

For example:

class Test
{
public static void main(String args[])
{
try
{
//code;
}
catch(Exception | ArithmeticException e)
{
//code;
}
}
}

(18) Difference between throw and throws keyword in java?


throw Keyword

  • throw keyword is basically used for custom exception and it is used to explicitly throw an exception.
  • throw is used within the method.
  • It is followed by an instance.
  • By the help of throw keyword, we cannot forward checked exception in calling chain.

throws Keyword:
  • It is used to declare an exception.
  • It is used with the method signature.
  • It is followed by a class.
  • By the help of throws keyword we can forward checked exception in calling chain.

(19) Difference between final, finally and finalize?


(20) When finally block does not get executed?

If we call System.exit() then finally block will not execute.

(21) What is the custom exception?

Custom exception is also called user define exception when user creates own exception is called a custom exception. By extending Exception class we can create the custom exception.

(22) Can we write return statements in try or finally block?

Yes, we can write return statements in both try and finally block.

For example:

int add()
{
try
{
return 30;
}

Visit Other Java Interview Articles, link given below.

Java Multithreading Interview Questions.
Servlet Interview Questions.
JSP Interview Questions.

I hope, this exception handling interview questions and answers post will help you to make java interview easy.

Share:

Sunday, 27 August 2017

Interview Questions on String in Java

String Interview Questions in Java

String Interview Questions

This post is all about of interview questions on String in java. Here we will cover all the java String handling interview questions and answers.

So let's start with string interview questions.


(1) What is String?

String is a sequence of characters i.e java string represents sequence of characters e.g "hi, i am anurag".

(2) Is String a keyword in java?

No, String is not a keyword in java. String is a final class and it is belongs to the java.lang package.


(3) How many ways we can create string objects?

There are two ways we can create string objects, The first is through literals(using double quotes) and the seconds is through new operator.

For example :

String name = "programming";//through literal

String name = new String("welcome");// through new operator


(4) What is string constant pool?

String constant pool is a space in heap memory when we create string object using literals(i.e using double quotes (" ")) then it goes into the constant pool. String constant pool does not take duplicate string object. It returns the same reference.

(5) is string is a primitive type or derived type?

String is not a primitive data type, it is derived type.

(6) is string mutable or immutable in java?

It is immutable in java i.e after creating string object we cannot update or modify this string object.


(7) is StringBuffer mutable or immutable?

StringBuffer is a mutable in java i.e it can be modified or change or update after created.

(8) is StringBuilder mutable or immutable?

StringBuilder object is a mutable in java.


(9) How many objects will be created in the following code?

String s = "welcome";

String s1 = "welcome"

only one object will be created in the constant pool because constant pool does not take duplicate object.

(10) What is toString() in java?

Java toString() is a method and it returns the string representation of any object and toString() method belongs to the Object class in java.

(11) Why string is declared immutable or final in java?

There are many reasons for this.
  • Security
  • Thread-safe
  • String pool

(12) What is the difference between String, StringBuffer and StringBuilder?

StringJava string object is a immutable i.e unmodifiable or unchangeable. It can be created by two ways first is using literal and second is new keyword.

StringBuffer: Java StringBuffer is a mutable i.e modifiable or changeable. It can be created by only one way through new keyword. StringBuffer is a synchronized i.e thread-safe.

StringBuilder: Java StringBuilder is a mutable just like StringBuffer but it is non-synchronized.


(13) How many object will be created in the following code?

String s = new String("welcome");

2 object will be created. One is through new keyword in heap memory and second is through literal in string constant pool.

(14) Why java uses literal concept in string?

Because it makes memory efficient(constant pool does not take duplicate objects which created by using string literal).

(15) Difference between " equals() " and " == "?

  • In java equals() is a method which comes from Object class whereas == is an operator.
  • In java equals() method is used to comparing the content of two string objects. If it is matched, return true otherwise false whereas == operator is used to comparing the reference of two strings.

(16) What is StringTokenizer in java?

StringTokenizer class belongs to the java.util package and it is used to breaks the string into tokens with the help of delimiters(symbols or spaces).

Read More: 

Java String Programming Interview Questions.
Java Collection Interview Questions.
Java JSP Interview Questions.
Difference Between String and StringBuffer in Java.
Difference Between Array and Collection in Java.

This String interview questions java will help you in any java interviews.

Share:

Thursday, 24 August 2017

Interview Questions on Interface in Java

Java Interface Interview Questions and Answers

Interview Questions on Interface in Java

Here we are going to cover all the top most interview questions on interface in java one by one. This article is very important for any java interview point of view.

Now start here in detail java interface questions and answers.


(1) What is interface in java?

Java interface is a blueprint of a class and it is used to achieve fully abstraction and it is a collection of abstract methods.

(2) Can we achieve multiple inheritance by using interface?

Yes, we can achieve multiple inheritance through the interface in java but it is not possible through class because java doesn't support multiple inheritance through class.


(3) How to declare an interface, write a syntax?

The syntax of declaring the interface in java by using interface keyword :

interface FirstInterface
{
fields;
methods;
}

(4) Can we create an object of an interface?

No, we cannot create an object of interface.


(5) Can we declare the interface as final?

No, we can't declare the interface as final because the implementation of the interface is provided by another class. If we make the interface as final, it will throw a compile-time error.


(6) Which keyword java compiler add before interface fields and methods?

In an interface, Java compiler adds public, static and final keywords before fields or data members and add public abstract keywords before methods. In other words, all the fields are public, static and final and all the methods are public and abstract by default in an interface.


(7) Does interface extend Object class by default?

No, Interface does not extend Object class in java by default but all the classes extend Object class by default.

(8) Can an interface extend another interface?

Yes, an interface can extend another interface.


(9) Can an interface extend a class?

No, A class can implement an interface but interface cannot extend a class.


(10) Can we put a static method in interfaces?

No, we cannot put static methods in interfaces because all the methods are by default public and abstract in the interface and we cannot use abstract and static keywords together.


(11) Can we declare an interface with the abstract keyword?

Yes, we can declare abstract keyword with interfaces but there is no need to write abstract keyword with interfaces because all the interfaces are abstract by default.


(12) What is default keyword in an interface?

By the help of default keyword, we can keep non-abstract method in java interface i.e with method body{}. This is the new feature of JAVA 8.

Syntax of default keyword :

interface Test
{
default void show()
{
-----
-----
}
}

(13) What is marker or tagged interface?

Marker interface is an interface that has no data member and method like Serializable, Cloneable, etc.


(14) Can we declare a constructor in the interface?

No.

(15) After compilation of interface program, .class file will be generated for every interface in java... true or false.?

This is true.

(16) Can we change the value of a field in interface after initialization?

No, Because all the fields of the interface are by default final.

(17) Difference between abstract class and interface?


abstract class

  • Through abstract class, you cannot achieve multiple inheritance.
  • You can keep non-abstract method(with method body) in the abstract class.
  • In the abstract class, fields are not public, static, final and methods are not public abstract by default.

interface
  • Through the interface, you can achieve multiple inheritance.
  • In an interface, you can't keep non-abstract method but since java 8 it is possible.
  • In an interface, fields are public, static, and final and methods are public and abstract by default.

Read More : 


Above we saw some most important Interview questions and answers on java interface which is mostly asked in core java interviews.
Share:

Wednesday, 23 August 2017

Difference Between HashMap and Hashtable

What is Difference Between HashMap and Hashtable in Java?

HashMap vs Hashtable

Here we will discuss, What is difference between HashMap and Hashtable in java. This is the most frequently asked question in any java interviews.


So let's start, What is the difference between hashmap and hashtable class?

Java HashMap

  • HashMap is non-synchronized i.e multiple threads can access it simultaneously. It is not a thread-safe.
  • HashMap allows one null key and multiple null values.
  • HashMap extends AbstractMap class.
  • HashMap provides fast performance.
  • HashMap is a new class and introduced in jdk 1.2.
  • HashMap returns the only iterator to traverse.
  • The iterator of HashMap is fail-fast and it throws ConcurrentModificationException.
  • It is good for single threaded applications but if you want to use HashMap class in multithreaded applications, You will have to use Collections.synchronizedMap(hashMap).


Now let's understand HashMap vs Hashtable with examples.

Java HashMap Example

This is the simple example of hash map class where we will insert some key and values and then traverse all the elements from the map.

import java.util.*;
class Demo
{
public static void main(String args[])
{
//declaring HashMap
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1000, "suman");//inserting key and value
hm.put(3000, "hari");
hm.put(99, "pooja");
hm.put(5000, "kiran");
hm.put(20000, "bheem");
for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

Output: 20000 bheem
              99 pooja
              1000 suman
              3000 hari
              5000 kiran


Note: HashMap class contains only unique elements i.e not duplicate elements and doesn't maintain insertion order of an element.


Java Hashtable

  • Hashtable is a synchronized i.e multiple threads can access it one by one. It is thread-safe.
  • Hashtable doesn't allow any null key and null values.
  • Hashtable extends Dictionary class.
  • Hashtable provides slow performance.
  • Hashtable is a legacy class.
  • Hashtable returns both Iterator as well as Enumeration for traversal.
  • Enumeration for Hashtable is not fail-fast.
  • It is good for the multithreaded environment.



Hashtable Example in Java

This is java hashtable example Where we will insert some elements and then traverse all the elements from the table.

import java.util.*;
class Test
{
public static void main(String args[])
{
//declaring Hashtable
Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
ht.put(20, "red");//inserting elements
ht.put(10, "black");
ht.put(50, "yellow");
ht.put(7, "pink");
ht.put(90, "brown");
for(Map.Entry m : ht.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

Output: 10 black
             20 red
             7 pink
             50 yellow
             90 brown

Note: Hashtable class contains only unique elements i.e not duplicate elements and it doesn't maintain any insertion order of an element.


So there are many differences between HashMap and Hashtable in java and you can see other differences topics in collection framework like Difference between ArrayList and LinkedList, ArrayList and Vector, etc.

Similarities Between HashMap and Hashtable class

  1. Both implements Map interface.
  2. Both don't maintain insertion order of an element.
  3. Both store the data in the form of key and value pairs.
  4. Both using the hashing technique to store the key & value pairs.
  5. Both contain only unique elements i.e cannot allow duplicate elements
Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate