Java Exam Flashcards and Notes
Java Subject Short Notes & Flashcards
Q.1 (a) POP vs OOP
POP (Procedure-Oriented Programming)
- Focus on functions
- Data is global
- Less reusable
OOP (Object-Oriented Programming)
- Focus on objects
- Data is encapsulated
- More reusable
Flashcard:
POP: Focus on functions, global data
OOP: Focus on objects, encapsulated data
Q.1 (b) Super Keyword
- Refers to parent class
- Used in inheritance
Example:
class Animal {
Java Exam Flashcards and Notes
void sound() { System.out.println("Animal sound"); }
class Dog extends Animal {
void sound() {
super.sound();
System.out.println("Dog barks");
Flashcard:
Super: Refers to parent class method/constructor in inheritance
Q.1 (c) Method Overriding
- Subclass redefines parent class method
- Same method name and parameters
Rules:
1. Same method name
2. Same parameters
3. Inherited method
Example:
class Animal {
void run() { System.out.println("Animal runs"); }
}
Java Exam Flashcards and Notes
class Dog extends Animal {
@Override
void run() { System.out.println("Dog runs fast"); }
Flashcard:
Method Overriding: Redefine parent method in subclass
Interface & Multiple Inheritance
- Interface: Collection of abstract methods
- Multiple inheritance using interfaces
Example:
interface A { void show(); }
interface B { void display(); }
class Test implements A, B {
public void show() { System.out.println("Show"); }
public void display() { System.out.println("Display"); }
Flashcard:
Interface: Abstract methods, multiple inheritance
Q.2 (a) Java Program Structure
1. Package Declaration
Java Exam Flashcards and Notes
2. Import Statements
3. Class Definition
4. Main Method
Example:
class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
Flashcard:
Program Structure: Package, Import, Class, Main Method
Q.2 (b) Static Keyword
- Belongs to class, not object
- Used for class-level methods/variables
Example:
class Test {
static int count = 0;
static void show() { System.out.println("Static method"); }
Flashcard:
Java Exam Flashcards and Notes
Static: Belongs to class, not object
Q.2 (c) Constructor
- Default Constructor: No arguments
- Parameterized Constructor: With arguments
- Copy Constructor: Copies object
Example:
class Student {
int id;
Student(int i) { id = i; }
Student(Student s) { id = s.id; }
Flashcard:
Constructor Types: Default, Parameterized, Copy
this Keyword
- Refers to current class object
Example:
class Demo {
int x;
Demo(int x) {
this.x = x;
Java Exam Flashcards and Notes
Flashcard:
this: Refers to current object
Inheritance
- Inheritance: Child class inherits from parent class
- Types: Single, Multilevel, Hierarchical
Example:
class A { void show() {} }
class B extends A { }
class C extends B { }
Flashcard:
Inheritance: Parent-child relationship, types include Single, Multilevel, Hierarchical
Q.3 (a) Type Conversion & Casting
- Implicit Conversion: Smaller type to larger type
- Explicit Casting: Larger type to smaller type
Example:
int a = (int) 10.5;
Java Exam Flashcards and Notes
Flashcard:
Type Conversion: Implicit (smaller to larger), Explicit (larger to smaller)
Q.3 (b) Visibility Controls
- private: Access within class
- default: Access within package
- protected: Access within package + subclass
- public: Access everywhere
Flashcard:
Visibility: private, default, protected, public
Q.3 (c) Thread
- Thread: A lightweight process
- Methods: start(), run(), sleep(), join()
- Life Cycle: New Runnable Running Waiting Terminated
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Running thread");
Flashcard:
Java Exam Flashcards and Notes
Thread Life Cycle: New, Runnable, Running, Waiting, Terminated
Q.4 (a) String vs StringBuffer
- String: Immutable
- StringBuffer: Mutable
Flashcard:
String: Immutable, StringBuffer: Mutable
Q.4 (b) Sum and Avg of Array
Example:
int[] a = {10, 20, 30, 40, 50};
int sum = 0;
for(int i : a) sum += i;
System.out.println("Sum: " + sum);
System.out.println("Avg: " + (sum / a.length));
Flashcard:
Array Sum & Avg: Loop through array, calculate sum and average
Q.4 (c) Abstract & Final Class
- Abstract Class: Cannot be instantiated, may have abstract methods
- Final Class: Cannot be subclassed
Java Exam Flashcards and Notes
Example:
abstract class Animal { abstract void sound(); }
final class Bike { }
Flashcard:
Abstract: Cannot instantiate, may have abstract methods
Final: Cannot subclass
Q.5 (a) File Operations (Write)
Example:
FileWriter fw = new FileWriter("test.txt");
fw.write("Hello World");
fw.close();
Flashcard:
File Write: Use FileWriter for writing to file
Q.5 (b) throw & finally
- throw: Used to explicitly throw an exception
- finally: Code block that executes whether or not an exception occurs
Example:
try {
throw new ArithmeticException("Error");
Java Exam Flashcards and Notes
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("Finally block");
Flashcard:
throw: Explicit exception
finally: Always executes
Q.5 (c) Polymorphism
- Polymorphism: Same method, different behavior
- Runtime Polymorphism: Method overridden in subclass
Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
Animal a = new Dog();
a.sound();
Flashcard:
Java Exam Flashcards and Notes
Polymorphism: Same method, different behavior