Java GTU 3153203 Exam Questions Guide
Java GTU 3153203 Exam Questions Guide
Java is considered platform-independent because of its bytecode and the Java Virtual Machine (JVM). When Java source code is compiled, it is converted into bytecode that can be run on any system with a JVM, regardless of the underlying architecture .
An abstract class in Java can have both abstract and concrete methods and is used when classes share a common design but differ in details. An interface, on the other hand, can only have abstract methods (though default methods were introduced in Java 8) and is used to define a contract that must be fulfilled by implementing classes. Abstract classes can have state (fields), while interfaces cannot. Interfaces support multiple inheritance, while abstract classes do not .
To implement a custom exception in Java, you create a class that extends the Exception class for checked exceptions or RuntimeException for unchecked exceptions. This custom exception can provide specific detail messages and methods for handling exceptional situations particular to the application's domain, thus improving error handling precision and offering a mechanism for managing user-defined conditions effectively .
Java achieves inter-thread communication through methods like `wait()`, `notify()`, and `notifyAll()`, which are part of any object's monitor. This mechanism allows one thread to suspend execution until another thread gives it a signal to proceed. It is crucial for implementing efficiently synchronized work-flows among threads, where threads can share resources or notify each other of changes in state, minimizing resource contention and leading to better performance and reliability in concurrent applications .
The 'static' keyword in Java signifies that a particular member belongs to the class rather than instances of the class. This means that static members are shared across all instances and can be accessed without creating an object of the class. For example, static variables can keep track of the number of objects created if incremented in a constructor, and static methods, such as `Math.pow()`, can be called without referring to an instance .
Java does not support multiple inheritance with classes to avoid the problem of the 'diamond of death,' where a subclass inherits from multiple superclasses with potential method conflicts. However, Java allows multiple inheritance through interfaces, as they provide a way to implement multiple contracts without the issues associated with multiple superclass inheritance. This allows a class to interact with numerous interfaces, offering flexibility and a form of multiple inheritance without ambiguity .
Synchronization in Java is a mechanism that ensures that only one thread can access a resource at a time. It is crucial in multithreaded programming to prevent thread interference and memory consistency errors. By using synchronized methods or blocks, Java provides a way to control the access of multiple threads to any shared resource, ensuring that critical sections of code are executed by only one thread at a time .
In Java, checked exceptions are exceptions that must be either caught or declared in the method where they can occur, such as IOException. These happen at compile-time. Unchecked exceptions, like NullPointerException, are not checked at compile-time but occur at runtime, often because of programming errors like logical mistakes. Checked exceptions are meant for recoverable conditions, while unchecked ones generally represent errors in the program's logic .
In Java, the 'super' keyword is used in multilevel inheritance to call the constructor of the superclass. This is essential because, in a class hierarchy, constructors are called from the base class upwards to the derived class. When a subclass constructor is called, it implicitly invokes the constructor of its superclass with 'super', although a specific use of 'super' can explicitly control which superclass constructor is called .
Method overloading in Java refers to defining multiple methods with the same name but different parameters (type, number, or order). It allows methods to perform similar behavior using different input parameters, enhancing polymorphism and readability. For example, an overloaded method `add(int a, int b)` and `add(double a, double b)` can handle integer and double addition without changing the method name, thus providing flexibility .