Today's Jobs :


CORE JAVA FREQUENTLY ASKED QUESTIONS AND ANSWERS-2


CORE JAVA FREQUENTLY ASKED QUESTIONS AND ANSWERS-2

Question: What are the Object and Class classes used for?   
Answer:   The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.  
 
Question: How does a try statement determine which catch clause should be used to handle an exception?   
Answer:   When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.  
 
Question: What are synchronized methods and synchronized statements?   
Answer:   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.  
 
Question: What is the difference between an if statement and a switch statement?   
Answer:   The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.  
 
Question: What is the diffrence between inner class and nested class?   
Answer:   When a class is defined within a scope od another class, then it becomes inner class. 
If the access modifier of the inner class is static, then it becomes nested class.  
 
Question: Diffrence between JRE And JVM AND JDK   
Answer:   The "JDK" is the Java Development Kit. I.e., the JDK is bundle of software that you can use to develop Java based software. The "JRE" is the Java Runtime Environment. I.e., the JRE is an implementation of the Java Virtual Machine which actually executes Java programs. Typically, each JDK contains one (or more) JRE's along with the various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.  
 
Question: Why is not recommended to have instance variables in Interface   
Answer:   By Default, All data members and methods in an Interface are public. Having public variables in a class that will be implementing it will be violation of the Encapsulation principal. I hope that's pretty ok.. If anybody has a better framed answer. 
 
Question: Can there be an abstract class with no abstract methods in it?   
Answer:   Yes  
 
Question: Can an Interface be final?   
Answer:   No  
 
Question: Can an Interface have an inner class?   
Answer:   Yes public interface abc { static int i=0; void dd(); class a1 { a1() { int j; System.out.println("in interfia"); }; public static void main(String a1[]) { System.out.println("in interfia"); } } }  
 
Question: Can we define private and protected modifiers for variables in interfaces?   
Answer:   No  
 
 
Question: What are some alternatives to inheritance?   
Answer:   Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn't force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).