Computer Application Exam Class X
Computer Application Exam Class X
Type casting in Java allows variables of one data type to be converted to another, providing flexibility in assignments between compatible types. In 'char c = (char)120;', an explicit cast is used to convert the integer 120 to its equivalent ASCII character, demonstrating narrowing conversion. Conversely, in 'int x = 'a';', an implicit cast occurs where the character 'a' is automatically converted to its ASCII integer value, showcasing widening conversion without the need for explicit casting .
In Java, attempting to divide an integer by zero results in a runtime error. This is because runtime errors occur during the execution of a program, as opposed to syntax errors that are identified during compilation. Therefore, the statement 'int r=100/0;' triggers a runtime error, as division by zero is not allowed .
Access modifiers in Java dictate the visibility of classes, methods, and other members. 'Public' is the most permissive access level, granting visibility to every other class regardless of the package. This means any public class member can be accessed from any other class without restrictions. However, using public access comes with the risk of reducing encapsulation by exposing internal states or behaviors to broader access than necessary, which might compromise modular design principles .
Wrapper classes in Java are examples of object-oriented programming concepts, specifically abstraction and encapsulation. They allow primitive data types (such as int, char, double) to be converted into objects, making them act as objects. This is achieved through 'boxing', where a primitive data type is converted to its corresponding wrapper class object like Integer, Character, or Double . Wrapper classes are part of the java.lang package .
Method overloading in Java allows multiple methods with the same name to coexist in a class, differentiated by their parameter lists. The 'Print()' method exemplifies this by having different implementations for distinct tasks. One version of 'Print(int x, int n)' computes the sum of a series based on input parameters, while another 'Print()' outputs a series of alphabets. Additionally, 'boolean Print(int a)' checks for Dudeney numbers, a specific numerical property. This overloading enables the 'Print()' method to perform tasks of varied complexity under a unified interface .
In a Java menu-driven program, a switch-case statement is used to execute different blocks of code based on user input. It evaluates the expression provided in the switch argument and matches it against various case labels. For example, for generating a number series (1, -3, 5, -7, ...) and displaying patterns, each option presented to the user would correspond to a case. Based on the user's choice, the corresponding block of code executes to produce the desired output, while 'break' statements ensure that subsequent cases do not execute inadvertently .
The 'BookFair' class in a Java program can encapsulate data about books and manage their discounts using methods. It can have instance variables for storing the book's name and price. The input() method could prompt for the book's name and price and store these values in the variables. The calculate() method could apply discount rules based on price, decreasing the price by a percentage (2%, 10%, or 15%) depending on the price range. The display() method would then print the adjusted price and book name, thus ensuring that all operations related to a book's pricing are handled within a single cohesive class structure .
Java evaluates arithmetic operations based on operator precedence, meaning that operators with higher precedence are evaluated before those with lower precedence. For instance, increment (++), and modulo (%) have higher precedence than logical operators like && and comparison operators like >=. In a given expression, operations are performed in order from higher to lower precedence, and if operators have the same precedence, they are evaluated from left to right .
Linear search involves traversing an array from the beginning to the end, comparing each element with the target value to find a match. The key steps include initializing an index, looping through the array, and checking each element against the desired name. If a match is found, the index is returned or relevant information displayed; if no match is found after traversing the entire array, an appropriate message is returned. The main limitation of linear search is its inefficiency on large data sets as its time complexity is O(n), making it slower compared to more sophisticated search algorithms like binary search .
Bubble sort is a simple sorting algorithm that sorts an array by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items, and swapping them if they are in the wrong order. This process is repeated until the list is sorted. The computational complexity of bubble sort is O(n^2), where n is the number of items being sorted, because each pass through the list potentially involves swapping n-1 pairs and n such passes might be required in the worst case .