3:53 pm - Monday March 25, 8757

Oops Interview Questions in java

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

1. What’s the difference between constructors and normal methods?

Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void.

2. What is the final keyword denotes?

final keyword denotes that it is the final implementation for that method or variable or class. You cant override that method/variable/class any more.

3.  What is the purpose of the Void class?

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.

4. What is the finalize method do?

Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected

5. What is JIT and its use?

Really, just a very fast compiler In this incarnation, pretty much a one-pass compiler no offline computations. So you can not look at the whole method, rank the expressions according to which ones are re-used the most, and then generate code. In theory terms, its an online problem( JIT is a compiler for Byte code, The JIT-Complier is part of the JVM, it complies byte code into executable code in real time, piece-by-piece on demand basis.)

6.  What is Abstraction?

Showing the essential and hiding the non-Essential is known as Abstraction.

7. What is Encapsulation?

The Wrapping up of data and functions into a single unit is known as Encapsulation. Encapsulation is the term given to the process of hiding the implementation details of the object. Once an object is encapsulated, its implementation details are not immediately accessible anymore. Instead, they are packaged and are only indirectly accessed via the interface of the object.

  8. What is meant by Inheritance?

Inheritance is the Process by which the Obj of one class acquires the properties of Objsanother Class.
Inheritance is the method of creating the new class based on already existing class, the new class derived is called Subclass which has all the features of the existing class and its own, i.e subclass.

Adv: Reusability of code, accessibility of variables and methods of the Base class by the Derived class.

9. What is Polymorphism?

The ability to take more that one form, it supports Method Overloading & Method Overriding.

10. What is Method overloading?

When a method in a class having the same method name with different arguments (diff Parameters or Signatures) is said to be Method Overloading. This is Compile time Polymorphism.

12. What is Method Overriding?

When a method in a subclass having the same method name with same arguments present in a superclass it is said to be Method Overriding. This is Run time Polymorphism.

13. What is mean by Dynamic dispatch?

Dynamic dispatch is a mechanism by which a call to the Overridden function is resolved at runtime rather than at Compile time, and this is how Java implements Run time Polymorphism.

14. What is mean by Dynamic Binding?

Means the code associated with the given procedure call is not known until the time of call the call at run time. (it is associated with Inheritance & Polymorphism).

15. What is mean by Bite code?

Is an optimized set of instructions designed to be executed by Java-run time system, which is called the Java Virtual machine (JVM), i.e. in its standard form, the JVM is an Interpreter for byte code.

16. What is a class variable?

A data item associated with a particular class as a whole–not with particular instances of the class. Class variables are defined in class definitions. Also called a static field.

17. What is an instance variable?

Any item of data that is associated with a particular object. Each instance of a class has its own copy of the instance variables defined in the class. Also called a field

18. What is the local variable?

A data item is known within a block, but inaccessible to code outside the block. For example, any variable defined within a method is a local variable and can’t be used outside of the method.

19. What is the class method?

A method that is invoked without reference to a particular object. Class methods affect the class as a whole, not a particular instance of the class. Also called a static method.

 20. What is an instance method?

Any method that is invoked with respect to an instance of a class. Also called simply a method.

 21. Define Interface&Rules?

Interfaces can be used to implement the Inheritance relationship between the non-related classes that do not belong to the same hierarchy, i.e. any Class and anywhere in the hierarchy.   Using Interface, you can specify what a class must do but not how it does.

Rules for defining an interface

  • A class can implement more than one Interface.
  • An Interface can extend one or more interfaces, by using the keyword extends.
  • All the data members in the interface are public, static and Final by default.
  • An Interface method can have only Public, default and Abstract modifiers.
  • An Interface is loaded in memory only when it is needed for the first time.
  • A Class, which implements an Interface, needs to provide the implementation of all the methods in that Interface.
  • If the Implementation for all the methods declared in the Interface is not provided, the class itself has to declare abstract, otherwise, the Class will not compile.
  • If a class Implements two interface and both the NTFS have identical method declaration, it is totally valid.
  • If a class implements two interfaces both have identical method name and argument list, but different return types, the code will not compile.
  • An Interface cant is instantiated. Intf Are designed to support dynamic method resolution at run time.
  • An interface cannot be native, static, synchronize, final, protected or private.
  • The Interface fields cannot be Private or Protected.
  • A Transient variables and Volatile variables can not be members of  Interface.
  • The extends keyword should not be used after the Implements keyword, the Extends must always come before the Implements keyword.
  • A top level Interface cannot be declared as static or final.
  • If an Interface species an exception list for a method, then the class implementing the interface need not declare the method with the exception list.
  • If an Interface cannot specify an exception list for a method, the class can not throw an exception.
  • If an Interface does not specify the exception list for a method, the class cannot throw any exception list.

1 2 3 4 5 6 7 8 9 10

thread-safety or thread-safe code in Java refers to code which can safely be used or shared in concurrent or multi-threading environment and they will behave as expected. any code, class or object which can behave differently from its contract on the concurrent environment is not thread-safe. thread-safety is one of the risks introduced by using threads in Java and I have seen java programmers and developers struggling to write thread-safe code or just understanding what is thread-safe code and what is not? This will not be a very detailed article on thread-safety or low-level details of synchronization in Java instead we will keep it simple and focus on one example of non-thread-safe code and try to understand what is thread-safety and how to make a code thread-safe.

Here is some points worth remembering writer thread-safe code in Java, this knowledge also helps you to avoid some serious concurrency issues in Java like a race condition or deadlock in Java:

1) Immutable objects are by default thread-safe because their state cannot be modified once created. Since String is immutable in Java, its inherently thread-safe.

2) Read only or final variables in Java are also thread-safe in Java.

3) Locking is one way of achieving thread-safety in Java.

4) Static variables if not synchronized properly becomes the major cause of thread-safety issues.

5) Example of thread-safe class in Java: Vector, Hashtable, ConcurrentHashMap, String etc.

6) Atomic operations in Java are thread-safe e.g. reading a 32-bit int from memory because its an atomic operation it can’t interleave with other thread.

7) local variables are also thread-safe because each thread has there own copy and using local variables is a good way to writing thread-safe code in Java.

8) In order to avoid thread-safety issue minimize sharing of objects between multiple threads.

9) Volatile keyword in Java can also be used to instruct thread not to cache variables and read from main memory and can also instruct JVM not to reorder or optimize code from threading perspective.

That’s all on how to write thread-safe class or code in Java and avoid serious concurrency issues in Java. To be frank, thread-safety is a little tricky concept to grasp, you need to think concurrently in order to catch whether a code is thread-safe or not. Also, JVM plays a spoiler since it can reorder code for optimization, so the code which looks sequential and runs fine in development environment not guaranteed to run similarly in a production environment because JVM may ergonomically adjust itself as server JVM and perform more optimization and reorder which cause thread-safety issues.



Filed in: Z-A Interview Questions

No comments yet.

Leave a Reply