Tuesday, October 2, 2012

Java program to check if a number is Armstrong or not.

//program for checking Armstrong Number
//An Armstrong number is one which is the sum of cube of all its digits
//Eg. 153=1*1*1+5*5*5+3*3*3=1+125+27=153..
import java.io.*;
class armstrong
{
 public static void main(String aa[]) throws IOException
{
 BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int a, b=0, c, d;
System.out.println("Enter a number to check for Armstong");
String S=br.readLine();
a=Integer.parseInt(S);
c=a;
while(c!=0)
{
 d=c%10;
 c=c/10;
 b+=d*d*d;
}
if(a==b)
System.out.println(a+" is an Armstrong no.");
else
System.out.println(a+" is NOT an Armstrong no.");
}
}

Sunday, January 8, 2012

2012 SPECIMEN PAPER SOLVED

SECTION A (40 Marks)
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])
}
}
}

WELCOME HERE

Here you can ask any questions related to ur ICSE Computer Applications syllabus and u ,ll have them answered here..