Java Interview Questions and Answers (Short - 50 Words Each)
1. Java Platform Independence:
Java code is compiled into bytecode, which runs on any machine with JVM. It follows the "Write
Once, Run Anywhere" principle, making it platform-independent.
2. C++ vs Java:
C++ uses pointers and supports multiple inheritance; Java avoids pointers and uses interfaces for
multiple inheritance. Java is platform-independent; C++ is not.
3. Role of JDK, JRE, and JVM:
JDK is used for Java development, JRE for running Java programs, and JVM executes bytecode,
ensuring platform independence and security.
4. Primitive vs Non-Primitive Data Types:
Primitive types (int, float) store basic values; non-primitive types (arrays, classes) store complex
objects and provide methods.
5. Java Control Flow Statements:
Includes if, switch for decisions; for, while, do-while for loops.
6. Object-Oriented Programming (OOP):
OOP organizes code around objects. Principles: Encapsulation, Inheritance, Polymorphism,
Abstraction.
7. Constructors in Java:
Constructors initialize objects. Default constructors have no parameters; parameterized constructors
accept arguments for custom initialization.
8. this vs super:
this refers to current class object; super refers to parent class object. super can call superclass
methods/constructors.
9. Method Overloading vs Overriding:
Overloading: same method name, different parameters (compile-time). Overriding: redefine parent
class method in subclass (runtime).
10. Aggregation vs Inheritance:
Aggregation is HAS-A relationship (loose coupling). Inheritance is IS-A relationship (tight coupling).
11. Dynamic Binding:
Method calls are resolved at runtime, enabling runtime polymorphism.
12. final Keyword:
final prevents further modification. Final variables can't be reassigned, methods can't be overridden,
classes can't be extended.
13. Abstract Class vs Interface:
Abstract classes can have concrete methods; interfaces only abstract methods (pre-Java 8).
14. instanceof Operator:
Checks object type at runtime.
15. Java Naming Conventions:
Class: PascalCase, Methods/Variables: camelCase, Constants: UPPER_CASE.
16. "Hello, Java!" Program:
class HelloJava {
public static void main(String[] args) {
[Link]("Hello, Java!");
17. Operators Example:
int a = 10, b = 5;
[Link](a + b);
[Link](a > b);
[Link](a > 5 && b < 10);
18. Control Statements Example:
if (a > b) { } else { }
switch(a) { case 1: break; }
for(int i=0;i<5;i++){}
while(i<5){}
do{}while(i<5);
19. Student Class Example:
class Student {
int id; String name;
void display() { [Link](id+" "+name); }
}
Student s = new Student();
[Link] = 1; [Link] = "John";
[Link]();
20. Constructor Overloading:
class Demo {
Demo() {}
Demo(int a) {}
21. Static Keyword Example:
class Demo {
static int count = 0;
static void show() { [Link](count); }
[Link]();
22. this Keyword Example:
class Demo {
int x;
Demo(int x) { this.x = x; }
23. Single and Multilevel Inheritance:
class A { }
class B extends A { }
class C extends B { }
24. Aggregation Example:
class Address { }
class Employee {
Address address;
25. Overloading and Overriding:
Overloading (compile-time polymorphism) has different signatures. Overriding (runtime
polymorphism) redefines parent class methods.
26. super Keyword Example:
class A {
A() { [Link]("A"); }
class B extends A {
B() { super(); }
27. instanceof Example:
Student s = new Student();
[Link](s instanceof Student);