ICSE Class 10 Java Program Questions
ICSE Class 10 Java Program Questions
Pascal's Triangle is a triangular array where each number is the sum of the two numbers directly above it. To print Pascal's Triangle up to n rows, start with a single '1' at the top. For each subsequent row, begin and end with '1', then calculate the intermediate values by summing the values from the previous row. This continues until n rows are printed.
In Java, all arguments are passed by value, meaning a copy of the argument is passed to the method. However, for objects, this effectively looks like call-by-reference since the reference to the object is passed by value, allowing modification of the object's data. For instance, modifying an ArrayList inside a method affects the original list outside the method because the reference points to the same memory location. For primitives, changes inside the method do not affect the original variable.
The bubble sort algorithm works by repeatedly stepping through the integer array, comparing adjacent pairs and swapping them if they are in the wrong order. This process is repeated until the array is sorted. During each pass, the largest unsorted element 'bubbles' up to its correct position. The algorithm stops when no more swaps are needed, indicating that the array is sorted.
To merge two arrays into a third array without duplicates in Java, follow these steps: 1. Create a new data structure such as a HashSet to hold the elements as it automatically manages duplicates. 2. Add elements from both arrays to this data structure. 3. Convert the HashSet to an array. This ensures that all elements in the final array are unique.
To check if a string is a palindrome, methods include comparing the string to its reverse or using two pointers from each end moving towards the center. The most efficient method depends on the context: using two pointers is generally more efficient as it avoids creating a new string or character array, saving both time and space.
An EvenPal is a number that is both a palindrome and has an even sum of its digits. To determine if a number is an EvenPal, first check if the number is a palindrome by reversing its digits and comparing it with the original number. Then, calculate the sum of all digits and check if it is even. If both conditions are met, the number is an EvenPal.
To reverse a string in Java, you can implement a method that follows these steps: 1. Convert the string into a character array. 2. Initialize two pointers, one at the start and another at the end of the array. 3. Swap the characters at these pointers and move the pointers towards each other. 4. Continue swapping until the pointers meet. 5. Convert the character array back to a string.
Wrapper classes in Java provide a way to use primitive data types as objects. They are significant for object-oriented programming as they allow primitive types to be used in collections like ArrayList. An example of using a wrapper method is Integer.parseInt(), which converts a numeric string to an integer. For instance, Integer.parseInt("123") returns the integer 123.
To check if a number is an Armstrong number, follow these steps: 1. Calculate the number of digits, n, in the number. 2. For each digit, calculate its nth power and sum these values. 3. If the sum is equal to the original number, then it is an Armstrong number.
To perform a binary search on a sorted integer array, follow these steps: 1. Start with two pointers, low at the beginning and high at the end of the array. 2. Calculate the mid index as (low + high) / 2. 3. If the element at mid is the target, return the index. 4. If the target element is less than the element at mid, set high to mid - 1. 5. If the target element is greater than the element at mid, set low to mid + 1. 6. Repeat steps 2-5 until the element is found or the search range is empty.