[go: up one dir, main page]

0% found this document useful (0 votes)
2 views9 pages

Prep

Java is a high-level, object-oriented programming language known for its platform independence, security, and robustness. Key concepts include OOP principles such as encapsulation, inheritance, and polymorphism, alongside differences between JDK, JRE, and JVM. The document also covers various Java features, variable types, memory management, design patterns, and key programming concepts.

Uploaded by

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

Prep

Java is a high-level, object-oriented programming language known for its platform independence, security, and robustness. Key concepts include OOP principles such as encapsulation, inheritance, and polymorphism, alongside differences between JDK, JRE, and JVM. The document also covers various Java features, variable types, memory management, design patterns, and key programming concepts.

Uploaded by

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

1. What is Java and what are its main features?

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle).
It is designed to be platform-independent, secure, and robust.
Main features of Java:
 Platform-independent (Write Once, Run Anywhere - WORA)
 Object-Oriented (supports OOP concepts like inheritance, polymorphism, encapsulation)
 Multi-threaded (supports concurrent execution of multiple tasks)
 Secure (provides a secure runtime environment with bytecode verification)
 Robust (has strong memory management and exception handling)
 Garbage Collected (automatic memory management)
 Distributed (supports networking & remote method invocation)

2. Difference between JDK, JRE, and JVM

Component Description

JDK (Java Development Kit) Includes JRE + development tools (compiler, debugger) for Java application development.

JRE (Java Runtime Environment) Provides libraries and JVM to run Java applications but does not include development tools.

JVM (Java Virtual Machine) Abstract machine that executes Java bytecode, making Java platform-independent.

3. What is platform independence in Java?


Platform independence means Java code can run on any system with a JVM, regardless of the underlying OS. Java achieves
this by compiling source code into bytecode, which the JVM executes.

4. Difference between Path and Classpath


 Path: Specifies the location of executable files like javac and java.
 Classpath: Specifies the location of .class files and external JARs required for execution.

5. Types of Variables in Java


1. Local Variables (declared inside a method, block, or constructor)
2. Instance Variables (declared inside a class but outside methods, associated with objects)
3. Static Variables (declared with static, shared across all objects of a class. Static variables belong to the class rather
than any specific instance. They are shared among all instances of the class)

Example of static

6. Difference between Local and Instance Variables

Local Variable Instance Variable

Declared inside a method or block Declared inside a class but outside methods
Local Variable Instance Variable

Scope is limited to the method/block Scope is throughout the instance (object)

Cannot have access modifiers Can have access modifiers

Not initialized by default Initialized with default values

8. What is a constructor?
A constructor is a special method used to initialize an object when it is created. It has the same name as the class and no
return type.

9. Types of Constructors in Java


1. Default Constructor: No parameters, initializes default values.
2. Parameterized Constructor: Takes arguments to initialize objects with specific values.
3. Copy Constructor: Used to create a new object by copying an existing object.

10. Difference between break and continue statements


 break: Exits the loop or switch statement immediately.
 continue: Skips the current iteration and moves to the next iteration.

11. Difference between final, finally, and finalize

Keyword Description

final Used for constants, prevents method overriding & inheritance.

finally A block that executes after try-catch, used for cleanup code.

finalize() A method in Object class, called before an object is garbage collected.

12. Difference between throw and throws


 throw: Used to explicitly throw an exception.
 throws: Declares exceptions a method might throw.
Example:
void myMethod() throws IOException { } // Using throws
throw new IOException("Error occurred"); // Using throw
13. Difference between == and .equals()
 == compares references (memory addresses).
 .equals() compares actual values (overridden in String, Integer, etc.).
Example:
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)

14. Difference between String, StringBuilder, and StringBuffer

Feature String StringBuilder StringBuffer

Mutability Immutable Mutable Mutable

Thread-Safe Yes No Yes

Performance Slow (new object created) Fast Slower than StringBuilder

15. Why is String immutable in Java?


 Security reasons (prevents modifications in sensitive data like passwords).
 String pool optimization (reusability of objects).
 Thread safety (immutable objects are inherently thread-safe).

16. What is the String pool?


A special memory area inside the heap where string literals are stored. It helps in memory optimization by reusing existing
String objects.
Example:
String s1 = "Java";
String s2 = "Java"; // Reuses the same object from the pool

17. What are wrapper classes?


Wrapper classes convert primitive types into objects. Example:
 int (primitive type)→ Integer(wrapper class)
 double → Double
Integer num = Integer.valueOf(10); // Wrapper object

18. Difference between Primitive and Reference Types

Primitive Type Reference Type

Stores actual value Stores reference to object


Primitive Type Reference Type

Examples: int, double, char Examples: String, Array, Objects

19. Difference between Stack and Heap Memory

Stack Heap

Stores method calls & local variables Stores objects and instance variables

Fast access Slower access

Auto-managed (LIFO) Requires Garbage Collection

20. Difference between Shallow Copy and Deep Copy

Shallow Copy Deep Copy

Copies only references, not actual data Creates a new independent copy of data

Changes in original affect the copy Independent objects

21. Difference between System.out.println() and System.err.println()


 System.out.println() prints normal output to stdout.
 System.err.println() prints errors to stderr.

22. Difference between import and static import


 import: Imports a class/package.
 static import: Imports static members of a class.
Example:
import static java.lang.Math.*; // Static import
System.out.println(sqrt(16)); // Directly using sqrt()

23. Difference between instanceof and isInstance()


 instanceof is an operator that checks if an object is an instance of a class.
 isInstance() is a method in Class class that does the same.
String s = "Java";
System.out.println(s instanceof String); // true
System.out.println(String.class.isInstance(s)); // true

What are the main principles of OOP?


The four pillars of OOP are:
1. Encapsulation → Data hiding using private variables and public methods.
2. Abstraction → Hiding implementation details and showing only essential features.
3. Inheritance → Acquiring properties & behavior from a parent class.
4. Polymorphism → Multiple forms of the same method (method overloading & overriding).

2️⃣ Difference between a Class and an Object

Feature Class Object

Definition A blueprint for creating objects An instance of a class

Memory Allocation No memory until an object is created Memory is allocated in the heap

Example class Car { int speed; } Car c = new Car();

3️⃣ What is Inheritance?


Inheritance allows a child class to acquire the properties and behavior of a parent class.
Example:
class Animal { // Parent Class
void sound() { System.out.println("Animal makes a sound"); }
}

class Dog extends Animal { // Child Class


void bark() { System.out.println("Dog barks"); }
}

4️⃣ Why doesn't Java support Multiple Inheritance?


Java does not support multiple inheritance with classes to avoid the diamond problem, where ambiguity arises due to
conflicting method implementations.
✅ Solution: Java supports multiple inheritance through interfaces.
interface A { void methodA(); }
interface B { void methodB(); }

class C implements A, B { // Multiple inheritance using interfaces


public void methodA() { }
public void methodB() { }
}

What is Method Overloading?


➡ Same method name but different parameters (Compile-time Polymorphism).
class MathUtils {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; } // Overloaded method
}

6️⃣ What is Method Overriding?


➡ Child class redefines a method of the parent class (Runtime Polymorphism).
class Parent {
void show() { System.out.println("Parent class"); }
}

class Child extends Parent {


void show() { System.out.println("Child class"); } // Overridden method
}

7️⃣ What is Polymorphism?


Polymorphism means one interface, multiple implementations.
1. Compile-time polymorphism → Method overloading
2. Runtime polymorphism → Method overriding

8️⃣ Difference between Compile-time and Runtime Polymorphism

Feature Compile-time Polymorphism Runtime Polymorphism

Method Type Method Overloading Method Overriding

Binding Static (Early) Binding Dynamic (Late) Binding

Performance Faster Slower

9️⃣ What is Abstraction?


➡ Hiding implementation details while showing essential features.
Achieved using:
1. Abstract classes
2. Interfaces
abstract class Car {
abstract void start(); // Abstract method (must be implemented by subclass)
}

🔟 What is Encapsulation?
➡ Wrapping data (variables) and code (methods) into a single unit (class) & restricting access.
class Person {
private String name; // Private variable
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
✅ The name variable cannot be accessed directly outside the class.

1️⃣1️⃣ Abstract Class vs Interface

Feature Abstract Class Interface

Can have both abstract & concrete


Methods Only abstract methods (Java 7), default/static methods (Java 8)
methods

Multiple
Not supported Supported
Inheritance

Access Modifiers Can have any Methods are public by default

1️⃣2️⃣ What is Composition?


➡ "Has-a" relationship where one class is made up of objects of another class.
class Engine { }
class Car {
private Engine engine; // Composition
}

1️⃣3️⃣ What is Aggregation?


➡ Weaker version of composition where objects can exist independently.
class Department { }
class University {
private List<Department> departments; // Aggregation
}

1️⃣4️⃣ Association vs Aggregation vs Composition

Relationship Association Aggregation Composition

Definition General connection between two objects Weak "has-a" relationship Strong "has-a" relationship

Lifespan Objects are independent Child can exist without parent Child cannot exist without parent

1️⃣5️⃣ What is the Singleton Pattern?


➡ Ensures only one instance of a class is created.
Example:
class Singleton {
private static Singleton instance;
private Singleton() { } // Private constructor
public static Singleton getInstance() {
if (instance == null) instance = new Singleton();
return instance;
}
}

What is the Factory Pattern?


➡ Used to create objects without specifying their concrete classes.
Example:
class ShapeFactory {
static Shape getShape(String type) {
if (type.equals("Circle")) return new Circle();
return new Rectangle();
}
}

What is Dependency Injection?


➡ A design pattern where objects get their dependencies from an external source instead of creating them inside the class.
Example using Constructor Injection:
class Service { }
class Client {
private Service service;
Client(Service service) { this.service = service; }
}

1️⃣8️⃣ Strategy Pattern vs Factory Pattern

Pattern Purpose

Factory Pattern Creates objects based on conditions

Strategy Pattern Selects an algorithm dynamically at runtime

1️⃣9️⃣ What is the Observer Pattern?


➡ One-to-many relationship where multiple objects listen to a subject's state changes.
Example:
 A YouTube channel notifies subscribers when a new video is uploaded.
2️⃣0️⃣ What is the Decorator Pattern?
➡ Adds new behavior dynamically to objects without modifying their structure.
Example:
 Wrapping a BufferedReader around a FileReader.

2️⃣1️⃣ Tight Coupling vs Loose Coupling

Coupling Type Description

Tight Coupling Classes depend on each other directly. Harder to modify.

Loose Coupling Classes depend on interfaces. Easier to modify and test.

2️⃣2️⃣ Concrete Class vs Abstract Class

Feature Concrete Class Abstract Class

Methods All methods have implementations Can have abstract methods

Instance Creation Can be instantiated Cannot be instantiated

2️⃣3️⃣ Static Method vs Instance Method

Feature Static Method Instance Method

Belongs to Class Object

Can access Only static members Both static & instance members

Invocation ClassName.method() obj.method()

You might also like