Sie sind auf Seite 1von 18

A Simple Java Question Paper (Unit III)

Section – A
Answer any four of the following:
1. Write a Program to calculate charges for sending particles when the charges are as
follows for the first 1 KG Rs.15.00, for additional weight, for every 500gm or fraction
thereof: Rs 8.00
Ans
class Prog1
{
static void test(double wt)//user enters the weight in KGs
{
System.out.println(“Parcel Weight is “+wt);
int icharge = 15;
System.out.println(“Initial charge is “+icharge);
int rwt = (int)((wt-1)*1000);
System.out.println(“Remaining weight after deducing 1Kg “+rwt);
int rcharge = (rwt/500)*8;
System.out.println(“Charge excluding fractional part is Rs.”+(icharge+rcharge));
int fcharge = (rwt%500>0)?8:0;
System.out.println(“Charge for fractional part is Rs. “+fcharge);
int tcharge = icharge+rcharge+fcharge;
System.out.println(“Total Charge is Rs. “+tcharge);
}
}
2 . A special two-digit number is such that when the sum of its digits is added to the
product of its digits , the result is equal to original number
Example : 59
5+9 = 14
5*9 = 45
45+14 = 59 Therefore , 59 is a special two digit number
Write a program in java to check whether a number is special two-digit number or not
Ans
class Prog2
{
static void test(int num)//user inputs a two-digit number

1
{
int m = num;
int product = 1;
int sum = 0;
while(m>0)
{
int dig = m%10;
sum = sum+dig;
product = product*dig;
m/=10;
}
int finalsum = sum+product;
if(finalsum==num)
System.out.println(num+” is a special two digit number “);
else
System.out.println(num+ ” is not a special two digit number “);
}
}
3.Write a Java Program to accept a month using Scanner class , and display the number
of days present in that month
Ans :
import java.util.*;
class Prog3
{
static void test()
{
Scanner in = new Scanner(System.in);
System.out.println(“Enter the number of month “);
int choice = in.nextInt();
switch(choice)
{
case 1:
case 3:
case 5:
case 7:

2
case 8:
case 10:
case 12:
System.out.println(“No. of days are 31 days “);
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(“No .days are 30 days “);
break;
case 2:
System.out.println(“No. Of days are 28 or 29 “);
break;
default : System.out.println(“Wrong Choice “);
}
}
}
4. Write a program to display the following pattern :
13579
35791
57813
79135
91357
Ans
class Program4
{
static void teja()
{
for(int i = 1,l=1;i<=5;i++,l+=2)
{
int k = 1;
for(int j = 1,m=l;j<=5;j++,m+=2)
{
if(m>9)

3
{
System.out.print(k+” “);
k+=2;
}
else
if(l==5&&m==9)
{
System.out.print(“8 “);
}
else
System.out.print(m+” “);
}
System.out.println();
}
}
}
5.Write a Program in java to obtain the first eight numbers of the following series :
1,11,111,1111………………
class Prog5
{
static void test()
{
double s=0.0;
int p;
for(int i = 0;i<=7;i++)
{
s=s+Math.pow(10,i);
p = (int)s;
System.out.print(p+” , “);
}
}
}

4
Section – B
Answer any Four
1.Java language provides various ways to get the data value within a program.
Compare the way of using Command line argument with the way of using Input Stream
Ans
1.To take the input through the Input Stream method , ‘java.io’ package should be imported
as the Input Stream is defined in that package whereas no package is needed to be imported
to take the input using Command Line Method
2.To take the input through the Input Stream Method , two classes , namely (i)
InputStreamReader , and (ii) BufferedReader
whereas in Command Line method no class is needed to be declared.
2.Explain the following Math functions with their output by taking -8.76 as input
(i)Math.floor()
Ans : Math.floor(-8.76) returns in a double type value -9.0 , as
-9.0 is the next lowest number to -8.76
(ii)Math.ceil()
Ans:Math.ceil(-8.76) returns in a double type value -7.0 , as
-7.0 is the next highest number to -8.76
3.What is the Syntax for :
(i)for loop
Ans :
for(initial value ; test condition ; step value)
{
Body of the loop
}
(ii)do….while loop
Ans :
Initial value
do
{
body of the loop
step value;
}
while(test condition);

5
4.What are the differences between if …else and switch…case ?
Ans :
1.If…else results in a Boolean type value whereas switch…case returns in a value of int or
char datatypes
2.If..else can also perform tests on Logical operations whereas switch…case can only
perform test for equality
3.If…else Can perform the test for Strings also whereas switch…case cannot perform tests on
String values
4.Default statement is applied in switch..case whereas it is not applied in if…else
5.What are the differences between Entry controlled loop and exit controlled loop ?
Ans :
1.Entry controlled loop is while loop whereas exit controlled loop is do…while loop
2.Do…while loop will execute at least once , even if the condition is not satisfied whereas
while loop will never execute if the condition is not satisfied
3.While loop will check the condition first and then will execute the block whereas
do…while loop will execute the block first and the check the condition

Section – C
Fill in the blanks

1 . _________ is the process in which a program is validated and _________ is the process in
which the errors in the program are removed
Ans : (i)Testing , (ii) Debugging

2.The ___________ is a selective structure where several possible constant values are
checked for specific vale
Ans : Switch case
3.A Java Program executes but doesn’t give the desired output and will not terminate from
the terminal window . Then this is a ______________ error in the program
Ans:Logical Error
4 . A __________ statement in a Java Programming is enclosed in braces
Ans : Compund Statement
5.Generally , for the fixed iterations ________ loop can be used and for the variable iterations
_________ loop can be used.
Ans : (i) for loop , (ii) while loop (or) do-while loop

6
Java Sample Question Paper 2 Solved(ICSE Model)
The paper is divided into two sections. Attempt all questions from Section-A and any four
from Section-B.Theintended marks for questions or parts of questions are given in
brackets[] , Follow Case-sensitivity.
Section – A(40 marks)
Attempt all questions from this Section.
Question – 1
(a)What is the role of keyword ‘void’ in Java in declaring a function ? [2]
Ans : The keyword ‘void’ in java is used to declare functions which do not return any value
of any datatype, ‘void’ does not return any data type.
Example : static void test()
here the method ‘test’ is declared as void and hence it returns no value.
(b) What does token ‘keyword’ refer to, in Java? [2]
Ans : It refers to the special words which are used in Java to perform some
specific task.Every keyword has its own task to be performed, there were many keywords in
Java, which have different built-in functions to be performed, when these keywords are
declared in Java programming, their built-in feature will be performed by them, so these
keywords perform an important role in Java programming
Example : the keyword ‘new’ is used in Java to declare an object of a class or any array ,
with the help of this keyword , the object of that particular class gets stored in the dynamic
memory.
(c) How many kinds of variables are there in Java ? Name them [2]
Ans : There are two types of variables in Java, namely :
1.static variables – These variables are common to all the objects of a class, all the objects of
a class use these variables commonly.
2.non static variables – These variables are not common to all the objects of a same class ,
each object uses these variables seperately.
(d) What is the output of the following code : [2]
char c = ‘A’;short m=26;int n=c+m; System.out.println(n);
Ans : The output of the code will be : 91
The Ascii value of A is 65 , and hence the value of n , will be 65+26 = 91
(e)Write the general syntax of a Single Dimensional Array, and an example of each primitive
datatype? [2]
Ans : Syntax for declaring a Single Dimensional Array :
<data type> <array name> = new <data type>[<size of the array>];

7
Example of each Primitive datatype: int i = 8;
float f = 0.5;
double d = 1.2356;
String st = “Teja”;
char ch = ‘T’;
Question – 2
(a)State the difference between primitive datatypes and user-defined datatype [2]
Ans : The differences are :
1.Primitive datatypes are defined implicitly , they are in-built datatypes whereas user-defined
datatypes are defined by the user during the execution of the program
2.Primitive datatypes are not dependent on any other datatypes whereas user-defined
datatypes are directly or indirectly dependent on Primitive datatypes
Example of primitive datatypes : int,float,double.String,char , etc
Example of user-defined dataypes : class
(b)What are the types of type casting shown by the following and state the outcome result?
[2]
(i)float a = ‘1’+’2′;
(ii)float b = (float)(21/2);
Ans : (i) this is implicit type casting , the outcome will be 99 in float datatype
(ii)this is explicit type casting, the outcome will be 10.5 in float datatype
(c)What are the pre-conditions for binary search to be performed for a Binary search be
performed on a Single Dimensional Array? [2]
Ans : The elements of the array should be in a sorted order i.e., either in ascending order or in
descending order
(d)Explain the terms : (i)Keyword ‘this’ (ii)Exception [2]
Ans : (i)Keyword ‘this’ : It is used in Java programming to reference the data members of the
current object of a class, there may be many objects in a class, and these objects have
different values for their data members, therefore to point out to a particular object , the
keyword ‘this’ is used in Java Programming.
(ii)Exception : The unexpected error which is raised in the program during its execution , is
known as Exception, it can be managed by ‘Exception Handling’ Examples : Number Format
Exception, String Out Of Bounds Exception , etc
(e)Mention any two characteristics of a Constructor ? [2]
Ans : The two characteristics of a Constructor are :
1.It has the same name as that of a class

8
2.It does not return any value of any datatype
Question – 3
(a)If the parameter ‘i’ is given the value of 10, in the following program then what is the
output [4]
class Recursive {
static void printArray(int i){
if(i!=0) printArray(i-1);
System.out.println(“Values are “+i);
}}
Ans : The output of the program will be :
Values are 0
(b)Given the following expressions: A) val=10 B) val==10 [2]
(i)How do they differ
(ii)What will be the result of the two if value of ‘val’ is 7 initially
Ans : (i) The expression in A is an assignment statement ,the value of ‘val’ is assigned as 10
whereas the expression B is a test for equality, the value of ‘val’ is compared to 10.
(ii)When the value of ‘val’ is 7 initially, in A value ‘val’ changes from 7 to 10 and in B , it
will result in false as 10 is not equal to 7
(c)How are primitive datatypes and reference datatypes are passed in Java, while invoking a
function ? [2]
Ans : The primitive and reference types in Java are passed by actual and formal parameters ,
the primitive datatypes are passed by ‘Pass by value’ and and reference types are passed by
‘Pass by Reference’ , in ‘Pass by Value’ the values of the actual parameters does not change
even if there is a change in the formal parameters , whereas in ‘pass By Reference’ , the
values of the actual parameters change with the change in formal parameters.
(d)Write the output of the following program segment : [2]
public static void output() {
String n = “Computer Knowledge”;
String m = “Computer Applications”;
System.out.print(n.substring(0,8).concat(m.substring(9)));
System.out.print(n.endsWith(‘e’)); }
Ans :The output will be :
ComputerApplicationstrue
(e)What are the different types of errors that can take place during the execution of a program
? [2]

9
Ans : The different types of errors that can take place during the execution of a program are :
1.Syntax Error
2.Run-time Error
3.Logical Error
(f)What is delimiter ? Give few Examples [2]
Ans : A delimiter is a special character used to input the values through scanner class
method, it seperates each token of an entered sentence, any text which is after a delimiter will
be considered as a seperate token.The default delimiter in Jva is White space.
Examples of delimiters : Semi colon(;) , Coma(,) , Question mark(?) , Single Quotation(‘) ,
Dot(.) , Not operator(!)
(g)Which OOP principle implements Function Overloading ? Explain [2]
Ans : Polymorphism implements Function Overloading , Polymorphism is the process of
declaring functions with the same name but for different purposes and so is meant by
Function Overloading , therefore , Polymorphism implements Function Overloading
(h) Java accomplishes encapsulation using visibility modes? Name them that are available in
Java [2]
Ans : The different visibility modes available in Java are :
1. Public
2.Private
3.Protected
(i)Explain the term ‘type casting’ ? [2]
Ans : In a mixed arithmetic expression, which contains different types of variables. the result
should be converted to a single data type. the process of converting the datatype of the result
in a mixed arithmetic expression is known as type casting . It is of two types :
1.Implicit type casting. it occurs according to the heirarchy of datatypes
2.Explicit type casting, it occurs on the intervention of the user
Section – B(60 marks)
Attempt any four questions from this section, each question carries 15 marks
Question – 4
Using Scanner class, write a program to accept a sentence and display only ‘Palindrome’
words. A word is said to be palindrome, if it appears to be same after reversing its characters
Sample Input : MOM AND DAD ARE NOT AT HOME
Sample Output : MOM
DAD
Answer :

10
import java.util.*;
class Ques4
{
static void teja()
{
Scanner in = new Scanner(System.in);
System.out.println(“Enter the Sentence : “);
while(in.hasNext())
{
String st = in.next();
int l = st.length();
String rev=””;
for(int i=0;i<l;i++)
{
char ch = st.charAt(i);
rev=ch+rev;
}
if(st.equals(rev))
System.out.println(st);
rev=””;
}
}
}
Question-5
Write a program in Java to initialize and array of 10 distinct names and initialize another
array with their respective telephone numbers. Search for a name by the user , in
the list.If found , display “Search Successful” along with their phone number else print
“Search Unsuccessful, Name not enlisted”
Answer :
import java.util.*;
class Ques5
{
static void teja()
{
Scanner in=new Scanner(System.in);

11
String name[] =
{“Teja”,”Buncy”,”Sidhu”,”Sure”,”Banti”,”Imran”,”Arun”,”Suresh”,”Chaitu”,”Chanti”};
long tel[] =
{9848251,9848874,8143781,8654247,9654782,9654781,8742569,8485214,9854785,847521
5};
System.out.println(“Enter the name to be searched : “);
String st= in.nextLine();
boolean isFnd= false;
for(int i=0;i<name.length;i++)
{
if(name[i].equals(st))
{
System.out.println(“Search Successful “);
System.out.println(“Telephone number is “+tel[i]);
isFnd = true;
break;
}
}
if(!(isFnd))
System.out.println(“Search Unsuccessful , Name not enlisted”);
}
}
Question – 6
Write a program in Java to accept 10 integers in an array . Now display only those numbers
having complete square root
Sample Input : 12 , 45 , 49 , 78 , 64 , 77 , 81 , 99 , 45 , 33
Sample Ouput : 49 , 64 , 81
Answer :
class Ques6
{
static void teja(int [] m)
{
int l =m.length;
for(int i=0;i<l;i++)
{

12
for(int j=0;j<m[i];j++)
{
if(j*j==m[i])
System.out.println(m[i]);
}
}
}
}
Question – 7
Define a class Employee having the following description :
Data members/Instance variables :
pan : to store the personal account number
name : to store the name
tax : to store the annual taxable income
incometax : to store the tax that is calculated
Member Funtions :
Employee : default constructor
void input() : to accept pan number , name and tax
void calc() : to calculate the tax for an employee according to the given conditions :
Total Annual Taxable income
upto 100000 : No tax
From Rs. 100001 to Rs.150000 : 10% of income tax
From Rs.150001 to Rs.250000 : Rs.5000+20% of tax
Above 250000 : Rs.25000+30% of tax
void display() : to output the name, pan number , Taxable Income and income tax
Answer :
import java.util.*;
class Employee
{
Scanner in = new Scanner(System.in);
int pan;
String name;
int tax;
double incometax;
Employee()

13
{
pan=0;
name=””;
tax=0;
incometax=0;
}
void input()
{
System.out.println(“Enter name : “);
String st = in.nextLine();
System.out.println(“Enter Pan : “);
int a = in.nextInt();
System.out.println(“Enter tax : “);
int b = in.nextInt();
name=st;
pan=a;
tax=b;
calc();
}
void calc()
{ int taxPer=0;
if(tax<=100000)
{
taxPer=0;
incometax=0;
}
else
if(tax>=100001&&tax<=150000)
{
taxPer = 10;
incometax=(10.0/100.0*tax);
}
else
if(tax>=150001&&tax<=250000)
{

14
taxPer = 20;
incometax= 5000+(20.0/100.0*tax);
}
else
if(tax>250000)
{
taxPer = 30;
incometax = 25000+(30/100*tax);
}
display();
}
void display()
{
System.out.println(“PAN Number : “+pan);
System.out.println(“Name : “+name);
System.out.println(“Taxable Income : “+tax);
System.out.println(“Tax : “+incometax);
}
}
Question – 8
Write a Program to print the Pascalene triangle as follows :

Answer :
class Pascals_Triangle
{
static void test(int num)
{
int m[] = new int[20];
m[0]=1;
int i,j;

15
for(i = 0;i<num;i++)
{
int a = m[i];
for(j=0;j<=i;j++)
System.out.print(m[j]+ ” “);
System.out.println();
for(j = i+1;j>0;j–)
m[j]=m[j]+m[j-1];
}
}
}
Question- 9
In an online competitive exam , a set of ‘N’ number of questions result in true or false, If
other than true or false are entered , answer will not be accepted and the question will be
repeated until either true or false is entered for that question
Using scanner class accept the no. of questions to be asked . Accept the answer for each
question, and print the frequency of true and that of false using Printwriter class
Answer :
import java.util.*;
import java.io.*;
class Ques9
{
static int k;
static String input()
{
Scanner in = new Scanner(System.in);
System.out.println(“Enter the answer for “+k+” question “);
String st = in.nextLine();
return st;
}
static void main()
{
PrintWriter pw = new PrintWriter(System.out,true);
k=1;
Scanner in = new Scanner(System.in);

16
pw.println(“Enter no. of questions to be asked “);
int n = in.nextInt();
int a=0;
int b=0;
for(int i=1;i<=n;i++)
{
String st = input();
if(!(st.equals(“true”)||st.equals(“false”)))
{
pw.println(“Enter either true or false only “);
st = input();
}
if(st.equals(“true”))
a++;
else
if(st.equals(“false”))
b++;
k++;
}
pw.println(“Frequency of true : “+a);
pw.println(“Frequency of false : “+b);
}
}
Advertisements
Report this ad
Report this ad
Share this:
 Twitter
 Facebook1
 Google

3 Comments

1.
Pragathi
OCTOBER 29, 2014 — 4:15 PM

17
REPLY
3rd blank ans is logical nah??
Like

o
tejabluej (Post author)
OCTOBER 31, 2014 — 1:40 PM
REPLY
ya..
Liked by 1 person
1 Pingback
1. Simple Java Question Paper with answers « bluejforicse
Leave a Reply

Create a free website or blog at WordPress.com.

18

Das könnte Ihnen auch gefallen