Java Questions
Java Questions
org/diverse/interviu/java/intrebari-si-raspunsuri-pentru-i
nterviurile-java/
http://www.landofcode.com/java-quiz/
purile de join
cum se face o relatie many-to many
ce`i foreign key sa stii
ce`i primary key
cum se fac relatiile
one-to-one
one-to-many
many-to-many
sa stii ce e group by
cum faci un insert,un update
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
http://www.programcreek.com/2013/03/hashmap-vs-treemap-vs-hashtable-vs-linkedhas
hmap/
sa stii care-i treaba cu exceptiile
cu try/catch/finally
clase abstracte, interfata
sa stii poliformism(daca o clasa mosteneste alta ce se intampla)
tutorialul oficial
de la oracle
are si exemplu de cod
numai ca e in engleza
1.What is Java Virtual Machine and how it is considered in context of Javas plat
form independent feature?
When Java is compiled, it is not compiled into platform specific machine, rathe
r into platform independent byte code. This byte code is distributed over the we
b and interpreted by virtual Machine (JVM) on whichever platform it is being run
.
2.Class Variable = variables declared with in a class, outside any method, with
the static keyword.
3. Steps for Object creation = Object is first declared, then instantiated and t
hen it is initialized;
4. Variables used in a switch statement can be used with which datatypes?
Variables used in a switch statement can only be a byte, short, int, or char
5.Why is StringBuffer called mutable?
The String class is considered as immutable, so that once it is created a String
object cannot be changed. If there is a necessity to make alot of modifications
to Strings of characters then StringBuffer should be used.
6.What is the difference between StringBuffer and StringBuilder class?
Use StringBuilder whenever possible because it is faster than StringBuffer. But,
if thread safety is necessary then use StringBuffer objects.
7.What is finalize() method?
HIBERNATE
=======================================
FetchType.LAZY = Doesnt load the relationships unless explicitly asked for via gett
er
FetchType.EAGER = Loads ALL relationships
In the case of one-to-one relationships , in the owner entity(the one that has t
he foreign key) we add the annotation @PrimaryJoinColumn and in the other entity
we add tHe mapped by attribute.In generaly we add the mapped by attribute to sp
ecift that this entity is not the owner of the foreign key
HOW TO ADD RECORDS USING HIBERNATE
====================================
session =sessionFactory.openSession();
Transaction tx = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form
object
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName(imran)
session.save(contact);
tx.commit();
HOW TO QUERY USING HSQL
=============================
Query query = session.createQuery("update Stock set stockName = :stockName" +
" where stockCode = :stockCode");
query.setParameter("stockName", "DIALOG1");
query.setParameter("stockCode", "7277");
int result = query.executeUpdate();
ABSTRACT CLASSES AND INTERFACES
==========================================
It is possible, however, to define a class that does not implement all of the in
terface s methods, provided that the class is declared to be abstract.
With interfaces, all fields are automatically public, static, and final, and all
methods that you declare or define (as default methods) are public.
METHOD HIDING
==================================================
Method hiding means subclass has defined a class method(static method) with the
same signature as a class method in the superclass.
The version of a method that is executed will NOT be determined by the object th
at is used to invoke it. In fact it will be determined by the type of the refere
nce variable used to invoke the method.
A Class Variable Shadows the Inherited Variable from Its Parent Classes
==========================================================================
When an instance variable in a subclass has the same name as an instance variab
le in a super class,
then the instance variable is chosen in the class that is the reference type.
Because variables names in Java are resolved by the reference type,
not the object they are referencing.
A class can declare a variable with the same name as an inherited variable from
its parent class,
thus "hiding" or shadowing the inherited version.
NESTED CLASSES
============================================================================
Nested classes are divided into two categories: static and non-static.
Nested classes that are declared static are called static nested classes.
Non-static nested classes are called inner classes
Non-static nested classes (inner classes) have access to other members of the en
closing class, even if they are declared private.
Static nested classes do not have access to other members of the enclosing clas
s. As a member of the OuterClass, a nested class can be declared private, public
, protected, or package private.
(Recall that outer classes can only be declared public or package private.)
A static nested class cannot refer directly to instance variables or methods de
fined in its enclosing class: it can use them only through an object reference.
Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
INNER CLASS
=============
An inner class is associated with an instance of its enclosing class and has
direct access to that object s methods and fields.
Also, because an inner class is associated with an instance, it cannot define
any static members itself.
An instance of InnerClass can exist only within an instance of OuterClass and
has direct access to the methods and fields of its enclosing instance.
To instantiate an inner class, you must first instantiate the outer class
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
You can use the same modifiers for inner classes that you use for other membe
rs of the outer class
ANONYMOUS CLASS
============================
Anonymous classes are expressions, which means that you define the class in an
other expression.
An anonymous class has access to the members of its enclosing class.
An anonymous class cannot access local variables in its enclosing scope that a
re not declared as final or effectively final.
Like a nested class, a declaration of a type (such as a variable) in an anonym
ous class shadows any other declarations in the
enclosing scope that have the same name.
You cannot declare static initializers or member interfaces in an anonymous cl
ass.
An anonymous class can have static members provided that they are constant var
iables.
YOU can have the following in anonymous classes:
Fields
Extra methods (even if they do not implement any methods of the supertype)
Local classes
Instance initializers
You cannot declare constructors in an anonymous class
this KEYWORD
===========================
From within a constructor, you can also use the this keyword to call another c
onstructor in the same class.
If present, the invocation of another constructor must be the first line in th
e constructor.
If a constructor does not explicitly invoke a superclass constructor, the Java c
ompiler automatically inserts a call to the no-argument constructor of the super
class. If the super class does not have a no-argument constructor, you will get
a compile-time error. Object does have such a constructor, so if Object is the o
nly superclass, there is no problem.
Contract between equals() and hashcode()
====================================================
hashcode() method is supported for the benefit of hashtables such as those provi
ded by java.util.Hashtable
Rules of contract:
1.Firstly, it states that the hash code returned by the hashCode method must be
consistently the same for multiple invocations during the same execution of the
application as long as the object is not modified to affect the equals method.
2.The second requirement of the contract is the hashCode counterpart of the requ
irement specified by the equals method.
It simply emphasizes the same relationship - equal objects must produce the same
hash code. However, the third point elaborates that unequal objects need not pr
oduce distinct hash codes.
Collections
===============================================================================
LIST-Interface
==========================================================
ArrayList vs LinkedList
===============================
arraylist has O(1) for get while linkedList has O(n)
linkedlist fares better at the insertion/removal of elements