Sample Java Papers Solved
Sample Java Papers Solved
By Teja
https://bluejforicse.wordpress.com/keys/
Section – A
2 . A special two-digit number is such that when the sum of its digits is added to the product of
3.Write a Java Program to accept a month using Scanner class , and display the number of days
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 “);
}
}
}
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)
{
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+” , “);
}
}
}
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
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
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
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
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
The paper is divided into two sections.Attempt all questions from Section-A and any four from
Section-B.The intended marks for questions or parts of questions are given in brackets[] , Follow
Case-sensitivity.
Question – 1
Ans : The keyword ‘void’ in java is used to declare functions which do not return any value of any
here the method ‘test’ is declared as void and hence it returns no value.
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]
2.non static variables – These variables are not common to all the objects of a same class , each
(e)Write the general syntax of a Single Dimensional Array, and an example of each primitive
datatype? [2]
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]
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
(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
Ans : The elements of the array should be in a sorted order i.e., either in ascending order or in
descending order
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’
(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,
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 {
if(i!=0) printArray(i-1);
}}
Values are 0
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
(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
System.out.print(n.substring(0,8).concat(m.substring(9)));
System.out.print(n.endsWith(‘e’)); }
ComputerApplicationstrue
(e)What are the different types of errors that can take place during the execution of a program ?
[2]
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
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
Examples of delimiters : Semi colon(;) , Coma(,) , Question mark(?) , Single Quotation(‘) , Dot(.)
, Not operator(!)
functions with the same name but for different purposes and so is meant by Function Overloading ,
[2]
1. Public
2.Private
3.Protected
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
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
Answer :
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);
String name[] = {“Teja”,”Buncy”,”Sidhu”,”Sure”,”Banti”,”Imran”,”Arun”,”Suresh”,”Chaitu”,”Chanti”};
long tel[] =
{9848251,9848874,8143781,8654247,9654782,9654781,8742569,8485214,9854785,8475215};
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
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++)
{
for(int j=0;j<m[i];j++)
{
if(j*j==m[i])
System.out.println(m[i]);
}
}
}
}
Question – 7
void calc() : to calculate the tax for an employee according to the given conditions :
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()
{
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)
{
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
pascal triangle
Answer :
class Pascals_Triangle
{
static void test(int num)
{
int m[] = new int[20];
m[0]=1;
int i,j;
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
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);
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);
}
}