0% found this document useful (0 votes)
27 views6 pages

OOP Java Complete Answers

The document covers fundamental concepts of Object-Oriented Programming in Java, including variable types, constructors, command-line arguments, and the 'this' keyword. It also discusses packages, encapsulation, method overloading and overriding, custom exceptions, string operations, exception handling, threading, synchronization, wrapper classes, collections, and generics. Each topic is illustrated with examples to demonstrate practical application.

Uploaded by

v62017469
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)
27 views6 pages

OOP Java Complete Answers

The document covers fundamental concepts of Object-Oriented Programming in Java, including variable types, constructors, command-line arguments, and the 'this' keyword. It also discusses packages, encapsulation, method overloading and overriding, custom exceptions, string operations, exception handling, threading, synchronization, wrapper classes, collections, and generics. Each topic is illustrated with examples to demonstrate practical application.

Uploaded by

v62017469
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/ 6

Object Oriented Programming using

Java - Complete Answers


Module 1 - Q1a

Variables in Java are used to store data. They are associated with a data type and must be
declared before use.

Types:
1. Local Variables – defined inside methods.
2. Instance Variables – non-static variables.
3. Static Variables – shared among all instances.

Arrays are data structures that store multiple values of the same type.

Example:
int[] numbers = {1, 2, 3};
String[] names = new String[3];

Module 1 - Q1b

Constructors are special methods invoked at object creation to initialize values.

Static members belong to the class, not objects.

Example:
class Student {
static int count = 0;
String name;

Student(String name) {
this.name = name;
count++;
}

static void showCount() {


System.out.println("Students: " + count);
}
}
Module 1 - Q2a

Command-line arguments allow user input to be passed when starting the Java program.

Example:
public class Demo {
public static void main(String[] args) {
System.out.println("Hello " + args[0]);
}
}
Run using: java Demo Shivam

Module 1 - Q2b

The "this" keyword refers to the current object.

class Person {
String name;
Person(String name) {
this.name = name;
}
}

Module 2 - Q3a

Packages in Java help organize classes and interfaces.

Advantages:
- Avoid name conflicts.
- Control access.
- Easier maintenance.

Syntax:
package mypackage;
public class MyClass {}

Module 2 - Q3b

Encapsulation uses private data and public methods.


Example:
class Account {
private double balance;
public void deposit(double amt) {
if (amt > 0) balance += amt;
}
public double getBalance() {
return balance;
}
}

Module 2 - Q4a

Method Overloading: Same method name, different signatures.


Method Overriding: Subclass provides new implementation.

Example:
class A {
void show() {}
}
class B extends A {
void show() {}
}

Module 2 - Q4b

Subclass can override a superclass method.

Example:
class Vehicle {
void move() { System.out.println("Vehicle moves"); }
}
class Bike extends Vehicle {
void move() { System.out.println("Bike moves"); }
}

Module 3 - Q5a

Custom exceptions are created by extending Exception.

Example:
class InvalidAgeException extends Exception {
InvalidAgeException(String msg) { super(msg); }
}

Module 3 - Q5b

String length operations:


- length(): O(1)
- concat(): O(n)
- equals(): O(n)

Example:
String s = "Hello";
s.length(); s.concat("World"); s.equals("Hi");

Module 3 - Q6a

String comparison:
- equals(): case-sensitive
- equalsIgnoreCase(): ignores case

Searching:
- indexOf(): returns position
- contains(): returns boolean

Example:
"Hello".contains("el");

Module 3 - Q6b

try-catch handles exceptions.


finally always runs.

Example:
try {
int x = 10/0;
} catch (ArithmeticException e) {
System.out.println("Error");
} finally {
System.out.println("Done");
}

Module 4 - Q7a

Thread creation:
1. Extending Thread
2. Implementing Runnable

Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}

Module 4 - Q7b

Synchronization avoids data inconsistency.

synchronized void method() {}

Use locks or synchronized blocks for thread safety.

Module 4 - Q8a

Thread priorities range from 1 to 10.

Set using setPriority(), get using getPriority().

Example:
Thread t = new Thread();
t.setPriority(8);

Module 4 - Q8b

Synchronization blocks provide fine-grained control.

Example:
synchronized(this) {
// critical section
}

Module 5 - Q9a

Wrapper classes provide object representations of primitives.

Examples: Integer, Double, Character.

int a = 10;
Integer obj = Integer.valueOf(a);

Module 5 - Q9b

Collections like ArrayList, HashSet, HashMap help store data.

Example:
List<Integer> list = new ArrayList<>();
Set<String> set = new HashSet<>();

Module 5 - Q10a

Overriding equals and hashCode ensures object equality.

Example:
public boolean equals(Object o) {...}
public int hashCode() {...}

Module 5 - Q10b

Generics allow reusable code.

Example:
class Box<T> {
T value;
void set(T v) { value = v; }
T get() { return value; }
}

You might also like