OOP in Java: Assignment Questions for B.Tech
OOP in Java: Assignment Questions for B.Tech
Method overriding in Java allows a subclass to provide a specific implementation of a method already provided by its parent class. The method in the subclass must have the same name, return type, and parameter list as in the parent class . Example: ``` class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Bark"); } } ```
Thread synchronization in Java ensures that only one thread can access a resource at a time to prevent data inconsistency and conflicts. It is achieved using synchronized methods or blocks, allowing safe resource sharing between threads by enforcing a mutual exclusion rule .
Primitive data types in Java include byte (8 bits), short (16 bits), int (32 bits), long (64 bits), float (32 bits), double (64 bits), char (16 bits), boolean (1 bit). They are predefined by the language and named by a reserved keyword. Unlike reference data types, which store references to actual data, primitive data types store actual values .
Exception handling in Java ensures the smooth execution of a program by managing runtime errors. Checked exceptions (e.g., IOExceptions) must be declared in a method or constructor's throws clause if they can be thrown by the execution, while unchecked exceptions (runtime exceptions, e.g., NullPointerException) are not checked at compile time. Developers should handle checked exceptions to prevent system crashes and enhance fault tolerance .
Static methods belong to the class, not instances, and can be called without an object. Non-static methods belong to an instance of the class and require an object for invocation. Example: ```java class Example { static void staticMethod() { System.out.println("Static Method"); } void nonStaticMethod() { System.out.println("Non-static Method"); } } Example.staticMethod(); // No object needed Example obj = new Example(); obj.nonStaticMethod(); // Object needed ```
The 'break' statement terminates the loop or switch statement, while 'continue' skips the current iteration of the loop. Example: ```java for (int i = 0; i < 10; i++) { if (i == 5) break; // exits loop when i is 5 if (i % 2 == 0) continue; // skips even numbers System.out.println(i); // prints odd numbers till 5 } ```
'finally' is a block associated with a try-catch structure that executes mandatory code after try/catch blocks, regardless of whether an exception is thrown. 'finalize' is a method that the garbage collector calls before object deletion to perform cleanup operations. Example of 'finally': ```java try { /* risky code */ } finally { /* cleanup code */ } ``` Example of 'finalize': ```java @Override protected void finalize() throws Throwable { /* cleanup code */ } ```
A Java interface is an abstract type used to specify a behavior that classes must implement. It supports multiple inheritance by allowing a class to implement multiple interfaces. This way, an object can use functionalities defined in multiple interfaces, overcoming the multiple inheritance limitation in class hierarchies .
The Producer-Consumer problem can be addressed using multithreading by managing shared resources with synchronization. A common approach uses wait() and notify() methods. Example: ```java class SharedResource { private int content; private boolean empty = true; public synchronized void produce(int value) throws InterruptedException { while (!empty) wait(); content = value; empty = false; notify(); } public synchronized int consume() throws InterruptedException { while (empty) wait(); empty = true; notify(); return content; } } ```
The CLASSPATH environment variable in Java specifies the path where the Java Runtime Environment (JRE) looks for classes and packages during execution. If CLASSPATH is set incorrectly, the Java programs may not find required classes or packages, leading to runtime errors .