Greens Java Interview Questions
Greens Java Interview Questions
1.What is java?
Java is a simple and most widely used programing language.
Java is fast,reliable and secure
2.Why are we go for java?
Freeware and opensource
It is platform independent i.e program written in one
operating system is capable of running in all other operating
systems due to bytecode concept.
It runs multiple application at a time.
3.What are the main features of java?
JRE:
----
Java Runtime Environment.
It is a pre-defined class files (i.e.) library files.
JVM:
----
Java Virtual Machine.
It is mainly used to allocate the memory and compiling.
8.What is mean by oops?
OOPS is Object Oriented Programming Structure.
OOPS is a method of implementation in which programs are
organised as collection of objects, class and methods.
9.What are the coding Standard used in java?
Pascal notation: Every word's first letter ,must be a capital
letter
Example:GreensTechnology
Camel notation: First word's first letter should be a small
letter, all the other suceeding word's first letter should be a
capital letter.
Example:greensTechnology
10.What is mean by class,method,object?
Class:
------
Class is a collection of objects and methods
Class contains attributes(variables and methods) that are
common to all the objects created in a class.
Method:
-------
Method defines the set of action to be performed.
Object:
-------
Object is the run time memory allocation.
Using object we call any methods.
11.What is mean by Encapsulation?
It is the structure of creating folders.
It wraps the data and code acting on data together in to a
single unit.
Example of encapsulation is POJO class.
It is otherwise called Data hiding.
12.What are the datatypes used in java?
byte
short
int
long
float
double
boolean
char
String
13.What is byte size and range of int datatypes?
Size of byte is 1 byte (8 bit)
Range formula =[-2^(n-1)] to [(2^(n-1))-1] for int n=32
14.What is mean by Wrapper class?
Classes of data types is called wrapper class.
It is used to convert any data type into an object.
All classes and wrapper classes default value is null.
15.What is the main use of Scanner class?
To get the inputs from the user at the run time.
16.What are the methods available in Scanner Class?
nextByte();
nextShort();
nextInt();
nextLong();
nextFloat();
nextDouble();
next().charAt(0);
next();
nextLine();
nextBoolean();
17.What is mean by inheritance?
Accessing one class Properties in another class without
multiple object creation.
It avoids time and memory wastage.
It ensures code reusability
18.What are the ways to access the methods /data from
another class?
We can access the another class methods either by creating
object or using extends keyword.
19.What is mean by polymorphism?
Poly-many.
Morphism-forms.
Taking more than one forms is called polymorphism or one
task implemented in many ways.
20.What are the difference between method overloading
and overriding?
Multiple inheritance:
-------------------------
More than one parent class directly supporting into same
child class.
Multiple inheritance not supported in java due to Compilation
problem and priority problem
We have achieve multiple inheritance in java through
interface.
Multilevel inheritance:
-----------------------
More than one parent class supporting into one child class in
tree level structure.
It is supported in java
24.What is mean by access specifier?
It defines the scope or level of access for variables,methods
and classes
25.What are the difference between public and protected?
Public:
-------
It is global level access( same package + different package).
Protected:
------------
can access Inside package ( object creation + extends )
26.What is mean by Abstraction?
Hiding the implementation part or business logic is called
abstraction.
27.What are the types of Abstraction?
1. Partially abstraction(using abstract class).
2. Fully abstraction(using interface).
28.Can we create Object for Abstract class?
No, we cant create object for abstract class.
29.What is mean by Interface?
It will support only abstract method(without business logic),
won't support non abstract method(method with business
logic)
In interface "public abstract" is default.
using "implements" keyword we can implement the interface
in a class where we can write the business logic for all
unimplemented methods.
30.What are the difference between Abstract and
Interface?
Abstract class:
-----------------
Using Abstract class,we can acheive partial abstraction.
It support both abstract method and non-abstract method.
using "extends" keyword you can inherit an abstract class.
For any abstract method we need to mention "public
abstract".
Interface:
-----------
Using interface,we can acheive full abstraction.
It supports only abstract method.
It is using "implements" keyword.
"public Abstract" is default, no need to mention it explicitly.
31.What is mean by String?
Collection of characters or words enclosed within double
quotes is called as String.
String is a class in java
String is index based
Example : "greenstechnology".
32.What are the method available in string?
equals();
equalsignorecase();
contains();
split();
toUpperCase();
toLowerCase();
subString();
isEmpty();
identifyHashCode();
startsWith();
endsWith();
CompareTo();
charAt();
indexOf();
lastIndexOf();
replace();
33.What is mean by constructor?
Constructor is a special method which is called by default
when object is created for that particular class.(implicit call)
Class name and constructor name must be same.
It doesn't have any return type.
It supports method overloading but won't support method
overriding.
purpose of constructor:It is used to initialise the values to
variables.
34.Explain the types of constructor?
Parameterized constructor
Non parameterized constructor
35.Do constructors have any return type?
No,constructor can't have any return type.
36.Write a syntax for creating constructor?
Final:
-----
Final varaible can't be modified.
Final method can't be overrided.
Final class can't be inherited.
Finally:
--------
Code given inside finally block will always get executed
whether exception occurs or not.
53.Where local,static and class variables stores in jvm?
Static variables are stored in the permGen section of heap
memory.
Local variables are stored in stack.
Class variables are stored in heap memory.
54.What is Exception?
Exception is an unexpected event which when occurs in a
program,your program will terminate abnormally.
We can avoid this abnormal termination using exception
handling mechanisms(try,catch,finally,throw,throws)
55.Explain about types of Expection?
Unchecked exception(Run time exception)
Checked exception(Compile time exception)
56.What are the difference between checked expection
and unchecked expection?
Unchecked exception:
-------------------------
It will occur at the Run time.
Checked exception:
----------------------
Checked exception will occur at the Compile time.
57.What is the super class for Exception and Error?
Throwable
Exception
58.Can we have try block without catch block?
Yes we can have try block without catch block.But in that
case finally block must be present.(There will be no syntax
error)
Possible but we will not able to handle the exception without
catch block.
59.Can we write multiple catch blocks under single try
block?
Yes,we write multiple catch blocks under single try block.
60.How to write user defined exception or custom
exception in java?
First customised exception must come under Exception class.
access_specifier method_name() throws customException {
throw new customException();
}
61.What are the different ways to print exception message
on console?
ref.printStackTrace() method is used to print the exception
message in the console.
62.What are the differences between final finally and
finalize in java?
Final:
-----
A final class variable whose value cannot be changed.
A final method is declared in class level, they cannot be
inherited.
A class declared as final can't be inherited.
Finally:
--------
It’s a block of statement that definitely executes after the try
catch block.
Exception occurs or not,finally block always get executed.
Finalize:
---------
It will clean up unused memory space.
63.What are the differences between throw and throws?
Throw:
------
Throw is a keyword, using which we can throw any any
exception.This keyword always given inside the method.
At a time we can throw only one exception using throw
keyword.
Throws:
---------
Throws is a keyword, it is used to handle the exception(given
in method level).
we can handle more than one exception using throws
keyword.
64.Explain Java Exception Hierarchy?
Exception
Checked
Unchecked exception(Run exception(Compile time
time exception) exception)
ArithmeticException IOException
NullPointerException SQLException
InputMismatchException FileNotFoundException
ArrayIndexOutOfBoundExcepio ClassNotFoundException
n
StringIndexOutOfBoundExcepio
n
IndexOutOfBoundExcepion
NumberFormatException
Advantage:
-------------
In a single variable we can store multiple values.
Disadvantages:
----------------
It support only similar data types.
Size fixed at compile time.
Memory wastage is high.
68.Different ways to intialise array?
Datatype refName[]= new Datatype[size];
Datatype[] refname={ value1,value2,....};
69.Can we change the memory size of array after
intialization?
No,we can't change the memory size of array after
intialization.
70.What is collection ?
It will support storage of multiple values with dissimilar data
types.
It is dynamic memory allocation.
No memory wastage like array.
71.What is the difference between ArrayList and Vector?
ArrayList:
----------
Asynchronized
It is not a thread safe
Vector:
-------
Synchronized
Thread safe
72.What is the difference between ArrayList and
LinkedList?
LinkedList:
-----------
Insertion and deletion is a best one.
Searching/retrieving is a worst.
It’s makes performance issue.
ArrayList:
----------
In Arraylist retrieve/searching is a best one
In ArrayList deletion and insertion is a worst one because if
we delete/insert one index value after all the index move to
forward/backward.
It makes performance issue.
73.Difference between Collection and Collections
Collection-Collection is an interface under which we have
list,set,queue
Collections-is an utility class in which we have lots of
predefined methods which we can apply over collection
objects.
Eg:Collections.min(),Collections.max(),Collections.sort()
74.Describe the Collections type hierarchy ? What are the
main interfaces ?
Collection:
------------
List
Set
Map----doesnt come under collection,it is a separate interface in
java
Hierarchy:
-----------
List:
----
ArrayList
LinkedList
Vector
Set:
----
Hashset
LinkedHashSet
Treeset
Map:
----
HashMap
LinkedHashMap
Hashtable
TreeMap
ConcurrentHahMap
75.What is difference between set and List?
Set:
----
It is a value based one.
It prints in random order.
It won't allow duplicates.
List:
-----
It is a Index based one.
It prints in insertion order.
It allow duplicates.
76.What is the difference between HashSet and TreeSet ?
HashSet:
---------
It prints in random order.
TreeSet:
---------
Treeset prints in ascending order
77.How to convert List into Set?
By addAll() we can convert List into set.(all the elements in
list will get added to set)
78.What is map?
It is key and value pair.
Here key+value is one entry.
Key ignore the duplicate value and value allow the
duplicates.
79.What is difference between Hash Map and Hash Table?
HashMap:
----------
Key allows single null.
Asynchronised(not thread safe).
Hashtable:
----------
Key and value won't allow null.
Synchronised(thread safe).
80.What is difference between set and Map?
Set:
----
It is a value based one.
It print in random order.
It won't allow duplicates.
Map:
----
It is key and value pair.
Here key+value is one entry.
Key ignore the duplicate value and value allow the
duplicates.
81.Can we iterator the list using normal for loop?
Yes,we can iterate the list using both normal and enhanced
for loop.
82.What are the methods available in list But not in set?
indexOf();
get();
lastIndexOf();
83.Explain about user defined Map?
It is key and value pair.
Here key+value is one entry.
Key ignore the duplicate value and value allow the
duplicates.
84.How much null allows in below maps:
HashMap :k?,v?
LinkedHashMap:k?,v?
TreeMap :k?,v?
HashTable :k?,v?
HashMap :k-1 null,v- n null
LinkedHashMap:k-1 null,v- n null
TreeMap :k-ignore null,v- allow null
HashTable :k-ignore null,v- ignore null
85.How to Iterate Map?
We can iterate the map by using entrySet() method.
86.What is the return type of entrySet?
Set<Entry<key,value>>
87.Write the methods to get the key only and value only?
For key only keySet() method is used.
For value only values() method is used.
88.What is mean by File? In which package it is available?
File is a class and it is used to achieve the file operation.
It is available in java.io package.
89.What are the methods available in File ?
mkdir();
mkdirs();
list();
createNewFile();
isDirectory();
isFile();
isHidden();
90.While creating a file if we not mention the format then
under which format it will save the file?
If we do not mention the file format it will automatically take
format as file.
91.What are the difference between append and updating
the file?
Enumeration:
--------------
It is an interface used to iterate only legacy class or
interface.
Only iterates in forward direction
Iterator:
---------
It is an interface used to iterate the collection objects
Only iterates in forward direction
List Iterator:
--------------
It is an interface used for iterating list type classes
iterates in forward as well as backward direction
93.Difference between Enumurator,Iterator and List
Iterator?
Enumerator:
------------
applicable only for legacy class and interface
no remove method is available.
no Backward direction is possible
Iterator:
----------
It is an Interface used to iterate the collection objects
remove method is available.
no Backward direction is possible.
ListIterator:
--------------
It is an interface used for iterating list type classes
remove method is available.
Backward direction is possible.
94.What are the methods available in
Enumerator,Iteratorand List Iterator?
Enumerator Methods:
------------------
hasMoreElements();
nextElement();
Iterator Methods:
----------------
hasNext();
next();
remove();
ListIterator Methods:
---------------------
hasNext();
next();
remove();
hasPrevious();
previous();
95.Explain JDBC connection steps?
Import JDBC packages.
Load and register the JDBC driver.
Open a connection to the database.
Create a statement object to perform a query.
Execute the statement object and return a query resultset.
Process the resultset.
Close the resultset and statement objects.
Close the connection.
96.What are control statement?
Statement which has control over the loop or program is
called control statements.
Example:if,if else,for,while,dowhile etc
97.Different control statements available in java
Break:
------
It is used to terminate the loop
Continue:
----------
It is used to skip the current iteration.
while and do while
----------
While:
------
It is entry check loop.
Do While:
---------
It is a exit check loop.
if and if else
===============
if
--
executes only when the condition becomes true.
if else
--------
executes the else part when the condition becomes false and
executes if part when condition becomes true.
98.Difference between immutable and mutable string
immutable and mutable string
==============================
Immutable string:
-----------------
Once created,we cant change the value in memory
In concatenation, it will create new memory
mutable string:
----------------
After creation,we can modify the value in reference(memory)
In concetanation, its takes same memory
99.Difference between Remove all() and Retain all
Remove all() and Retain all
============================
removeAll():
------------
removeAll() is a method , it is used to compare the 2 lists
and remove all the common values
retainAll():
------------
retainAll() is a method, it is used to compare both lists and
retains only the common values
100.Difference between Literal String and Non literal
string
Literal String and Non literal string
======================================
Literal String:
---------------
In case of String duplicates,it will share the same memory
address
Its stored inside the heap memory(string pool or string
constant).
It share the memory if same value (duplicate value)
Non literal string:
--------------------
Even incase of String duplicates,it will have different memory
address.
It’s stored in the heap memory.
Its create a new memory every time even if it is a duplicate
value(same value)
101.Difference between Heap and stack memory
Heap and stack memory
=================
Heap memory:
------------
Heap is used for dynamic memory allocation.
Memory access is slow.
Static memory:
--------------
Stack is used for static memory allocation.
Variables allocated on the stack are stored directly to the
memory and access will be very fast.
102.What is the default Package in java?
java.lang
103.What are the difference between equals() &
hashcode()?
equals:
-------
Used to compare the two string.
Hashcode:
----------
Used to return the address where it stored.
104.How can we make Array list As a synchronized?
collections.SynchronisedList(refName of array);