Java Exam Preparation Notes
1. Describe Java as an Object-Oriented Programming Language. What are the
key features of Java?
Java is an object-oriented language, meaning it organizes code around objects and classes.
It follows OOP concepts like:
- Encapsulation: Data hiding using classes
- Inheritance: One class inherits properties of another
- Polymorphism: One interface, many implementations
- Abstraction: Hiding implementation details
Key Features of Java:
- Simple
- Object-Oriented
- Platform Independent
- Secure
- Robust
- Multithreaded
- Portable
- High Performance
- Distributed
- Dynamic
2. Explain in detail the features of Java
- Simple: Easy syntax, no pointers.
- Object-Oriented: Code based on objects and classes.
- Platform Independent: Write once, run anywhere using JVM.
- Secure: No direct memory access.
- Robust: Strong memory management and exception handling.
- Multithreaded: Supports multitasking.
- Portable: Code runs on different machines.
- High Performance: Uses JIT compiler.
- Distributed: Networked applications supported.
- Dynamic: Loads classes at runtime.
3. Explain Different Access Specifiers in Java
- public: Accessible everywhere.
- private: Accessible only within the class.
- protected: Accessible in same package or subclasses.
- default: Accessible only within the same package (no modifier).
4. Why is main() declared as “public static void main(String[] args)”?
- public: JVM can access it from anywhere.
- static: Can run without creating object.
- void: No return value.
- main: Entry point of the program.
- String[] args: Accepts command-line arguments.
5. Explain all 3 usages of final. What is the use of this and super keyword?
final:
- final variable: Value can’t change.
- final method: Cannot be overridden.
- final class: Cannot be inherited.
this keyword:
- Refers to current object.
- Used to resolve variable name conflicts.
- Can call another constructor.
super keyword:
- Refers to parent class.
- Used to call parent constructor or method.
6. What is the difference between int and Integer?
int: Primitive type, faster, can't be null.
Integer: Wrapper class, can be used with collections, supports null.
7. Describe garbage collection in Java. How does it work?
- Java removes unused objects from memory automatically.
- JVM decides when to run GC.
- Can request GC using System.gc(), but JVM controls timing.
8. How can a derived class constructor pass argument to base class constructor?
Using super():
Example:
class Child extends Parent {
Child() {
super("value"); // passes to Parent constructor
}
}
9. What is the difference between an abstract class and an interface in Java?
Abstract Class:
- Can have abstract and concrete methods.
- Can have constructors.
- Single inheritance.
Interface:
- Only abstract methods (Java 7) or default/static (Java 8+).
- No constructors.
- Multiple inheritance supported.
10. Why we need both run and start method in multithreading
- run(): Contains code to be executed by thread.
- start(): Creates a new thread and calls run().
Calling run() directly won't start a new thread.
11. What are the uses of “super” and “this” keyword? Explain
this:
- Refers to current object.
- Calls other constructors.
super:
- Refers to parent class.
- Calls parent class constructor/methods.
12. Explain JVM, JRE and JDK with suitable diagrams and explain all
functionalities.
- JVM: Runs bytecode.
- JRE: JVM + libraries.
- JDK: JRE + development tools like compiler.
Diagram:
JDK
├── JRE
│ ├── JVM
│ └── Libraries
├── Compiler
├── Debugger
13. Explain Exception Handling with an example program. Discuss throw and
throws keywords.
try {
int a = 10/0;
} catch(Exception e) {
System.out.println("Error");
}
throw: Used to manually throw an exception.
throws: Declares exception in method signature.
14. What is the role of stack in exception handling?
- Stack keeps method call order.
- If exception occurs, JVM looks through stack for catch block.
- Helps in tracking method execution and handling errors.
15. Array in Java along with its working
Array is a group of same-type elements.
Example:
int[] arr = new int[5];
arr[0] = 10;
- Fixed size, indexed from 0.
- Stored in continuous memory.
- Fast access.