0% found this document useful (0 votes)
2 views

Java Questions

java questions

Uploaded by

Kishore My
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Questions

java questions

Uploaded by

Kishore My
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Here’s a summary of key terms and concepts in Core Java with brief explanations:

1. Java Basics

 JVM (Java Virtual Machine): Abstract machine that provides a runtime environment
for Java bytecode.
 JRE (Java Runtime Environment): Provides libraries, JVM, and other components to
run applications written in Java.
 JDK (Java Development Kit): Software development kit for Java, which includes JRE
and development tools.
 Bytecode: The intermediate code generated after the Java source code is compiled,
executed by the JVM.
 Class: Blueprint from which individual objects are created in Java.
 Object: Instance of a class, containing methods and properties.
 Inheritance: Mechanism in Java where one class acquires the properties and behaviors of
another class.
 Polymorphism: Ability of one object to take on many forms, typically achieved through
method overloading or overriding.
 Encapsulation: Bundling data and methods that operate on the data within one unit
(class), restricting direct access to some of the object's components.
 Abstraction: Hiding the complex implementation details and showing only the necessary
functionalities.
 Method Overloading: Defining multiple methods with the same name but different
parameter lists in the same class.
 Method Overriding: When a subclass provides a specific implementation for a method
that is already defined in its superclass.
 Interface: A reference type in Java, similar to a class, which can contain only constants,
method signatures, default methods, and static methods.
 Package: A namespace that organizes classes and interfaces by functionality.

2. OOP Concepts

 Constructor: Special method used to initialize objects.


 this keyword: Refers to the current instance of the class.
 super keyword: Refers to the superclass object from which the current object is derived.
 final keyword: Used to declare constants, prevent method overriding, or prevent
inheritance.
 static keyword: Allows a method or variable to be called or accessed without creating an
instance of the class.

3. Data Types and Operators

 Primitive Data Types: Basic types such as int, char, double, boolean, etc.
 Non-Primitive Data Types: Includes arrays, classes, and interfaces.
 Operators: Arithmetic (+, -, *, /), relational (<, >, ==, etc.), logical (&&, ||, !), bitwise,
and assignment operators.

4. Control Flow

 if-else statement: Conditional branching statement.


 switch statement: Allows for multiple possible execution paths based on a variable's
value.
 for loop: Iterates over a range or array.
 while loop: Repeats a statement while a condition is true.
 do-while loop: Similar to while, but executes at least once.

5. Exception Handling

 Exception: An event that disrupts the normal flow of a program.


 try-catch block: Used to handle exceptions, where the try block contains code that
might throw an exception, and the catch block handles the exception.
 finally block: Always executes after try-catch block, used for cleanup operations.
 throw: Used to explicitly throw an exception.
 throws: Declares exceptions that a method might throw.

6. Multithreading

 Thread: A separate path of execution.


 Runnable Interface: Used to define a class whose instances can be executed by a thread.
 synchronized keyword: Used to control access to a resource by multiple threads.
 wait() and notify(): Methods to coordinate the communication between threads.

7. Collections Framework
 ArrayList: Dynamic array, part of the List interface.
 HashMap: Stores key-value pairs, part of the Map interface.
 Set Interface: Ensures that no duplicate elements are stored.
 Iterator: Interface used to iterate over collections.

8. I/O (Input/Output)

 BufferedReader: Used to read text from an input stream.


 FileReader/FileWriter: Used for reading and writing files in Java.
 Serializable Interface: Enables an object’s state to be represented as a sequence of bytes.

9. Java 8 Features

 Lambda Expressions: Provide a clear and concise way to represent one method interface
using an expression.
 Streams: Used to process collections of objects with operations such as filtering,
mapping, etc.
 Optional Class: Used to avoid NullPointerException.

10. Memory Management and Garbage Collection

 Heap Memory: Runtime memory where objects are stored.


 Stack Memory: Memory used for execution of threads.
 Garbage Collection: Automatic memory management that destroys unused objects.
 finalize() method: Used to perform cleanup before an object is collected by the garbage
collector.

11. Java Annotations

 @Override: Indicates that a method is overriding a method in a superclass.


 @Deprecated: Marks a method as outdated and suggests alternatives.
 @FunctionalInterface: Specifies that an interface is intended to be a functional
interface.

12. JDBC (Java Database Connectivity)

 DriverManager: Manages a list of database drivers.


 Connection: Represents a connection to a database.
 Statement: Used to execute SQL queries.
 ResultSet: A table of data representing the results of a query.
Here is a list of definitions for all the important Java keywords:

1. abstract

 Used to declare an abstract class or method. Abstract classes cannot be instantiated, and
abstract methods must be implemented by subclasses.

2. assert

 Used to make an assertion—a condition that must be true. If the condition is false, the
program throws an AssertionError.

3. boolean

 Used to declare a variable that can hold true or false.

Boolean data type Size is 1 bit

4. break

 Terminates the nearest enclosing loop or switch statement.

5. byte

 A primitive data type that stores an 8-bit integer value.

6. case

 Defines a branch in a switch statement.

7. catch

 Used to handle exceptions. It defines a block of code to execute when an exception


occurs in a try block.

8. char

 A primitive data type that stores a single 16-bit Unicode character.


9. class

 Used to declare a class, which is a blueprint for creating objects.

10. const (unused keyword)

 Not used in Java; reserved for future use.

11. continue

 Skips the current iteration of a loop and proceeds to the next iteration.

12. default

 Specifies the default block of code in a switch statement that executes if no case
matches the expression.

13. do

 Used in the do-while loop, which executes a block of code at least once before checking
a condition.

14. double

 A primitive data type that holds a 64-bit floating-point number.

15. else

 Used with if to specify an alternative block of code if the if condition evaluates to false.

16. enum

 Defines a set of named constants. An enum is a special type of class.

17. extends

 Used to indicate that a class is inheriting another class.

18. final
 Used to declare constants, prevent inheritance of a class, or prevent overriding of
methods.

19. finally

 Used with try-catch to define a block of code that always executes after the try and
catch blocks, even if an exception is thrown.

20. float

 A primitive data type that holds a 32-bit floating-point number.

21. for

 Used to create a loop that executes a block of code a specific number of times.

22. goto (unused keyword)

 Not used in Java; reserved for future use.

23. if

 Used to create a conditional statement that executes a block of code only if a specified
condition is true.

24. implements

 Used to indicate that a class is implementing an interface.

25. import

 Used to include classes from other packages in your code.

26. instanceof

 Tests whether an object is an instance of a specified class or implements a particular


interface.

27. int

 A primitive data type that holds a 32-bit integer.


28. interface

 Used to declare an interface, which is a reference type similar to a class that can contain
only abstract methods and static constants.

29. long

 A primitive data type that holds a 64-bit integer.

30. native

 Specifies that a method is implemented in platform-dependent code, such as C or C++.

31. new

 Used to create new objects or arrays.

32. null

 Represents the absence of a value for a reference type.

33. package

 Defines a namespace for organizing classes and interfaces.

34. private

 Used to declare members (fields, methods) of a class that are only accessible within that
class.

35. protected

 Used to declare members of a class that are accessible within the same package or by
subclasses.

36. public

 Used to declare members of a class that are accessible from any other class.

37. return
 Exits from a method and optionally returns a value.

38. short

 A primitive data type that holds a 16-bit integer.

39. static

 Used to declare class-level fields and methods, which are shared among all instances of
the class and can be accessed without creating an instance.

40. strictfp

 Ensures floating-point calculations are consistent across different platforms by adhering


to strict IEEE 754 standards.

41. super

 Refers to the superclass of the current object and can be used to call methods or
constructors of the superclass.

42. switch

 Used to create a multi-way branch based on the value of an expression.

43. synchronized

 Used to ensure that a method or block of code is executed by only one thread at a time.

44. this

 Refers to the current instance of a class.

45. throw

 Used to explicitly throw an exception.

46. throws

 Declares exceptions that a method might throw.


47. transient

 Prevents a field from being serialized when an object is persisted to streams.

48. try

 Used to define a block of code that might throw an exception, followed by one or more
catch blocks or a finally block.

49. void

 Specifies that a method does not return a value.

50. volatile

 Indicates that a variable's value may change unexpectedly from outside the current
thread, so the JVM should not cache its value.

51. while

 Used to create a loop that repeatedly executes a block of code as long as the specified
condition is true.

Here’s a quick glance of Java for interviews, focusing on the most important concepts,
keywords, and patterns:

1. Object-Oriented Programming (OOP) Concepts

 Class: Blueprint for creating objects (instances). Contains fields (attributes) and methods
(behaviors).
 Object: Instance of a class with its own state and behavior.
 Inheritance: Allows one class to inherit the properties and methods of another class
(extends keyword).
 Polymorphism: Ability for a method to behave differently based on the object that calls
it. Achieved via:
o Method Overloading: Same method name, different parameter lists.
o Method Overriding: Subclass provides a specific implementation of a method
from the parent class.
 Encapsulation: Wrapping data (variables) and code (methods) together, restricting direct
access to some of the object's components (using private and public).
 Abstraction: Hiding complex implementation details, showing only essential features.
Achieved via:
o Abstract Classes: Contain abstract methods (without body) and concrete
methods.
o Interfaces: 100% abstract, used to define a contract that classes must follow
(implements keyword).

2. Core Java Concepts

 JVM, JRE, JDK:


o JVM (Java Virtual Machine): Executes Java bytecode.
o JRE (Java Runtime Environment): Provides the environment to run Java
programs (includes JVM + libraries).
o JDK (Java Development Kit): Contains tools to develop Java programs
(includes JRE + development tools).
 Garbage Collection: Automatic memory management in Java that deallocates objects no
longer in use.
 Access Modifiers:
o public: Accessible from any class.
o protected: Accessible within the same package or subclasses.
o private: Accessible only within the class.
o default (no modifier): Accessible within the same package.

3. Exception Handling

 try-catch: Used to handle exceptions (runtime errors). try contains the code that might
throw an exception, and catch handles it.
 finally: Always executes after try-catch, used for cleanup operations.
 throw: Explicitly throws an exception.
 throws: Declares that a method might throw exceptions.
4. Memory Management

 Stack Memory: Used for method execution and local variables.


 Heap Memory: Used for storing objects created via new.
 finalize(): Method called by the garbage collector before an object is destroyed.

5. Multithreading

 Thread: Separate path of execution. Can be created by:


o Implementing Runnable interface.
o Extending the Thread class.
 synchronized: Ensures thread-safe access to critical sections of code.
 wait(), notify(), and notifyAll(): Methods for inter-thread communication.

6. Java Collections Framework

 List: Ordered collection (e.g., ArrayList, LinkedList).


 Set: Unordered collection, no duplicates (e.g., HashSet, TreeSet).
 Map: Key-value pairs (e.g., HashMap, TreeMap).
 Queue: Follows the FIFO principle (e.g., PriorityQueue).

7. Java 8 Features

 Lambda Expressions: Short way of representing anonymous methods. Used mainly in


functional programming.

java
Copy code
(parameters) -> expression;

 Streams: Used to process collections of objects in a functional manner (e.g., filtering,


mapping).
 Optional: Container class to avoid NullPointerException.
 Functional Interfaces: An interface with exactly one abstract method (e.g., Runnable,
Callable).

8. Important Java Keywords

 static: Used for class-level variables and methods (common to all instances).
 final: Used to declare constants, prevent method overriding, or inheritance.
 this: Refers to the current instance of a class.
 super: Refers to the superclass of the current object.
 transient: Prevents a variable from being serialized.
 volatile: Ensures visibility of changes to variables across threads.

9. Commonly Asked Concepts

 Pass by Value: Java passes arguments by value, meaning that copies of variables are
passed to methods.
 Immutable String: Strings in Java are immutable, meaning once created, their value
cannot change.
 Singleton Design Pattern: Ensures that only one instance of a class is created during the
lifetime of the application.

10. Java Coding Examples

 Factorial using Recursion:

java
Copy code
public int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

 Palindrome Check:

java
Copy code
public boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) return false;
i++;
j--;
}
return true;
}

11. Common Interview Questions

 What is the difference between an abstract class and an interface?


o Abstract class can have both abstract and non-abstract methods, while interfaces
can have only abstract methods (before Java 8).
 How does garbage collection work in Java?
o Java automatically deallocates memory by destroying objects that are no longer
referenced.
 Explain equals() and hashCode() methods.
o equals() compares two objects for equality, while hashCode() provides a
unique integer representation of an object. Objects that are equal must have the
same hash code.
 What is the difference between ArrayList and LinkedList?
o ArrayList is better for random access (index-based), while LinkedList is better
for insertions and deletions.
 How does synchronized work in Java?
o synchronized ensures that only one thread can access a resource at a time,
providing thread safety for shared resources.

12. Best Practices for Interviews

 Be clear about OOP principles: Inheritance, Polymorphism, Encapsulation,


Abstraction.
 Practice coding questions: Solve problems on arrays, strings, recursion, and data
structures like trees and linked lists.
 Understand collections and algorithms: Be familiar with HashMap, ArrayList, and
algorithms for searching/sorting.
 Know Java 8 features: Lambdas, Streams, Optional.

You might also like