Computer IX
Computer IX
(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.
/ %
Returns the quotient of division operation Returns the remainder of division operation
Example: int a = 5 / 2; Here a will get the Example: int b = 5 % 2; Here b will get the
value of 2 which is the quotient of this value of 1 which is the remainder of this
division operation division operation
do-while while
do-while is suitable when we need to while loop is helpful in situations where numbers
display a menu to the user. of iterations is not known.
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
-2
-6
-12
Explanation
i Output
0 0
1 1-1*1=0
2 2 - 2 * 2 = -2
3 3 - 3 * 3 = -6
4 4 - 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 It is the equality operator used to check if a variable
assigning a value to a variable is equal to another variable or literal
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
Output
4
10
11
Explanation
a Sopln(a) Remarks
3 Initial value
4 4 1st Iteration
6 6 3rd Iteration
7 7 4th Iteration
8 8 5th Iteration
9 9 6th Iteration
10 10 7th Iteration
11 11 8th Iteration
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:
import java.util.Scanner;
Output
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;
while (y != 0) {
int t = y;
y = x % y;
x = t;
}
Output
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;
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
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
Output
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
22
333
4444
55555
Example 2:
Input: Type 2 for an inverted triangle
Enter your choice: 2
Sample Output:
55555
4444
333
22
1
import java.util.Scanner;
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;
}
}
}