Attempt all questions
Question 1
(a) Name any four tokens of Java. [2]
Answer:
Four tokens in Java are:
1. Literals
2. Keywords
3. Identifiers
4. Operators
(b) Give the difference between actual parameter and formal parameter. [2]
Answer:
Formal parameters are the parameters as they are known in the function definition.
Actual parameters (also known as arguments) are what are passed by the caller.
Formal parameters are used as they are in the function.
The values of actual parameters are copied into formal parameters and then they are used in the function.
void add(int a, int b)//int a, int b are formal parameteres in function definition
{
System.out.println( a+b);
}
add(5, 10);// 5, 10 are actual parameters
int A=5,B=10;
add(A,B);//A,B are actual parameters
The values of formal parameters can be changed by the function, but the values of actual parameters can't be changed.
(c) What is an identifier? [2]
Answer:
Identifiers are the names of variables, methods, classes, packages and interfaces. Unlike literals they are not the things themselves, just ways of referring to them. For example:
int A;//A is an identifier
public class ABC//ABC is an identifier
(d) Write an expression in Java for cos x + sqrt(a2+b2) . [2]
Answer:
Math.cos(x)+Math.sqrt(a*a+b*b);
(e) What is the result produced by 2 – 10*3 + 100/11? Show the steps. [2]
Answer:
2-10*3+100/11
2.0-10.0*3.0+100.0/11.0
2.0-10.0*3.0+9.09090909
2.0-30.0+9.09090909
37.09090909
Question 2
(a) What is the difference between local variable and instance variable? [2]
Answer:
Local variable is declared inside a function or a block wheras instance variable is declared in the global area of a class(outside teh functions, inside the class).
Instance variables belong to the object of the class and can be accessed by all memebers of the class, wheras the scope of local variables is limited to the local block in the function.
instance variables are non static, wheras local variables can be static.
(Note: there is a difference between global and instance variables, don,t get confused)
(b) int x =20, y = 10, z;
What is the value of z in
z = ++x * (y – –) – y ?
Show the steps. [2]
Answer:
z=++x*(y--)-y
z=21*10-y//x=21, y=9
z=210-9
z=201
(c) What is the purpose of default in a switch? [2]
Answer:
Sometimes, in a switch statement, there is no match found is all the 'cases' we have provided. In such a situation, we may need the program to perform a 'default' action which occurs when there is no match found for the variable. default serves the purpose of performing the action when there is no match found.
(d) Give the difference between linear search and binary search. [2]
Answer:
Linear search is the process of searching for a value by comparing it with all values one after another starting with the first value in the array, wheras binary search includes comparing with a mid element and deciding which half of the array the data to be searched lies in.
Linear search does noy require the data ro be sorted, but binary search require the array to be sorted.
The fastest result in linear search is when the element is at the first position in the array, but in the same case binary search gives the slowest result.
Not considering the exceptions, we can say that the linear search technique is slow while the Binary search technique is fast.
(e) What will be the output of the following code?float x = 7.87;
System.out.println(Math.ceil(x);
System.out.println(Math.floor(x);
Answer:
8.00000000
7.00000000
Question 3
(a) State the difference between if-else if ladder and switch...case. [2]
Answer:
In the if-else if ladder, if the condition of an if clause is found true,the statements in the comlplete if block are executed and the corresponding else and remaining if's are not checked is not executed, but in switch...case, if a case matches, then fall-through occurs and all the remaining stetements in th switch statements are executed until a break is encountered or the statement ends.
(b) Explain the concept of constructor overloading with an example. [2]
Answer:
Just like function overloading, we have contructor overloading. A class can have more than one explicit constructors(of course having the same name) having different signatures. When an object of the class is created, the corresponding constructor is called depending upon how the object is created adn which constructor does the call correspond to.
Example
class MyClass
{
int a,b;
public MyClass()//first constructor
{
a=0; b=0;
}
public MyClass(int x, int y)//second constructor
{
a=x;
b=y;
}
}
Now when an object of this class is created:
MyClass obj1=new MyClass();//first constructor called
MyClass obj2=new MyClass(2,3);//Second Constructor called
(c) What will be the output of the following program segments?
(i) String s = “application”;
int p = s.indexOf(‘a’);
System.out.println(p);
System.out.println(p+s); [2]
Answer:
0
0application
(ii) String st = “PROGRAM”;
System.out.println(st.indexOf(st.charAt(4))); [2]
Answer:
2
reason:
st.charAt(4) = 'R'
st.indexOf('R')=2
(iii) int a = 0;
if(a>0 && a<20)
a++;
else a-- ;
System.out.println(a); [2]
Answer:
-1
(iv) int a= 5, b = 2,c;
if (a>b || a ! = b)
c = ++a+--b;
System.out.print(c+ “ ”+a+ “ ”+b); [2]
Answer:
7 6 1
(v) int i = 1;
while(i++<=1)
{
i++;
System.out.print(i + “ ” );
}
System.out.print(i); [2]
Answer:
3 4
reason:
when i++<=1 is checked, first time, it is true, and i becomes 2.
Then inside the while, i becomes 3 and is printed with a space after it.
Then the condition is checked again, which comes out to be false, and i increases to 4.
After the while, i's value is 4 and that is printed.
(d) Differentiate between isUpperCase(char) and toUpperCase(char). [2]
Answer:
isUpperCase(char) returns a boolean value which is true if the char passed to the function is upper case, Wheras toUpperCase(char) converts the argument to UpperCase(if it isinlower case) and returns the Upper Case form.
(e) What is the difference between a constructor function and a member
function of a class? [2]
Answer:
Constructor function has the same name as that of class wheras the member function has a different name.
Constructor function is called only when a new object is created of the class, wheras member functions are called later.
Constructor function has an implicit return type which is the Ocject of its class, wheras the member functions, return types have to be given explicitly.
Constructor can not be static, member functions can be sattic or non-static.
(f) What is the difference between a static member function and a member
function which is not static?
Answer:
A static member function does not require an object of the class to be called. From inside the class, it can be called just by its name, and from outside the class it can be called through class name:
On the other hand a non-static member function can only be called through an object of the class, or from other non-static member function.
A static member function can not access the instance variables of the class without object name, wheras non-static member functions can access the instance variables of the class. These become the variables of the object that is used to call this function.
SECTION B (60 Marks)
Attempt any four questions from this Section.
The answers in this Section should consist of the Programs in either Blue J environment
or any program environment with Java as the base. Each program should be written
using Variable descriptions/Mnemonic Codes such that the logic of the program is
clearly depicted.
Flow-Charts and Algorithms are not required.
Question 4
Define a class taximeter having the following description:
Data members/instance variables
int taxino - to store taxi number
String name - to store passenger's name
int km - to store number of kilometres travelled
Member functions:
taximeter() -- constructor to initialize taxino to 0, name to “ ”and b to 0.
input() - to store taxino,name,km
calculate() - to calculate bill for a customer according to given conditions
kilometers travelled(km) Rate/km
<= 1 km Rs 25
1 < km <= 6 Rs 10
6 < km <= 12 Rs 15
12 < km <= 18 Rs 20
>18 km Rs 25
display()- To display the details in the following format
Taxino Name Kilometres travelled Bill amount
- - - -
Create an object in the main method and call all the above methods in it. [15]
Answer:
import java.io.*;
class taximeter
{
taximeter()
{
taxino=0;
name="";
km=0;
}
void input() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String S;
System.out.println("Enter Taxi number: ");
S=br.readLine();
taxino=Integer.parseInt(S);
System.out.println("Enter Passanger Name: ");
name=br.readLine();
System.out.println("Enter No.Of Kilometres travelled: ");
S=br.readLine();
km=Integer.parseInt(S);
}
int calculate()
{
if(km<=1)
return 25
else if(km<=6)
return km*10;
else if(km<=12)
return km*15;
else if(km<=18)
return km*20;
else return km*25;
}
void display()
{
System.out.println("\t"+taxino+"\t"+name+"\t"+km+"\t"+this.calculate());
//this.display will use the same object to call calculate() which was used to call display()
}
public static void main(String aa[])
{
taximeter obj=new taximeter();
obj.input();
obj.calculate();
System.out.println("\ttaxino \t name \t km travelled \tBill Amount);
obj.display();
}
}
Question 5
Write a menu driven program to find the sum of the following series depending on the
user choosing 1 or 2
1. S=1/4+1/8+1/12.........upto n terms
2. S=1/1!-2/2!+3/3!.......upto n terms
where ! stands for factorial of the number and the factorial value of a number is the
product of all integers from 1 to that number, e.g. 5! = 1 × 2 × 3 × 4 × 5.
(use switch-case). [15]
Answer:
import java.io.*;
class series
{
public static double series1(int n)
{
double s=0.0;
for(int i=1; i<=n; i++)
s+=1.0/(4.0*i);
return s;
}
public static int factorial(int x)
{
if(x==0||x==1) return 1;
int f=1;
while(x>1)
f*=x--;
return f
}
public static double series2(int n)
{
double s=0.0;
for(int i=1; i<=n; i++)
if(1%2==1) s+=1.0/factorial(i);
else s-=1.0/factorial(i);
return s;
}
public static void main(String aa[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in());
System.out.println("Enter 1 or 2 as your choice for the series: ");
String s=br.readLine();
int c=Integer.parseInt(s);
int n;
if(c==1||c==2)
{
System.out.println("Enter n")
s=br.readLine();
n=Integer.parseInt(s);
}
switch(c)
{
case 1:
series1(n);
break;
case 2:
series2(n)
break;
default:
System.out.println("Wrong choice enetered. You had to eneter ony 1 or 2, you entered "+c);
}//close of switch
}close of main
}close of class
Question 6
Write a program to accept a sentence and print only the first letter of each word of the
sentence in capital letters separated by a full stop.
Example :
INPUT SENTENCE : “This is a cat”
OUTPUT : T.I.A.C. [15]
Answer:
import java.io.*;
class abc
{
public static void main(String aa[]) throws IOException
{
BufferedReader br=new BufferedReader(System.in));
System.out.println("Enter a sentence:");
String s=br.readLine();
s=s.trim();
s=" "+s;
for(int i=0; i< s.length()-1; i++)
if(s.charAt(i)==' '&&s.charAt(i+1)!=' ')
System.out.println(Character.toUpperCase(s.charAt(++i))+".")
}
}
Question 7
Write a program to create an array to store 10 integers and print the largest integer and
the smallest integer in that array. [15]
Answer
import java.io.*;
class abc
{
public static void main(String aa[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a[]=new int[10];
int small, large;
System.out.println("Enter 10 integers 1 by one and press enter after each: ");
for(int i=0; i<=9; i++)
{
String s=br.readLine();
a[i]=Integer.parseInt(s);
if(i==0)
{
small=a[i];
large=a[i];
}
else
{
if(small>a[i]) small=a[i];
if(large< a[i]) large=a[i];
}
}
System.out.println("Largest number= "+large);
System.out.println("Smallest number= "+small);
}
}
Question 8
Write a program to calculate the sum of all the prime numbers between the range of
1 and 100. [15]
Answer
class abc
{
public static boolean checkPrime(int a)
{
for(int b=2; b<=a/2; b++)
if(a%b==0)//a divisor of 'a' has been found except for 1 and 'a' itself
return false;
return true;//no divisor of 'a' was found except 1 and 'a' itself
}
public static void main(String aa[])
{
int sum=0;
for(int i=1; i<=100; i++)
if(checkPrime(i)==true)
sum+=i;
System.out.println(sum);
}
}
Question 9
Write a program to store 10 names in an array. Arrange these in alphabetical order by
sorting. Print the sorted list. Take single word names, all in capital letters,
e.g. SAMSON, AJAY, LUCY, etc. [15]
Answer:
import java.io.*;
class ac
{
public static void main(String aa[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String a[]=new String[10];
String s;
System.out.println("Enter the names 1-by-1 and press enter after each name");
for(int i=0; i<10; i++)
{
a[i]=br.readLine();
a[i]=a[i].toUpperCase();
}
for(int i=0; i<=8; i++)
{
for(int j=0; j<=9-i-1; j++)
{
if(a[j].compareTo(a[j+1])>0)
{
s=a[j];
a[j]=a[j+1];
a[j+1]=s;
}
}
}
System.out.println("The sorted list is...");
for(int i=0; i<10; i++)
{
System.out.println(a[i])
}
}
}
Dear friends, if you have any programs that you want to be solved, please post your questions as comments; feel free to ask.
ReplyDeleteI'll answer them as soon as possible(I know you people have your exam in 4 days from now)
Want more Java programs..???
iscjavaprograms.blogspot.com
Sample, practice and fun java programs for those who love programming in Java...
please give how to write variable description in a function using program where we have 2 vara8bles but with same name one is an instance variable the other is an formal argument
Deletethank you for your valuable solution for this specimen
Deleteregards
R sakthivel
icse
I want all these programs in scanner class method
Delete@shivam ! thanks man really ........it helped a lot thanks a lot !!! :D
ReplyDeleteyour welcome my friend..
DeleteThanks
ReplyDeleteYour welcome.. yash
DeleteI was wondering what exactly was the mistake you were talking about in a comment you deleted.. ;)
Wap to enter a number and check whether it is an armstrong number or not.
ReplyDeletePlease help me.
Hey Abhirupa, click this link for the Armstrong number's program post on my other blog:
ReplyDeleteJava Program For checking if a number is an Armstrong Number
Thanks Buddy........Thanks a lot!
ReplyDeleteimport java.util.*;
ReplyDeletepublic class Taximeter
{
int taxino,km;String name;int bill=0;
Taximeter()
{
taxino=0;
name=" ";
km=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the taxinumber");
taxino=sc.nextInt();
System.out.println("Enter the name");
name=sc.next();
System.out.println("Enter the kilometres");
km=sc.nextInt();
}
void calculate()
{
if(km<=1)
bill=25;
else if(km>1&&km<=6)
bill=25+(km-1)*10;
else if(km>6&&km<=12)
bill=25+5*10+(km-6)*15;
else if(km>12&&km<=18)
bill=25+5*10+12*15+(km-12)*20;
else
bill=25+5*10+12*15+18*20+(km-18)*25;
}
void display()
{
System.out.println("taxino\tname\tkm travelled\tBill Amount");
System.out.println(taxino+"\t"+name+"\t\t"+km+"\t\t"+bill);
}
void main()
{
Taximeter t=new Taximeter();
t.input();
t.calculate();
t.display();
}}
Please check and tell whether it is correct.
see whether the billing conditions you have used are correct or not depend on the way you interpret the conditions given in the question.
DeleteThe way you interpreted may be correct, as may be the way I interpreted.
I see the way you interpreted it and for that your solution is fine.
But I feel that what I interpreted was more correct. ;-) :-P
one thing is wrong.....
Deletethe variable 'bill' does not exist as a global variable, in the question........
so what u have to do is.......
1) use 'double calculate' instead for 'void calculate'.
2) create a double type local variable there for bill.
3) return the variable bill to 'double calculate'.....and continue normally
.......if u still don't get me.........or if my English is bad........
then here is the correct code..........(try running it for your assurance)...........................................
import java.util.Scanner;
class Taxi_Meter
{
int taxino;
String name;
int kms;
Taxi_Meter()
{
taxino=0;
name="";
kms=0;
}
void input()
{
Scanner Sc=new Scanner(System.in);
System.out.println("enter your name");
name=Sc.nextLine();
System.out.println("");
System.out.println("enter taxi number");
taxino=Sc.nextInt();
System.out.println("Enter kilometers travelled");
kms=Sc.nextInt();
}
double calculate()
{
double bill=0;
if(kms<=1)
{
bill=kms*25;
}
else if(kms>=1&&kms<=6)
{
bill=1*25+((1-kms)*10);
}
else if(kms>6&&kms<=12)
{
bill=(1*25)+(6*10)+((7-kms)*15);
}
else if(kms>12&&kms<=18)
{
bill=(1*25)+(6*10)+(12*15)+(((6+12)-kms)*20);
}
else if(kms>18)
{
bill=(1*25)+(6*10)+(12*15)+(18*20)+(kms*25);
}
return bill;
}
void display()
{
System.out.println("taxino\tname\tkm travelled\tBill Amount");
System.out.println(taxino+"\t"+name+"\t\t"+kms+"\t\t"+calculate());
}
public static void main(String args[])
{
Taxi_Meter obj=new Taxi_Meter();
obj.input();
obj.display();
}
}
I want all these programs in the scanner class method plz.....
DeleteSolve the spacemen paper of exam 18 website
ReplyDeleteGive variable descriptions of each programs
ReplyDeleteThank you so much sir.I am much grateful for this☺
ReplyDeleteI have an bunch of questions can you solve it sir.please!!!
ReplyDeleteYes sure.
DeleteCan you do those programs in scanner class... Please I need it for my exam...
ReplyDeleteHi. I would need your email to help you out with this.
DeleteSir This Series-
ReplyDeleteS=(a+b)^2/2 + (a+b)^3/3 + (a+b)^n/n
Sir what is buffered reader
ReplyDeleteVery useful sir..thank you so much...
ReplyDeleteCannot find 2020 sample paper solved of computer
ReplyDeletePlease send fast because tomorrow is my exam
ReplyDeleteQuestion 6 is having a very small answer will I receive full marks for it in icse examination?
ReplyDeleteI am quite weak in java programming.Monday I have my comp. exam(ICSE).What suggestions u give for me at the last moment?
ReplyDeleteIt helped me alot thnx
ReplyDeleteIt helped me alot thnx
ReplyDeleteThanks a lot for this help. It would surely help me to make a lot of progress.😀😀
ReplyDeleteThank u so much for answer
ReplyDeleteThanks buddy for solution
ReplyDeleteHi Everyone,
ReplyDeleteI am Er.Prashant Yadav Founder of Prashnottar Pvt Ltd(www.prashnottar.in) lead by Engineers and Doctors we provide Assignment Solution for all K12 to B.tech Subjects so if you want help in any subject pls lets us know we @prashnottar welcomes your questions!!!
Thank u bro for this 😊☺️ answers
ReplyDeleteUr's welcome friend
DeletePlz tell the above programs without buffer class
ReplyDeletePlease check whether this program is right or not
ReplyDeleteIT ALWAYS SHOW ILLEGAL START OF EXPRESSION ERROR
import java.io.*;
class prime
{
private int n;
{
void input()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number to check whether it is prime or not");
n =Integer. parseInt(br.readLine());
}
private void prime()
{
int c;
c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
++c;
}
if(c==2)
{
System.out.println("it is a prime number");
}
System.out.println("it is not a prime number");
}
private static void main(String args [])throws IOException
{
prime obj=new prime();
obj.input();
}
}
}