SECTION A(40 MARKS)
Question 1:
a) What is Inheritance ? [2]
Ans. It is process by which objects of one class
acquire the properties of objects of another class. In OOP the concepts of
inheritance provides the idea of re-usability.
b)Name the operators listed below
[2]
i)< ii)
++ iii)&& iv)?:
Ans.
i) Relational Operator ii) Increment
Operator
iii)
Logical Operator iv)
Ternary Operator
c) State the number of bytes
occupied by char and int data types.[2]
Ans.
int 4 bytes.
char 2 byte.
d) Write one difference between /
and % operator. [2]
Ans.
The arithmetical operator / is used to find quotient of two numbers. E.g.: 7/3
is
2.The arithmetical operator % is used to find the remainder. E.g.: 7%3 is 1.
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()); [2]
Ans.
i)NOKIA
ii)
8
Question 2:
a) Name of the following : [2]
i) A keyword used to call a package
in the program.
ii) Any one reference data types.
Ans.
i)import ii) array
b) What are the two ways of
invoking functions? [2]
Ans.
Call by value and call by reference are the two ways of invoking functions.
c) State the data type and value of
res after the following is executed : [2]
char ch=‘t’;
res=Character.toUpperCase(ch);
Ans. Date type of res = char and value of res = T
d) Give the output of the following
program segment and also mention the number of times the loop is executed: [2]
int a,b;
for(a=6,b=4;a<=24;a=a+6)
{
if(a%b==0)
break;
}
System.out.println(a);
Ans.
Output: 12
Loop
is executed- 2 times
e)Write the Output: [2]
char ch= ‘F’;
int m=ch;
m=m+5;
System.out.println(m+ “ “ +ch);
Ans.
Output
75 F
Question 3:
a) Write a Java expression for the
following : [2]
Ans. a*x*x*x*x*x + b*x*x*x + c
OR a*Math.pow(x,5) + b*Math.pow(x,3) + c
b) What is the value of x1 if x=5 ?
[2]
x1=++x – x++ + --x
Ans.
6
c) Why is an object called an
instance of a class? [2]
Ans.
In object-oriented programming (OOP), an instance is a specific realization of
any
object. Formally, "instance" is synonymous with "object" as
they are each a particular value (realization), and these may be called an
instance object; "instance" emphasizes the distinct identity of the
object. The creation of a realized instance is called instantiation. In real-world example
of an object would be ‘Doberman’, which is an instance of a class called ‘Dog’.
d) Convert following do-while loop
into for loop. [2]
int i=1;
int d=5;
do{
d=d*2
System.out.println(d);
i++;
}while(i<=5);
Ans.
int
d=5;
for(int
i=1;i<=5;i++)
{
d=d*2;
System.out.println(d);
}
e) Difference between constructor
and function.
Ans.
i)A constructor has no return type which a method has a return type.
ii)
The name of the constructor should be the same as that of the class while the
name of a method can be any valid identifier.
iii)
A constructor is automatically called upon object creation while methods are
invoked explicitly.
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”);
Ans.
Output:
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));
Ans.
i) 5.83
ii) 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.println(“Result 2=”+d);
Ans.
Output:
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.
Ans.
i) 3 times .
Output:
WIN
WIN
j) What is the difference between
the Scanner class functions next() and nextLine()?
Ans.
next() can read the input only till the space. It can't read two words
separated by space. But nextLine() reads input including space between the
words (that is, it reads till the end of line \n).
SECTION B(60 MARKS)
Question 4. [15]
Define a class Electric Bill with
the following specifications:
class :
Electric Bill
Instance Variable/ 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
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.5.00
A surcharge of 2.5% charged if the
number of units consumed is above 300 units.
Void print()- To print the details
as follows :
Name
of the customer……………………….
Number
of units consumed………………….
Bill
amount…………………………………..
Write a main method to create an
object of the class and call the above member methods.
Ans.
import java.util.*;
class
ElectricBill
{
String n;
int units;
double bill;
void accept()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the
customer name:");
n=sc.nextLine();
System.out.println("Enter the
Number of units consumed:");
units=sc.nextInt();
}
void calculate()
{
if(units<=100)
bill=units*2.00;
else if(units<=200)
bill=(100*2.00)+(units-100)*3.00;
else if(units<=300)
bill=(100*2.00)+(100*3.00)+(units-200)*5.00;
else
bill=(100*2.00)+(100*3.00)+(100*5.00)+(units-300)*2.5;
}
void display()
{
System.out.println("Name 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 ob= new ElectricBill();
ob.accept();
ob.calculate();
ob.display();
}
}
Output:
Enter
the customer name:
shib
shankar
Enter
the Number of units consumed:
250
Name
of the customer:shib shankar
Number
of units consumed:250
Bill
amount:750.0
Question 5. [15]
Write a program to accept a number
and check and display whether it is a spy number or not.
(A number is spy if the sum its
digits equals the product of the digits.)
Example: consider the number 1124 ,
sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 X 1 X 2 X 4=8
Ans.
import
java.util.*;
class
spynumber
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int s=0,m=1,r,n;
System.out.println("Enter the
no");
n=sc.nextInt();
while(n>0)
{
r=n%10;
s=s+r;
m=m*r;
n=n/10;
}
if(s==m)
System.out.println("Spy
number");
else
System.out.println("Not spy
number");
}
}
Output
:
Enter
the no
1124
Spy
number
Question 6. [15]
Using switch statement, write a
menu driven program for the following :
i) To find and display the sum of
the series given below:
S=
(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.
Ans.
import
java.util.*;
class
Question6
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int n;
double s=0;
System.out.println("1.S=
x1-x2+x3-x4....");
System.out.println("2.1 11 111
1111 11111");
System.out.println("Enter your
choice");
n=sc.nextInt();
switch(n)
{
case 1:
int x=2,y=2;
for(int i=1;i<=20;i++)
{
if(y%2==0)
s=s + Math.pow(x,i);
else
s=s - Math.pow(x,i);
y++;
}
System.out.println("S="+s);
break;
case 2:
int d=1,s1=0;
for(int i=1;i<=5;i++)
{
s1=s1*10+d;
System.out.print(s1+"\t");
}
break;
default:
System.out.println("Invalid
Input");
}
}
}
Question7. [15]
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
Ans.
import
java.util.*;
class
Quesion7
{
public static void main(String args[])
{
int a[]=new int[20];
Scanner sc = new Scanner(System.in);
int max,min,s=0;
System.out.println("Enter the 20
numbers");
for(int i=0;i<19;i++)
{
a[i]=sc.nextInt();
}
max=a[0];min=a[0];
for(int i=0;i<19;i++)
{
s=s+a[i];
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
System.out.println("Largest
no="+max);
System.out.println("Smallest
no="+min);
System.out.println("Sum of all
elements="+s);
}}
Question 8:
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 Output
Str= “success” number of s present is=3
ch= ‘s’
ii) void check (String s1) – to display
only the vowels from string s1 , after converting it to lower case.
Example :
Input:
S1= “computer” output: o u e
Ans.
import
java.util.*;
class
Question8
{
void check(String str,char ch)
{
int c=0;
for(int j=0; j<str.length(); j++)
{
char ch2=str.charAt(j);
if(ch==ch2)
c++;
}
if(c!=0)
{
System.out.println("Number of
"+ch+" present is "+c);
}
}
void check(String s1)
{
String s=s1.toLowerCase();
System.out.println("Vowels
are");
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
System.out.print(ch+" ");
}
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String str,s2;char ch;
System.out.print("Enter any
string: ");
str=sc.nextLine();
System.out.print("Enter any char:
");
ch=sc.next().charAt(0);
Question8 ob = new Question8();
ob.check(str,ch);
ob.check(str);
}
}
Output:
Enter
any string: success
Enter
any char: s
Number
of s present is 3
Vowels
are
u
e
Enter
any string: COMPUTER
Enter
any char: M
Number
of M present is 1
Vowels
are
o
u e
Question 9:
Write a program to input forty words in
an array. Arrange these words in descending order of alphabets, using selection
sort technique. Print the sorted array.
Ans.
import
java.util.*;
public
class Question9
{
public static void main(String[] args)
{
String temp;
Scanner s = new Scanner(System.in);
String names[] = new String[40];
Scanner sc = new
Scanner(System.in);
System.out.println("Enter all
the names:");
for(int i = 0; i < 5; i++)
{
names[i] = sc.nextLine();
}
for (int i = 0; i < 5; i++)
{
for (int j = i + 1; j < 5; j++)
{
if
(names[i].compareTo(names[j])<0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in
Sorted Order:");
for (int i = 0; i <5; i++)
{
System.out.print(names[i] +
",");
}
}
}