Tricky Core Java Interview Question with Answers

Describe the three OOO principles ?
Encapsulation - It is the way the code and data are confined and are in isolation from the outside environment of the system.

EXAMPLE: In a car the engine can be thought about as an encapsulated which is controlled by the starter key and the gear. The operation of the engine does not affect the functioning of other parts of car like the headlight and wiper. In JAVA basis of encapsulation is the CLASS.

Inheritance - It is the process by which one object acquires the properties of another object.

Polymorphism - It is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation.

EXAMPLE: In a college cafeteria if an old music is put the students would not like it , but if the latest number of Brittney Spears is put then they would love it. If analyzed then we can see that even though the process of listening is the same i.e. for the old song the students listen it from there ears and even for the latest Brittney Spears number they listen it from there ear. But there is a difference by which the students react. This difference can be explained by the concept polymorphism.


How many types of literals are there in JAVA ?
A3. There are four types of literals they are Integer literals, Floating point literals, Boolean literals and character literals.


A note on compiling & executing a JAVA program ?
A4. (i) The name of the source file is called in terms of .java
(ii) A source file is called a compilation unit. This has one or more class definitions.
(iii) The name of the class should be same as that of the file.
(iv) Once compiled the .java file creates a .class file. This is done by the compiler javac
(v) This class file contains the byte code version of the program.

 
A note on PUBLIC, PRIVATE, STATIC, VOID & MAIN ?
 
  (i) All Java applications begin execution by calling main ()
(ii) When a class member is defined as public. Then that member may be accessed by code outside the class in which it is declared.
(iii) The opposite of public is private which prevents a member from being used by code defined outside of its class.
(iv) The keyword static allows main() to be called without having to instantiate a particular instance of the class. This is mandatory because main () is called by the Java interpreter before any objects are made.
(v) CASE SENSITIVE: Main () is different from main(). It is important to know that that Main() would be compiled. But the Java interpreter would report an error if it would not find main().


What is meant by Garbage collection ?
A6. The technique that automatically destroys the dynamically created objects is called garbage collection. When no reference to an object exists, that object is assumed to be no longer needed, and memory occupied by that object can be reclaimed.


What are the access modifiers ?
A7. There are three types of access modifiers.
(i) Private - Makes a method or a variable accessible only from within its own class.
(ii) Protected - Makes a method or a variable accessible only to classes in the same package or subclasses of the class.
(iii) Public - Makes a class , method or variable accessible from any other class.

 
A note on keywords for Error handling ?
 (i) Catch - Declares the block of code used to handle an exception.
(ii) Finally - Block of code , usually following a typecatch statement, which is executed no matter what program flow occurs when dealing with an exception.
(iii) Throw - Used to pass an exception up to the method that calls this method.
(iv) Throws - Indicates the method will pass an exception to the method that called it.
(v) Try - Block of code that will be tried but which may cause an exception.
(vi) Assert - Evaluates a conditional _expression to verify the programmer's assumption.

 
How many ways can you represent integer numbers in JAVA ?
There are three ways , you can represent integer numbers in JAVA. They are decimal (base 10) , octal (base 8) , and hexadecimal (base 16).


A note on arrays of object references ?
  If the array type is CLASS then one can put objects of any subclass of the declared type into the array. The following example on sports explains the above concept :

class sports { }
class football extends sports { }
class hockey extends sports { }
class baseball extends sports { }
sports [ ] mysports = { new football (),
new hockey (),
new baseball ()};
 

What is meant by "instanceof" comparison ?
It is used for object reference variables only. You can use it to check whether an object is of a particular type.

 
when is a method said to be overloaded ?
Two or more methods are defined within the same class that share the same name and their parameter declarations are different then the methods are said to be overloaded.

 
What is meant by Recursion ?
 It is the process of defining something in terms of itself. In terms of JAVA it is the attribute that allows a method to call itself. The following example of calculating a factorial gives an example of recursion.

class Factorial {
int fact (int n) {
int result;
if (n= 1) return 1;
result = fact(n -1) * n;
return result;
}
}
class Recursion {
Public static void main (string args[ ]) {
Factorial f = new Factorial ();
system.out.println ("Factorial of 10 is " + f.fact(10));
}
}

 
A cool example to explain the concept of METHOD in JAVA ?
 Let us say you are in Mcdonalds and you order for #7 for here with medium coke. The cashier takes your order and punches it on the computer. The folks in the kitchen get the order and they get the crispy chicken and pass it on to the guy who puts a medium fries and finally a medium coke is filled and the order is served to you. In other terms if all this was supposed to be done by a robot then it could have been programmed the following way.

 
void #7forherewithmediumcoke( )
{
Get (crispy chicken, lattice, butter, fries, coke);
make (sandwich);
fill (coke, fries);
}

 
Difference between a process and a thread ?
I remember having read this answer somewhere. "Threads are like tiny ropes. A Process would denote a rope made out from these threads." Fun apart, a process can contain multiple threads. Also a process gets its own memory address space while a thread doesn't.

 
What are checked and unchecked exceptions ?
Checked exceptions are the ones which you expect beforehand to be raised when an exceptional condition occurs and so write your code in a try-catch block to handle that sufficiently. For example: InsuffucientBalanceException which might be raised when money is being withdrawn from a bank account and the account has insufficient balance. Checked exceptions are sub classes of Exception. Unchecked exceptions are the ones which cannot be handled in the code. These are rather unexpected exceptions like NullPointerException, OutOfMemoryError, DivideByZeroException, typically, programming errors. Unchecked exceptions are subclasses of RunTimeExceptions.

 
Is synchronised a modifier?indentifier??what is it ?
It's a modifier. Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.


What is singleton class? where is it used ?
Singleton is a design pattern meant to provide one and only one instance of an object. Other objects can get a reference to this instance through a static method (class constructor is kept private). Why do we need one? Sometimes it is necessary, and often sufficient, to create a single instance of a given class. This has advantages in memory management, and for Java, in garbage collection. Moreover, restricting the number of instances may be necessary or desirable for technological or business reasons--for example, we may only want a single instance of a pool of database connections.


Name the null interfaces in java? what are they ?
No idea..ranchers come to my rescue.


How to avoid deadlock ?
Don't go to sleep holding a lock Coordinate thread access to synchronized methods using wait and notify.

 
Garbage collection thread belongs to which priority…Min?normal?max ?
The Java garbage collection is implemented as a low priority thread


What is meant by time slicing ?
Its a task scheduling method. With time slicing, or "Round-Robin Systems", several processes are executed sequentially to completion. Each executable task is assigned a fixed-time quantum called a time slice in which to execute.


What is a compilation unit ?
The smallest unit of source code that can be compiled, i.e. a .java file. 


Why is the main method static ?
So that it can be invoked without creating an instance of that class

What is the difference between class variable,member variable and automatic(local) variable ?
class variable is a static variable and does not belong to instance of class but rather shared across all the instances member variable belongs to a particular instance of class and can be called from any method of the class automatic or local variable is created on entry to a method and has only method scope

When are static and non static variables of the class initialized ?
The static variables are initialized when the class is loadedNon static variables are initialized just before the constructor is called

When are automatic variable initialized ?
Automatic variable have to be initialized explicitly

How is an argument passed in java, by copy or by reference ?
If the variable is primitive datatype then it is passed by copy.
If the variable is an object then it is passed by reference
 

Search:

Recent Posts