Object Oriented Programming using
Java - Detailed Answers
Module 1 - Q1a
Variables in Java are containers that store data values. Each variable in Java has a specific
data type...
Module 1 - Q1b
Constructors are special methods called when an object is created. Static members belong
to the class rather than instances...
Example:
class Student { static int count = 0; ... }
Module 1 - Q2a
Command-line arguments are passed during the program execution from the command
line...
Module 1 - Q2b
The 'this' keyword refers to the current object instance of a class. It is commonly used to
eliminate confusion between class attributes and parameters...
Module 2 - Q3a
Packages in Java are used to group related classes and interfaces, improving modularity and
preventing name conflicts...
Module 2 - Q3b
Encapsulation is demonstrated using private variables and public getter/setter methods...
Example:
class Account { private double balance; ... }
Module 2 - Q4a
Method Overloading occurs within the same class with different parameter lists. Method
Overriding occurs in subclasses with the same method signature...
Module 2 - Q4b
A subclass can override a superclass method using the same signature...
Example:
class Vehicle { void move() { ... } } class Bike extends Vehicle { ... }
Module 3 - Q5a
User-defined exceptions are custom classes derived from Exception. They help define
specific error scenarios...
Module 3 - Q5b
String operations such as length(), concat(), and equals() have different time complexities
depending on the size of the string...
Module 3 - Q6a
String comparison can be done using equals(), equalsIgnoreCase(), etc. Searching includes
indexOf(), contains()...
Example: str1.contains('abc')
Module 3 - Q6b
Exception handling in Java uses try-catch-finally blocks to manage runtime errors...
Module 4 - Q7a
Java supports threads via Thread class or Runnable interface...
Example:
class MyThread extends Thread { public void run() { ... } }
Module 4 - Q7b
Synchronization prevents concurrent access issues. Techniques like synchronized methods
and blocks ensure safety...
Module 4 - Q8a
Thread priorities help JVM schedule threads. They range from Thread.MIN_PRIORITY (1) to
Thread.MAX_PRIORITY (10)...
Module 4 - Q8b
Synchronization blocks restrict access to critical code sections...
Example:
synchronized(this) { ... }
Module 5 - Q9a
Wrapper classes convert primitives to objects. Examples include Integer, Double, etc...
Example: Integer a = Integer.valueOf(5);
Module 5 - Q9b
Collection classes include ArrayList, HashSet, HashMap which store groups of objects...
Module 5 - Q10a
The equals() and hashCode() methods can be overridden to ensure proper comparison and
storage in hash-based collections...
Module 5 - Q10b
Generics allow classes and methods to operate on objects of various types while providing
compile-time type safety...
Example:
class Box<T> { ... }