SECTION A
(Attempt all questions.)
Question 1:
a. What is inheritance?
Answer: Inheritance is a process by using which one class acquires the properties (fields and methods) of another class.
b. Name the operators listed below are:
i. <
ii. ++
iii. &&
iv. ? :
Answer:
i. Relational operator
ii. Unary operator
iii. Logical operator
iv. Ternary operator
c. State the number of bytes occupied by char and int data types.
The char data occupies 2 bytes whereas int data type occupies 4 bytes.
d. Write one difference between / and % operators.
Answer: The / operator is used for division whereas the % operator is used to finding the remainder.
e. String x[ ] = {“SAMSUNG”, “NOKIA”, “SONY”, “MICROMAX”, “BLACKBERRY”};
Give the output of the following statements :
i. System.out.println(x[1]);
ii. System.out.println(x[3].length{ )); ,
Answer:
i. NOKIA
ii. 8
Question 2:
a. Name the following :
i. A keyword used to call a package in the program.
ii. Any one reference data type.
Answer:
i. import
ii. Array
b. What are the two ways of invoking functions?
Answer: Following the type to invoke function are:
- call by value
- call by reference
c. State the data type and value of res after the following are executed:
char ch = ‘t’;
res=Character. toUpperCase(ch);
Answer: res is a char type of data variable and it will stores “T”.
d. Give the output of the following program segment and also mention the number of times the loop is executed:
int a,b;
for (a=6, b=4; a<=24; a=a+6)
{
if (a%b==0)
break;
}
System.out.println(a);
Answer: Output is 12. and the loop will run 2 times.
e. Write the output:
charch= 'F';
int m= ch;
m=m+5;
System.out.println(m+ " " +ch);
Answer: Output 75 F
Question 3:
a. Write a Java expression for the following :
ax5 + bx3 + c
Answer: a.Math.pow(x,5) + b.Math.pow(x,3) +c;
What is the value of x1 if x=5?
x1 = + +x – X++ + –x
Answer: The value of x1 = 6.
Why is an object called an instance of a class?
Answer: An object is a software bundle of related states and behavior. A class is a blueprint or prototype from which objects are created. An instance is a single and unique unit of a class.
d. Convert following do-while loop into for loop.
int i = 1;
int d=5;
do {
d=d*2;
System.out.println(d);
i++ ; } while ( i<=5);
Answer:
for(i = 1, d = 5; i <= 5; i++){
d = d * 2;
System.out.println(d);
}
e. Differentiate between constructor and function.
Answer: Constructors must be named with the same name as the class name. They cannot return anything, even void (the object itself is the implicit return).
Functions must be declared to return something, although they can be void.
f. Write the output for the following:
String s="Today is Test" ;
System.out.println(s.indexOf('T'));
System.out.println(s.substring(0,7) + " " +"Holiday");
Answer: 0
Today i Holiday
g. What are the values stored in variables r1 and r2:
i. double r1=Math.abs(Math.min(-2.83,-5.83));
ii. double r2=Math.sqrt(Math.floor(16.3));
Answer:
i. r1 = 5.83
ii. r2 = 4.0
h. Give the output of the following code:
String A = “26”, B=”100″;
String D =A+B+”200″;
int x = Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x+y;
System.out.println(“Result 1 = ”+D);
System.out.prinln(“Result 2 = “+d); ,
Answer: Result 1 = 26100200
Result 2 = 126
i. Analyze the given program segment and answer the following questions :
for(int i = 3; i <= 4; i++) {
for(int j = 2; j <i; j++) {
System.out.print(""); }
System.out.println("WIN"); }
i. How many times does the inner loop execute?
ii. Write the output of the program segment.
Answer: i. 3 times
ii. WIN
WIN
j. What is the difference between the Scanner class functions next() and
nextLine()?
Answer: next ( ) can read the input only till space. It cannot read two words separated by space. Also, next( ) places the cursor in the same line after reading the input.
nextLine( ) reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine( ) positions the cursor in the next line.
Section B
Question 4:
Define a class ElectricBill with the following specifications :
class: ElectricBill
Instance variables /data member :
String n – to store the name of the customer
int units – to store the number of units consumed
double bill – to store the amount to be paid
Member methods :
void accept ( ) – to accept the name of the customer and number of units consumed.
void calculate ( ) – to calculate the bill as per the following tariff :
Number of units | Rate per unit |
First 100 units | Rs. 2.00 |
Next 200 units | Rs. 3.00 |
Above 300 units | Rs. 4.00 |
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
void display( ) — To print the details as follows:
Name of the customer:………………………..
Name of the unit consumed:………………….
Bill amount:……………………….
Write a main method to create an object of the class and call the above member methods.
import java.util.*;
class ElectricBill
{
String n;
int units;
double bill ;
void accept()
{
Scanner in = new Scanner(System.in);
System.out.print("Name of the customer = ");
n = in.nextLine();
System.out.print( "Number of units consumed = ");
units=in.nextInt();
}
void calculate()
{
if(units <= 100)
bill = 2.00*units;
if(units<=300)
bill = 3.00*units;
if (units > 300)
bill = 5.00*units;
}
void print()
{
System.out.println("Naine of the customer:" +n);
System.out.println("Number of units consumed:" +units);
System.out.println("Bill amount:" + bill);
}
public static void main(String args[])
{
ElectricBill eb = new ElectricBill( );
eb. accept();
eb.calculate();
eb.print();
}
}
Output:
Name of the customer = Manoj Mahato
Number of units consumed = 560
Naine of the customer:Manoj Mahato
Number of units consumed:560
Bill amount:2800.0
Question 5: Write a program to accept a number and check and display whether it is a spy number or not. (A number is a spy if the sum of its digits equals the product of its digits.)
Example: consider the number 1124,
Sum of the digits = l + l+ 2 + 4 = 8
Product of the digits = 1×1 x2x4 = 8
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print ("Enter the number: ");
int n = in.nextInt ();
int s,p,no,d;
s=0;
p=1;
no=n;
while(n != 0)
{
d = n % 10;
s=s+d;
p=p*d;
n = n/10;
}
if(s==p)
System.out.print ("Spy No : " + no);
else
System.out.print (" Not Spy No : " + no);
}
}
Output:
Enter the number: 1124
Spy No : 1124
Question 6:
Using switch statement, write a menu driven program for the following:
(i) To find and display the sum of the series given below:
𝑆 = 𝑥1 – x2 +x3– x4 +x5……………-x20
(where x = 2)
(ii) To display the following series:
1 11 111 1111 11111
For an incorrect option, an appropriate error message should be displayed.
import java.util.*;
class Main
{
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
int i,j, x,ch,p;
double s;
System.out.println("l-Sum of Series:");
System.out.println("2-Display Special Series:");
System.out.println("Enter your choice:");
ch = in.nextInt();
switch(ch)
{
case 1:
x=2;
s=0;
for(i=1;i<=20;i++)
{
if(i%2==0)
s=s-Math.pow(x,i);
else
s=s+Math.pow(x,i);
}
System.out.println("Sum Of Series : "+s);
break;
case 2:
p=0;
for(i=1;i<=5;i++)
{
p=p*10+1;
System.out.print(p+" ");
}
break;
default:
System.out.println("Wrong Choice !!!!");
}
}
}
Output:
l-Sum of Series:
2-Display Special Series:
Enter your choice:
1
Sum Of Series : -699050.0
l-Sum of Series:
2-Display Special Series:
Enter your choice:
2
1 11 111 1111 11111
Write a program to input integer elements into an array of size 20 and perform the
following operations:
(i) Display largest number from the array.
(ii) Display smallest number from the array.
(iii) Display sum of all the elements of the array
Answer:
import java.util.*;
class Main
{
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
int i,j, h,l,s,n;
System.out.println("Enter The No Of Element in Array");
n = in.nextInt();
int arr[] = new int[n];
System.out.println("Enter The Element in Array");
for (i=0;i<n;i++)
arr[i]= in.nextInt();
s=0;
l=arr[0];
h=arr[0];
for (i=1;i<n;i++)
{
if (arr[i]>h)
h=arr[i];
if(arr[i]<l)
l=arr[i];
s=s+arr[i];
}
System.out.println("Largest No In Array : "+h);
System.out.println("Smallest No In Array : "+l);
System.out.println("Sum Of The Array : "+s);
}
}
Output:
Enter The No Of Element in Array
5
Enter The Element in Array
75
95
10
-900
63
Largest No In Array : 95
Smallest No In Array : -900
Sum Of The Array : -732
Design a class to overload a function check ( ) as follows :
(i) void check (String str, char ch) – to find and print the frequency of a character in a string.
Example :
Input:
str = “success”
ch = ‘s’ .
Output: number of s present is = 3
(ii) void check(String si) – to display only vowels from string si, after converting it to lower case.
Example:
Input:
s1 = “computer”
Output: o u e
Answer
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s;
int i,j,l,ch;
System.out.println("::::MENU::::");
System.out.println(" 1. To display ABCD Pattern");
System.out.println(" 2. To display Word Pattern");
System.out.println("Enter your choice:");
ch= sc.nextInt();
switch(ch)
{
case 1:
for (i=65;i<=69;i++)
{
for(j=65;j<=i;j++)
{
System.out.print(((char)j));
}
System.out.println();
}
break;
case 2:
System.out.print("Enter your String:");
s= sc.next();
l=s.length();
for (i = 0; i <=l;i++)
{
System.out.println(s.substring(0,i));
}
break;
default:
System.out.println("Invalid Input");
break;
}
}
}
Output:
::::MENU::::
1. To display ABCD Pattern
2. To display Word Pattern
Enter your choice:
1
A
AB
ABC
ABCD
ABCDE
::::MENU::::
1. To display ABCD Pattern
2. To display Word Pattern
Enter your choice:
2
Enter your String:BLUE
B
BL
BLU
BLUE
Question 9:
Write a program to accept a name and total marks of N number of students in two single subscript array name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N Number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average]
Answer:
import java. util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double average;
System.out.print("Enter number of students:");
int n = sc.nextInt( );
String name[] = new String[n];
int totalmarks[] = new int[n];
double deviation[] = new double[n];
double s = 0;
for (int i = 0; i < n; i++)
{
System.out.print("Enter Name of the Student:");
name[i] = sc.next( );
System.out.print("Enter Marks:");
totalmarks[i] = sc.nextInt();
s = s + totalmarks[i];
}
average = s / n;
System.out.println("The average of the total marks of " +n+" number of students:" +average);
for (int i = 0; i < n; i++) {
deviation[i] = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + "/'s marks with the average:" +deviation[i]);
}
}
}
Output:
Enter number of students:5
Enter Name of the Student:Manoj
Enter Marks:98
Enter Name of the Student:Abhijeet
Enter Marks:98
Enter Name of the Student:Avinash
Enter Marks:93
Enter Name of the Student:Bikash
Enter Marks:94
Enter Name of the Student:Jayshree
Enter Marks:75
The average of the total marks of 5 number of students:91.6
Deviation of Manoj's marks with the average:6.400000000000006
Deviation of Abhijeet's marks with the average:6.400000000000006
Deviation of Avinash's marks with the average:1.4000000000000057
Deviation of Bikash's marks with the average:2.4000000000000057
Deviation of Jayshree's marks with the average:-16.599999999999994