Docsity

Java Programming — Complete Exam Handbook (PYQ Analysis, Solved Papers, Notes & One-Day, Summaries of Java Programming

Partial preview of the text

Download Java Programming — Complete Exam Handbook (PYQ Analysis, Solved Papers, Notes & One-Day and more Summaries Java Programming in PDF only on Docsity!

n Java & Object-Oriented Programming

Complete Study Guide

Units 1-4 • Notes, Programs, and Exam Q&A

Table of Contents

n Unit 1: Introduction to OOP and Java 6 hrs | HIGH

nn Unit 2: OOP in Java 7 hrs | VERY HIGH

n Unit 3: Inheritance 7 hrs | VERY HIGH

n Unit 4: Interfaces and Packages 7 hrs | HIGH

Functions are primary Objects are primary

Data is global Data is encapsulated

Less secure More secure

Examples: C, Pascal Examples: Java, C++

Primitive Data Types

Type Size Range

byte 1 byte -128 to 127

short 2 bytes -32768 to 32767

int 4 bytes -2^31 to 2^31-

long 8 bytes -2^63 to 2^63-

float 4 bytes 3.4e-38 to 3.4e+

double 8 bytes 1.7e-308 to 1.7e+

char 2 bytes 0 to 65535

boolean 1 bit true/false

Example Programs

Hello World

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!");

// Variables and Data Types int age = 20; double gpa = 8.5; String name = "Student"; boolean passed = true;

System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("GPA: " + gpa); System.out.println("Passed: " + passed); } }

Output:

Hello, World! Name: Student Age: 20 GPA: 8. Passed: true

2-Mark Questions

Q1. What is JVM?

JVM (Java Virtual Machine) is an abstract machine that provides a runtime environment to execute Java bytecode. It

converts bytecode into machine-specific code. JVM is platform-dependent but Java bytecode is

platform-independent.

Q2. What is the difference between JDK, JRE, and JVM?

JVM executes bytecode. JRE = JVM + class libraries. JDK = JRE + development tools (compiler, debugger).

Q3. List any 4 features of Java.

1) Platform Independent 2) Object-Oriented 3) Secure 4) Robust 5) Multithreaded 6) Simple

Q4. What is a bytecode?

Bytecode is an intermediate representation of Java code generated by the Java compiler (javac). It is

platform-independent and executed by the JVM.

6-Mark Questions

Q1. Explain features of Java with examples.

Java has 8+ key features:1. Simple: No pointers, automatic garbage collection2. Object-Oriented:

Everything is an object; supports encapsulation, inheritance, polymorphism3. Platform Independent: Compiled

to bytecode, runs on any JVM4. Secure: No explicit pointers; class loader checks5. Robust: Strong type

checking; exception handling6. Multithreaded: Built-in Thread class and Runnable interface7. Portable:

Same bytecode runs everywhere8. High Performance: JIT (Just-In-Time) compiler optimizes at runtime

Q2. Explain JVM architecture in detail.

JVM has 3 main components:1. Class Loader: Loads .class files2. Memory Area: Method Area, Heap,

Stack, PC Register, Native Method Stack3. Execution Engine: Interpreter + JIT Compiler + Garbage

CollectorExecution flow: .java -> javac -> .class (bytecode) -> JVM -> Machine Code -> Output

Parameterized Constructor Takes parameters to initialize object

Copy Constructor Takes object of same class as parameter

Method Overloading (IMPORTANT)

Same method name, different parameters (type/number/order) in same class.

  • Same method name
  • Different parameter list
  • Return type can be same or different
  • Compile-time polymorphism (Static binding)

Access Modifiers

Modifier Class Package Subclass World

private Yes No No No

default Yes Yes No No

protected Yes Yes Yes No

public Yes Yes Yes Yes

Static Members

  • static variable : Shared among all objects (class-level variable)
  • static method : Can be called without creating object
  • static block : Executed when class is loaded

this Keyword

  • Refers to current class instance
  • Used to differentiate instance vs local variable, call constructor (this()), pass current object

final Keyword

Usage Meaning

final variable Constant (cannot be changed)

final method Cannot be overridden

final class Cannot be inherited

Example Programs

Class, Object & Constructor

class Student { String name; int rollNo; double marks;

// Default Constructor Student() { name = "Unknown"; rollNo = 0; marks = 0.0; }

// Parameterized Constructor Student(String n, int r, double m) { this.name = n; // 'this' refers to current object this.rollNo = r;

this.marks = m; }

void display() { System.out.println("Name: " + name); System.out.println("Roll No: " + rollNo); System.out.println("Marks: " + marks); } }

public class Main { public static void main(String[] args) { Student s1 = new Student(); // Default constructor Student s2 = new Student("Raj", 101, 85.5); // Parameterized

System.out.println("--- Student 1 ---"); s1.display(); System.out.println("--- Student 2 ---"); s2.display(); } }

Output:

--- Student 1 --- Name: Unknown Roll No: 0 Marks: 0. --- Student 2 --- Name: Raj Roll No: 101 Marks: 85.

Method Overloading

class Calculator { // Method Overloading - same name, different parameters int add(int a, int b) { return a + b; }

double add(double a, double b) { return a + b; }

int add(int a, int b, int c) { return a + b + c; } }

public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 3)); // 8 System.out.println(calc.add(5.5, 3.2)); // 8. System.out.println(calc.add(1, 2, 3)); // 6 } }

Output:

2-Mark Questions

Q1. What is a class?

A class is a user-defined blueprint/template from which objects are created. It defines attributes (data members) and

behaviors (methods) that objects of the class will have.

n Unit 3: Inheritance

Duration: 7 hours | Exam Importance: VERY HIGH

Topics Covered

  • Inheritance in Java
  • Super and Sub Class
  • Defining a Subclass
  • Types of Inheritance
  • Method Overriding
  • using super keyword
  • Finalizers
  • Abstract Class and Methods
  • Visibility Controls
  • using final with Inheritance

Notes

Inheritance (MOST IMPORTANT)

Inheritance is the mechanism where one class (child/subclass) acquires properties and behaviors of another class

(parent/superclass).

Keyword: extends

class ParentClass { // members } class ChildClass extends ParentClass { // inherits + new members }

Advantages: Code Reusability, Method Overriding (Runtime Polymorphism), Extensibility

Types of Inheritance in Java

Single: A -> B Multilevel: A -> B -> C Hierarchical: A -> B, A -> C Multiple: NOT directly supported (use Interfaces) Hybrid: Combination (via Interfaces)

Method Overriding (IMPORTANT)

Redefining a parent class method in child class with same name, same parameters.

Overloading Overriding

Same class Parent-Child class

Different parameters Same parameters

Compile-time Runtime

Static binding Dynamic binding

super Keyword

1. Access parent class variable: super.variable

2. Call parent class method: super.method()

3. Call parent constructor: super() -- must be first statement

Abstract Class

  • Declared with abstract keyword
  • Cannot be instantiated
  • Can have abstract methods (no body) and concrete methods
  • Subclass must implement all abstract methods

abstract class Shape { abstract double area(); // abstract method void display() { // concrete method System.out.println("I am a shape"); } }

final with Inheritance

  • final class : Cannot be extended (e.g., String class)
  • final method : Cannot be overridden
  • final variable : Constant

Example Programs

Inheritance & super keyword

class Animal { String name;

Animal(String name) { this.name = name; }

void eat() { System.out.println(name + " is eating"); }

void sound() { System.out.println("Some sound"); } }

class Dog extends Animal { String breed;

Dog(String name, String breed) { super(name); // calls parent constructor this.breed = breed; }

@Override void sound() { // Method Overriding System.out.println(name + " says: Woof!"); }

void info() { super.eat(); // calls parent method System.out.println("Breed: " + breed); } }

public class Main { public static void main(String[] args) { Dog d = new Dog("Bruno", "Labrador"); d.sound(); d.info(); } }

Output:

Q1. What is inheritance?

Inheritance is a mechanism where a child class (subclass) acquires properties and methods of a parent class

(superclass) using the 'extends' keyword. It promotes code reusability.

Q2. What is method overriding?

Method overriding is redefining a parent class method in the child class with the same name, same parameters, and

same return type. It achieves runtime polymorphism.

Q3. What is the super keyword?

super is a reference variable used in a subclass to refer to the immediate parent class. It can access parent variables

(super.var), methods (super.method()), and constructor (super()).

Q4. What is an abstract class?

An abstract class is a class declared with the 'abstract' keyword that cannot be instantiated. It may contain abstract

methods (without body) that must be implemented by subclasses.

Q5. What types of inheritance does Java support?

Java supports Single, Multilevel, and Hierarchical inheritance. Multiple inheritance is NOT supported through classes

(only through interfaces) to avoid the Diamond Problem.

6-Mark Questions

Q1. Explain types of inheritance with diagrams.

Types:1. Single: One parent, one child (A->B)2. Multilevel: Chain (A->B->C)3. Hierarchical: One

parent, multiple children (A->B, A->C)4. Multiple: Multiple parents (NOT in Java via classes, only via

interfaces)5. Hybrid: CombinationJava doesn't support multiple inheritance through classes to avoid

the Diamond Problem (ambiguity).

n Unit 4: Interfaces and Packages

Duration: 7 hours | Exam Importance: HIGH

Topics Covered

  • Interface in Java
  • Defining Interfaces
  • Extending Interfaces
  • Implementing Interfaces
  • Polymorphism via Interfaces
  • Defining Packages
  • Class path Variable
  • Creation of Package
  • Importing Packages

Notes

Interface (IMPORTANT)

An interface is a 100% abstract class (before Java 8). It contains only abstract methods and final variables.

Keyword: interface, implements

interface InterfaceName { // abstract methods (public abstract by default) void methodName();

// constants (public static final by default) int VALUE = 10; }

Interface vs Abstract Class

Feature Interface Abstract Class

Methods Abstract only (before Java 8) Abstract + Concrete

Variables public static final Any type

Constructor Not allowed Allowed

Multiple Yes (implements A, B) No

Keyword interface + implements abstract + extends

Speed Slow Fast

Multiple Interface Implementation

class ClassName implements Interface1, Interface2 { // implement all methods }

Extending Interface

interface Child extends Parent1, Parent2 { // additional methods }

Packages