Prashnikaप्रश्निका

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

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

Practise these questions with filters
2021 · 15 marks · ProgramOpen: Write a program to accept a sentence which may be terminated by either ‘.’ …

Write the program described below.

Write a program to accept a sentence which may be terminated by either ‘.’ , ‘?’ or ‘!’ only. The words are to be separated by a single blank space and are in lower case. Perform the following tasks: (a) Check for the validity of the accepted sentence and for the terminating character. (b) Arrange the words contained in the sentence according to the size of the words in ascending order. If two words are of the same length then the first occurring comes first. The sentence should begin with a capital alphabet in both the cases i.e. Input and Output. (c) Display both the sentences separately with each sentence beginning with a capital alphabet. Design your program which will enable the output in the format given below:

Sample input/output

Sample 1
INPUT: the lines are printed in reverse order.

OUTPUT:
The lines are printed in reverse order.
In the are lines order printed reverse.

Sample 2
INPUT: print the sentence in ascending order.

OUTPUT:
Print the sentence in ascending order.
In the print order sentence ascending.

Sample 3
INPUT: i love my country.

OUTPUT:
I love my country.
I my love country.
Show answer

Answer

AI
2(a): This part checks the validity and the terminating character. The complete program for parts (a), (b) and (c) is:
import java.util.Scanner;

public class SentenceSort
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("INPUT: ");
        String s = sc.nextLine();

        // (a) validity of the sentence and of the terminating character
        boolean valid = s.length() > 1;
        if (valid)
        {
            char last = s.charAt(s.length() - 1);
            if (last != '.' && last != '?' && last != '!')
                valid = false;
        }
        if (valid)
        {
            String body = s.substring(0, s.length() - 1);
            if (body.startsWith(" ") || body.endsWith(" ") || body.indexOf("  ") != -1)
                valid = false;
            for (int i = 0; i < body.length() && valid; i++)
            {
                char c = body.charAt(i);
                if (!(c >= 'a' && c <= 'z') && c != ' ')
                    valid = false;
            }
        }
        if (!valid)
        {
            System.out.println("INVALID INPUT");
            return;
        }

        char term = s.charAt(s.length() - 1);
        String body = s.substring(0, s.length() - 1);
        String w[] = body.split(" ");
        int n = w.length;

        // (b) arrange the words by length (insertion sort keeps the original order of equal lengths)
        for (int i = 1; i < n; i++)
        {
            String key = w[i];
            int j = i - 1;
            while (j >= 0 && w[j].length() > key.length())
            {
                w[j + 1] = w[j];
                j--;
            }
            w[j + 1] = key;
        }

        String sorted = "";
        for (int i = 0; i < n; i++)
            sorted = sorted + w[i] + " ";
        sorted = sorted.trim();

        // (c) display both sentences, each beginning with a capital letter
        String first = Character.toUpperCase(body.charAt(0)) + body.substring(1) + term;
        String second = Character.toUpperCase(sorted.charAt(0)) + sorted.substring(1) + term;
        System.out.println();
        System.out.println("OUTPUT:");
        System.out.println(first);
        System.out.println(second);
    }
}
Validity check: the sentence is accepted only if its last character is '.', '?' or '!', it has at least one word, it has no leading or trailing blank, no two blanks together (words are separated by a single blank) and every other character is a lower case letter. Otherwise the message INVALID INPUT is printed and the program stops. Tested: 'hello world' (no terminating character) and 'two spaces here.' (double blank) both give INVALID INPUT. 2(b): The sentence (without the terminating character) is split into words with split(" "). The words are arranged in ascending order of their length with insertion sort:
for (int i = 1; i < n; i++)
{
    String key = w[i];
    int j = i - 1;
    while (j >= 0 && w[j].length() > key.length())
    {
        w[j + 1] = w[j];
        j--;
    }
    w[j + 1] = key;
}
A word is shifted only when the word before it is strictly longer, so words of the same length keep their original order (the first occurring comes first). Example: 'print the sentence in ascending order' becomes in the print order sentence ascending (in = 2, the = 3, print = 5, order = 5, sentence = 8, ascending = 9 letters). 2(c): Both sentences are displayed with the first letter changed to a capital letter using Character.toUpperCase() and with the original terminating character added at the end:
String first = Character.toUpperCase(body.charAt(0)) + body.substring(1) + term;
String second = Character.toUpperCase(sorted.charAt(0)) + sorted.substring(1) + term;
System.out.println(first);
System.out.println(second);
Tested with the three samples: INPUT: the lines are printed in reverse order. OUTPUT: The lines are printed in reverse order. In the are lines order printed reverse. INPUT: print the sentence in ascending order. OUTPUT: Print the sentence in ascending order. In the print order sentence ascending. INPUT: i love my country. OUTPUT: I love my country. I my love country.
2020 · 2 marks · NumericalOpen: Each element of an array requires ' ' bytes of storage. If the address of is…

Solve the following.

Each element of an array $arr[15][20]$ requires '$W$' bytes of storage. If the address of $arr[6][8]$ is 4440 and the Base Address at $arr[1][1]$ is 4000, find the width '$W$' of each cell in the array $arr[ ][ ]$ when the array is stored as Column Major Wise.
Show answer

Answer

AI
Column major formula: Address of $arr[I][J] = BA + W \times [(J - L_c) \times M + (I - L_r)]$ Given: number of rows $M = 15$, $BA = 4000$ at $arr[1][1]$ so $L_r = 1$, $L_c = 1$; $I = 6$, $J = 8$; address of $arr[6][8] = 4440$. $4440 = 4000 + W \times [(8 - 1) \times 15 + (6 - 1)]$ $4440 = 4000 + W \times [105 + 5]$ $440 = 110 \times W$ $W = 440 / 110 = 4$ bytes

Final answer: 4 bytes

2020 · 2 marks · NumericalOpen: A matrix is stored in the memory with each element requiring 2 bytes of…

Solve the following.

A matrix $B[10][20]$ is stored in the memory with each element requiring 2 bytes of storage. If the base address at $B[2][1]$ is 2140, find the address of $B[5][4]$ when the matrix is stored in Column Major Wise.
Show answer

Answer

AI
Column major formula (relative to the element whose address is known): Address of $B[I][J] = BA + W \times [(J - L_c) \times M + (I - L_r)]$ Given: number of rows $M = 10$, $W = 2$ bytes, $BA = 2140$ at $B[2][1]$, so $L_r = 2$, $L_c = 1$; $I = 5$, $J = 4$. Address of $B[5][4] = 2140 + 2 \times [(4 - 1) \times 10 + (5 - 2)]$ $= 2140 + 2 \times [30 + 3]$ $= 2140 + 66 = 2206$

Final answer: 2206

2019 · 2 marks · NumericalOpen: A matrix is stored in the memory with each element requiring 4 bytes of…

Solve the following.

A matrix $ARR[-4\ldots6, 3\ldots8]$ is stored in the memory with each element requiring 4 bytes of storage. If the base address is 1430, find the address of $ARR[3][6]$ when the matrix is stored in Row Major Wise.
Show answer

Answer

AI
Row range $-4$ to $6$ $\to$ number of rows $M = 6-(-4)+1 = 11$ Column range $3$ to $8$ $\to$ number of columns $N = 8-3+1 = 6$ Each element $W = 4$ bytes; Base address $=1430$; target $ARR[3][6]$ Row Major formula: $Address(ARR[i][j]) = Base + W \times [(i - L_r) \times N + (j - L_c)]$ Here $i=3, j=6, L_r=-4, L_c=3$ $= 1430 + 4 \times [(3-(-4)) \times 6 + (6-3)]$ $= 1430 + 4 \times [42 + 3]$ $= 1430 + 4 \times 45$ $= 1430 + 180$

Final answer: 1610

2018 · 2 marks · One wordOpen: A matrix A[m][m] is stored in the memory with each element requiring 4 bytes of…

Answer the following.

A matrix A[m][m] is stored in the memory with each element requiring 4 bytes of storage. If the base address at A[1][1] is 1500 and the address of A[4][5] is 1608, determine the order of the matrix when it is stored in Column Major Wise.
Show answer

Answer

AI
Given: matrix A[m][m], W=4 bytes, Base Address A[1][1]=1500, Address of A[4][5]=1608, Column Major storage. Column Major formula (lower bound 1 for both row and column): Address(A[i][j]) = Base + W x [(j-1) x M + (i-1)], where M = number of rows = m (order of the square matrix) Here i=4, j=5: 1608 = 1500 + 4 x [(5-1) x m + (4-1)] 108 = 4 x (4m+3) 27 = 4m+3 4m = 24 m = 6 So the order of the matrix is 6 x 6.

Final answer: 6 x 6

2017 · 10 marks · ProgramOpen: A class Adder has been defined to add any two accepted time. Example: Time A -…

Write the program described below.

A class Adder has been defined to add any two accepted time. Example: Time A - 6 hours 35 minutes Time B - 7 hours 45 minutes Their sum is - 14 hours 20 minutes ( where 60 minutes = 1 hour) The details of the members of the class are given below: Class name : Adder Data member/instance variable: a[ ] : integer array to hold two elements (hours and minutes) Member functions/methods: Adder( ) : constructor to assign 0 to the array elements void readtime( ) : to enter the elements of the array void addtime( Adder X, Adder Y) : adds the time of the two parameterized objects X and Y and stores the sum in the current calling object void disptime( ) : displays the array elements with an appropriate message (i.e. hours = and minutes = ) Specify the class Adder giving details of the constructor( ), void readtime( ), void addtime(Adder, Adder) and void disptime( ). Define the main( ) function to create objects and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class Adder
{
    int a[] = new int[2];   // a[0] = hours, a[1] = minutes

    Adder()
    {
        a[0] = 0;
        a[1] = 0;
    }

    void readtime()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter hours: ");
        a[0] = sc.nextInt();
        System.out.print("Enter minutes: ");
        a[1] = sc.nextInt();
    }

    void addtime(Adder X, Adder Y)
    {
        a[1] = X.a[1] + Y.a[1];
        a[0] = X.a[0] + Y.a[0] + a[1] / 60;   // carry of minutes goes to hours
        a[1] = a[1] % 60;
    }

    void disptime()
    {
        System.out.println("hours = " + a[0] + " and minutes = " + a[1]);
    }

    public static void main(String args[])
    {
        Adder A = new Adder();
        Adder B = new Adder();
        Adder C = new Adder();
        System.out.println("Enter Time A:");
        A.readtime();
        System.out.println("Enter Time B:");
        B.readtime();
        C.addtime(A, B);
        System.out.println("Sum of the two times:");
        C.disptime();
    }
}
Explanation: addtime() adds the minutes of X and Y, adds the hours plus the carry (minutes / 60) and keeps the remainder (minutes % 60) as the minutes of the calling object. Tested by running: 6 h 35 min + 7 h 45 min gives hours = 14 and minutes = 20.
2017 · 10 marks · ProgramOpen: A class SwapSort has been defined to perform string related operations on a…

Write the program described below.

A class SwapSort has been defined to perform string related operations on a word input. Some of the members of the class are as follows: Class name : SwapSort Data members/instance variables: wrd : to store a word len : integer to store length of the word swapwrd : to store the swapped word sortwrd : to store the sorted word Member functions/methods: SwapSort( ) : default constructor to initialize data members with legal initial values void readword( ) : to accept a word in UPPER CASE void swapchar( ) : to interchange/swap the first and last characters of the word in 'wrd' and stores the new word in 'swapwrd' void sortword( ) : sorts the characters of the original word in alphabetical order and stores it in 'sortwrd' void display( ) : displays the original word, swapped word and the sorted word Specify the class SwapSort, giving the details of the constructor( ), void readword( ), void swapchar( ), void sortword( ) and void display( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.
Show answer

Answer

AI
import java.util.Scanner;

class SwapSort
{
    String wrd, swapwrd, sortwrd;
    int len;

    SwapSort()
    {
        wrd = "";
        swapwrd = "";
        sortwrd = "";
        len = 0;
    }

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

    void swapchar()
    {
        if (len > 1)
            swapwrd = wrd.charAt(len - 1) + wrd.substring(1, len - 1) + wrd.charAt(0);
        else
            swapwrd = wrd;
    }

    void sortword()
    {
        char ch[] = wrd.toCharArray();
        for (int i = 0; i < len - 1; i++)
        {
            for (int j = 0; j < len - 1 - i; j++)
            {
                if (ch[j] > ch[j + 1])
                {
                    char t = ch[j];
                    ch[j] = ch[j + 1];
                    ch[j + 1] = t;
                }
            }
        }
        sortwrd = new String(ch);
    }

    void display()
    {
        System.out.println("Original word : " + wrd);
        System.out.println("Swapped word  : " + swapwrd);
        System.out.println("Sorted word   : " + sortwrd);
    }

    public static void main(String args[])
    {
        SwapSort obj = new SwapSort();
        obj.readword();
        obj.swapchar();
        obj.sortword();
        obj.display();
    }
}
Explanation: swapchar() builds the new word as last character + middle part + first character. sortword() copies the word into a char array and sorts it with bubble sort, then stores it as a String in sortwrd. Tested by running: input HELLO gives swapped word OELLH and sorted word EHLLO.

Questions on other pages on Arrays, Strings

Other Computer Science chapters