Class X Computer Applications Exam
Class X Computer Applications Exam
Boxing in Java is the automatic conversion of primitive types to their corresponding object wrapper classes (e.g., converting an 'int' to an 'Integer'). Unboxing is the reverse process. Explicit type conversion is manually converting one data type to another, while implicit type conversion is done automatically by the Java compiler. Unboxing can sometimes be considered a form of implicit conversion since it happens automatically when an object is needed as a primitive .
A binary search in Java requires a sorted array. It works by repeatedly dividing the search interval in half, comparing the target value to the middle element. If equal, the search concludes; if smaller, it continues in the left subarray; if larger, in the right subarray. Binary search is more efficient than linear search with a time complexity of O(log n), versus O(n) for linear search, making it much faster for larger datasets .
Java provides several methods for string transformations: 'substring()' extracts parts of a string; 'toLowerCase()' and 'toUpperCase()' change a string's case; 'replace()' swaps specified characters or sub-strings. These operations are crucial for data processing, allowing programs to manipulate and format textual data according to specific requirements or user input, enhancing flexibility and functionality in Java applications .
Local variables in Java are declared within methods and are accessible only within the method's scope. They are created when a method is called and destroyed when it exits. This limited scope reduces memory usage and minimizes side effects, as local variables do not affect the state outside their method. Global (or class-level) variables, however, persist throughout the program's execution and can be accessed by all class methods, but they increase the risk of unexpected changes in program state .
Iterative construction of patterns in Java through function overloading involves defining multiple methods with the same name but different parameters to print various patterns. For instance, methods named 'pattern()' could be overloaded to print numeric and alphabetic sequences differently. This allows flexible pattern generation, where the specific method called depends on input data types, enabling significant code reuse and abstraction .
The absence of a break statement in a Java switch-case structure causes control to 'fall through' the subsequent cases, executing them until a break is encountered or the switch ends. This can lead to logical errors if unintended cases execute, making the program behave unexpectedly. Such fall-through behavior can be used intentionally to execute multiple cases with the same outcome, but it requires careful control to avoid logic flaws .
In Java, the Math.abs() function returns the absolute value of a number, removing its sign. When you have a negative floating-point number and cast it to an integer, the fractional part is discarded. Therefore, combining Math.abs() with explicit type casting in the code 'double x= -3.142; int y =(int)Math.abs(x)+(int)x;' results in: Math.abs(-3.142) becomes 3.142, casting it to an integer truncates to 3, and casting x directly to an integer truncates -3.142 to -3. Adding these gives the output of 0 .
Constructors in Java have two primary characteristics: they have no return type, and their names match the class name. Constructors are special methods used to initialize objects and allocate resources during object creation. Unlike regular methods, constructors cannot be overridden, but they can be overloaded to provide multiple ways to instantiate an object. They set initial states for objects, making them fundamental for object-oriented programming .
Access specifiers in Java determine the visibility of class members. 'Private' restricts visibility to the class itself, providing the highest level of security. 'Protected' allows visibility to subclasses and classes within the same package, offering moderate security. 'Default' or 'friendly' access, with no modifier, restricts access to the package level, providing the least security compared to 'private' and 'protected'. These levels affect how classes expose their data and interfaces to other parts of an application .
The 'Object' class in Java is significant because it is the root of the class hierarchy; every class has 'Object' as a superclass. This means that all objects in Java inherit methods from the 'Object' class, such as 'toString()', 'equals()', and 'hashCode()'. It provides a basic set of functionalities that are required by all objects and serves as a base for type safety across the Java framework .