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

Implementation of algorithms to solve problems - ISC Class 12 Computer Science Questions with Answers

24 past-paper questions on Implementation of algorithms to solve problems from ISC Class 12 Computer Science papers (2026-2018), newest first, in full. Questions 1-20 are on this page, 20 to a page. Tap "Show answer" under a question to see its answer. The syllabus covers Algorithm design, Program implementation in Java and Debugging.

24questions
6years
2026newest
20262 questions
3 marks · Case basedOpen: boolean isNiven(int n) { int sum = 0, temp = ?1?; while (temp > 0) { sum +=…

Fill in the blanks marked ?1?, ?2? ... so that the code works.

boolean isNiven(int n)
{
    int sum = 0, temp = ?1?;
    while (temp > 0)
    {
        sum += ?2?;
        temp /= 10;
    }
    return (?3?);
}
The following function isNiven( ) is a part of some class which is used to check if a given number is a Niven number or not. There are some places in the code marked by ?1? , ?2? , ?3? which may be replaced by a statement / expression so that the function works properly. A Niven number is a number which is exactly divisible by the sum of its digits. Example: 120 = 1+2+0 = 3 and 120 is exactly divisible by 3
(1)[1]
What is the expression or statement at ?1?
(2)[1]
What is the expression or statement at ?2?
(3)[1]
What is the expression or statement at ?3?
Show answer

Answer

Blanks: $n$; $temp \% 10$; $(sum \ne 0\ \&\&\ n \% sum == 0)$

?1? = $n$ — initialise temp with n, the number whose digit sum is to be found. ?2? = $temp \% 10$ — adds the last (units) digit of temp to sum. ?3? = $(sum \ne 0\ \&\&\ n \% sum == 0)$ — true only when n is exactly divisible by the digit sum (and sum is non-zero, to avoid division by zero). (Verified by running the completed function: isNiven(120) returns true, isNiven(123) returns false.)
3 marks · Fill inOpen: The following function isTech( ) is a part of some class which is used to check…

Fill in the blanks marked ?1?, ?2? ... so that the code works.

The following function isTech( ) is a part of some class which is used to check if a given number is a Tech number or not. There are some places in the code marked by ?1? , ?2? , ?3? which may be replaced by a statement / expression so that the function works properly. A number is Tech number if the count of digits is even and the square of the sum of its two equal halves is equal to the number itself. Example: $2025 = 20 + 25 = (45)^2 = 2025$
boolean isTech(int n)
{
    String s = String.valueOf(n);
    int len = ?1?;
    if (len % 2 != 0)
        return ?2?;
    int first = Integer.parseInt(s.substring(0, len / 2));
    int second = Integer.parseInt(s.substring(len / 2));
    int sum = first + second;
    return ?3? == n;
}
Show answer

Answer

Blanks: s.length(); false; sum * sum

?1? = s.length() ?2? = false ?3? = sum * sum
20253 questions
3 marks · Case basedOpen: The following function duck( ) is a part of some class which is used to check…

Fill in the blanks marked ?1?, ?2? ... so that the code works.

The following function duck( ) is a part of some class which is used to check if a given number is a duck number or not. There are some places in the code marked by ?1?, ?2?, ?3? which may be replaced by a statement / expression so that the function works properly. A number is said to be Duck if the digit zero (0) is present in it.
boolean duck(int a)
{ int f=-1;
    if(a==0)
        return true;
    for(int i=a; i!=0;?1?)
    {   int c = i%10;
        if(c==?2?)
            {f=1; break; }
    }
    return (f==?3?)? false:true;
}
(a)[1]
What is the expression or statement at ?1?
(b)[1]
What is the expression or statement at ?2?
(c)[1]
What is the expression or statement at ?3?
Show answer

Answer (a)

Blanks: i = i / 10

?1? = $i = i / 10$ (or $i /= 10$): removes the last digit so that the loop moves to the next digit.

Answer (b)

Blanks: 0

?2? = $0$: the digit $c$ is compared with zero.

Answer (c)

Blanks: -1

?3? = $-1$: if $f$ is still $-1$, no zero digit was found, so false is returned.
3 marks · Case basedOpen: The following is a function of class Armstrong. This recursive function…

Fill in the blanks marked ?1?, ?2? ... so that the code works.

The following is a function of class Armstrong. This recursive function calculates and returns the sum of the cubes of all the digits of num, where num is an integer data member of the class Armstrong. [A number is said to be Armstrong if the sum of the cubes of all its digits is equal to the original number]. There are some places in the code marked by ?1?, ?2?,?3? which may be replaced by a statement/expression so, that the function works properly.
public int sumOfPowers(int num)  
    { 
            if (num == 0)  
                   return ?1?; 
           int digit = ?2?; 
                  return (int) Math.pow(digit, 3) + ?3?; 
    } 
(a)[1]
What is the expression or statement at ?1?
(b)[1]
What is the expression or statement at ?2?
(c)[1]
What is the expression or statement at ?3?
Show answer

Answer (a)

Official answer key

Blanks: 0

?1? = 0

Answer (b)

Official answer key

Blanks: num % 10

?2? = num % 10

Answer (c)

Official answer key

Blanks: sumOfPowers(num / 10)

?3? = sumOfPowers(num / 10)
3 marks · Case basedOpen: The following function getPart( ) is a part of some class which is used to…

Fill in the blanks marked ?1?, ?2? ... so that the code works.

The following function getPart( ) is a part of some class which is used to extract h number of characters starting from index b of a string str. There are some places in the code marked by ?1?,?2?,?3? which must be replaced by an expression / statement so that the function works correctly.
String getPart(String str, int b, int h)
{
    int beg= ?1?;
    int end =b+h-1;
    String g="";
    while(beg<=?2? && beg>=0 && end<str.length() )
    {
        g+=str.charAt(beg);
        ?3?;
    }
    return g;
}
(a)[1]
What is the expression or statement at ?1?
(b)[1]
What is the expression or statement at ?2?
(c)[1]
What is the expression or statement at ?3?
Show answer

Answer (a)

Blanks: b

?1? = $b$: the position of the first character to extract.

Answer (b)

Blanks: end

?2? = $end$: the loop runs while the index does not pass the last required character.

Answer (c)

Blanks: beg++

?3? = $beg++$: moves to the next character.
20242 questions
3 marks · Case basedOpen: The following function is a part of some class which computes and returns the…

Fill in the blanks marked ?1?, ?2? ... so that the code works.

The following function is a part of some class which computes and returns the value of a number ‘p’ raised to the power ‘q’ ($p^q$). There are some places in the code marked by ?1? , ?2? , ?3? which must be replaced by an expression / a statement so that the function works correctly.
double power ( double p , int q )
{   double r = ?1? ;
    int c = ( q<0 ) ? -q : q ;
    if ( q == 0)
        return  1 ;
    else
    {   for (int i = 1; i <= c ;?2?, i++);
        return (q>0)? r : ?3?;
    }
}
(a)[1]
What is the expression or statement at ?1?
(b)[1]
What is the expression or statement at ?2?
(c)[1]
What is the expression or statement at ?3?
Show answer

Answer (a)

Blanks: 1

?1? = 1

Answer (b)

Blanks: r = r * p

?2? = r = r * p

Answer (c)

Blanks: 1 / r

?3? = 1 / r
3 marks · Case basedOpen: int small_dig(int n) { int min = ?1? ; while (n != 0) { int q=n/10; int r = ?2?…

Fill in the blanks marked ?1?, ?2? ... so that the code works.

int small_dig(int n)
{   int min = ?1? ;
    while (n != 0)
    {
        int q=n/10;
        int r = ?2? * 10;
        min = r > min ? ?3? : r;
        n=q;
    }
    return min;
}
The following function is a part of some class which is used to find the smallest digit present in a number. There are some places in the code marked by ?1?, ?2?, ?3? which must be replaced by an expression / a statement so that the function works correctly.
(a)[1]
What is the expression or statement at ?1?
(b)[1]
What is the expression or statement at ?2?
(c)[1]
What is the expression or statement at ?3?
Show answer

Answer (a)

Blanks: 9

?1? = $9$: the largest possible digit, so any digit found is smaller or equal.

Answer (b)

Blanks: n - q

?2? = $n - q$: since $n - q * 10$ gives the last digit (by operator precedence).

Answer (c)

Blanks: min

?3? = $min$: keep the current minimum when the digit is larger.
202210 questions
1 mark · MCQOpen: What is the statement or expression at ?3?

Choose the correct option.

What is the statement or expression at ?3?
  • (i)key
  • (ii)array[j]
  • (iii)i+1
  • (iv)n
Show the code
void insertionSort(int array[])
{
    int n = ?1?;
    for (int j = 1; j < n; j++)
    {
        int key = ?2?;
        int i = j - 1;
        while ((i > -1) && (array[i] > ?3?))
        {
            array[i + 1] = ?4?;
            i--;
        }
        ?5? = key;
    }
}
Show answer

Answer

Correct option: (i)

Answer: (i) key Elements greater than key are shifted right.
1 mark · MCQOpen: What is the statement or expression at ?4?

Choose the correct option.

What is the statement or expression at ?4?
  • (i)j+1
  • (ii)key
  • (iii)array[j]
  • (iv)array[i]
Show answer

Answer

Correct option: (iv)

Answer: (iv) array[i] The larger element array[i] is shifted to array[i+1].
1 mark · MCQOpen: What is the statement or expression at ?1?

Choose the correct option.

What is the statement or expression at ?1?
  • (i)array.length()
  • (ii)array.length
  • (iii)length
  • (iv)-1
Show answer

Answer

Correct option: (ii)

Answer: (ii) array.length The length of an array in Java is the field array.length (no brackets).
1 mark · MCQOpen: What is the statement or expression at ?5?

Choose the correct option.

What is the statement or expression at ?5?
  • (i)array[i+1]
  • (ii)i+1
  • (iii)j
  • (iv)array[i]
Show answer

Answer

Correct option: (i)

Answer: (i) array[i+1] After shifting, key is placed at position i+1.
1 mark · MCQOpen: What is the statement or expression at ?1?

Choose the correct option.

What is the statement or expression at ?1?
  • (i)-1
  • (ii)0
  • (iii)10
  • (iv)2
Show the code
boolean Palindrome(int N)
{
    int rev = ?1?;
    int num = N;
    while (num > 0)
    {
        int f = num / 10;
        int s = ?2?;
        int digit = num - ?3?;
        rev = ?4? + digit;
        num /= ?5?;
    }
    if(rev == N)
        return true;
    else
        return false;
}
Show answer

Answer

Correct option: (ii)

Answer: (ii) 0 rev accumulates the reversed number, so it starts at 0.
1 mark · MCQOpen: What is the statement or expression at ?2?

Choose the correct option.

What is the statement or expression at ?2?
  • (i)s * 10
  • (ii)f / 10
  • (iii)rev
  • (iv)f * 10
Show answer

Answer

Correct option: (iv)

Answer: (iv) f * 10 f = num/10, so f*10 is num with its last digit removed (zeroed).
1 mark · MCQOpen: What is the statement or expression at ?2?

Choose the correct option.

What is the statement or expression at ?2?
  • (i)j
  • (ii)array[j+1]
  • (iii)array[j]
  • (iv)0
Show answer

Answer

Correct option: (iii)

Answer: (iii) array[j] key holds the element being inserted, array[j].
1 mark · MCQOpen: What is the statement or expression at ?3?

Choose the correct option.

What is the statement or expression at ?3?
  • (i)s
  • (ii)rev
  • (iii)f
  • (iv)digit * 10
Show answer

Answer

Correct option: (i)

Answer: (i) s With s = f*10, num - s is the last digit.
1 mark · MCQOpen: What is the statement or expression at ?5?

Choose the correct option.

What is the statement or expression at ?5?
  • (i)1
  • (ii)100
  • (iii)10
  • (iv)rev
Show answer

Answer

Correct option: (iii)

Answer: (iii) 10 num /= 10 removes the last digit each time.
1 mark · MCQOpen: What is the statement or expression at ?4?

Choose the correct option.

What is the statement or expression at ?4?
  • (i)s * 10
  • (ii)rev * 10
  • (iii)f
  • (iv)rev
Show answer

Answer

Correct option: (ii)

Answer: (ii) rev * 10 The reversed number is shifted one place left before adding the new digit: rev = rev*10 + digit.
20212 questions
15 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.
15 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.
20181 question
1 mark · Short answerOpen: What is the statement or expression at ?4?

Fill in the blanks marked ?1?, ?2? ... so that the code works.

What is the statement or expression at ?4?
Show answer

Answer

$?4? = rev*10$ (i.e. $rev = rev*10 + digit$) - the standard technique to build up the reversed number digit by digit.

About Implementation of algorithms to solve problems

Implementation of algorithms to solve problems is a unit of the ISC Class 12 Computer Science syllabus.

The syllabus lists these topics: Algorithm design, Program implementation in Java and Debugging.

Prashnika has 24 past-paper questions on Implementation of algorithms to solve problems, from papers of 2018-2026, asked in 6 different years. By kind: MCQ (10), Case based (6), Short answer (5) and Program (2). They carry between 1 and 15 marks, and most are worth 1 mark.

To practise, use the paper generator, which can build a practice paper from this chapter.

Common questions

What topics are in Implementation of algorithms to solve problems in ISC Class 12 Computer Science?

The syllabus lists Algorithm design, Program implementation in Java and Debugging.

How many questions on Implementation of algorithms to solve problems are in ISC Class 12 Computer Science past papers?

Prashnika has 24 questions on Implementation of algorithms to solve problems, from papers of 2018-2026 (board, specimen, improvement and practice papers together).

What kinds of questions are asked on Implementation of algorithms to solve problems?

By kind: MCQ (10), Case based (6), Short answer (5) and Program (2).

Questions on other pages on Implementation of algorithms to solve problems