This article all about Basic or fundamental java interview questions. Some times these Basic java interview questions makes us difficult in technical round,even experienced software developers gets confused to give proper answer for basic java questions. Already i written Java String interview questions and answers my previous post. These basic java interview questions very useful for beginners as well as experienced candidates.Who have depth knowledge on java fundamentals those software developers can survival in any company.
Then Why late to read all fallowing Basic Java Interview Questions and answers.
1. What is importance of main() method in java?
Ans: main() method is entry point of any standalone java application.
Syntax: public static void main(String args[])
main() method is public and static so that java can access it without initializing the class.
The input parameter is an array of String through which we can pass runtime arguments to the java program.
void means it does't return any value
Ans: No, we can not have more than one public class in a single java source file. But single source file can have multiple classes that are not public.
3. What is static keyword?
Ans: Static is a keyword , it can be variable and methods but top level classes can not be static. methods can be static and we can have static blocks. They belongs to class not to an object or instance of class. Because of this, they are shared across instances.
This keyword mainly used when static member variables is to define common resources or constants.
Static methods are used to define utility methods such as Collection class and wrapper classes in java.
From java 8, static methods can be used within interfaces.
4. Can we declare class as final?
Ans: we can't declare a top level class as static however an inner class can be declared as static. If inner class is declared as static,It is called static nested class.
5. What is try-with-resources in java?
Ans: This is one of the Java 7 feature try-with-resource management. Before java 7, there was no auto resource management and we should explicitly close the resource. Usually, it was done in the finally block of try-catch statement. This approach used to cause memory leaks when we forgot to close the resource.
From java 7, we can create resource inside try block and use it. Java takes care of closing it as soon as try-catch block get finished.
6. How to use multi-catch block in java?
Ans: In Java 7, one of the improvement was multi-catch block where we can catch multiple exceptions in a single catch block. This makes are code short and clean when every catch block has similar code.
You can write catch block when you handling multiple exceptions separate them using pipe(|)
7. what is difference between abstract class and interface?
21. What do you mean by platform independence of java?
Ans: Platform independence means that you can run the same java program in any Operating system. For example, you can write java program in windows and run in Linux OS.
22. Is JVM platform Independent?
Ans: JVM means Java Virtual Machine. It is heart of the java programming language. It is responsible for converting byte code into machine readable code. JVM is not platform independent, you have different JVM for different OS.
23. What makes java secured?
Ans: we must say java is secured because no explicit pointer and programs runs inside virtual machine sandbox. Unlike C++ that uses runtime environment of OS, Java uses runtime environment of its own.
24. What is Method Overloading and overriding in java?
Ans:
Method Overloading:- same method name and different signature.
Method Overriding:- same method name and same signature.
Method overloading is performed within class. Overriding occurs in two classes that IS-A relationship.
Method overloading is used to increase the readability of the program. Method overriding is used to provide specific implementation of the method that is already provided by its super class.
25. What is Package and which package is imported by default?
Ans: Java package is the mechanism to organize the java classes and interfaces by grouping them. Example is java.lang.Object is the fully classified name of the Object class that is part of java.lang.package.
java.lang.package is imported by default and we don't need to import any class from this package explicitly.
Then Why late to read all fallowing Basic Java Interview Questions and answers.
1. What is importance of main() method in java?
Ans: main() method is entry point of any standalone java application.
Syntax: public static void main(String args[])
main() method is public and static so that java can access it without initializing the class.
The input parameter is an array of String through which we can pass runtime arguments to the java program.
void means it does't return any value
Ans: No, we can not have more than one public class in a single java source file. But single source file can have multiple classes that are not public.
3. What is static keyword?
Ans: Static is a keyword , it can be variable and methods but top level classes can not be static. methods can be static and we can have static blocks. They belongs to class not to an object or instance of class. Because of this, they are shared across instances.
This keyword mainly used when static member variables is to define common resources or constants.
Static methods are used to define utility methods such as Collection class and wrapper classes in java.
From java 8, static methods can be used within interfaces.
4. Can we declare class as final?
Ans: we can't declare a top level class as static however an inner class can be declared as static. If inner class is declared as static,It is called static nested class.
5. What is try-with-resources in java?
Ans: This is one of the Java 7 feature try-with-resource management. Before java 7, there was no auto resource management and we should explicitly close the resource. Usually, it was done in the finally block of try-catch statement. This approach used to cause memory leaks when we forgot to close the resource.
From java 7, we can create resource inside try block and use it. Java takes care of closing it as soon as try-catch block get finished.
6. How to use multi-catch block in java?
Ans: In Java 7, one of the improvement was multi-catch block where we can catch multiple exceptions in a single catch block. This makes are code short and clean when every catch block has similar code.
You can write catch block when you handling multiple exceptions separate them using pipe(|)
7. what is difference between abstract class and interface?
Ans:
- Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods
- Abstract class doesn't support multiple inheritance,whereas interface support.
- Abstract can have final,static and not-static variable but interface has only static and final variables
- Abstract class can have static methods,main method and constructor. Interface can't have static methods,main method or constructor
Ans: NO, interface don't implement other interface, they extend it. Because interface can not have method implementation. but it can extend multiple interfaces, therefore interface supports multiple inheritance.
9. Is there any difference between Nested classes and inner classes?
Ans: Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.
10. Can we access the non-final local variable, inside the local inner class?
Ans: No, local variable must be constant if you want to access it in local inner calss
11. Can a class have an interface?
Ans: Yes, it is known as nested interface that means any interface declared inside the interface or class,is known as nested interface. It is static by default.
12. Can constructor be inherited?
Ans: No, constructor can not be inherited, though a derived class can call the base class constructor.
13. Difference between constructor and methods?
Ans :
- Constructor purpose is to create an instance of a class whereas methods are to group java statements.
- Constructor can not be abstract,final,native,static or synchronized.But methods can support.
- Constructor has no return type,not even void but methods have valid return type or void.
14. How can use this() and super() used with constructors?
Ans: Constructors use this to refer to another constructor in the same class with a different parameter list.
Constructor use super to invoke the super class's constructor. If a constructor uses super then it must use it in the first line otherwise the compiler will arise error.
15. How can differentiate class method and instance method?
Ans:
- class methods are methods which are declared as static. This method can be called without creating an instance of the class.But instance methods require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword.
- class methods can only operate on class members and not on instance members as class methods are unaware of instance members. instance methods of the class can also not be called from within a class method unless they are being called on an instance of the class
Ans: Garbage collection is a process of reclaiming the runtime unused objects. It is performed for memory management.
17. What is the use of Runtime class?
Ans: The purpose of the Runtime class is provide access to the Java runtime system.
18. How will you invoke any external process in java?
Ans: By using this
Runtime.getRuntime().exec(?) method
19. When we want to pass and use the object from one JVM to another JVM, what needs to be take care?
Ans: Just make sure object that needs to maintain state and passed from one JVM to another to be serializable.
20. What are immutable objects?
Ans: immutable objects are instances whose state doesn't change after it has been initialized. For example, String is an immutable class and once instantiated its value never changes.
Immutable objects are good for caching purpose because you don't need to worry about the value changes.
21. What do you mean by platform independence of java?
Ans: Platform independence means that you can run the same java program in any Operating system. For example, you can write java program in windows and run in Linux OS.
22. Is JVM platform Independent?
Ans: JVM means Java Virtual Machine. It is heart of the java programming language. It is responsible for converting byte code into machine readable code. JVM is not platform independent, you have different JVM for different OS.
23. What makes java secured?
Ans: we must say java is secured because no explicit pointer and programs runs inside virtual machine sandbox. Unlike C++ that uses runtime environment of OS, Java uses runtime environment of its own.
24. What is Method Overloading and overriding in java?
Ans:
Method Overloading:- same method name and different signature.
Method Overriding:- same method name and same signature.
Method overloading is performed within class. Overriding occurs in two classes that IS-A relationship.
Method overloading is used to increase the readability of the program. Method overriding is used to provide specific implementation of the method that is already provided by its super class.
25. What is Package and which package is imported by default?
Ans: Java package is the mechanism to organize the java classes and interfaces by grouping them. Example is java.lang.Object is the fully classified name of the Object class that is part of java.lang.package.
java.lang.package is imported by default and we don't need to import any class from this package explicitly.
No comments:
Post a Comment