0% found this document useful (0 votes)
2 views5 pages

ICSE Computer Methods Arrays Questions

The document presents various programming problems and their solutions related to user-defined methods and arrays in CSE Computer Applications. It includes tasks such as character comparison, checking sums, sorting algorithms, prime number checking, and finding maximum subarrays. Each problem is accompanied by logic explanations and code implementations.

Uploaded by

mrakeshreddy31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

ICSE Computer Methods Arrays Questions

The document presents various programming problems and their solutions related to user-defined methods and arrays in CSE Computer Applications. It includes tasks such as character comparison, checking sums, sorting algorithms, prime number checking, and finding maximum subarrays. Each problem is accompanied by logic explanations and code implementations.

Uploaded by

mrakeshreddy31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSE Computer Applications: User-Defined Methods & Arrays (PYQ + Toughest Questions)

1. Compare Two Characters

Q: Write a method compareChar(char c1, char c2) that returns 0 if equal, -1 if c1<c2, 1 otherwise.

Logic: Simple char comparison using if-else chain.

Code:

int compareChar(char c1, char c2) {

if(c1==c2) return 0;

else if(c1<c2) return -1;

else return 1;

2. Check if a + b + c = d

Q: Method satis(a,b,c,d) returns 0 if a+b+c==d, else -1.

Logic: Use cube and compare using ternary.

Code:

int satis(int a, int b, int c, int d) {

return (a*a*a + b*b*b + c*c*c == d*d*d) ? 0 : -1;

3. Overloaded max() Methods

Q: Write overloaded max methodsmax(double,int) and max(int,double) returning larger value.

Logic: Demonstrates compile-time polymorphism.

Code:

static double max(double x, int y) {

return x > y ? x : y;

static double max(int x, double y) {


CSE Computer Applications: User-Defined Methods & Arrays (PYQ + Toughest Questions)

return x > y ? x : y;

4. Prime Number Checker

Q: Write a method returning boolean true if n is prime.

Logic: Use loop to check divisibility up to sqrt(n).

Code:

static boolean isPrime(int n) {

if(n < 2) return false;

for(int i = 2; i <= Math.sqrt(n); i++) {

if(n % i == 0) return false;

return true;

5. Selection Sort (Ascending)

Q: Input 10 integers, sort using selection sort, display sorted array.

Logic: Each position selects the smallest element from unsorted part.

Code:

for(int i = 0; i < 9; i++) {

int idx = i;

for(int j = i + 1; j < 10; j++) {

if(arr[j] < arr[idx]) idx = j;

int temp = arr[i];

arr[i] = arr[idx];

arr[idx] = temp;

}
CSE Computer Applications: User-Defined Methods & Arrays (PYQ + Toughest Questions)

6. Check Special 3x3 Matrix

Q: Accept 3x3 array, check if sum of evens equals sum of odds.

Logic: Use nested loop, accumulate even/odd sums separately.

Code:

int[][] a = new int[3][3];

int even = 0, odd = 0;

for(int i = 0; i < 3; i++) {

for(int j = 0; j < 3; j++) {

int v = in.nextInt();

if(v % 2 == 0) even += v;

else odd += v;

System.out.println(even == odd ? "Special" : "Not special");

7. Bubble Sort (Descending)

Q: Sort 10 integers in descending order using bubble sort.

Logic: Compare and swap if current < next.

Code:

for(int i = 0; i < 9; i++) {

for(int j = 0; j < 9 - i; j++) {

if(arr[j] < arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
CSE Computer Applications: User-Defined Methods & Arrays (PYQ + Toughest Questions)

8. 2D Array Row Sums

Q: For each row in a 2D array, compute and print sum.

Logic: Use nested loops; outer for row, inner for columns.

Code:

for(int i = 0; i < rows; i++) {

int sum = 0;

for(int j = 0; j < cols; j++) {

sum += matrix[i][j];

System.out.println("Row " + i + " sum = " + sum);

9. Classify One/Two Digit Sums

Q: Input 10 integers, sum one-digit and two-digit separately.

Logic: Check range using conditionals.

Code:

int sum1 = 0, sum2 = 0;

for(int v : arr) {

if(v >= 0 && v <= 9) sum1 += v;

else if(v >= 10 && v <= 99) sum2 += v;

System.out.println("One-digit sum: " + sum1);

System.out.println("Two-digit sum: " + sum2);

10. Tough Question: Subarray with Maximum Sum

Q: Find the contiguous subarray with the largest sum (Kadane's Algorithm).

Logic: Track max_ending_here and max_so_far.


CSE Computer Applications: User-Defined Methods & Arrays (PYQ + Toughest Questions)

Code:

int maxSubarraySum(int[] arr) {

int maxSoFar = arr[0], maxEndingHere = arr[0];

for(int i = 1; i < arr.length; i++) {

maxEndingHere = Math.max(arr[i], maxEndingHere + arr[i]);

maxSoFar = Math.max(maxSoFar, maxEndingHere);

return maxSoFar;

You might also like