Core Java Interview Questions - pdf-2 PDF
Core Java Interview Questions - pdf-2 PDF
Core Java Interview Questions - pdf-2 PDF
TestLeaf
Core Java Interview Questions TestLeaf
Integer count =
Integer.valueOf(2);
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.
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
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
So, if you wish to have only numbers, then use this code
str.replaceAll("\\D+",""); // this will delete the non-digits
Core Java Interview Questions 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
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
Once created, cannot can change the value of can change the value of
change the object the object
Example String SA = new String StringBuffer SA = new StringBuilder SA = new
("Test"); StringBuffer("Test"); StringBuilder ("Test");
String SB = SA; StringBuffer SB = SA; StringBuilder SB = SA;
SA = SA+"Leaf"; SA = SA.append("Leaf"); SA = SA.append("Leaf");
//constructor
public Point(int x, int y) {
this.x = x; // this points to the local variable and not argument variable
this.y = y;
}
}
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.
Core Java Interview Questions TestLeaf
As we know, for all the primitive types we have wrapper classes such as Integer,
Long etc that provides some additional methods.
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.
Core Java Interview Questions TestLeaf
finalize() is a special method in Object class that we can override in our classes.
This method get’s 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.
Core Java Interview Questions TestLeaf
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.
1 import java.lang.Math;
2
3 //inside class
4 double test = Math.PI * 5;
We can do the same thing by importing the static method or variable only and then
use it in the class as if it belongs to it.
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 can’t change it.
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, it’s better to
use interfaces as super class in most of the cases.
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
if we have no control in designing superclass, it can lead to security holes.
Composition allows us to provide restricted access to the methods and hence more
secure.
We can get runtime binding in composition where inheritance binds the classes
at compile time. So composition provides flexibility in invocation of methods.
We can run the garbage collector with code Runtime.getRuntime().gc() or use utility
method System.gc()
System class is final so that we can’t subclass and override it’s behavior through
inheritance. System class doesn’t provide any public constructors, so we can’t
instantiate this class and that’s why all of it’s methods are static.
Some of the utility methods of System class are for array copy, get current time,
reading environment variables.
Heap memory is used by all the parts of the application whereas stack memory
is used only by one thread of execution.
Whenever an object is created, it’s 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 it’s more complex
in Heap memory because it’s used globally.