Java Programming Exam Questions Summary
Java Programming Exam Questions Summary
Java enables multitasking through the use of threads. It supports creating threads either by extending the Thread class or by implementing the Runnable interface. Extending the Thread class involves overriding its run() method, while implementing Runnable requires passing a Runnable instance to a Thread object, which then invokes the run() method. These approaches allow concurrent execution of different parts of a program, enhancing efficiency .
In Java applets, the <param> tag is used to pass parameters from a web page to the applet. It is placed within the <applet> or <object> tags in an HTML file, specifying name-value pairs of parameters that the applet can read using methods such as getParameter(). This tag allows external data to be input into the applet, providing flexibility and dynamic interaction with the applet's environment .
Java's platform independence is primarily achieved through the Java Virtual Machine (JVM). The JVM allows Java programs to run on any device or operating system where the JVM is installed, making Java programs platform independent. This is because Java programs are compiled into bytecode, which the JVM interprets at runtime, rather than being compiled into machine-specific code .
Java supports single, multilevel, and hierarchical inheritance but does not support multiple inheritance with classes to prevent complexity and ambiguity. Multiple inheritance is realized through interfaces in Java. By implementing multiple interfaces, Java classes can achieve a form of multiple inheritance while maintaining clear method resolutions without the diamond problem found in other object-oriented languages .
Constructors in Java are special methods used to initialize objects. When a class is defined without a constructor, Java automatically provides a default constructor. This default constructor initializes object members with default values (e.g., null for objects, zero for numeric types) to ensure that the object is in a valid state upon creation .
A thread in Java goes through several states: New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated. In the New state, a thread has been created but not yet started. Once started, it moves to the Runnable state, where it is ready for execution. It may enter the Blocked or Waiting states while waiting for resources or conditions. Timed Waiting is a similar state with a time limit. Finally, the thread reaches the Terminated state after completing execution. These states allow Java to effectively manage multithreading by controlling the timing and order of thread execution .
Implicit type conversion, also known as widening or automatic conversion, occurs when a smaller data type is converted to a larger data type automatically, such as converting an int to a double. Explicit type conversion, or narrowing, requires the programmer to manually cast one data type to another, such as converting a double to an int using a cast operator (e.g., (int)3.14). This can lead to data loss if the larger data type contains more precision or scale than the target type .
The 'this' keyword in Java is used to refer to the current object within an instance method or constructor. It is particularly useful in constructors to differentiate between class fields and parameters that have the same names. For example, this.name refers to the class field, while name refers to the method parameter. 'This' can also be used to invoke other constructors within the class .
The JVM provides a runtime environment for executing Java bytecode. Its significance lies in its ability to allow Java applications to run on any device that has the JVM installed, thus facilitating Java's cross-platform capabilities. It acts as an intermediary layer that abstracts the underlying hardware and operating system, making Java programs universally executable without modification .
The String class is immutable, meaning its objects cannot be modified after creation, leading to the creation of a new object for any modifications. The StringBuffer class, however, is mutable and allows modifications to the object without creating a new instance. Some methods of the String class include length(), charAt(), substring(), and indexOf().