Prashnikaप्रश्निका

Arrays, Strings - ISC Class 12 Computer Science Questions with Answers, Page 2

48 past-paper questions on Arrays, Strings from ISC Class 12 Computer Science papers (2026-2017), newest first, in full. Questions 21-40 are on this page, 20 to a page. Tap "Show answer" under a question to see its answer.

Practise these questions with filters
2025 · 10 marks · ProgramOpen: [Arrays] Given below is an example of a double dimensional array with unequal…

Write the program described below.

[Arrays] Given below is an example of a double dimensional array with unequal number of rows and columns. It must be arranged in such a way that the elements in the even rows are in ascending order and the elements in the odd rows are in descending order, using Insertion sort technique. The members of the class are given below: Class name : Mix_sort Data members / instance variables: a[][] : to store integers in the double dimensional array. m : to store the number of rows. n : to store the number of columns. Methods / Member functions : Mix_sort(int row, int col) : parameterised constructor to initialise the m=row, n=col and allocate memory for a[][] void input() : to enter the integer elements in a [][] void ascEven(int x[]) : arrange the elements of x[] in ascending order using Insertion sort technique. void dscOdd(int x[]) : arrange the elements of x[] in descending order using Insertion sort technique. void arrange() : to arrange the rows in even position in Ascending order using ascEven(.. ) method and rows in odd position in descending order using dscOdd(..) method. Finally store the sorted elements into a[][] void display( ) : display the original a[][] and sorted a[][] . Define the class Mix_sort with details of the constructor, void input(), void arrange(), void ascEven(int []), void dscOdd(int []) and void display( ). Define a main() function to create an object and call the functions accordingly to enable the task.

Sample input/output

Sample Input :
Row=3
Column=4
Enter the array elements
1 2 3 4
9 8 7 6
4 5 6 7

Before sort:
1   2   3   4
9   8   7   6
4   5   6   7

After sort:
4   3   2   1
6   7   8   9
7   6   5   4
Show answer

Answer

AI
import java.util.Scanner;

class Mix_sort
{
    int a[][];   // stores the integer array
    int m;       // number of rows
    int n;       // number of columns

    // parameterised constructor
    Mix_sort(int row, int col)
    {
        m = row;
        n = col;
        a = new int[m][n];
    }

    // accepts the integer elements into a[][]
    void input()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = sc.nextInt();
    }

    // arranges x[] in ascending order using insertion sort
    void ascEven(int x[])
    {
        for (int i = 1; i < x.length; i++)
        {
            int key = x[i];
            int j = i - 1;
            while (j >= 0 && x[j] > key)
            {
                x[j + 1] = x[j];
                j--;
            }
            x[j + 1] = key;
        }
    }

    // arranges x[] in descending order using insertion sort
    void dscOdd(int x[])
    {
        for (int i = 1; i < x.length; i++)
        {
            int key = x[i];
            int j = i - 1;
            while (j >= 0 && x[j] < key)
            {
                x[j + 1] = x[j];
                j--;
            }
            x[j + 1] = key;
        }
    }

    // sorts each row: even (1-indexed) row positions ascending, odd row positions descending
    void arrange()
    {
        for (int i = 0; i < m; i++)
        {
            int row[] = new int[n];
            for (int j = 0; j < n; j++)
                row[j] = a[i][j];
            if ((i + 1) % 2 == 0)
                ascEven(row);
            else
                dscOdd(row);
            for (int j = 0; j < n; j++)
                a[i][j] = row[j];
        }
    }

    // displays the original array, then arranges it and displays the sorted array
    void display()
    {
        System.out.println("Before sort:");
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(a[i][j] + "\t");
            System.out.println();
        }
        arrange();
        System.out.println("After sort:");
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(a[i][j] + "\t");
            System.out.println();
        }
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Row=");
        int row = sc.nextInt();
        System.out.print("Column=");
        int col = sc.nextInt();
        Mix_sort obj = new Mix_sort(row, col);
        System.out.println("Enter the array elements");
        obj.input();
        obj.display();
    }
}
Explanation: ascEven() and dscOdd() are both plain insertion sort, just with the comparison reversed. arrange() copies each row into a temporary array, sorts it ascending if the row's 1-indexed position is even and descending if odd, then writes it back into a[][]. display() prints the array as first entered, calls arrange(), then prints the result. Tested (run for real) with the question's own 3x4 example (rows 1 2 3 4 / 9 8 7 6 / 4 5 6 7): output was exactly 'Before sort: 1 2 3 4 / 9 8 7 6 / 4 5 6 7' then 'After sort: 4 3 2 1 / 6 7 8 9 / 7 6 5 4', matching the sample in the question.
2024 · 10 marks · ProgramOpen: A class InsSort contains an array of integers which sorts the elements in a…

Write the program described below.

A class InsSort contains an array of integers which sorts the elements in a particular order. Some of the members of the class are given below.
Class name:InsSort
Data members/instance variables:
arr[ ]:stores the array elements
size:stores the number of elements in the array
Methods / Member functions:
InsSort(int s):constructor to initialise size = s
void getArray( ):accepts the array elements
void insertionSort( ):sorts the elements of the array in descending order using the Insertion Sort technique
double find( ):calculates and returns the average of all the odd numbers in the array
void display():displays the elements of the array in a sorted order along with the average of all the odd numbers in the array by invoking the function find( ) with an appropriate message
Specify the class InsSort giving details of the constructor( ), void getArray( ), void insertionSort( ), double find() and void display( ). Define a main() function to create an object and call all the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class InsSort
{
    int arr[];
    int size;

    InsSort(int s)
    {
        size = s;
        arr = new int[size];
    }

    void getArray()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < size; i++)
        {
            System.out.print("Enter element " + (i + 1) + ": ");
            arr[i] = sc.nextInt();
        }
    }

    void insertionSort()
    {
        for (int i = 1; i < size; i++)
        {
            int key = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] < key)
            {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = key;
        }
    }

    double find()
    {
        int sum = 0, count = 0;
        for (int i = 0; i < size; i++)
        {
            if (arr[i] % 2 != 0)
            {
                sum += arr[i];
                count++;
            }
        }
        if (count == 0)
            return 0;
        return (double) sum / count;
    }

    void display()
    {
        System.out.print("Sorted array (descending order): ");
        for (int i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
        System.out.println("Average of odd numbers = " + find());
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size of array: ");
        int s = sc.nextInt();
        InsSort obj = new InsSort(s);
        obj.getArray();
        obj.insertionSort();
        obj.display();
    }
}
Explanation: The constructor allocates arr[] of the given size. getArray() reads the elements. insertionSort() implements the standard Insertion Sort but shifts elements while the key is greater than the compared element, so the array ends up sorted in descending order. find() sums all odd-valued elements and returns their average (0 if there are none). display() prints the sorted array and calls find() to print the average of odd numbers. Tested with input {12,5,9,20,3,8}: sorted output is 20 12 9 8 5 3, and the average of the odd numbers (5,9,3) is correctly computed as 5.666666666666667.
2024 · 2 marks · NumericalOpen: An array ARR [ -5 .....15, 10.....20 ] stores elements in Row Major Wise with…

Answer the following.

An array ARR [ -5 .....15, 10.....20 ] stores elements in Row Major Wise with each element requiring 2 bytes of storage. Find the address of ARR [10] [15] when the base address is 2500.
Show answer

Answer

AI
Rows: $-5$ to $15 \Rightarrow N = 15-(-5)+1 = 21$ Columns: $10$ to $20 \Rightarrow M = 20-10+1 = 11$ Row Major formula: $Address(ARR[i][j]) = Base + W \times [(i-L_r) \times M + (j-L_c)]$ Here $i=10, j=15, L_r=-5, L_c=10, W=2$ $= 2500 + 2 \times [(10-(-5)) \times 11 + (15-10)]$ $= 2500 + 2 \times [165+5] = 2500+340$

Final answer: 2840

2024 · 10 marks · ProgramOpen: Design a class Coding to perform some string related operations on a word…

Write the program described below.

Design a class Coding to perform some string related operations on a word containing alphabets only. Example: Input: “Java” Output: Original word: Java J = 74 a = 97 v = 118 a = 97 Lowest ASCII code: 74 Highest ASCII code: 118 Some of the members of the class are given below.
Class name:Coding
Data members/instance variables:
wrd:stores the word
len:stores the length of the word
Methods / Member functions:
Coding():constructor to initialise the data members with legal initial values
void accept( ):to accept a word
void find():to display all the characters of ‘wrd’ along with their ASCII codes. Also display the lowest ASCII code and the highest ASCII code, in ‘wrd’
void show( ):to display the original word and all the characters of ‘wrd’ along with their ASCII codes. Also display the lowest ASCII code and the highest ASCII code in ‘wrd’, by invoking the function find( )
Specify the class Coding giving details of the constructor( ), void accept( ), void find( ) and void show( ). Define a main() function to create an object and call all the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class Coding
{
    String wrd;
    int len;

    Coding()
    {
        wrd = "";
        len = 0;
    }

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word (alphabets only): ");
        wrd = sc.next();
        len = wrd.length();
    }

    void find()
    {
        int lowest = wrd.charAt(0);
        int highest = wrd.charAt(0);
        for (int i = 0; i < len; i++)
        {
            char ch = wrd.charAt(i);
            int code = (int) ch;
            System.out.println(ch + " = " + code);
            if (code < lowest)
                lowest = code;
            if (code > highest)
                highest = code;
        }
        System.out.println("Lowest ASCII code: " + lowest);
        System.out.println("Highest ASCII code: " + highest);
    }

    void show()
    {
        System.out.println("Original word: " + wrd);
        find();
    }

    public static void main(String[] args)
    {
        Coding obj = new Coding();
        obj.accept();
        obj.show();
    }
}
Explanation: accept() reads the word and stores its length in len. find() loops through each character of wrd, prints the character with its ASCII code, and tracks the lowest and highest ASCII codes seen; these are printed at the end. show() prints the original word and then calls find(). Tested with input "Java": output is exactly as specified - J=74, a=97, v=118, a=97, Lowest ASCII code: 74, Highest ASCII code: 118.
2023 · 10 marks · ProgramOpen: A class SortAlpha has been defined to sort the words in the sentence in…

Write the program described below.

A class SortAlpha has been defined to sort the words in the sentence in alphabetical order. Example: Input: THE SKY IS BLUE Output: BLUE IS SKY THE Some of the members of the class are given below:
Class name:SortAlpha
Data members/instance variables:
sent:to store a sentence
n:integer to store the number of words in a sentence
Methods / Member functions:
SortAlpha( ):default constructor to initialise data members with legal initial values
void acceptsent( ):to accept a sentence in UPPER CASE
void sort(SortAlpha P):sorts the words of the sentence of object P in alphabetical order and stores the sorted sentence in the current object
void display( ):displays the original sentence along with the sorted sentence by invoking the method sort()
Specify the class SortAlpha giving details of the constructor( ), void acceptsent( ), void sort(SortAlpha) and void display(). Define a main() function to create an object and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class SortAlpha
{
    String sent;   // the sentence
    int n;         // number of words in the sentence

    // default constructor
    SortAlpha()
    {
        sent = "";
        n = 0;
    }

    // accepts a sentence in upper case and counts its words
    void acceptsent()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence in UPPER CASE: ");
        sent = sc.nextLine().trim().toUpperCase();
        n = sent.split("\\s+").length;
    }

    // sorts the words of P's sentence alphabetically and stores the result in this object
    void sort(SortAlpha P)
    {
        String w[] = P.sent.split("\\s+");
        int len = w.length;
        // bubble sort on the words
        for (int i = 0; i < len - 1; i++)
        {
            for (int j = 0; j < len - 1 - i; j++)
            {
                if (w[j].compareTo(w[j + 1]) > 0)
                {
                    String t = w[j];
                    w[j] = w[j + 1];
                    w[j + 1] = t;
                }
            }
        }
        sent = "";
        for (int i = 0; i < len; i++)
            sent = sent + w[i] + " ";
        sent = sent.trim();
        n = len;
    }

    // displays the original sentence and the sorted sentence
    void display()
    {
        SortAlpha ob = new SortAlpha();
        ob.sort(this);
        System.out.println("Original sentence: " + sent);
        System.out.println("Sorted sentence: " + ob.sent);
    }

    public static void main(String args[])
    {
        SortAlpha obj = new SortAlpha();
        obj.acceptsent();
        obj.display();
    }
}
sort() splits the sentence of object P into words, arranges them with bubble sort using compareTo(), and joins them back into the current object's sent. display() creates a second object, calls sort(this) on it so the original sentence is kept unchanged, and prints both. Sample run:
Enter a sentence in UPPER CASE: THE SKY IS BLUE
Original sentence: THE SKY IS BLUE
Sorted sentence: BLUE IS SKY THE
2023 · 10 marks · ProgramOpen: A class Trans is defined to find the transpose of a square matrix. A transpose…

Write the program described below.

A class Trans is defined to find the transpose of a square matrix. A transpose of a matrix is obtained by interchanging the elements of the rows and columns. Example: If size of the matrix = 3, then
ORIGINALTRANSPOSE
11571181
81395136
16207920
Some of the members of the class are given below:
Class name:Trans
Data members/instance variables:
arr[ ][ ]:to store integers in the matrix
m:integer to store the size of the matrix
Methods / Member functions:
Trans(int mm):parameterised constructor to initialise the data member $m = mm$
void fillarray( ):to enter integer elements in the matrix
void transpose( ):to create the transpose of the given matrix
void display( ):displays the original matrix and the transposed matrix by invoking the method transpose()
Specify the class Trans giving details of the constructor( ), void fillarray( ), void transpose( ) and void display(). Define a main() function to create an object and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class Trans
{
    static Scanner sc = new Scanner(System.in);
    int arr[][];   // the matrix
    int m;         // size of the matrix

    // parameterised constructor
    Trans(int mm)
    {
        m = mm;
        arr = new int[m][m];
    }

    // accepts the elements of the matrix
    void fillarray()
    {
        System.out.println("Enter " + (m * m) + " elements:");
        for (int i = 0; i < m; i++)
            for (int j = 0; j < m; j++)
                arr[i][j] = sc.nextInt();
    }

    // transposes the matrix in place by swapping arr[i][j] and arr[j][i]
    void transpose()
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = i + 1; j < m; j++)
            {
                int t = arr[i][j];
                arr[i][j] = arr[j][i];
                arr[j][i] = t;
            }
        }
    }

    // prints the matrix row by row
    void print()
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < m; j++)
                System.out.print(arr[i][j] + "\t");
            System.out.println();
        }
    }

    // displays the original matrix, then the transposed matrix
    void display()
    {
        System.out.println("ORIGINAL MATRIX");
        print();
        transpose();
        System.out.println("TRANSPOSE");
        print();
    }

    public static void main(String args[])
    {
        System.out.print("Enter the size of the matrix: ");
        int size = sc.nextInt();
        Trans ob = new Trans(size);
        ob.fillarray();
        ob.display();
    }
}
transpose() swaps each element above the main diagonal with its mirror element below it (arr[i][j] with arr[j][i]), so rows become columns. display() prints the original matrix, invokes transpose() and prints the result. Sample run:
Enter the size of the matrix: 3
Enter 9 elements:
11 5 7
8 13 9
1 6 20
ORIGINAL MATRIX
11	5	7	
8	13	9	
1	6	20	
TRANSPOSE
11	8	1	
5	13	6	
7	9	20	
2023 · 10 marks · ProgramOpen: Design a class OddEven to arrange two single dimensional arrays into one single…

Write the program described below.

Design a class OddEven to arrange two single dimensional arrays into one single dimensional array, such that the odd numbers from both the arrays are at the beginning followed by the even numbers. Example: Array 1: { 2, 13, 6, 19, 26, 11, 4 } Array 2: { 7, 22, 4, 17, 12, 45 } Arranged Array = { 13, 19 11, 7, 17, 45, 2, 6, 26, 4, 22, 4, 12 } Some of the members of the class are given below:
Class name:OddEven
Data members/instance variables:
a[ ]:to store integers in the array
m:integer to store the size of the array
Methods / Member functions:
OddEven(int mm):parameterised constructor to initialize the data member m=mm
void fillarray( ):to enter integer elements in the array
OddEven arrange(OddEven P, OddEven Q ):stores the odd numbers from both the parameterized object arrays followed by the even numbers from both the arrays and returns the object with the arranged array
void display( ):displays the elements of the arranged array
Specify the class OddEven giving details of the constructor( ), void fillarray( ), OddEven arrange(OddEven, OddEven) and void display( ). Define a main( ) function to create objects and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class OddEven
{
    int a[];
    int m;

    OddEven(int mm)
    {
        m = mm;
        a = new int[m];
    }

    void fillarray()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < m; i++)
        {
            System.out.print("Enter element " + (i + 1) + ": ");
            a[i] = sc.nextInt();
        }
    }

    OddEven arrange(OddEven P, OddEven Q)
    {
        OddEven R = new OddEven(P.m + Q.m);
        int k = 0;
        for (int i = 0; i < P.m; i++)
            if (P.a[i] % 2 != 0)
                R.a[k++] = P.a[i];
        for (int i = 0; i < Q.m; i++)
            if (Q.a[i] % 2 != 0)
                R.a[k++] = Q.a[i];
        for (int i = 0; i < P.m; i++)
            if (P.a[i] % 2 == 0)
                R.a[k++] = P.a[i];
        for (int i = 0; i < Q.m; i++)
            if (Q.a[i] % 2 == 0)
                R.a[k++] = Q.a[i];
        return R;
    }

    void display()
    {
        for (int i = 0; i < m; i++)
            System.out.print(a[i] + " ");
        System.out.println();
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size of array 1: ");
        int m1 = sc.nextInt();
        OddEven o1 = new OddEven(m1);
        o1.fillarray();

        System.out.print("Enter size of array 2: ");
        int m2 = sc.nextInt();
        OddEven o2 = new OddEven(m2);
        o2.fillarray();

        OddEven o3 = new OddEven(0);
        OddEven result = o3.arrange(o1, o2);
        result.display();
    }
}
Explanation: arrange() first copies all odd elements of P in order, then all odd elements of Q, then all even elements of P, then all even elements of Q, into a new OddEven object R sized m of P plus m of Q, and returns R. Tested (run for real) with Array1={2,13,6,19,26,11,4} and Array2={7,22,4,17,12,45): the program printed "13 19 11 7 17 45 2 6 26 4 22 4 12", which exactly matches the Arranged Array given in the question.
2023 · 2 marks · NumericalOpen: A matrix N[11][8] is stored in the memory with each element requiring 2 bytes…

Answer the following.

A matrix N[11][8] is stored in the memory with each element requiring 2 bytes of storage. If the base address at N[2][3] is 2140, find the address of N[7][5] when the matrix is stored in Row Major Wise.
Show answer

Answer

AI
Row Major formula relative to a known base address: $Address(N[i][j]) = Base(N[2][3]) + W \times [(i-2)\times C + (j-3)]$, where $C$ = number of columns = 8, $W$ = 2 bytes For $N[7][5]$: $= 2140 + 2 \times [(7-2)\times 8 + (5-3)]$ $= 2140 + 2 \times [40+2]$ $= 2140 + 84$

Final answer: 2224 bytes (address)

2023 · 10 marks · ProgramOpen: A class Encrypt has been defined to replace only the vowels in a word by the…

Write the program described below.

A class Encrypt has been defined to replace only the vowels in a word by the next corresponding vowel and forms a new word. i.e. A → E, E → I, I → O, O → U and U → A Example: Input: COMPUTER Output: CUMPATIR Some of the members of the class are given below:
Class name:Encrypt
Data members/instance variables:
wrd:to store a word
len:integer to store the length of the word
newwrd:to store the encrypted word
Methods / Member functions:
Encrypt( ):default constructor to initialize data members with legal initial values
void acceptword( ):to accept a word in UPPER CASE
void freqvowcon( ):finds the frequency of the vowels and consonants in the word stored in ‘wrd’ and displays them with an appropriate message
void nextVowel( ):replaces only the vowels from the word stored in ‘wrd’ by the next corresponding vowel and assigns it to ‘newwrd’, with the remaining alphabets unchanged
void disp( ):Displays the original word along with the encrypted word
Specify the class Encrypt giving details of the constructor( ), void acceptword( ), void freqvowcon( ), void nextVowel( ) and void disp( ). Define a main( ) function to create an object and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class Encrypt
{
    String wrd, newwrd;
    int len;

    Encrypt()
    {
        wrd = "";
        newwrd = "";
        len = 0;
    }

    void acceptword()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word in UPPER CASE: ");
        wrd = sc.nextLine();
        len = wrd.length();
    }

    void freqvowcon()
    {
        int vcount = 0, ccount = 0;
        for (int i = 0; i < len; i++)
        {
            char ch = wrd.charAt(i);
            if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
                vcount++;
            else
                ccount++;
        }
        System.out.println("Number of vowels: " + vcount);
        System.out.println("Number of consonants: " + ccount);
    }

    void nextVowel()
    {
        for (int i = 0; i < len; i++)
        {
            char ch = wrd.charAt(i);
            switch (ch)
            {
                case 'A': newwrd += 'E'; break;
                case 'E': newwrd += 'I'; break;
                case 'I': newwrd += 'O'; break;
                case 'O': newwrd += 'U'; break;
                case 'U': newwrd += 'A'; break;
                default: newwrd += ch;
            }
        }
    }

    void disp()
    {
        System.out.println("Original word  : " + wrd);
        System.out.println("Encrypted word : " + newwrd);
    }

    public static void main(String args[])
    {
        Encrypt obj = new Encrypt();
        obj.acceptword();
        obj.freqvowcon();
        obj.nextVowel();
        obj.disp();
    }
}
Explanation: nextVowel() scans each character of wrd and, if it is one of the vowels A/E/I/O/U, replaces it by the next vowel in the cycle A->E->I->O->U->A using a switch statement, copying all other (consonant) characters unchanged into newwrd. freqvowcon() separately counts and displays the number of vowels and consonants in wrd. Tested (run for real): for wrd="COMPUTER" the program printed "Number of vowels: 3", "Number of consonants: 5", and Encrypted word "CUMPATIR", matching the example in the question exactly.
2022 · 2 marks · MCQOpen: What is the conditional statement to check for the Non-boundary elements in a…

Choose the correct option.

What is the conditional statement to check for the Non-boundary elements in a double dimensional array of ‘M’ number of rows and ‘N’ number of columns? The row index is represented by ‘r’ and the column index is represented by ‘c’.
  • (a)(r>0 || r<M-1 && c>0 || c<N-1)
  • (b)(r>0 && r<M-1 || c>0 && c<N-1)
  • (c)(r>0 && r<M-1 && c>0 && c<N-1)
  • (d)(r>0 || r<M-1 || c>0 || c<N-1)
Show answer

Answer

AI

Correct option: (c)

Answer: (c) (r>0 && r<M-1 && c>0 && c<N-1) Non-boundary elements are not in the first or last row nor the first or last column, so all four conditions must hold together.
2022 · 2 marks · MCQOpen: What will be the output of the method dimen() when the value of n[][] =…

Choose the correct option.

What will be the output of the method dimen() when the value of n[][] = {{2,3,7},{1,5,9},{10,-3,8}}?
  • (i)42
  • (ii)33
  • (iii)5
  • (iv)37
Show the code
void dimen(int n[][])
{
    int p = 0;
    for (int i = 0; i < n.length; i++)
        for(int j = 0; j < n[0].length; j++)
        {
            if(i == 0 || i == n.length - 1 || j == 0 || j == n[0].length - 1)
                p = p + n[i][j];
        }
    System.out.print(p);
}
Show answer

Answer

AI

Correct option: (iv)

Answer: (iv) 37 All elements except the centre 5 are boundary: 2+3+7+1+9+10-3+8 = 37.
2022 · 1 mark · MCQOpen: What is the method dimen() performing?

Choose the correct option.

What is the method dimen() performing?
  • (i)Finding the product of the boundary elements
  • (ii)Finding the sum of the non-boundary elements
  • (iii)Finding the sum of the boundary elements
  • (iv)Finding the sum of the matrix elements
Show answer

Answer

AI

Correct option: (iii)

Answer: (iii) Finding the sum of the boundary elements The condition selects the first/last row and first/last column, and their values are added to p.
2022 · 1 mark · MCQOpen: What is the method single() performing?

Choose the correct option.

What is the method single() performing?
  • (i)Sum of the positive odd elements
  • (ii)Product of the even elements
  • (iii)Product of the positive even elements
  • (iv)Sum of the positive even elements
Show the code
void single(int x[])
{
    int w = 1;
    for(int y = 0; y < x.length; y++)
    {
        if(x[y] % 2 == 0 && x[y] > 0)
            w = w * x[y];
    }
    System.out.print(w);
}
Show answer

Answer

AI

Correct option: (iii)

Answer: (iii) Product of the positive even elements w is multiplied only by elements that are even and greater than 0.
2022 · 2 marks · MCQOpen: A matrix MAT[10][15] is stored in the memory in Row Major Wise with each…

Choose the correct option.

A matrix MAT[10][15] is stored in the memory in Row Major Wise with each element requiring 2 bytes of storage. If the base address at MAT[1][2] is 2215, then the address of MAT[3][7] will be:
  • (a)2285
  • (b)2315
  • (c)2319
  • (d)None of the above
Show answer

Answer

AI

Correct option: (a)

Answer: (a) 2285 Row major with 15 columns: distance from MAT[1][2] to MAT[3][7] = (2 rows x 15 + 5) = 35 elements, x 2 bytes = 70. Address = 2215 + 70 = 2285.

Questions on other pages on Arrays, Strings

Other Computer Science chapters