Prashnikaप्रश्निका
Code
About
Computer ScienceSpecimenClass 12

ISC Computer Science 2021 Specimen Paper (Paper 2) with Answers

The ISC Computer Science 2021 Specimen Paper (Paper 2) for Class 12: all 3 questions, 30 marks in all, in printed order and in full. It is free to download: tap "Show answer" under a question to see its answer, or use the download button to save the questions as a PDF, with or without answers; "Print this paper" below prints it as it was set.

3questions
30marks
2chapters
Q115 marks · ProgramOpen: Design a program which takes two integer parameters namely number of the day…

Write the program described below.

Design a program which takes two integer parameters namely number of the day (between 1 and 366) and the year (in 4 digits) as inputs and displays the date i.e. day, month and year. Also find the corresponding date exactly after (N) days of the present date by accepting the value of (N) from the user, where the value of (N) is in the limit ($1 \le N \le 100$). Design your program which will enable the output in the format given below:

Sample input/output

Sample 1
INPUT: DAY NUMBER : 233
       YEAR : 2020
       DATE AFTER : 17

OUTPUT:
20TH. AUGUST 2020
DATE AFTER 17 DAYS :
6TH. SEPTEMBER 2020

Sample 2
INPUT: DAY NUMBER : 360
       YEAR : 2020
       DATE AFTER : 45

OUTPUT:
25TH. DECEMBER 2020
DATE AFTER 45 DAYS :
8TH. FEBRUARY 2021
Show answer

Answer

import java.util.Scanner;

public class DayToDate
{
    static String months[] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY",
                              "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};

    static boolean isLeap(int y)
    {
        return (y % 400 == 0) || (y % 100 != 0 && y % 4 == 0);
    }

    static String suffix(int d)
    {
        if (d >= 11 && d <= 13)
            return "TH.";
        switch (d % 10)
        {
            case 1: return "ST.";
            case 2: return "ND.";
            case 3: return "RD.";
            default: return "TH.";
        }
    }

    // converts a day number of a year to a date; rolls over into the next year if needed
    static String toDate(int dn, int y)
    {
        while (dn > (isLeap(y) ? 366 : 365))
        {
            dn = dn - (isLeap(y) ? 366 : 365);
            y++;
        }
        int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (isLeap(y))
            days[1] = 29;
        int m = 0;
        while (dn > days[m])
        {
            dn = dn - days[m];
            m++;
        }
        return dn + suffix(dn) + " " + months[m] + " " + y;
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("DAY NUMBER : ");
        int dn = sc.nextInt();
        System.out.print("YEAR : ");
        int y = sc.nextInt();
        System.out.print("DATE AFTER : ");
        int n = sc.nextInt();

        int maxDays = isLeap(y) ? 366 : 365;
        if (y < 1000 || y > 9999 || dn < 1 || dn > maxDays || n < 1 || n > 100)
        {
            System.out.println("INVALID INPUT");
            return;
        }
        System.out.println();
        System.out.println(toDate(dn, y));
        System.out.println("DATE AFTER " + n + " DAYS :");
        System.out.println(toDate(dn + n, y));
    }
}
Explanation: isLeap() tells whether the year is a leap year (divisible by 400, or by 4 but not by 100), which decides whether February has 28 or 29 days and whether the year has 366 or 365 days. toDate(dn, y) first moves into the next year while the day number is larger than the number of days in the year (needed for the date after N days), then subtracts the days of each month one by one until the remaining number fits in a month; that remainder is the date. suffix() adds ST., ND., RD. or TH. (TH. for 11, 12 and 13). main() accepts the day number (1 to 366), the 4-digit year and N (1 to 100), rejects invalid values, and prints the date for the day number and the date after N more days. Tested with both samples: 233, 2020, 17 gives 20TH. AUGUST 2020 and 6TH. SEPTEMBER 2020; 360, 2020, 45 gives 25TH. DECEMBER 2020 and 8TH. FEBRUARY 2021.
Q215 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

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.
Q315 marks · ProgramOpen: A MOBIUS function M(N) returns the value -1 or 0 or 1 for a natural number (N)…

Write the program described below.

A MOBIUS function $M(N)$ returns the value $-1$ or $0$ or $1$ for a natural number ($N$) by the following conditions are defined: When, $M(N) = 1$ if $N = 1$. $M(N) = 0$ if any prime factor of $N$ is contained more than once. $M(N) = (-1)^P$ if $N$ is the product of ‘P’ distinct prime factors. Write a program to accept a positive natural number ($N$) and display the MOBIUS result with proper message. Design your program which will enable the output in the format given below:

Sample input/output

Sample 1
INPUT: 78
OUTPUT: 78 = 2 x 3 x 13
NUMBER OF DISTINCT PRIME FACTORS = 3
M(78) = -1

Sample 2
INPUT: 34
OUTPUT: 34 = 2 x 17
NUMBER OF DISTINCT PRIME FACTORS = 2
M(34) = 1

Sample 3
INPUT: 12
OUTPUT: 12 = 2 x 2 x 3
DUPLICATE PRIME FACTORS
M(12) = 0

Sample 4
INPUT: 1
OUTPUT: 1 = 1
NO PRIME FACTORS
M(1) = 1
Show answer

Answer

import java.util.Scanner;

public class Mobius
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("INPUT: ");
        int n = sc.nextInt();
        if (n < 1)
        {
            System.out.println("INVALID INPUT");
            return;
        }

        int t = n;
        String expr = "";
        int distinct = 0;
        boolean dup = false;
        int prev = 0;
        for (int i = 2; i <= t; i++)
        {
            while (t % i == 0)
            {
                if (expr.equals(""))
                    expr = "" + i;
                else
                    expr = expr + " x " + i;
                if (i == prev)
                    dup = true;
                else
                    distinct++;
                prev = i;
                t = t / i;
            }
        }

        System.out.println();
        if (n == 1)
        {
            System.out.println("1 = 1");
            System.out.println("NO PRIME FACTORS");
            System.out.println("M(1) = 1");
        }
        else
        {
            System.out.println(n + " = " + expr);
            int m;
            if (dup)
            {
                System.out.println("DUPLICATE PRIME FACTORS");
                m = 0;
            }
            else
            {
                System.out.println("NUMBER OF DISTINCT PRIME FACTORS = " + distinct);
                m = (distinct % 2 == 0) ? 1 : -1;
            }
            System.out.println("M(" + n + ") = " + m);
        }
    }
}
Explanation: The number is divided repeatedly by i = 2, 3, 4, ... (a composite i never divides the remaining number because its prime factors have already been removed), so the divisors found are exactly the prime factors in ascending order, and they are joined with ' x ' to print the factorisation. Since equal prime factors come one after the other, a factor equal to the previous one means a prime factor is repeated (dup = true); otherwise the count of distinct prime factors is increased. For N = 1 the special output (NO PRIME FACTORS, M(1) = 1) is printed. If a prime is repeated, M(N) = 0; otherwise M(N) = (-1)^P, which is 1 for even P and -1 for odd P. Tested with all four samples: 78 gives M(78) = -1, 34 gives M(34) = 1, 12 gives DUPLICATE PRIME FACTORS and M(12) = 0, and 1 gives M(1) = 1.

About this specimen paper

The ISC Computer Science 2021 Specimen Paper (Paper 2) is a specimen paper for Class 12: 3 questions in printed order, 30 marks in all, asked from 2 chapters. A specimen paper is the sample question paper CISCE publishes before the exam to show the layout and style of that year’s paper. It is a good guide to the pattern you will meet.

The layout of the current Computer Science paper (sections, marks, choices) is in the paper pattern guide.

By kind of question

KindQuestionsMarks printed*
Program345

By chapter

ChapterQuestionsMarks printed*
Implementation of algorithms to solve problems230
Arrays, Strings115

* Marks are those printed on each question; where a question offers an either-or choice, both options are counted, so the totals can come to more than the paper’s own total.

Common questions

How many questions are in the ISC Computer Science 2021 Specimen Paper (Paper 2)?

3 questions, 30 marks in all, shown in full and in printed order on this page.

Which chapters does the ISC Computer Science 2021 Specimen Paper (Paper 2) ask most about?

By number of questions: Implementation of algorithms to solve problems (2) and Arrays, Strings (1).

Does this page have the answers to the ISC Computer Science 2021 Specimen Paper (Paper 2)?

Tap “Show answer” under a question to see its answer. The download button saves the paper as a PDF, with or without the answers.