Wednesday 24 February 2016

Interviews  Questions -  Answers

JAVA INTERVIEW QUESTIONS

Core Java Interview Questions

1.What is the most important feature of Java?
A.Java is a platform independent language
.
2.What do you mean by platform independence?
A.Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).
3.What is a JVM?
A.JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
4.Are JVMs platform independent?
A.JVMs are not platform independent. JVMs are platform specific run time implementation provided by the vendor.

5.What is the difference between a JDK and a JVM?
A.JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM.

6.What is a pointer and does Java support pointers?
A.Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesnt support the usage of pointers.

7.What is the base class of all classes?
A.java.lang.Object

8.Does Java support multiple inheritance?
A.Java doesnt support multiple inheritance.

9.Is Java a pure object oriented language?
A.Java uses primitive data types and hence is not a pure object oriented language.

10.Are arrays primitive data types?
A.In Java, Arrays are objects.

11.What is difference between Path and Classpath?
A.Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.
12.What are local variables?
A.Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.

13.What are instance variables?
A.Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

14.Should a main() method be compulsorily declared in all java classes?
A.No not required. main() method should be defined only if the source class is a java application.

15.What is the return type of the main() method?
A.Main() method doesnt return anything hence declared void.

16.Why is the main() method declared static?
A.main() method is called by the JVM even before the instantiation of the class hence it is declared as static.

17.What is the arguement of main() method?
A.main() method accepts an array of String object as arguement.

18.Can a main() method be overloaded?
A.Yes. You can have any number of main() methods with different method signature and implementation in the class.

19.Can a main() method be declared final?
A.Yes. Any inheriting class will not be able to have its own default main() method.

20.Does the order of public and static declaration matter in main() method?
A.No. It doesnt matter but void should always come before main().

21.Can a source file contain more than one class declaration?
A.Yes a single source file can contain any number of Class declarations but only one of the class can be declared as public.

22.What is a package?
A.Package is a collection of related classes and interfaces. package declaration should be first statement in a java class.

23.Which package is imported by default?
A.java.lang package is imported by default even without a package declaration.

24.Can a class declared as private be accessed outside its package?
A.Not possible.

25.Can a class be declared as protected?
A.A class cant be declared as protected. only methods can be declared as protected.

26.What is the access scope of a protected method?
A.A protected method can be accessed by the classes within the same package or by the subclasses of the class in any package.

27.What is the purpose of declaring a variable as final?
A.A final variables value cant be changed. final variables should be initialized before using them.

28.What is the impact of declaring a method as final?
A.A method declared as final cant be overridden. A sub-class cant have the same method signature with a different implementation.

29.I dont want my class to be inherited by any other class. What should i do?
A.You should declared your class as final. But you cant define your class as final, if it is an abstract class. A class declared as final cant be extended by any other class.

30.Can you give few examples of final classes defined in Java API?
A.java.lang.String, java.lang.Math are final classes.

31.How is final different from finally and finalize()?
A.final is a modifier which can be applied to a class or a method or a variable. final class cant be inherited, final method cant be overridden and final variable cant be changed. finally is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment. finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.

32.When will you define a method as static?
A.When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static.

33.What are the restriction imposed on a static method or a static block of code?
A.A static method should not refer to instance variables without creating an instance and cannot use "this" operator to refer the instance.

34.I want to print "Hello" even before main() is executed. How will you acheive that?
A.Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main() method. And it will be executed only once.

35. What is the importance of static variable?
A.static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change gets reflected in all the objects.

36.Can we declare a static variable inside a method?
A.Static varaibles are class level variables and they cant be declared inside a method. If declared, the class will not compile.

37.What is an Abstract Class and what is its purpose?
A.A Class which doesnt provide complete implementation is defined as an abstract class. Abstract classes enforce abstraction.

38.Can a abstract class be declared final?
A.Not possible. An abstract class without being inherited is of no use and hence will result in compile time error.

39.What is use of a abstract variable?
A.Variables cant be declared as abstract. only classes and methods can be declared as abstract.

40.Can you create an object of an abstract class?
A.Not possible. Abstract classes cant be instantiated.

41.Can a abstract class be defined without any abstract methods?
A.Yes its possible. This is basically to avoid instance creation of the class.

42.Class C implements Interface I containing method m1 and m2 declarations. Class C has provided implementation for method m2. Can i create an object of Class C?
A.No not possible. Class C should provide implementation for all the methods in the Interface I. Since Class C didnt provide implementation for m1 method, it has to be declared as abstract. Abstract classes cant be instantiated.

43.Can a method inside a Interface be declared as final?
A.No not possible. Doing so will result in compilation error. public and abstract are the only applicable modifiers for method declaration in an interface.

44.Can an Interface implement another Interface?
A.Intefaces doesnt provide implementation hence a interface cannot implement another interface.

45.Can an Interface extend another Interface?
A.Yes an Interface can inherit another Interface, for that matter an Interface can extend more than one Interface.

46.Can an Interface extend another Interface?
A.Yes an Interface can inherit another Interface, for that matter an Interface can extend more than one Interface.

47.Can a Class extend more than one Class?
A.Not possible. A Class can extend only one class but can implement any number of Interfaces.

48.Why is an Interface be able to extend more than one Interface but a Class cant extend more than one Class?
A.Basically Java doesnt allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface is a pure abstraction model and doesnt have inheritance hierarchy like classes(do remember that the base class of all classes is Object). So an Interface is allowed to extend more than one Interface.

49.Can an Interface be final?
A.Not possible. Doing so so will result in compilation error.

50.Can a class be defined inside an Interface?
A.Yes its possible.

51.Can an Interface be defined inside a class?
A.Yes its possible.

52.What is a Marker Interface?
A.An Interface which doesnt have any declaration inside but still enforces a mechanism.

53.Which object oriented Concept is achieved by using overloading and overriding?
A.Which object oriented Concept is achieved by using overloading and overriding?

54.Which object oriented Concept is achieved by using overloading and overriding?
A.

55.Which object oriented Concept is achieved by using overloading and overriding?
A.Polymorphism.

56.Why does Java not support operator overloading?
A.Operator overloading makes the code very difficult to read and maintain. To maintain code simplicity, Java doesnt support operator overloading.

57.Can we define private and protected modifiers for variables in interfaces?
A.No.

58.What is Externalizable?
A.Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in).

59.What modifiers are allowed for methods in an Interface?
A.Only public and abstract modifiers are allowed for methods in interfaces.

60.What is a local, member and a class variable?
A.Variables declared within a method are "local" variables. Variables declared within the class i.e not within any methods are "member" variables (global variables). Variables declared within the class i.e not within any methods and are defined as "static" are class variables.

61.What is an abstract method?
A.An abstract method is a method whose implementation is deferred to a subclass.

62.What value does read() return when it has reached the end of a file?
A.The read() method returns -1 when it has reached the end of a file.

63.Can a Byte object be cast to a double value?
A.No, an object cannot be cast to a primitive value.

64.What is an objects lock and which objects have locks?
A.An objects lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the objects lock. All objects and classes have locks. A classes lock is acquired on the classes Class object.

Spring Interview Questions

1.  What is IOC (or Dependency Injection)?
A. The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.
i.e., Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects.

2. What are the different types of IOC (dependency injection) ?

A. There are three types of dependency injection: 
     1.Constructor Injection (e.g. Pico container, Spring etc): Dependencies are provided as constructor parameters.
     2.Setter Injection (e.g. Spring): Dependencies are assigned through JavaBeans properties (ex: setter methods).
    3.Interface Injection (e.g. Avalon): Injection is done through an interface.
Note: Spring supports only Constructor and Setter Injection
3. What are the benefits of IOC (Dependency Injection)?
A. Benefits of IOC (Dependency Injection) are as follows:
   a.Minimizes the amount of code in your application. With IOC containers you do not care about how services are created and how you get references to the ones you need. You can also easily add additional services by adding a new constructor or a setter method with little or no extra configuration.
   b.Make your application more testable by not requiring any singletons or JNDI lookup mechanisms in your unit test cases. IOC containers make unit testing and switching implementations very easy by manually allowing you to inject your own objects into the object under test.
   c.Loose coupling is promoted with minimal effort and least intrusive mechanism. The factory design pattern is more intrusive because components or services need to be requested explicitly whereas in IOC the dependency is injected into requesting piece of code. Also some containers promote the design to interfaces not to implementations design concept by encouraging managed objects to implement a well-defined service interface of your own.
4. What is Spring ?
A. Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development. 
5. What are the advantages of Spring framework?
A. The advantages of Spring are as follows: 
       a. Spring has layered architecture. Use what you need and leave you don't need now. 
       b. Spring Enables POJO Programming. There is no behind the scene magic here. POJO programming enables continuous integration and testability.
       c. Dependency Injection and Inversion of Control Simplifies JDBC.
       d. Open source and no vendor lock-in.

6. What are features of Spring ?

A. Lightweight : spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB. And the processing overhead is also very negligible.
    Inversion of control (IOC) : Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
    Aspect oriented (AOP) : Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.
    Container : Spring contains and manages the life cycle and configuration of application objects.
7. How many modules are there in Spring? 
A. 1.The core container
     2.Spring context
     3.Spring AOP
     4.Spring DAO
     5.Spring ORM
     6.Spring Web module
     7.Spring MVC framework
8. What are the types of Dependency Injection Spring supports?
A. a)Setter Injection:

Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
     b)Constructor Injection:
Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.
9. What is Bean Factory ?
A. A BeanFactory is like a factory class that contains a collection of beans. The BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whenever asked for by clients.
10. What is Application Context?
A. A bean factory is fine to simple applications, but to take advantage of the full power of the Spring framework, you may want to move up to Springs more advanced container, the application context. On the surface, an application context is same as a bean factory.Both load bean definitions, wire beans together, and dispense beans upon request. 
11. What is the difference between Bean Factory and Application Context ?  
A.  a. Application contexts provide a means for resolving text messages, including support for i18n of those messages.
     b. Application contexts provide a generic way to load file resources, such as images. 
     c. Application contexts can publish events to beans that are registered as listeners. 
     d. Certain operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context. 
     e. ResourceLoader support: Spring’s Resource interface us a flexible generic abstraction for handling low-level resources. An application context itself is a ResourceLoader, Hence provides an application with access to deployment-specific Resource instances. 
     f. MessageSource support: The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable.

12. What are the common implementations of the Application Context ?

A.  a. ClassPathXmlApplicationContext : It Loads context definition from an XML file located in the classpath, treating context definitions as classpath resources.
      b. FileSystemXmlApplicationContext : It loads context definition from an XML file in the filesystem. 
      c. XmlWebApplicationContext : It loads context definition from an XML file contained within a web application.
13. How is a typical spring implementation look like ?
A. For a typical Spring Application we need the following files:
     1. An interface that defines the functions.
     2.An Implementation that contains properties, its setter and getter methods, functions etc.,
     3.Spring AOP (Aspect Oriented Programming)
     4.A XML file called Spring configuration file.
     5.Client program that uses the function.
14. What is the typical Bean life cycle in Spring Bean Factory Container ?
A. 1.The spring container finds the bean’s definition from the XML file and instantiates the bean. 
     2.Using the dependency injection, spring populates all of the properties as specified in the bean definition.
     3.If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID. 
     4.If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself. 
     5.If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called.
     6.If an init-method is specified for the bean, it will be called.
     7.Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.

15. What do you mean by Bean wiring ?

A. The act of creating associations between application components (beans) within the Spring container is reffered to as Bean wiring.

16. What do you mean by Auto Wiring?
A. The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory. The autowiring functionality has five modes.
       1. no
       2. byName
       3. byType
       4. constructor
       5. autodirect 

17. What is DelegatingVariableResolver?

A. Spring provides a custom JavaServer Faces VariableResolver implementation that extends the standard Java Server Faces managed beans mechanism which lets you use JSF and Spring together. This variable resolver is called as DelegatingVariableResolver.
18. How to integrate  Java Server Faces (JSF) with Spring? 
A. JSF and Spring do share some of the same features, most noticeably in the area of IOC services. By declaring JSF managed-beans in the faces-config.xml configuration file, you allow the FacesServlet to instantiate that bean at startup. Your JSF pages have access to these beans and all of their properties.
19. What is  Java Server Faces (JSF) - Spring integration mechanism? 
A. Spring provides a custom JavaServer Faces VariableResolver implementation that extends the standard JavaServer Faces managed beans mechanism. When asked to resolve a variable name, the following algorithm is performed:
       a. Does a bean with the specified name already exist in some scope (request, session, application)? If so, return it.
       b. Is there a standard JavaServer Faces managed bean definition for this variable name? If so, invoke it in the usual way, and return the bean that was created. 
       c. Is there configuration information for this variable name in the Spring WebApplicationContext for this application? If so, use it to create and configure an instance, and return that instance to the caller.
       d. If there is no managed bean or Spring definition for this variable name, return null instead.
       e. BeanFactory also takes part in the life cycle of a bean, making calls to custom initialization and destruction methods.
20. What is Significance of JSF- Spring integration ?
A. Spring - JSF integration is useful when an event handler wishes to explicitly invoke the bean factory to create beans on demand, such as a bean that encapsulates the business logic to be performed when a submit button is pressed. 
21. How to integrate your Struts application with Spring?  
A. To integrate your Struts application with Spring, we have two options: 
      1. Configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependencies in a Spring context file.
      2. Subclass Spring's ActionSupport classes and grab your Spring-managed beans explicitly using a getWebApplicationContext() method. 
22. What are ORM’s Spring supports ? 
A. Spring supports the following ORM’s : 
      1. Hibernate
      2. iBatis 
      3. JPA (Java Persistence API) 
      4. TopLink
      5. JDO (Java Data Objects)
      6. OJB
23. What are the ways to access Hibernate using Spring ?
A. There are two approaches to Spring’s Hibernate integration:
       a. Inversion of Control with a HibernateTemplate and Callback 
       b. Extending HibernateDaoSupport and Applying an AOP Interceptor.
24. How to integrate Spring and Hibernate using HibernateDaoSupport?
A.  Spring and Hibernate can integrate using Spring’s SessionFactory called LocalSessionFactory. The integration process is of 3 steps.
      a. Configure the Hibernate SessionFactory 
      b. Extend your DAO Implementation from HibernateDaoSupport
      c. Wire in Transaction Support with AOP 
25. What is AOP?
A. Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management. The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. 
26. How the AOP used in Spring?
A. AOP is used in the Spring Framework: To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management, which builds on the Spring Framework's transaction abstraction.To allow users to implement custom aspects, complementing their use of OOP with AOP. 
27. What do you mean by Aspect ? 
A. A modularization of a concern that cuts across multiple objects. Transaction management is a good example of a crosscutting concern in J2EE applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (@AspectJ style). 
28. What do you mean by JointPoint? 
A. A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution. 
29. What do you mean by Advice?
A. Action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors "around" the join point. 
30. What are the types of Advice? 
A. Types of advice: 
      a. Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception). 
      b. After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.
      c. After throwing advice: Advice to be executed if a method exits by throwing an exception.
      d. After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
      e. Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

No comments:

Post a Comment

1 comment:

  1. Hello Admin,

    As of now, in my last post I posted about the linux bearings which are basically used truly coming to fruition of the cloud.

    Regards,
    Thanks

    RITU

    ReplyDelete