Skip to main content

Java collections interview questions and answers For Fresher's and experienced | Interview Tips

Hello guys today we are going to learn about Java questions and answers. This will help you to get success in your interviews.

java interview questions and answers for freshers and experienced

1) List any Five features of Java?

Ans: Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded.

2) Why is Java Architectural Neutral?

Ans: It’s compiler generates an architecture-neutral object file format, which makes the compiled code to be executable on many processors, with the presence of Java runtime system
Why is Java Architectural Neutral, Java

                          Java Architectural Neutral

3) How Java enabled High Performance?

Ans: Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java bytecode, which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor.

4) Why Java is considered dynamic?

Ans: It is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

5) What is Java Virtual Machine and how it is considered in context of Java’s platform independent feature?

Ans: When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.

6)What do you mean by Object?

Ans: Object is a runtime entity and it’s state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

7)Define class?

java class

Ans: A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

8) Is a Local Variable?

Ans: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.
Interview Questions and Tips-

9) What is a Instance Variable?

Ans: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.

10) What is a Class Variable?

Ans: These are variables declared with in a class, outside any method, with the static keyword.

11)What is Singleton class?

Ans: Singleton class control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.

12)What do you mean by Constructor?

What do you mean by Constructor

Ans: Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.
Interview Questions and Tips-

13)What is a static variable?

Ans: Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

14)What do you mean by Access Modifier?

What do you mean by Access Modifier?

Ans: Java provides access modifiers to set access levels for classes, variables, methods and constructors. A member has package or default accessibility when no accessibility modifier is specified.

15)Which package is used for pattern matching with regular expressions?

Ans: java.util.regex package is used for this purpose.



16)java.util.regex consists of which classes?

Ans: java.util.regex consists of three classes − Pattern class, Matcher class and PatternSyntaxException class.

17) What is finalize() Method? 

What is finalize() Method in java

Ans: It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.

18)What is an Exception?

Ans: An exception is a problem that arises during the execution of a program. Exceptions are caught by handlers positioned along the thread's method invocation stack.

19)What do you mean by Checked Exceptions?

Ans: It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

20)Explain Runtime Exceptions?

Ans: It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of completion.

21)Which are the two subclasses under Exception class?

Ans: The Exception class has two main subclasses : IOException class and RuntimeException Class.

22)When throws keyword is used?

Ans: If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature.

23)When throw keyword is used?

Ans: An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.

24)How finally used under Exception Handling?

Ans: The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

25)Define Inheritance?

Ans: It is the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.

26)When super keyword is used?

Ans: If the method overrides one of its superclass's methods, overridden method can be invoked through the use of the keyword super. It can be also used to refer to a hidden field.

27)What is Polymorphism?

What is Polymorphism?, java

Ans: Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

28)What is Abstraction?

Ans: It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.

29)What is Abstract class?

Ans: These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.

30)When Abstract methods are used?

Ans: If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

31)What is Encapsulation?

What is Encapsulation?, java

Ans: It is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore encapsulation is also referred to as data hiding.

32)What is an Interface?

Ans: An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

33)Give some features of Interface?

Ans: It includes −Interface cannot be instantiated.
An interface does not contain any constructors.
All of the methods in an interface are abstract.

34)Define Packages in Java?

Ans: A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management.

35)Why Packages are used?

Ans: Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations, etc., easier.



36)What do you mean by Multi threaded program?

Ans: A multi threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.

37)What are the two ways in which Thread can be created?

Ans: Thread can be created by: implementing Runnable interface, extending the Thread class.

38)What is an applet?

What is an applet?, java

Ans: An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.

39)Explain garbage collection in Java?

Ans: It uses garbage collection to free the memory. By cleaning those objects that is no longer reference by any of the program.

40)Define immutable object?

Ans: An immutable object can’t be changed once it is created.

41)Difference between throw and throws?

Ans: Throw is used to trigger an exception where as throws is used in declaration of exception.

Without throws, Checked exception cannot be handled where as checked exception can be propagated with throws.

42)Explain the following line used under Java Program − public static void main (String args[ ])?

Ans: public − it is the access specifier.
static − it allows main() to be called without instantiating a particular instance of a class.
void − it affirns the compiler that no value is returned by main().
main() − this method is called at the beginning of a Java program.
String args[ ] − args parameter is an instance array of class String.

43)Define JRE i.e. Java Runtime Environment?

Ans: Java Runtime Environment is an implementation of the Java Virtual Machine which executes Java programs. It provides the minimum requirements for executing a Java application.

44)What is JAR file?

Ans: JAR files is Java Archive fles and it aggregates many files into one. It holds Java classes in a library. JAR files are built on ZIP file format and have .jar file extension.

45)What is a WAR file?

Ans: This is Web Archive File and used to store XML, java classes, and JavaServer pages. which is used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, static Web pages etc.

46)Define JIT compiler?

Ans: It improves the runtime performance of computer programs based on byte code.

47)What is static block?

Ans: It is used to initialize the static data member, It is executed before main method at the time of class loading.

48)What is function overloading?

Ans: If a class has multiple functions by same name but different parameters, it is known as Method Overloading.

49)What is function overriding?

Ans: If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.

50)Difference between Overloading and Overriding?

Ans: Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its super class parameter must be different in case of overloading, parameter must be same in case of overriding.
Difference between Overloading and Overriding?, java






51)What is final class?

Ans: Final classes are created so the methods implemented by that class cannot be overridden. It can’t be inherited.

52)What is NullPointerException?

Ans: A NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field of a null object etc.

53)What are Wrapper classes?

Ans: These are classes that allow primitive types to be accessed as objects. Example: Integer, Character, Double, Boolean etc.

54)What is the difference between a Window and a Frame?

Ans: The Frame class extends Window to define a main application window that can have a menu bar.

55)Which package has light weight components?

Ans: javax.Swing package. All components in Swing, except JApplet, JDialog, JFrame and JWindow are lightweight components.

56)What is the purpose of File class?

Ans: It is used to create objects that provide access to the files and directories of a local file system.

57)What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

Ans: The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.

58)What is the difference between static and non-static variables?

Ans: 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.

59)What is the difference between Swing and AWT components?

Ans: AWT components are heavy-weight, whereas Swing components are lightweight. Heavy weight components depend on the local windowing toolkit. For example, java.awt.Button is a heavy weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.

60)What's the difference between constructors and other methods?

Ans: 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.

61)What is the difference between a break statement and a continue statement?

Ans: A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.
What is the difference between a break statement and a continue statement?

62)What is the difference between an Interface and an Abstract class?

Ans: An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation.

63)Why do we need wrapper classes?

Ans: We can pass them around as method parameters where a method expects an object. It also provides utility methods.

64)What is the difference between error and an exception?

Ans: An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. Exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist.

65)Is it necessary that each try block must be followed by a catch block?

Ans: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block or a finally block.

66)What are the advantages of ArrayList over arrays?

Ans: ArrayList can grow dynamically and provides more powerful insertion and search mechanisms than arrays.

67)Can I import same package/class twice? Will the JVM load the package twice at runtime?

Ans: One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.

68)Is it possible to instantiate the abstract class?

Ans: No, abstract class can never be instantiated.

69)What is JDBC?

Ans: JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connects to the database.

70)What is JDBC Driver?

Ans: JDBC Driver is a software component that enables java application to interact with the database.There are 4 types of JDBC drivers:

JDBC-ODBC bridge driver
Native-API driver (partially java driver)
Network Protocol driver (fully java driver)
Thin driver (fully java driver).

71)What are the steps to connect to the database in java?

Ans: Registering the driver class
Creating connection
Creating statement
Executing queries
Closing connection

72)What is the difference between Statement and Prepared Statement interface?

Ans: In case of Statement, query is complied each time whereas in case of Prepared Statement, query is complied only once. So performance of Prepared Statement is better than Statement.

73)How can we execute stored procedures and functions?

Ans: By using Callable statement interface, we can execute procedures and functions.

74)What is the default value of an object reference declared as an instance variable?

Ans: The default value will be null unless we define it explicitly.

75)What type of parameter passing does Java support?

Ans: In Java the arguments are always passed by value.

76)What is serialization?

Ans: Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.
What is serialization?, java

Comments

Popular posts from this blog

How To give Self introduction in an Interviews | Introduce yourself Self Introduction Sample

How To give Self introduction in Interviews Introduce yourself H ello guys today we are going to learn how to introduce yourself in interviews in English. "How to give self introduction in an Interview"? The same question can be asked in different types like Introduce yourself,describe yourself, tell something about yourself..etc. It is very important that you have to present it properly in front of interviewer. Your presentation of your personal introduction will decide your job. So here we are Going to see some samples of " How to introduce yourself in an interview" . Steps To Follow : Step :1 First of all be polite to wish the interviewers. You can wish by saying Good morning, Good afternoon and Good evening etc. Likewise if you are an experienced candidate, you can shake hand with the interviewer with a firm smile on your face. Step: 2 Tell your full name and where are you from. Rather than mentioning your place name only, you should a

How To Introduce Yourself In Interview | How To Introduce Yourself At Interview

How To Introduce Yourself In Interview Most of the people don't know how to give Self introduction in an Interviews in proper way. The same question can be asked in different types like Introduce yourself,describe yourself, tell something about yourself, how to introduce yourself at interview, how to introduce yourself in interview, How to introduce yourself, how to introduce yourself for interview, how to introduce yourself example, how to introduce yourself professionally, how to introduce yourself in English, How to give Self introduction, how to give self introduction in interview,.. etc. It is very important that you have to present it properly in front of interviewer. Your presentation of your personal introduction will decide your job. Tell me about yourself Example 1: Good morning, Thank you so much for giving me the opportunity to introduce myself. My name is Nagashetty, I am from Karnataka, I born & brought up in Karnataka. Coming to my qualifi