KnowledgeBoat Logo

Sample Paper

Model Sample Paper 2 Solution

Class 9 - APC Understanding Computer Applications with BlueJ


Section A

Question 1

(a)
(i) An object is also called an instance of a class.
(ii) A constant which gives the exact representation of data is called literal

(b) What is the difference between / and % operator? Give suitable examples.

Division operatorModulus operator
Returns the quotient of division operationReturns the remainder of division operation
Example: int a = 5 / 2; Here a will get the value of 2 which is the quotient of this division operationExample: int b = 5 % 2; Here b will get the value of 1 which is the remainder of this division operation

(c) Give two differences between do-while and while loop.

do-whilewhile
do-while is an exit-controlled loopwhile is an entry-controlled loop
do-while is suitable when we need to display a menu to the user.while loop is helpful in situations where numbers of iterations is not known.

(d) What does the following statement mean?
      Employee staff = new Employee();
This statement calls the default constructor of Employee class to create its object and assigns it to the variable staff which is of type Employee.

(e) What do you understand by primitive and non-primitive data types? Give an example of each.
A primitive data type is predefined by the Java language and is named by a reserved word. Java defines 8 primitive data types — byte, short, int, long, float, double, char, boolean.
For example,
int a = 10; — Here, data type of variable a is the primitive type int.
A number of primitive data types are used together to represent a non-primitive data type. Array, Class and interface are examples of non-primitive data type.
For example,
int arr[] = {1, 2, 3, 4}; — Here, data type of variable arr is an int array which is a reference type.

Question 2

(a) What is Object Oriented Programming (OOP)? Name two OOP languages.
Object Oriented Programming is a way of writing computer programs which is using the idea of objects to represent data and methods. These objects talk to each other changing the data in those objects through the methods provided by the object. Java and C++ are two OOP languages.

(b) Why is class called an object factory? Explain.
A class has the complete description of the data elements the object will contain, the methods the object can do, the way these data elements and methods can be accessed. Given the values of the characteristics, a class knows how to create an object of itself. This is similar to what happens in a factory. Take the example of a car factory, there will be one design of a car based on which many cars of different colours will be manufactured. We can consider design of the car as class and the different coloured cars as objects. Hence class is called an object factory.

(c) What is use of the keyword 'import' in java programming?
import keyword is used to import built-in and user-defined packages into our Java program.

(d) What is the purpose of default statement in a switch statement?
When none of the case values are equal to the expression of switch statement then default case is executed.

(e) Why is it must to use 'break' after each case in a switch statement?
Omitting break statement after a case leads to program execution continuing into the next case and onwards till a break statement is encountered or end of switch is reached. To avoid this, we must use break after each case in a switch statement.

Question 3

(a) What is for loop? Explain the construct with an example.
for loop is an entry-controlled loop. Below is an example of for loop:

for (int i = 1; i <= 12; i++) {
    int a = 2 * i;
    System.out.println("2 x " + i + "\t= " + a);           
}

This for loop prints the table of 2 till 12. int i = 1 is the initialization part of the for loop, it is executed only once when the loop gets executed for the first time. i <= 12 is the condition part of the for loop, it is executed before the start of each iteration. Loop iterates as long as this condition remains true. Once it becomes false, execution of the loop is stopped. i++ is the update part of the for loop. It is executed at the end of each iteration.

(b) Distinguish between a continuous loop and a step loop
In a continuous loop, loop control variable is updated by 1 in each iteration whereas in a step loop, loop control variable is updated by a given value (other than 1) in each iteration.

(c) Predict the output and the number of times the loop runs:

class Test {
    public static void main(String args[]) {
        int i;
        for(i=0;i<5;i++)
            System.out.println(i-i*i);
    }
}
Output
0
0
-2
-6
-12

Loop executes 5 times

Explanation
iOutput
00
11 - 1 * 1 = 0
22 - 2 * 2 = -2
33 - 3 * 3 = -6
44 - 4 * 4 = -12

Question 4

(a) Write Java expression for :
(i) m = 2÷3(a2 - b2)h
double m = 2 / 3 * (a * a - b * b) * h
(ii) f = 3uv÷(u+v)
double f = 3 * u * v / (u + v)

(b) State the difference between = and ==

===
It is the assignment operator used for assigning a value to a variableIt is the equality operator used to check if a variable is equal to another variable or literal
E.g. int a = 10; assigns 10 to variable aE.g. if (a == 10) checks if variable a is equal to 10 or not

(c) Give the output of the snippet:

int a=10,b=12;
if(a>=10)
a++;
else
++b;
System.out.println(a + "and" +b);
Output
11and12
Explanation

As a is 10, the condition if(a>=10) tests true, a++ is executed incrementing the value of a to 11. System.out.println(a + "and" +b); prints 11and12

(d) Predict the output:

int a=6,b=5,c;
c = (a++ % b++) *a + ++a*b++;
Output
c = 55
Explanation

c = (a++ % b++) *a + ++a*b++
c = (6 % 5) * 7 + 8 * 6
c = 1 * 7 + 8 * 6
c = 7 + 48
c = 55

(e) Give the output of the snippet:

int a = 3;
while (a<=10)
{
a++;
if(a== 5)
continue;
System.out.println(a);
}
Output
4
6
7
8
9
10
11
Explanation
aSopln(a)Remarks
3Initial value
441st Iteration
5Not executed due to continue2nd Iteration
663rd Iteration
774th Iteration
885th Iteration
996th Iteration
10107th Iteration
11118th Iteration
  Loop stops as a is greater than 10

Section B

Question 5

Write a program to accept marks in Physics, Chemistry and Biology. The program calculates the average and displays the stream accordingly:

Average MarksStream
80% and aboveComputer Science
60% or more but less than 80%Bio-Science
40% or more but less than 60%Commerce
import java.util.Scanner;

public class KboatAvgNStream
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter marks in Physics: ");
        int phy = in.nextInt();
        System.out.print("Enter marks in Chemistry: ");
        int chem = in.nextInt();
        System.out.print("Enter marks in Biology: ");
        int bio = in.nextInt();
        double avg = (phy + chem + bio) / 3.0;
        if (avg < 40)
            System.out.println("No stream");
        else if (avg < 60)
            System.out.println("Commerce");
        else if (avg < 80)
            System.out.println("Bio-Science");
        else
            System.out.println("Computer Science");
    }
}
Output
BlueJ output of KboatAvgNStream.java

Question 6

Write a program to accept any two numbers and print the 'GCD' of them. The GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the larger number by the smaller, the remainder then divides the previous divisor. The process is repeated till the remainder is zero. The divisor then results the GCD.
Sample Input: 25 35
Sample Output: GCD of the numbers 25 and 35 is 5.

import java.util.Scanner;

public class KboatGCD
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int x = in.nextInt();
        System.out.print("Enter second number: ");
        int y = in.nextInt();
        
        while (y != 0) {
            int t = y;
            y = x % y;
            x = t;
        }
        
        System.out.println("GCD = " + x);
    }
}
Output
BlueJ output of KboatGCD.java

Question 7

Write a program to accept a number from the user and check whether it is a Palindrome number or not. A number is a Palindrome which when reads in reverse order is same as in the right order.
Sample Input: 242
Sample Output: A Palindrome number
Sample Input: 467
Sample Output: Not a Palindrome number

import java.util.Scanner;

public class KboatPalindromeNumber
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number: ");
        int num = in.nextInt();
        int copyNum = num;
        int revNum = 0;

        while(copyNum != 0) {
            int digit = copyNum % 10;
            copyNum /= 10;
            revNum = revNum * 10 + digit;
        }

        if (revNum == num) 
            System.out.println("A Palindrome number");
        else
            System.out.println("Not a Palindrome number");
    }
}
Output
BlueJ output of KboatPalindromeNumber.java
BlueJ output of KboatPalindromeNumber.java

Question 8

Write a program to find the sum of the given series:
(a) S = 1 + (1*2) + (1*2*3) + --------- to 10 terms.

public class KboatSeriesSum
{
    public static void main(String args[]) {
        int sum = 0;
        for (int i = 1; i <= 10; i++) {
            int p = 1;
            for (int j = 1; j <= i; j++)
                p *= j;
            sum += p;
        }
        System.out.println("Sum = " + sum);
    }
}
Output
BlueJ output of KboatSeriesSum.java

(b) S = (2/3) + (4/5) + (8/7) + --------- to n terms.

import java.util.Scanner;

public class KboatSeriesSum
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = in.nextInt();
        double sum = 0.0;
        for (int i = 1, j = 3; i <= n; i++, j+= 2)
            sum += Math.pow(2, i) / j;
        System.out.println("Sum = " + sum);
    }
}
Output
BlueJ output of KboatSeriesSum.java

Question 9

Write a program to generate a triangle or an inverted triangle based upon user's choice of triangle to be displayed.

Example 1:
Input: Type 1 for a triangle
Enter your choice: 1
Sample Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Example 2:
Input: Type 2 for an inverted triangle
Enter your choice: 2
Sample Output:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

import java.util.Scanner;

public class KboatPattern
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type 1 for a triangle");
        System.out.println("Type 2 for an inverted triangle");
        
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        
        switch (ch) {
            case 1:
            for (int i = 1; i <= 5; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(i + " ");
                }
                System.out.println();
            }
            break;
            
            case 2:
            for (int i = 5; i > 0; i--) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(i + " ");
                }
                System.out.println();
            }
            break;
            
            default:
            System.out.println("Incorrect Choice");
            break;
        }
    }
}
Output
BlueJ output of KboatPattern.java
BlueJ output of KboatPattern.java
PrevNext