Tuesday, November 22, 2011

core java interview questions

  • upcasting vs downcasting

Java permits an object of a subclass type to be treated as an object of any superclass type. This is called upcasting.Upcasting is done automatically

downcasting must be manually

  • What is a JVM?

JVM is Java Virtual Machine which is a run time environment for the compiled java class files.

  • What is a pointer and does Java support pointers?

Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn't support the usage of pointers.

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

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

  • What is difference between Path and Classpath?
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.



  • What are local variables?
Local varaiables are those which are declared within a block of code like methods.
Local variables should be initialised before accessing them

  • What are instance variables?
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


  • How to define a constant variable in Java?
The variable should be declared as static and final.
So only one copy of the variable exists for all instances of the class and the value can't be changed also.
static final int PI = 2.14; is an example for constant

  • What is the return type of the main() method?
Main() method doesn't return anything hence declared void.

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


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

  • Does the order of public and static declaration matter in main() method?
No. It doesn't matter but void should always come before main().

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

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

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



  • Can a class be declared as protected?
A class can't be declared as protected. only methods can be declared as protected.

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

  • What is the purpose of declaring a variable as final?
A final variable's value can't be changed. final variables should be initialized before using them

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

  • I don't want my class to be inherited by any other class. What should i do?
You should declare your class as final.
But you can't define your class as final, if it is an abstract class final class can't be inherited, final method can't be overridden and final variable can't be changed.

  • Can a class be declared as static?
No a class cannot be defined as static. Only a method, a variable or a block of code can be declared as static

  • When do you define a method as static?
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.

  • What is the importance of static variable?
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

  • Can we declare a static variable inside a method?
Static varaibles are class level variables and they can't be declared inside a method.
If declared, the class will not compile.

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

  • Can you create an object of an abstract class?
Not possible. Abstract classes can't be instantiated.

  • 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?
No not possible. Class C should provide implementation for all the methods in the Interface I.
Since Class C didn't provide implementation for m1 method, it has to be declared as abstract. Abstract classes can't be instantiated.


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

  • Which class is extended by all other classes?
The Object class is extended by all other classes.

  • What is casting?
There are two types of casting, casting between primitive numeric types and casting between object references.
Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values.
Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

  • What do you understand by private, protected and public?
These are accessibility modifiers.
Private is the most restrictive, while public is the least restrictive.
There is no real difference between protected and the default type (also known as package protected) within the context of the same package, however the protected keyword allows visibility to a derived class in a different package.

  • What is a native method?
A native method is a method that is implemented in a language other than Java.

  • Is null a keyword?
The null value is not a keyword.


  • If a class is declared without any access modifiers, where may the class be accessed?

A class that is declared without any access modifiers is said to have package access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package

  • Are true and false keywords?
The values true and false are not keywords.


  • What is the diffrence between inner class and nested class?
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.


  • What is the difference between a public and a non-public class?
A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package

  • What is a Java package and how is it used?
A Java package is a naming context for classes and interfaces


  • What is the difference between method overriding and overloading?
Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments


  • How are this() and super() used with constructors?

this() is used to invoke a constructor of the same class.
super() is used to invoke a superclass constructor

  • What is a transient variable?
Transient variable is a variable that may not be serialized.

  • What is the difference between swing and Awt?
AWT are heavy-weight componenets. Swings are light-weight components. Thus, swing is faster than AWT.

  • What are pass by reference and pass by value?
Pass by reference means passing the address itself rather than passing the value.
On the other hand, pass by value means passing a copy of the value to be passed

  • What is the difference between final, finally and finalize?
final – declare constant
finally – handles exception
finalize – helps in garbage collection

  • Name primitive Java types.
The 8 primitive types are byte, char, short, int, long, float, double, and boolean

  • What is super?

super is a keyword which is used to access the method or member variables from the superclass.
If a method hides one of the member variables in its superclass, the method can refer to the hidden variable through the use of the super keyword


  • What is final modifier?

The final modifier keyword makes that the programmer cannot change the value anymore.
The actual meaning depends on whether it is applied to a class, a variable, or a method.

final Classes- A final class cannot have subclasses.
final Variables- A final variable cannot be changed once it is initialized.
final Methods- A final method cannot be overridden by subclasses


  • What is the difference between static and non-static variables?
A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.


http://www.interview-questions-java.com/
http://forum.codecall.net/java-tutorials/20719-upcasting-downcasting.html
http://javarevisited.blogspot.com/2011/04/top-20-core-java-interview-questions.html
http://technical-interview.com/Java_Questions_1.aspx


  • What is JNDI for?
JNDI (Java Naming and Directory Interface) enables Java platform-based applications to access multiple naming and directory services.
directory services such as Lightweight Directory Access Protocol (LDAP),
distributed object systems such as the Common Object Request Broker Architecture (CORBA), Java Remote Method Invocation (RMI), and Enterprise JavaBeans (EJB).



  • Classpath

Classpath is a parameter—set either on the command-line, or through an environment variable—that tells the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.
https://en.wikipedia.org/wiki/Classpath_%28Java%29




  • An application with WEB-INF folder is a web application. Meaning it will be packed as a WAR file and deployed on a server


If you are using an IDE like eclipse, and you export the project as a WAR file, it will automatically take jars under the lib folder and pack them with the WAR and that will make them available for the application running on the server (jars under WEB-INF/lib are included in the application classpath).


If you just put them anywhere else and include them in the build path, when you export the project you have to declare that you want the jars in the build path to be included as well.


Basically, there is no big difference but if you know you need this jar in runtime (i.e. after deploying the application to the server), it is better to put it under the WEB-INF/lib folder.




What do you mean by build path?
Most of the JARs you use need to be available during building (compiling) because your code depends on them - otherwise it won't compile.


The real question is between placing JARs in WEB-INF/lib vs. your container /lib directory
In general you should always put your JARs inside an application in WEB-INF/lib


Placing them globally has several consequences:
singletons are now global to all web applications (one class loader) if multiple deployed
it's easier to introduce memory leak if a library holds a reference to any of your classes (see above)
you don't duplicate the same classes (each web application will reuse the same class as opposed to having a separate copy in each class loader
http://stackoverflow.com/questions/13679112/java-configuring-build-paths-or-web-inf-lib-folder




  • By default, Tomcat container doesn’t contain any jstl library. To fix it, declares jstl.jar in your Maven pom.xml file if you are working in Maven project or add it to your application's classpath


<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
  </dependency>
http://stackoverflow.com/questions/15113628/java-lang-classnotfoundexception-javax-servlet-jsp-jstl-core-config

No comments:

Post a Comment