Core Java Interview Questions
Core Java Interview Questions
TestLeaf
TestLeaf
HashMap
HashTable
non-synchronized
synchronized
Thread-safe
Allow Null
Faster
Slower in relative
Synchronization
Overloading
Overriding
Meaning
argument(s)
When it occurs?
Why?
During compile
@ Runtime
Customized implementation of
the program.
Where?
Within a class
Return type
Same
Can be different
Interface
Abstract Class
Meaning
implementation to it).
instance variables
Cannot have
Can have
Constructor
Cannot have
Can have
Slower in relative
Faster
Relationship
Speed
TestLeaf
List
ArrayList
interface
implementation of List
interface
Length
Stores
in Java
ArrayList
int
Integer
Example
int count = 2;
Integer count = 2;
which is interpreted as
Integer count =
Integer.valueOf(2);
Set
List
and unordered
allow duplicates
getWindowHandles() returns
Vector
ArrayList
synchronized
non-synchronized
Slower in relative
Faster
resized
TestLeaf
10. What will happen if you call return statement or System.exit on try or
catch finally block?
Finally block will execute even if you put return statement in try block or catch block
but finally block won't run if you call System.exit form try or catch.
11.
You cannot override private or static method in Java, if you create similar method with
same return type and same method arguments that's called method hiding.
12.
You cannot override private or static method in Java, if you create similar method with
same return type and same method arguments that's called method hiding.
13.
static
final
Scope
Variables, methods
How it works?
do not require an
object
Speed
14.
Slower in relative
Faster
Creating an object of other class in your class and calling other class variables and
methods is known as composition (known as has-a relationship as one class "has a"
object of other class).
15.
How to fetch only specific character from a given string like only numbers,
special characters etc.?
This can be done either of the given ways:
a) Use Regular Expressions with predefined character classes. For example, here are
the pattern API predefinitions in Java:
Construct
Description
\d
A digit: [0-9]
\D
A non-digit: [^0-9]
\s
\S
\w
\W
So, if you wish to have only numbers, then use this code
str.replaceAll("\\D+",""); // this will delete the non-digits
TestLeaf
b) Use ASCII characters to find and remove the unexpected values from the given
string. For example, the ascii values for 0 to 9 is 47 to 58. In that case, use
the following code, to delete others
final StringBuilder sb = new StringBuilder(input.length())
for(int i = 0; i < input.length(); i++){
final char c = input.charAt(i);
if(c > 47 && c < 58){
sb.append(c);
}
}
return sb.toString();
16.
How to fetch only specific character from a given string like only numbers,
special characters etc.?
Features
String
StringBuffer
StringBuilder
Mutable
Immutable
mutable
mutable
Example
change
the object
the object
StringBuffer SA = new
StringBuffer("Test");
StringBuffer SB = SA;
SA = SA.append("Leaf");
StringBuilder SA = new
StringBuilder ("Test");
StringBuilder SB = SA;
SA = SA.append("Leaf");
System.out.println(SA);
System.out.println(SA);
System.out.println(SA);
System.out.println(SB);
System.out.println(SB);
System.out.println(SB);
// SA will print as
TestLeaf; whereas SB
print as - TestLeaf
print as - TestLeaf
Thread-Safe
Thread-Safe
Not Thread-Safe
Synchronized
Not Synchronized
Very slow
Fast
threads simultaneously.
Performance
17.
Fast
You can use Math class floor method or in case of double - convert to int to deduct
from the original value;
double d = 12345.4566; // option 1
double v = d - (int) d;
System.out.println(v);
Float f = 12345.4566f; // option 2
v = f - Math.floor(f);
System.out.println(v);
String s = ""+12345.4566; // option 3 (if you need just the decimals)
int index = s.indexOf('.');
System.out.println(Integer.parseInt(s.substring(index + 1)));
TestLeaf
18.
extends
implements
class.
interface
How many?
as multilevel inheritance.
19.
20.
TestLeaf
21.
22.
23.
Wait
sleep
Class
java.lang.Object
java.lang.Thread
Method Type
Instance
Static
Can awake?
No, cannot
notifyAll() method
Condition based?
24.
Yes
No
JDK provides all the tools, executables and binaries required to compile, debug and
execute a Java Program. The execution part is handled by JVM to provide machine
independence.
TestLeaf
26.
27.
28.
As we know, for all the primitive types we have wrapper classes such as Integer,
Long etc that provides some additional methods.
29.
Classpath is specific to java and used by java executables to locate class files. We
can provide the classpath location while running java application and it can be a
directory, ZIP files, JAR files etc.
TestLeaf
31.
32.
33.
34.
35.
finalize() is a special method in Object class that we can override in our classes.
This method gets called by garbage collector when the object is getting garbage
collected. This method is usually overridden to release system resources when object
is garbage collected.
TestLeaf
37.
Since an anonymous class has no name, it is not possible to define a constructor for
an anonymous class. Anonymous inner classes are accessible only at the point where
it is defined.
38.
39.
TestLeaf
41.
If a catch block handles multiple exception, you can separate them using a pipe (|)
and in this case exception parameter (ex) is final, so you cant change it.
42.
What is an interface?
Interfaces are core part of java programming language and used a lot not only in JDK
but also java design patterns, most of the frameworks and tools. Interfaces provide
a way to achieve abstraction in java and used to define the contract for the
subclasses to implement.
Interfaces are good for starting point to define Type and create top level hierarchy
in our code. Since a java class can implements multiple interfaces, its better to
use interfaces as super class in most of the cases.
43.
44.
TestLeaf
46.
47.
48.
49.
50.
TestLeaf
Any change in the superclass might affect subclass even though we might not
be using the superclass methods. For example, if we have a method test() in subclass
and suddenly somebody introduces a method test() in superclass, we will get
compilation errors in subclass. Composition will never face this issue because we
are using only what methods we need.
Inheritance exposes all the super class methods and variables to client and
We can get runtime binding in composition where inheritance binds the classes
52.
53.
D.
Extensions Class Loader It loads classes from the JDK extensions directory,
System Class Loader It loads classes from the current classpath that can be
set while invoking a program using -cp or -classpath command line options.
54.
TestLeaf
56.
57.
58.
59.
60.
TestLeaf
62.
63.
64.
65.
Heap memory is used by all the parts of the application whereas stack memory
Whenever an object is created, its always stored in the Heap space and stack
memory contains the reference to it. Stack memory only contains local primitive
variables and reference variables to objects in heap space.
Memory management in stack is done in LIFO manner whereas its more complex