SECTION – A (40 Marks)
(a) What is the difference between an object
and a class ? [2]
Ans: Class is the
template and object is the instance of a class. A class defines a blue print
according to which objects are being made. Objects are the real entity.
(b) What does the token
‘keyword’ refer to in the context of
Java? Give an example for keyword. [2]
Ans: There are certain
reserved words in Java language which cannot be used as names for class,
variable or method in a program. Examples of such keyword is ‘int’, ‘break’
etc.
(c) State the difference between entry controlled loop
and exit controlled loop. [2]
Ans: A loop has two components – control statement and body of the loop.
The control statement determines whether the loop will execute or not and body
contains the statements which will be executed. In entry controlled loop,
control statement appears first and then the body while in exit controlled
loop, the body is placed above the control statement. An entry controlled may
not execute for a single time but exit controlled loop will execute for at
least once.
(d) What are the two
ways of invoking functions? [2]
Ans: Functions are invoked
using two techniques – call by value and call by reference. In call by value,
both the actual and formal arguments are variables while in call by reference –
they are objects.
(e) What is difference between / and %
operator? [2]
Ans: ‘/’ is division operator
and when applied on ‘int’ or ‘long’ values it returns the quotient only like
5/2 will yields 2. ‘/’ operator on float or double value returns fractional
value.
‘%’ is known as mod or
modulo operator and it returns the remainder like 5%2 will give 1.
Question 2
(a)
State the total size
in bytes, of the arrays a [4] of char data type and p [4] of float data
type. [2]
Ans: Size of the first array is 4 bytes and size of
the second array is 16 bytes.
(b) (i) Name the package that contains
Scanner class.
Ans: util package contains the Scanner class. ‘util’ package is
contained in java package.
(ii) Which unit
of the class gets called, when the object of the class is created?
[2]
Ans: Constructor of a class
is called when the object of the class is created.
(c) Give the
output of the following: [2]
String n = “Computer
Knowledge”;
String m = “Computer
Applications”;
System.out.println(n.substring
(0,8). concat (m.substring(9)));
Ans: ComputerApplications
System.out.println(n.endsWith(“e”));
Ans: true
(d) Write the output
of the following: [2]
(i)
System.out.println (Character.isUpperCase(‘R’));
Ans: true
(ii)
System.out.println(Character.toUpperCase(‘j’));
Ans: false
(e) What is the
role of keyword void in declaring
functions?
[2]
Ans: Use of void in
function denotes that the function will not return any value to it’s caller and
it will only execute the codes written within it’s body when invoked.
Question 3
(a) Analyse the
following program segment and determine how many times
the loop will be
executed and what will be the output of the program segment ?
[2]
int p = 200;
while(true)
{
if (p<100)
break;
break;
p=p-20;
}
System.out.println(p);
Ans: 80
(b) What will be
the output of the following code? [2]
(i)
int k = 5, j = 9;
k+=k++ – ++j + k;
System.out.println(“k=
” +k);
System.out.println(“j=
” +j);
Ans:
6
10
(ii)
double b = -15.6; [2]
double a = Math.rint
(Math.abs (b));
System.out.println(“a=”
+a);
Ans: 16.0
(c) Explain the
concept of constructor overloading with an example. [2]
In java programming,
in many situations number of constructor are defined within a class and as per
rule of overloading argument list of the constructors will be different.
Constructor overloading is very much essential feature in java.
Suppose we have
defined a class named MyClass and the constructors defined in the class are
MyClass()
{
a=0;
}
And
MyClass(int n)
{
a=n;
}
Then we can say that
class MyClass has two overloaded constructors.
(d) Give the
prototype of a function search which receives a sentence sentnc
and a word wrd and
returns 1 or 0 ? [2]
Ans: int myFunc(String sentnc, String wrd)
(e)
Write an expression in Java for z = (5x3 + 2y ) / (
x + y) [2]
Ans: z=(5*Math.pow(x,3)+2*y)/ (x+y);
(f)
Write a statement each to perform the following task on a string: [2]
(i)
Find and display the position of the last space in a string s.
Ans: int i=s.lastIndexOf(‘ ‘);
(ii)
Convert a number stored in a string variable x to
double data type
Ans: double d=Double.parseDouble(x);
(g)
Name the keyword that: [2]
(i)
informs that an error has occurred in an input/output operation.
Ans: java.lang.IOException
(ii)
distinguishes between instance variable and class variables.
Ans: instance variables are
those variables whose existence comes after creating the object and each copy
of object contains separate copy of instance variable.
Class variables are
static variables and they do not depend on the creation of object and only one
copy of class variable comes into existence for number of objects. All the
objects of a class share the same copy of class variable.
(h)
What are library classes ? Give an example. [2]
Ans: Library classes are
those classes which are already defined by java creators and stored within
packages. Examples of library classes are String, BufferedReader etc.
(i)
Write one difference
between Linear Search and Binary Search . [2]
Ans: Linear search searches each and every element stored in an array
while binary search never searches all the elements in the array.
Programming Part
Question 4.
Define a class called mobike with the following
description: [15]
Instance variables/data members: int bno – to store the bike’s number
int phno – to store the
phone number of the customer
String name – to store the
name of the customer
int days – to store the
number of days the bike is taken on rent
int charge – to calculate
and store the rental charge
Member methods:
void input( ) – to input and
store the detail of the customer.
void computer( ) – to compute the
rental charge
The rent for a mobike is charged on the following basis.
First five
days Rs
500 per day;
Next five days
Rs 400 per day
Rest of the
days Rs 200 per day
void display ( ) – to display the
details in the following format:
Bike No.
PhoneNo.
No. of days Charge
import java.io.*;
class mobike
{
int bno, phno, days, charge;
String name;
BufferedReader br=new
BufferedReader (new InputStreamReader (System.in));
public void input() throws Exception
{
System.out.println("Enter the name of
the customer:");
name=br.readLine();
System.out.println("Enter the Phone No
of the customer:");
phno=Integer.parseInt(br.readLine());
System.out.println("Enter the Bike
No:");
bno=Integer.parseInt(br.readLine());
System.out.println("Bike was taken for
how many days?:");
days=Integer.parseInt(br.readLine());
compute();
}
void compute()
{
if(days>5)
{
charge=500*5;
days=days-5;
}
else
{
charge=500*days;
days=0;
}
if(days>5)
{
charge=charge+400*5;
days=days-5;
}
else if(days>0)
{
charge=400*days;
days=0;
}
if(days>0)
{
charge=charge+200*days;
}
display();
}
void display()
{
System.out.println("Bike
No.\t\tPhoneNo.\t\tNo. of days\t\tCharge");
System.out.println(bno+"\t\t\t\t"+phno+"\t\t\t\t"+days+"\t\t\t\t"+charge);
}
public static void
main(String args[])throws Exception
{
mobike ob=new
mobike();
ob.input();
}
}
Question 5.
Write a program to input
and sort the weight of ten people. Sort and display them in descending
order using the selection sort technique.
[15]
import java.io.*;
class Weight
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int i,j,t;
int arr[]=new int[10];
public void take() throws Exception
{
for(i=0;i< 10;i++)
{
{
System.out.println("Enter
Weight:");
arr[i]=Integer.parseInt(br.readLine());
}
}
void sort()
{
for(i=0;i<10;i++)
{
{
for(j=i+1;j<9;j++)
{
{
if(arr[i]
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
}
void show()
{
System.out.println("Weight in
descending order:");
for(i=0;i<10;i++)
System.out.print(" "+arr[i]);
System.out.print(" "+arr[i]);
}
public static void main(String args[])
throws Exception
{
Weight ob=new Weight();
ob.take();
ob.sort();
ob.show();
}
}
Question 6.
Write a program to
input a number and print whether the number is a special number or not.
(A number is said to be a special number, if the sum of the factorial of
the digits of the number is same as the original number).
[15]
import java.io.*;
class Special
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int i,n,sum=0;
public void take() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
}
void show()
{
for(i=1;i< n;i++)
{
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
System.out.println(n +" is a special
number");
else
System.out.println(n +" is not a special
number");
}
public static void main(String args[])
throws Exception
{
Special ob=new Special ();
ob.take();
ob.show();
Question 7
Write a program to
accept a word and convert it into lowercase if it is in uppercase, and display
the new word by replacing only the vowels
with the character following it. [15]
Example
Sample Input :
computer
Sample Output :
cpmpvtfr
import java.io.*;
class Sent
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String s,s1="";
char ch;
int i,len;
public void take() throws Exception
{
System.out.println("Enter the
word:");
s=br.readLine().toLowerCase();
}
void show()
{
len=s.length();
for(i=0;i< len; i++)
{
{
ch=s.charAt(i);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
ch=(char)(ch+1);
break;
}
s1=s1+ch;
}
System.out.println("Modified
word:"+s1);
}
public static void main(String args[])
throws Exception
{
Sent ob=new Sent();
ob.take();
ob.show();
}
Question 8.
Design a class to
overload a function compare ( ) as follows: [15]
(a) void
compare (int, int) – to compare two integer values and print
the greater of the two integers.
(b) void
compare (char, char) – to compare the numeric value of
two character with higher numeric value
(c) void
compare (String, String) – to compare the length of the
two strings and print the longer of the two.
import java.io.*;
class OverLoad
{
void compare(int x,int y)
{
if(x>y)
System.out.println("Greater value is
:"+x);
else if(y>x)
System.out.println("Greater value is
:"+y);
else
System.out.println("Both the values are
same");
}
void compare(char x,char y)
{
if(x>y)
System.out.println("Greater character
is :"+x);
else if(y>x)
System.out.println("Greater character
is :"+y);
else
System.out.println("Both the characters
are same");
}
void compare(String x,String y)
{
if(x.length()>y.length())
System.out.println("Greater String is
:"+x);
else if(y.length()>x.length())
System.out.println("Greater String is
:"+y);
else
System.out.println("Both the Strings
are of same length");
}
public static void main(String args[])
throws Exception
{
OverLoad ob=new OverLoad ();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String s,s1;
char ch,ch1;
int i,j;
System.out.println("Enter first
number:");
i=Integer.parseInt(br.readLine());
System.out.println("Enter second
number:");
j=Integer.parseInt(br.readLine());
ob.compare(i,j);
System.out.println("Enter first character:");
ch=(char)br.read();
System.out.println("Enter second
character:");
ch1=(char)br.read();
ob.compare(ch,ch1);
System.out.println("Enter first
sentence:");
s=br.readLine();
System.out.println("Enter second
sentence:");
s1=br.readLine();
ob.compare(s,s1);
}
}
Question 9:
Write a menu driven
program to perform the following . (Use switch-case statement) [15]
(a)
To print the series 0, 3, 7, 15, 24 ……. n terms
(value of ‘n’ is to be an input by the user).
(b)
(b)
To find the sum of the series given below:
S = ½ + ¾ + 5/6 + 7/8 ……..
19/20
import java.io.*;
class Over
{
void
series1(int n)
{
int
i,x=0,b=3;
for(i=0; i< n; i++)
{
System.out.print(" "+x);
x=x+b;
b=b+2;
}
}
void series2()
{
int i;
double sum=0;
for(i=1;i < 20; i++)
{
{
sum=sum+(double)i/(i+1);
}
System.out.print("Result= "+sum);
}
public static void main(String args[]) throws Exception
{
Over
ob=new Over();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int
choice,n;
System.out.println("Enter your choice (1 for first series and 2 for
second series):");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
System.out.println("Enter the value of
'n':");
System.out.println("Enter second number:");
n=Integer.parseInt(br.readLine());
ob.series1(n);
break;
case 2:
ob.series2();
break;
default:
System.out.println("Wrong Choice:");
}
}
}
No comments:
Post a Comment