Explain the Encapsulation principle.
Encapsulation is a process of binding or wrapping the data and the
codes that operates on the data into a single entity. This keeps the data safe
from outside interface and misuse. Objects allow procedures to be encapsulated
with their data to reduce potential interference. One way to think about
encapsulation is as a protective wrapper that prevents code and data from being
arbitrarily accessed by other code defined outside the wrapper.
What do you understand by a variable?
Variable is a named memory location that can be easily referred in
the program. The variable is used to hold the data and it can be changed during
the course of the execution of the program.
What do you understand by numeric promotion?
The Numeric promotion is the conversion of a smaller numeric type
to a larger numeric type, so that integral and floating-point operations may
take place. In the numerical promotion process the byte, char, and short values
are converted to int values. The int
values are also converted to long values, if necessary. The long and float
values are converted to double values, as required.
What do you understand by casting in java language? What are the
types of casting?
The process of converting one data type to another is called
Casting. There are two types of casting in Java; these are implicit casting and
explicit casting.
What is the first argument of the String array in main method?
The String array is empty. It does not have any element. This is
unlike C/C++ where the first element by default is the program name. If we do
not provide any arguments on the command line, then the String array of main
method will be empty but not null.
How can one prove that the array is not null but empty?
Print array.length. It will print 0.
That means it is empty. But if it would have been null then it would have
thrown a NullPointerException on attempting to print array.length.
Can an application have multiple classes having main method?
Yes. While starting the application we mention the class name to
be run. The JVM will look for the main method only in the class whose name you
have mentioned. Hence there is not conflict amongst the multiple classes having
main method.
When is static variable loaded? Is it at compile time or
runtime? When exactly a static block is loaded in Java?
Static variable are loaded when classloader
brings the class to the JVM. It is not necessary that an object has to be
created. Static variables will be allocated memory space when they have been
loaded. The code in a static block is loaded/executed
only once i.e. when the class is first initialized. A class can have any number
of static blocks. Static block is not member of a class, they do not have a
return statement and they cannot be called directly. Cannot contain this or
super. They are primarily used to initialize static fields.
Can I have multiple main methods in the same class?
We can have multiple
overloaded main methods but there can be only one main method with the following signature :
public static void main(String[] args) {}
No the program fails to compile. The compiler says that the main
method is already defined in the class.
Explain working of Java Virtual Machine (JVM)?
JVM is an abstract computing machine like any other real computing
machine which first converts .java file into .class file by using Compiler
(.class is nothing but byte code file.) and Interpreter reads byte codes.
How can I swap two variables without using a third variable?
Add two variables and assign the value into First variable.
Subtract the Second value with the result Value. and
assign to Second variable. Subtract the Result of First Variable With Result of
Second Variable and Assign to First Variable. Example:
int a=5,b=10;a=a+b;
b=a-b; a=a-b;
An
other approach to the same
question
You use an XOR swap.
for
example:
int a = 5; int b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
What is data encapsulation?
Encapsulation may be used by creating ‘get’ and ’set’ methods in a
class (JAVABEAN) which are used to access the fields of the object. Typically
the fields are made private while the get and set methods are public.
Encapsulation can be used to validate the data that is to be stored, to do
calculations on data that is stored in a field or fields, or for use in
introspection (often the case when using javabeans in
Struts, for instance). Wrapping of data and function into a single unit is
called as data encapsulation. Encapsulation is nothing but wrapping up the data
and associated methods into a single unit in such a way that data can be
accessed with the help of associated methods. Encapsulation provides data
security. It is nothing but data hiding.
What is reflection API? How are they implemented?
Reflection is the process of introspecting the features and state
of a class at runtime and dynamically manipulate at run time. This is supported
using Reflection API with built-in classes like Class, Method, Fields, Constructors etc. Example: Using Java Reflection API we can
get the class name, by using the getName method.
Does JVM maintain a cache by itself? Does the JVM allocate
objects in heap? Is this the OS heap or the heap maintained by the JVM? Why
Yes, the JVM maintains a cache by itself. It creates the Objects
on the HEAP, but references to those objects are on the STACK.
What is phantom memory?
Phantom memory is false memory. Memory that does
not exist in reality.
Can a method be static and synchronized?
A static method can be synchronized. If you do so, the JVM will
obtain a lock on the java.lang.
Class instance associated with the object. It is similar to saying:
synchronized(XYZ.class) {}
What is difference between String and StringTokenizer?
A StringTokenizer is utility class used
to break up string.
Example:
StringTokenizer st =
new StringTokenizer(”Hello World”);
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
Output:
Hello
World
what is difference between
class and object?
Class is a template(type) or blue print its state how
objects should be and behave. eg consider a construction
plan which is a class with this description mentioned in a
plan we can constructs 'n' number of buildings of same type.
these buildings are consider to be an objects come under
same type of that plan.
Object is defined as a software construct which binds data
and logic or methods(functions)together.
No comments:
Post a Comment