Q1. (a)
What is meant by precedence of operators? [2 marks]
Ans. Precedence of operators
defines the order of execution of operators within an expression. This order of
execution is same as normal arithmetic. The BODMAS rule is applied in program
also.
(b) What
is a literal? [2 marks]
ANS (b) In any program there are
certain variables which are given value in the source code of the program,
normally defined while declared like int a=4; or String s=”This is a test”;
These values are also known as constant value ( not constant variable) and
these values are known as literal.
Q1. (c)
State the Java concept that is implemented through:
i) A
superclass and a subclass
ii) the
act of representing essential features without including background details
[2
marks]
ANS
i)
Inheritance
ii)
Abstraction
Q1. (d)
Give a difference between constructor and a method. [2 marks]
ANS
Constructor
is also a method but it has the same name as the class while the name of a
method should be different from the class name.
Constructor
does not have any return type, not even void while method must have a return
type.
There is
a concept of default constructor but no default method is supplied by java
compiler.
Constructor
execution is a must while a method may not be invoked in any entire program.
Q1. (e) What are the types of casting shown by the following examples?
i)
double x = 15.2;
int y = (int)x;
ii)
int x = 12;
long y = x;
[2 marks]
ANS
i) Explicit typecast
ii) Implicit typecast
Q2. (a) Name any two wrapper classes. [2 marks]
ANS
Integer, Double
Q2. (b) What is the difference between break and continue statements when
they occur in a loop? [2 marks]
ANS
The break statement causes premature termination of the loop. The break
statement transfers the control out of the loop body and thus stops the loop
iteration.
The continue statement starts premature iteration of the loop by skipping
certain statement or statements of the loop body. In for loop when the continue
statement is encountered by the control, the control goes directly to the
reinitialazation statement skipping the remaining statements of the loop body
and the loop continues to run. In while and do-while, continue statement sends
the control to conditional statement.
Q2. (c) Write statements to show how finding the length of a character
array named ch differs from finding the length of a String object str. [2
marks]
ANS
Length of a character array: ch.length
Length of a String array: str.length()
Q2. (d) Name the Java keyword that:
i) indicates a method has no return type
ii) Stores the address of the currently calling object
[2 marks]
ANS
i) void
ii) this
Q2. (e) What is an exception? [2 marks]
ANS
An exception is the abnormality which occurs during runtime. If any
abnormality occurs during execution of a code, java automatically creates an
exception object and throws it. For example if we write a statement like int
a=6/0; ArithmeticException object will be created by java and.
Q3. (a) Write Java statement to create an object mp4 of class digital. [2
marks]
ANS
digital mp4 = new digital();
Q3. (b) State the values stored in variables str1 and str2
String s1 = "good"; String s2 = "world matters";
String str1 = s2.substring(5).replace('t','n');
String str2 = s1.concat(str1);
[2 marks]
ANS
str1 = ” manners”
str2 = “good manners”
Q3. (c) What does a class encapsulate? [2 marks]
ANS
A class encapsulates the data members and method members. Data members
are covered by the method members so that data members are safe from
unauthorized access.
Q3. (d) Rewrite the following program segment using the if..else
statement.
comm = (sale>15000)?sale*5/100:0;
[2 marks]
ANS
if(sale > 15000)
comm = sale * 5 / 100;
else
comm = 0;
Q3. (e) How many times will the following loop execute? What value will
be returned?
int x = 2, y = 50;
do{
++x;
y-=x++;
}while(x<=10);
return y;
[2 marks]
ANS
The loop will execute 5 times. The value returned will be 15.
Explanation: x increased by 2 each iteration. It starts with 2, then 4,
6, 8, 10. So, totally it executes 5 times. The amount by which y reduces each
iteration is 3, 5, 7, 9, 11, hence the value of y returned is 50-3-5-7-9-11=15.
Q3. (f) What is the data type that the following library functions
return?
i) isWhitespace(char ch)
ii) Math.random()
[2 marks]
ANS
i) boolean
ii) double
Q3. (g) Write a Java expression for ut + ½ ft2 [2 marks]
ANS
u*t + 1.0 / 2.0 * f * t * t
Q3. (h) If int[] n = {1, 2, 3, 5, 7, 9, 13, 16} what are the values of x
and y?
x=Math.pow(n[4],n[2]);
y=Math.sqrt(n[5]+n[7]);
[2 marks]
ANS
x is 343.0 and y is 5.0
Q3. (i) What is the final value of ctr after the iteration process given
below, executes?
int ctr = 0;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j+=2)
++ctr;
[2 marks]
ANS
(i) 15
Explanation: The inner loop executes 3 times for each of the 5 iterations
of the outer loop.
Q3. (j) Name the methods of the Scanner class that:
i) Is used to input an integer from standard input stream
ii) Is used to input a string data from standard input stream
[2 marks]
ANS
i) nextInt()
ii) next() and nextLine()
Program Part
Q4. Define a class called FruitJuice with the following description:
Instance variables/data members:
int product_code – stores the product code number
String flavour – stores the flavor of the juice. (orangle, apple, etc.)
String pack_type – stores the type of packaging (tetra-pack, bottle,
etc.)
int pack_size – stores package size (200ml, 400ml, etc.)
int product_price – stores the price of the product
Member methods:
FruitJuice() – default constructor to initialize integer data members to
zero and string data members to “”
void input() – to input and store the product code, flavor, pack type,
pack size and product price
void discount() – to reduce the product price by 10
void display() – to display the product code, flavor, pack type, pack
size and product price
[15 marks]
import java.io.*;
class FruitJuice
{
BufferedReader br=new BufferedReader (new
InputStreamReader (System.in));
int
product_code;
String
flavour;
String
pack_type;
int
pack_size, product_price;
public
FruitJuice()
{
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
public
void input()throws Exception
{
System.out.println("Enter the product code:");
product_code = Integer.parseInt(br.readLine());
System.out.println("Enter the flavour:");
flavour = br.readLine();
System.out.println("Enter the pack type:");
pack_type = br.readLine();
System.out.println("Enter the pack size:");
pack_size = Integer.parseInt(br.readLine());
System.out.println("Enter the product price:");
product_price = Integer.parseInt(br.readLine());
}
public
void discount()
{
product_price -= 10;
}
public
void display()
{
System.out.println("Product code : " + product_code);
System.out.println("Flavour
: " + flavour);
System.out.println("Pack type
: " + pack_type);
System.out.println("Pack size
: " + pack_size + " ml.");
System.out.println("Product price: Rs." + product_price );
}
public
static void main(String args[])throws Exception
{
FruitJuice obj = new FruitJuice();
obj.input();
System.out.println("Before Discounr.");
obj.display();
obj.discount();
System.out.println("After Discounr.");
obj.display();
}
}
Q5. The International Standard Book Number (ISBN) is a unique numeric
book identifier which is printed on every book. The ISBN is based upon a
10-digit code. The ISBN is legal if 1*digit1 + 2*digit2 + 3*digit3 + 4*digit4 +
5*digit5 + 6*digit6 + 7*digit7 + 8*digit8 + 9*digit9 + 10*digit10 is divisible
by 11.
Example: For an ISBN 1401601499
Sum=1*1 + 2*4 + 0*0 + 4*1 + 5*6 + 6*0 + 7*1 + 8*4 + 9*9 + 10*9 = 253
which is divisible by 11.
Write a program to:
(i) input the ISBN code as a 10-digit number
(ii) If the ISBN is not a 10-digit number, output the message “Illegal
ISBN” and terminate the program
(iii) If the number is 10-digit, extract the digits of the number and
compute the sum as explained above.
If the sum is divisible by 11, output the message “Legal ISBN”. If the
sum is not divisible by 11, output the message “Illegal ISBN”.
[15 marks]
import java.io.*;
class Prog1
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
String str;
int
digit,c=1,x=0,i,len;
char ch;
public void
show()throws Exception
{
System.out.println("Enter the ISBN code:");
str=br.readLine();
len=str.length();
if(len!=10)
{
System.out.println("Invalid input.");
return;
}
for(i=len-1;i>=0;i--)
{
ch=str.charAt(i);
digit=ch-48;
x=x+digit*c;
c++;
}
System.out.println("\nSum="+x);
if(x%11==0)
System.out.println("Leaves no remainder - valid ISBN code.");
else
System.out.println("Leaves remainder - invalid ISBN code.");
}
public static void main(String args[]) throws Exception
{
Prog1 ob=new Prog1();
ob.show();
}
}
Q6. Write a program that encodes a word in Piglatin. To translate word into Piglatin word, convert the
word into uppercase and then place the first vowel of the original word as the
start of the new word along with the remaining alphabets. The alphabets present
before the vowel being shifted towards the end followed by “AY”.
Sample Input(1): London Sample Output(1): ONDONLAY
Sample Input(2): Olympics Sample Output(2): OLYMPICSAY
[15 marks]
import java.io.*;
class Str3
{
String s1,w;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void
takeString() throws Exception
{
System.out.println("Enter the string:");
s1=br.readLine().toUpperCase();
show();
}
private void
show ()
{
int j=0,i;
char ch;
for(i=0;i< s1.length();i++)
{
ch=s1.charAt(i);
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
j=1;
break;
}
if(j==1)
break;
}
if(j==0)
System.out.println("No vowel in the entered string.");
else
{
w=s1.substring(i);
s1=s1.substring(0,i);
w=w+s1+"AY";
System.out.println("Piglatin form="+w);
}
}
public
static void main(String args[])throws Exception
{
Str3
ob=new Str3();
ob.takeString();
}
}
Q7. Write a program to input 10 integer elements in an array and sort
them in descending order using bubble sort technique.
[15 marks]
import java.io.*;
class BubbleSort
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int i,j,temp,arr[],nflag;
public void show() throws Exception
{
System.out.println("How many values to store:");
n=Integer.parseInt(br.readLine());
arr=new int[n];
for(i=0;i< n;i++)
{
System.out.println("Enter value:");
arr[i]=Integer.parseInt(br.readLine());
}
for(i=0;i< n;i++)
{
flag=0;
for(j=0;j< n-i-1;j++)
{
if(arr[j] > arr[j+1])
{
flag=1;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
If(flag==0)
break;
}
System.out.println("Sorted values:");
for(i=0;i< n;i++)
System.out.print(arr[i]+" ");
}
public static void main(String args[]) throws Exception
{
BubbleSort ob=new BubbleSort();
ob.show();
}
}
Q8. Design a class to overload a function series() as follows:
(i) double series(double n) with one double argument and returns the sum
of the series.
sum = 1/1 + 1/2 + 1/3 + … 1/n
(ii) double series(double a, double n) with two double arguments and
returns the sum of the series.
sum = 1/a2 + 4/a5 + 7/a8 + 10/A … to n terms
[15 marks]
import java.io.*;
class Series
{
public double series(int n)
{
double sum = 0.0;
for (int i=1;i<=n;i++)
{
sum=sum+(double)1/i;
}
return sum;
}
public double
series(int a, int n)
{
double
sum = 0.0;
for(int
i=1;i<=n;i++)
{
sum
=sum+1/Math.pow(a,i);
}
return
sum;
}
public
static void main(String[] args) throws Exception
{
int
a,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Series ob=new Series();
System.out.println("Enter the value of
'n':");
n=Integer.parseInt(br.readLine());
System.out.println("Calling the first
overloaded version of series () function:");
System.out.println(ob.series(n));
System.out.println("Enter the value of
'a':");
a=Integer.parseInt(br.readLine());
System.out.println("Enter the value of
'n':");
n=Integer.parseInt(br.readLine());
System.out.println("Calling the second
overloaded version of series () function:");
System.out.println(ob.series(a,n));
}
}
Q9. Using the switch statement, write a menu driven program:
(i) To check and display whether the number input by the user is a composite number or not (A
number is said to be a composite, if it has one or more than one factors
excluding 1 and the number itself).
Example: 4, 6, 8, 9, …
(ii) To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
[15 marks]
import java.io.*;
class Smallest
{
public int
smallDigit(int n)
{
int
small=9;
while(n>0)
{
if(n%10
small=n%10;
n=n/10;
}
return
small;
}
public
boolean isComposite(int n)
{
int i;
for(i=2;i< n;i++)
{
if(n%i==0)
break;
}
if(i==n)
return
false;
else
return
true;
}
public
static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int
n,choice;
Smallest
ob=new Smallest();
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
System.out.println("Enter your choice ( 1 for smallest digit and 2
for composite number:");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
System.out.println("Smallest Digit of "+n+" is="+ob.smallDigit(n));
break;
case 2:
System.out.println(n+" is composite
number="+ob.isComposite(n));
break;
default:
System.out.println("Wrong Choice");
}
}
}
No comments:
Post a Comment