Java

Interview  Questions:

1.what is the most important feature of Java?

The most important feature of Java is platform independent language.

2.what do you mean by platform independence?

It means that we can write and compile the java code in one platform (eg.Windows) and executes in any other supported platform(Linux,solaris,etc)

3.what is JVM?

JVM is Java virtual machine which produces run time environment for java compiled code.

4. what is JDK?

JDK is Java Development kit  which is for development purpose and it includes execution environment also.

5.what is a pointer?

Pointer is used to store the address of a variable.

6.Does java supports pointers?

No,because it  leads to memory leaks and produces confussion to the programmer.

7. What is the base class of all classes?

Java.lang.Object

8. Is Java a pure object oriented language?

No,because java uses primitive data type.

9. Are arrays primitive data types?

No,in  java arrays are objects.

10.what are local variables?


Local variables are the variables which are declared with in the block only.


11.what are instance variables?

The variables which are declared at class level.

12.How to define a constant variable in Java?

By using final keyword at the time of declaring a variable.

13.what is the return type of main method?

main() does not return anything that’s why it is declared as void.

14.what is the argument of main method?

main() method accepts an array of String object  as argument.

15.what is overloading?

Declaring more than one method with the same name but difference in the number of arguments.

16.Can main() method be overloaded?

Yes. We can have any number of main() methods with different method signature and implementation in the class.

17.Can main() method be declared final?

Yes.But if we declare main() method as final then we cannot inherit in future.

18. Does the order of public and static declaration matter in main() method?

No,It does not matters but void should come before main() only.

19. What is a package?

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

20. Which package is imported by default?

Java.lang package is imported by default without any package declaration.

21.What is the access scope of protected method?

We can access protected members with in the same package and sub class of other packages.

22. What is the access scope of private  method?

We can access private members with in the class only.

23.what is the purpose of declaring a variable as final?

If we are declaring a variable as final then the variable value is fixed.We cannot modify the value of a variable.

24. What is the impact of declaring a method as final?.

If we are declaring a method as final then the Defination of the method is fixed and it is not possible to override the final method.

25. Can you give few examples of final classes defined in Java API?

Java.lang .String,java.lang.Math are final classes.

26. How is final different from finally and finalize()?

final is a modifier that which can be applied to a class or a method or a variable.Here final class cannot be inherited,final method cannot be overridden and final variable cannot 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 and which can be executed by the JVM
and to perform memory clean up operations.

27.  Can a class be declared as static?

No,We cannot declare a top level class as static but inner classes can be declared as static.

28. When will you define a method as static?

If we want to access the method before the object creation then the method should be declared as static method.

29.  I want to print "Hello" even before main() is executed. How will you achieve that?

By using static block we can achieve this.

30. What is the importance of static variable?

If we declare a variable as static then we can call the variable directly by using class name without creating an object.

31.Can we declare a static variable inside a method?

No,we cannot declare a static variable inside a method because the class will not compile.

32.what is final class?

If we declare a class as final then it is not possible to extend the class but we can create object to the class.

33.what is abstract and how many ways it can be used?

Abstact is an access modifier and can be used in two ways.They are 1)abstract methods 2)abstract classes

34.what is concrete method?

If the method is declared along with its method definition(method body) then it is called as concrete method.

35.What are abstract methods?

The method is declared without its definition(method body) is called abstract method.

36.What is abstract class?

Abstract class is collection of zero or more abstract methods.If a class consists atleast one abstract method then the class must and should be declared as abstract class.

37.Whether you can create an object to abstract class?

It is not possible.

40. Can a abstract class be declared final?

No,We should not declare an abstract class as final because it contains abstract methods which have to be implemented in the derived classes.If we declare an abstract class  as final then we cannot derive its methods to implement in sub classes.

41.What is Encapsulation?Give example.

The process of binding the data members in to single unit is known as encapsulation.we can say class is the best example of Encapsulation.

42.What is Abstraction?

The process of hiding the data members and methods by using access specifiers is called Abstraction.

43.What is polymorphism?

One in many forms that means single object  showing multiple behaviours.

44.What is Constructor overloading?

If a constructor satisfy’s any one of the below three conditions then we can say those constructors are in overloading.The conditions are
Ø  Number of parameters should not be same.
Ø  Type of parameters should not be same.
Ø  Order of parameters should not be same.

45.Out of do while and while  which loop is efficient?                          

In do while loop,First time the statements are executed with out testing the condition.Where as in while loop the condition is tested first and then only the statements are executed.so,while loop is more efficient then do while.

46.Why goto statements are not available in Java?

goto statements are not available in java because it lead to confussion for the programmer.

47.On which memory,arrays are created in Java?

Arrays are created on dynamic memory by JVM.There is no topic of static memory in Java.Everyting (variables,array,object etc) are created on dynamic memory only.  

48.What is object reference?

Object reference is a unique hexadecimal number representing the memory address of the object.

49.What is the difference between == and equals() while comparing strings?

== operator compares the references of the string objects.It does not compares the content of objects  where equals() compares the content of the objects.

50.What is string constant pool?

String constant pool is a separate block of memory where the string objects are held by JVM.

51.What is the difference between String and StringBuffer classes?

String class objects content cannot be modified.StringBuffer class object content can be modified.

52.What is the difference between StringBuffer and StringBuilder classes?

When the programmer want to use several threads then he should use StringBuffer.If only onethread is used then he should use StringBuilder because it improves the execution time.

53.What is the difference between object oriented programming and object based programming languages?

Object oriented programming languages follows all the features of OOPS.Example C++,Java.
Object basedprogramming languages follow all the features of OOPS excepet inheritance.

54.What is hash code?

Hash code is a unique identification number allotted to the objects by the JVM.

55.When is a constructor called,before or after creating object?

A constructor is called concurrently when the object creation is going on.

56.How are objects are passed to methods in Java?

Primitive data types,objects,even object references every thing is passed to methods using ‘pass by value’ or ‘call by value’ concept.

57.What are factory methods?

A factory method is a method that creates and returns an object to the class which it belongs.

58.What is object graph?

Object graph is a graph showing relationship between different objects in memory.

59.What is anonymous inner class?

It is an inner class whose name is not written in the outer class and which only one object is created.

60.What is the advantage of inheritance?

Programmer can reuse the supper class code into sub class without writing the code again by using extends keyword.

61.Why multiple inheritance is not available in Java?

Because it causes a big confusion to the programmer.Java does not supports multiple inheritance because of diamond problem.

62.How can you achieve multiple inheritance in Java?

By using interfaces concept we can achieve.

63.What is coercion?

It is the automatic conversion between different data types done by the compiler.

64.What is the conversion?

 Conversion is the explicit change in the data type specified by the cast operator.

65.What is method signature?

It represents the method name along with method parameters.

66.What is method overloading?

Writing two or more methods in the same class with same method name and with different method signatures.

67.What is method overriding?

Writing two or more methods in super and sub classes with same method name and with same method signatures.

68.What is implicit casting?

Automatically casting done by the compiler internally.implicit casting is used to convert  a lower data typr in to higher data type.

69.What is explicit casting?

The casting done by the programmer is called explicit casting.It is compulsory while converting higher data type to lower data type.

70.What is widening in Java?

Converting lower data type into higher data type is called widening.

71.What is narrowing in Java?

Converting higher data type into  lower data type is called narrowing.

72.Which method is used in cloning?

clone() method of Object  class is used in cloning.

73.Why the methods of interface are public and abstract by default?

Interface methods are public since they should be available to third party vendors to provide implementation. They are abstract because their implementation is left for third party vendors.

74.Can you implement one interface from another?

No we can’t because implementing an interface means writing body for the methods.

75.Can you write a class with in an interface?

Yes,it is possible to write a class within an interface.

76.What is package?

A package represents a directory that contains related group of classes and interfaces.

77.How can you call the garbage collector?

We can call the garbage collector of JVM to delete any unused variables and object references  from memory by using gc().

78.What is CLASSPATH?

The CLASSPATH is an environment variable that tells the Java compiler where to look for class files to import.CLASSPATH is generally set to directory or a JAR(Java Archive) file.

79.What is JAR file?

A java Archive file(JAR) is a file which contains .class files,audio files,image files or directories.

80.What is the scope of default access specifier?

Default members are available with in the same package ,but not outside of the package.So their scope is package scope.


No comments:

Post a Comment