(FAQ) General Java Interview Questions
FAQ : General Java Interview Questions
Why do we need public static void main(String
args[]) method in Java
We need
-
public: The method can be accessed outside the class /package
-
static: You need not have an instance of the class toaccess the method
-
void: Your application need not return a value, as the JVMlauncher would return the value when it exits
-
main(): This is the entry point for the application
If
the main() was not static, you would require an instance of theclass in
order to execute the method.
If this is the case, what would create the instance of the class? Whatif
your class did not have a public constructor?
Whatis the difference between an Interface
and an Abstract class
In
abstract class you can defineas well as declare methods, the methods which
are declared are to bemarked as abstract.
In interface all we just declare methods and the definition is providedby
the class which is implementing it
Explainserialization
Serialization
means storing astate of a java object by coverting it to byte stream
Whatare the rules of serialization
Rules:
1. Static fileds are not serialized because they are not part of anyone
particular object
2. Fileds from the base class are handled only if hose are serializable
3. Transient fileds are not serialized
Whatis difference between error and exception
Error
occurs at runtime andcannot be recovered, Outofmemory is one such example.
Exceptions on the other hand are due conditions which the application
encounters such asFileNotFound exception or IO exceptions
Whatdo you mean by object oreiented
programming
In
object oreinted programmingthe emphasis is more on data than on the
procedure and the program isdivided into objects.
The data fields are hidden and they cant be accessed by externalfunctions.
The design approach is bottom up.
The functions operate on data that is tied together in data structure
What are 4 pillars of objectoreinted
programming
1.
Abstraction
It means hiding the details and only exposing the essentioal parts
2. Polymorphism
Polymorphism means having many forms. In java you can see polymorphismwhen
you have multiple methods with the same name
3. Inheritance
Inheritance means the child class inherits the non private propertiesof
the parent class
4. Encapsulation
It means data hiding. In java with encapsulate the data by making
itprivate and even we want some other class to work on that data then
thesetter and getter methods are provided
Differencebetween procedural and object
oreinted language
In
procedural programming theinstructions are executed one after another and
the data is exposed tothe whole program
In OOPs programming the unit of program is an object which is nothingbut
combination of data and code and the data is not exposed outsidethe object
Whatis the difference between constructor and
method
Constructor
will beautomatically invoked when an object is created whereas method has
tobe called explicitly.
Whatis the difference between parameters and
arguments
While
defining method, variablespassed in the method are called parameters.
While using those methods,values passed to those variables are called
arguments.
Whatis reflection in java
Reflection
allows Java code todiscover information about the fields, methods and
constructors ofloaded classes and to dynamically invoke them
Whatis a cloneable interface and how many
methods does it contain
It
is not having any methodbecause it is a TAGGED or MARKER interface
What'sthe difference between a queue and a
stack
Stacks
works bylast-in-first-out rule (LIFO), while queues use the FIFO rule
Canyou make an instance of abstract class
No
you cannot create an instanceof abstract class
Whatare parsers
Parsers
are used for processingXML documents. There are 2 types of parsers DOM
parser and SAX Parser
Differencebetween SAX and DOM parser
DOM
parsers are Object based andSAX parsers are event based
DOM parsers creates Tree in the memory whereas SAX parser does not
andhence it is faster than DOM
DOM parser are useful when we have to modify the XML, with SAX parseryou
cannot modify the xml, it is read only
Whatis the difference between Java Bean and
Java Class
Basically
a Bean is a java classbut it has getter and setter method and it does not
have any logic init, it is used for holding data.
On the other hand the Java class can have what a java bean has and alsohas
some logic inside it
Whatare null or Marker interfaces in Java
The
null interfaces are markerinterfaces, they do not have function
declarations in them, they areempty interfaces, this is to convey the
compiler that they have to betreated differently
Doesjava Support multiple inheritance
Java
does not support multipleinheritance directly like C++, because then it is
prone to ambiguity,example if a class extends 2 other classes and these 2
parent classeshave same method names then there is ambiguity. Hence in
Java Multipleinheritance is supported using Interfaces
Whatare virtual function
In
OOP when a derived classinherits from a base class, an object of the
derived class may bereferred to (or cast) as either being the base class
type or thederived class type. If there are base class functions
overridden by thederived class, a problem then arises when a derived
object has beencast as the base class type. When a derived object is
referred to asbeing of the base's type, the desired function call behavior
isambiguous.
The distinction between virtual and not virtual is provided to solvethis
issue. If the function in question is designated "virtual"
thenthe derived class's function would be called (if it exists). If it
isnot virtual, the base class's function would be called.
Doesjava support virtual functions
No
java does not support virtualfunctions direclty like in C++, but it
supports using Abstract classand interfaces
Describewhat happens when an object is
created in Java
Several
things happen in aparticular order to ensure the object is constructed
properly:
1. Memory is allocated from heap to hold all instance variables
andimplementation-specific data of the
object and its superclasses. Implemenation-specific data includespointers
to class and method data.
2. The instance variables of the objects are initialized to theirdefault
values.
3. The constructor for the most derived class is invoked. The firstthing a
constructor does is call the
consctructor for its superclasses. This process continues until
theconstrcutor for java.lang.Object is called,
as java.lang.Object is the base class for all objects in java.
4. Before the body of the constructor is executed, all instancevariable
initializers and initialization blocks
are executed. Then the body of the constructor is executed. Thus,
theconstructor for the base class
completes first and constructor for the most derived class completeslast.
Whatis
the purpose of System Class
The purpose of the system classis to provide the access to the
System reources
Whatis
instanceOf operator usedfor
It is used to if an object canbe cast into a specific type
without throwing Class cast exception
Whywe
should not have instance variable in an interface
Since all data fields andmethods in an Interface are public by
default, then we implement thatinterface in our class then we have public
members in our class andthis class will expose these data members and this
is violation ofencapsulation as now the data is not secure
Whatis
JVM
When we install a java package.It contains 2 things
* The Java Runtime Environment (JRE)
* The Java Development Kit (JDK)
The JRE provides runtime support for Java applications. The JDKprovides
the Java compiler and other development tools. The JDKincludes the JRE.
Both the JRE and the JDK include a Java Virtual Machine (JVM). This isthe
application that executes a Java program. A Java program requires aJVM to
run on a particular platform
Canabstract
class be final
No, abstract class cannot befinal
Whena
new object of derived Class is created, whose constructor will becalled
first, childs or parents
Even when the new object ofchild class is created, first the Base
class constructor gets executedand then the child classes constructor
Whatis
a singleton class
A singleton is an object thatcannot be instantiated. The
restriction on the singleton is that therecan be only one instance of a
singleton created by the Java VirtualMachine (JVM) - by prevent direct
instantiation we can ensure thatdevelopers don't create a second copy.
Canan
abstract class have final method
Yes
Cana
final class have an abstract method
No, the compiler will give anerror
Whatis
the difference between Authentication and Authorization
Authentication is a process forverifying that an individual is
who they say they are. Authorization isan additional level of security,
and it means that a particular user(usually authenticated), may have
access to a particular resource sayrecord, file, directory or script.
- guru's blog
- Login to post comments
