Java Interview Questions for Freshers
-
What is Java? What are its key features?
Java is a high-level, object-oriented programming language known for its portability, platform independence, and security features. It is designed to be simple, robust, and dynamic, making it suitable for a wide range of applications.
-
Explain the difference between JDK, JRE, and JVM.
JDK (Java Development Kit) is a software development kit that provides tools for developing Java applications. JRE (Java Runtime Environment) is a set of tools for executing Java applications, and it includes the JVM (Java Virtual Machine). JVM is an abstract machine that provides an environment for Java bytecode to be executed.
-
Describe encapsulation in Java.
Encapsulation is one of the four fundamental Object-Oriented Programming (OOP) concepts. It involves bundling the data (attributes) and the methods (functions) that operate on the data into a single unit known as a class. Access to the data is restricted to methods within the class, ensuring data integrity and security.
-
What is the difference between "==" and ".equals()" when comparing strings?
The "==" operator checks for reference equality, i.e., whether two objects refer to the same memory location. On the other hand, ".equals()" method is used to compare the content of two strings, checking if the characters in the strings are the same. It is important to use ".equals()" when comparing the content of strings.
-
Explain the concept of inheritance in Java.
Inheritance is a feature of OOP that allows a class to inherit properties and behaviors from another class. The class that is inherited from is called the superclass, and the class that inherits is called the subclass. This promotes code reuse, as the subclass can access the members of the superclass.
-
How is multithreading achieved in Java?
Multithreading in Java is achieved by extending the
Threadclass or implementing theRunnableinterface. TheThreadclass provides methods to create and control threads, while theRunnableinterface allows a class to be executed by a thread. Multithreading enhances performance by allowing multiple tasks to be executed concurrently. -
What is an exception in Java?
An exception in Java is an event that disrupts the normal flow of a program during runtime. Exceptions can occur due to various reasons, such as incorrect user input or a file not being found. Exception handling in Java involves using try, catch, and finally blocks to gracefully handle these unexpected events.
-
Explain the purpose of the "Map" interface in Java.
The "Map" interface in Java is part of the Java Collections Framework and represents a collection of key-value pairs. It does not allow duplicate keys, and each key is associated with exactly one value. Common implementations of the Map interface include HashMap and TreeMap.
-
How can you read data from a file in Java?
Reading data from a file in Java is typically done using classes from the
java.iopackage. One common approach is to use aFileReaderin combination with aBufferedReaderto efficiently read data line by line.try (BufferedReader br = new BufferedReader(new FileReader("filename.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } -
Name some of the Java keywords and explain their usage.
Some Java keywords include "public," "static," "void," "class," "if," and "else." These keywords have specific meanings in the Java language. For example, "public" is an access modifier that specifies the visibility of a class, method, or field, while "static" indicates that a method or variable belongs to the class rather than an instance.
No comments:
Post a Comment