Computer HY Solution IX
Computer HY Solution IX
Computer HY Solution IX
SOLUTION
Section A[40 marks]
Question 1. [1x20=20]
(i) The OOP principle by which an object acquires the properties of another object is called
(a) polymorphism (b) inheritance (c) abstraction (d) classification
(ii) _______ is an instance of a class.
(a) Object (b) Method (c) Function (d) Data member
(iii) How many primitive data types are there in Java?
(a) 6 (b) 7 (c) 8 (d) 9
(iv) If int p=24, q=8, what value will be stored in r, where r=p%q?
(a) 3 (b) 3.0 (c) 192 (d) 0
(v) Given int a=5, b=9. Then a+= a++ - ++b +a will give the result as
(a) -10 (b) 1 (c) 6 (d) -6
(vi) Which of the following is the Java expression for the given mathematical operation?
z=x3+ y3 - xy
x-y
(a) z=x*x*x + y*y*y – (x*y)/(x-y) (b) z=x+x+x*y+y+y – (x*y)/x-y
(c) z=x*x*x + y*y*y – x*y/x-y (d) z=(x*x*x + y*y*y – x*y)/x-y
(vii) Which of the following is the correct syntax of the given Java statement?
if(!(2a>a+b));
(a) if(2*a>a+b) (b) if(!(2*a>a+b)) (c) if(2*a>a+b); (d) if(not(2*a>a+b))
(viii) The size of float type of data in terms of bytes is ____
(a) 1 byte (b) 2 bytes (c) 3 bytes (d) 4 bytes
(ix) What is the default java package imported in a program?
(a) java.io (b) java.math (c) java.lang (d) java.util
(x) What is the entry point of a program in Java?
(a) main( ) (b) first line of code (c) main class (d) last line of code
(xi) The syntax to create an object Lotus of the class Flower is ______.
(a) Flower Lotus=new( ); (b) Flower Lotus=new Flower( );
(c) Flower Lotus=new Flower; (d) Flower Lotus=new Flower( )
(xii) A class is a ____ data type.
(a) primitive (b) composite (c) user-defined (d) both (b) and (c)
(xiii) A set of characters is assigned to ____
(a) String variable (b) static variable (c) boolean variable (d) char variable
(xiv) Two arithmetic expression can be compared with if statement using _____ operator.
(a) arithmetic (b) relational (c) logical (d) ternary
(xv) In a switch-case structure, ____ is optional.
(a) case (b) default (c) switch (d) switch variable
(xvi) Which of the following is not a keyword?
(a) false (b) void (c) or (d) class
(xvii) Which of the following is implied from the given format?
variable=(condition)?expression1:expression2;
(a) expression1 will be evaluated if the condition is true
(b) expression2 will be evaluated if the condition is true
(c) both the expressions will be evaluated if the condition is true
(d) both the expressions will be evaluated irrespective of the condition
(xviii) When the condition of an if-statement is false, which of the following is true?
(a) if block is executed (b) else block is executed
(c) Both if and else blocks are skipped (d) Both if and else blocks are executed
(xix) What is the output of the following Java code?
if(1)
System.out.println("OK");
(a) ok (b) no output (c) compiler error (d) runtime error
(xx) In a Java program, multiline comment is created using
(a) // (b) /* */ (c) <!----> (d) “ ”
Question 2. [2x10=20]
a) Define:
(i) literals
(ii) bytecode
- (i) Literals are fixed values(constants) used in a Java program.
(ii) Bytecode is the intermediate representation of a Java program after compilation. (When a
Java program is compiled, bytecode is generated in the form of a . class file.)
b) Using an object, sc, of the Scanner class name the method to accept:
(i) an integer from the console (from the user)
(ii) a character from the console (from the user)
- (i) sc.nextInt() (ii) sc.next().charAt(0)
c) Write two ways in which encapsulation is helpful in Object Oriented Programming.
- (i) The source code of an object could be maintained independently.
(ii) The object maintains the privacy of the data members.
d) What are the types of conversions shown by the following examples?
(i) char c=(char)120;
(ii) int x= ‘t’;
- (i) Explicit type conversion (ii) Implicit type conversion
e) Name the type of error in each case given below:
(i) float x;y
(ii) System.out.println(10/0);
- (i) Syntax error
(ii) Runtime error
Question 5.
a) Write a program in Java that accepts the seconds as input from the user and converts them into
the corresponding number of hours, minutes and seconds. A sample output is shown below:
Enter Total Seconds: 5000
1 Hour(s) 23 Minute(s) 20 Second(s) [8]
Solution:
import java.util.Scanner;
class timeConvert
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter time in seconds: ");
long secs = in.nextLong();
long hrs = secs / 3600;
secs %= 3600;
long mins = secs / 60;
secs %= 60;
System.out.println(hrs + " Hour(s) " + mins + " Minute(s) " + secs + " Second(s)");
}
}
b) Write a program in Java that uses the given values to calculate the Simple Interest and the
Total Amount: (No input required from the user)
Principle Amount = Rs. 20000, Rate = 8.9%, Time = 3 years [7]
Solution:
class SI
{
public static void main(String args[])
{
int p = 20000;
double r = 8.9;
int t = 3;
double si = p * r * t / 100.0;
double amt = p + si;
System.out.println("Simple Interest = " + si);
System.out.println("Total Amount = " + amt);
}
}
Question 6.
Write a program to input the cost price and the selling price of an article. If the selling price is more
than the cost price then calculate and display actual profit and profit per cent otherwise, calculate and
display actual loss and loss per cent. If the cost price and the selling price are equal, the program
displays the message 'Neither profit nor loss'. [15]
Solution:
import java.util.*;
class profit_loss
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter cost price of the article: ");
double cp = in.nextDouble();
System.out.print("Enter selling price of the article: ");
double sp = in.nextDouble();
double pl=0, percent=0;
if (sp > cp)
{
pl = sp - cp;
percent = pl / cp * 100;
System.out.println("Profit = " + pl);
System.out.println("Profit % = " + percent);
}
else if (sp < cp)
{
pl = cp - sp;
percent = pl / cp * 100;
System.out.println("Loss = " + pl);
System.out.println("Loss % = " + percent);
}
else
{
System.out.println("Neither profit nor loss");
}
}
}
Question 7.
A showroom has announced festival discounts and the gifts on the purchase of items, based on the
total cost as given below:
Write a program to input the total cost. Compute and display the amount to be paid by the customer
along with the gift. [15]
Solution:
import java.util.Scanner;
class Discount
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter total cost: ");
double cost = in.nextDouble();
String gift;
double amt;
if (cost <= 2000.0)
{
amt = cost - (cost * 5 / 100);
gift = "Calculator";
}
else if (cost <= 5000.0)
{
amt = cost - (cost * 10 / 100);
gift = "School Bag";
}
else if (cost <= 10000.0)
{
amt = cost - (cost * 15 / 100);
gift = "Wall Clock";
}
else
{
amt = cost - (cost * 20 / 100);
gift = "Wrist Watch";
}
System.out.println("Amount to be paid: " + amt);
System.out.println("Gift: " + gift);
}
}
Question 8.
The equivalent resistance of series and parallel connections of two resistances are given by the
formula:
(a) R1 = r1 + r2 (Series)
(b) R2 = (r1 * r2) / (r1 + r2) (Parallel)
Using a switch case statement to choose between series and parallel connections from the user, write a
program to enter the value of r1 and r2 and calculate the equivalent resistances accordingly. Display
the calculated equivalent resistance. [15]
Solution:
import java.util.Scanner;
class resistance
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("1. Series");
System.out.println("2. Parallel");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
boolean isChoiceValid = true;
System.out.print("Enter r1: ");
double r1 = in.nextDouble();
System.out.print("Enter r2: ");
double r2 = in.nextDouble();
double eqr = 0.0;
switch (choice) {
case 1:
eqr = r1 + r2;
System.out.println("Equivalent resistance = " + eqr);
break;
case 2:
eqr = (r1 * r2) / (r1 + r2);
System.out.println("Equivalent resistance = " + eqr);
break;
default:
System.out.println("Incorrect choice");
}
}
}
Question 9.
a) Write a program to input two unequal numbers. Display the numbers after swapping their
values in the variables using a third variable.
Sample Input: a = 23, b = 56
Sample Output: a = 56, b = 23 [8]
Solution:
import java.util.*;
class swap
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter two unequal numbers");
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
int c;
c = a;
a = b;
b = c;
System.out.println("a = " + a + " b = " + b);
}
}
b) Write a program to accept a number and check whether the number is positive or negative or
equal to 0. The program displays the message accordingly. [7]
import java.util.Scanner;
class Check
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
if (num>0)
System.out.println("Number is positive");
else if (num<0)
System.out.println("Number is negative");
else
System.out.println("Number is equal to 0");
}
}
Question 10.
Write a program to input three angles of a triangle and check whether a triangle is possible or not. If
possible then check whether it is an acute-angled triangle, right-angled or an obtuse-angled triangle
otherwise, display 'Triangle not possible'. [15]
Solution:
import java.util.Scanner;
class triangle
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter first angle: ");
int a1 = in.nextInt();
System.out.print("Enter second angle: ");
int a2 = in.nextInt();
System.out.print("Enter third angle: ");
int a3 = in.nextInt();
int angleSum = a1 + a2 + a3;
if (angleSum = = 180 && a1 > 0 && a2 > 0 && a3 > 0)
{
if (a1 < 90 && a2 < 90 && a3 < 90)
{
System.out.println("Acute-angled Triangle");
}
else if (a1 = = 90 || a2 = = 90 || a3 = = 90)
{
System.out.println("Right-angled Triangle");
}
else
{
System.out.println("Obtuse-angled Triangle");
}
}
else
{
System.out.println("Triangle not possible");
}
}
}
………