Computer Preboard Icse 2012
DEPAUL SCHOOL I – Pre Board Examination, 2011‐12. Computer Applications ‐ K E Y SECTION – A (40 Marks) Question 1: (a) Type Casting. The explicit conversion of an operand to a specific type is called Type Casting. (b) Identifier. An identifier is a named memory location, which holds a data of a particular data type. (c) Function Overloading. A function name having several definitions in the same scope that are differentiable by the number or types of their arguments is said to be an overloaded function. The process of creating overloaded functions is called function overloading. (d) Use of Exception Handing. (i) Exception handling separates error‐handling code from normal code. (ii) It makes clear, robust, fault‐tolerant programs. (e) Instance Variable. Instance variables are those variables for which the memory is allocated when the object of the class is created. These variables can be accessed to all the function within the program. class Sample{ private int i; //instance variable } Question 2: (a) ‐2 (b) Features of a Constructor. (i) It automatically executes when the object of the class is create(ii) It is used to initialize the object of the class with a legal valu(c) Differences between Pure and Impure Functions. Pure Function Impure Function It does not change the state of the object It changes the state of the object It does not contain any output statements It may or may not contain output statements (d) void. (i) It is used when the function does not return any values. (iii) Only on(e) Call by Value. In a function call_by_value the actual parameters are copied to formal parameters. A change in the formal parameters will not reflect a change in the actual parameters. Page 1 of 9 Question 3: (a) Java Virtual Machine(JVM). Programs written in Java are compiled into Java Byte Code, which is then interpreted by a special Java Interpreter for specific platform. This Java interpreter is known as the Java Virtual Machine. (b) Difference between equals() and == equal() == It is a method It is an operator It is used to compare two string It is used to compare two primitive operands (c) Advantages of Functions. (i) They lessen the complexity of the programs (ii) They enhance reusability of the code. (d) Difference between static and non‐static member methods. Static non‐static These are known as class methods These are known as object methods They are invoked with the help of the class name. They are invoked with the help of the object Ex. Math.sqrt() Ex. br.readLine() where ‘br’ is an object of the class BufferedReader (e) Math.sqrt(Math.sin(a)+Math.atan(a)-Math.exp(2*x); Question 4: #1# -j<=i #2# -i+1 #3# -j>0 #4# -j-- #4# -pas[j-1] Working Page 2 of 9 SECTION – B (60 Marks) Question 5: //To find the roots and nature of the quadratic equation class QuadraticEquation { //begin of the class private double alpha,beta,discriminant; public void findRoots(double a,double b,double c) { //begin of the method discriminant=b*b-4*a*c; //discriminant if(discriminant>0) //discriminant is greater than zero { alpha=(-b-Math.sqrt(b*b-4*a*c))/(2*a); beta=(-b+Math.sqrt(b*b-4*a*c))/(2*a); System.out.println("The positive root = "+alpha); System.out.println("The negative root = "+beta); System.out.println("The roots are real and . unequal"); } else if(discriminant==0) //discriminant equals zero { alpha=-b/(2*a); beta=-b/(2*a); System.out.println("The positive root = "+alpha); System.out.println("The negative root = "+beta); System.out.println("The roots are real and equal"); } else { //discriminant is less than zero System.out.println("The roots are imaginary"); } } //end of the method } //end of the class output: The positive root = -3.414213562373095 The negative root = -0.5857864376269049 The roots are real and unequal Variable Description. Sl.No. Identifier Data Type Purpose 1 a double To store the co‐efficient of x22 b double To store the co‐efficient of x 3 c double To store the constant 4 discriminant double To store the discriminant 5 alpha double To store the positive root 6 beta double To store the negative root Page 3 of 9 Question 6: //Menu Driven -Arithmatic menu import java.io.*; class ArithmaticMenu { private int choice,x,y,r; private String s; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); public void main(String args[])throws IOException { //display the arithmatic menu do { System.out.println("========================================== ==="); System.out.println("============ARITHMATIC MENU=================="); System.out.println("========================================== ==="); System.out.println(" 1. Addition"); System.out.println(" 2. Subtraction"); System.out.println(" 3. Multiplication"); System.out.println(" 4. Division"); System.out.println(" 5. Exit"); System.out.println("========================================== ==="); System.out.print(" Enter your choice from 1 to 5 : "); s=br.readLine(); choice=Integer.parseInt(s); }while(choice<1||choice>5); //processing switch(choice) { case 1: readData(); r=x+y; System.out.print("The sum of "+x+" and "+y+" = "+r); break; case 2: readData(); r=x-y; System.out.print("The difference of "+x+" and "+y+ " = "+r); break; case 3: readData(); r=x*y; System.out.print("The product of "+x+" and "+y+ " = "+r); break; Page 4 of 9 case 4: readData(); r=x/y; System.out.print("The quotient of "+x+" and "+y+ " = "+r); break; case 5: System.out.println("Thank you"); System.exit(0); } //end of the switch…case } //end of the main method //function to read two numbers private void readData()throws IOException { //read the first number System.out.print("Enter a number : "); s=br.readLine(); x=Integer.parseInt(s); //read the second number System.out.print("Enter a number : "); s=br.readLine(); y=Integer.parseInt(s); } } Output: ============================================= ============ARITHMATIC MENU================== ============================================= 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit ============================================= Enter your choice from 1 to 5 : 1 Enter a number : 10 Enter a number : 20 The sum of 10 and 20 = 30 Variable Description. S.no Identifier Data Type Purpose 1 choice int To store the user’s choice 2 x int To store the first integer 3 y int To store the second integer 4 r int To store the result of binary operation 5 isr InputStreamReader Instance of InputStreamReader 6 br BufferedReader Instance of BufferedReader 7 s String Instance of String, to read the string from the console Page 5 of 9 Question 7: //Generate all the Armstrong numbers from 1 to 1000 class Armstrong { private int i,n,r,sum=0; public void main(String args[]) { System.out.println("The Armstrong Numbers : "); for(i=1;i<=1000;i++) //iterates for 1000 numbers { n=i; //store in a temporary variable while(n!=0) //finds Armstrong { r=n%10; sum=sum+r*r*r; n=n/10; } //end of while loop if(i==sum) //equating with original number System.out.print(i+"\t"); //display Armstrong number sum=0; //reset sum to zero } //end of for loop } //end of the main method } //end of the class Output: The Armstrong Numbers 1 153 370 371 407 Variable Description. S.no Identifier Data Type Purpose 1 i int Loop variable 2 n int To store the number to be tested for Armstrong 3 r int To store the remainder 4 sum int To store the sum of the cubes of the individual digits of a number Page 6 of 9 Question 8: //Finds the reverse of the String import java.io.*; import java.util.*; class StringReverse { private String s,temp,target=""; public void main(String args[])throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); //Read the string from the user System.out.print("Enter a string : "); s=br.readLine(); //Converting String to StringTokenizer StringTokenizer st=new StringTokenizer(s); //Extract the tokens in the string while(st.hasMoreTokens()) { temp=st.nextToken(); //collect each token StringBuffer sb=new StringBuffer(temp); sb.reverse(); //reverse the string target=target+sb+" "; //collect the resultant string } //display the resultant string System.out.println("The reverse = "+target); } } Output: Enter a string : Welcome To Java The reverse = emocleW oT avaJ Variable Description. S.no Identifier Data Type Purpose 1 isr InputStreamReader Instance of InputStreamReader 2 br BufferedReader Instance of BufferedReader 3 sb StringBuffer Instance of StringBuffer 4 s String Instance of String, to read the string from the console 5 temp String To store the token from the string 6 target String To store the reverse of the string Page 7 of 9 Question 9: //Bubble Sort import java.io.*; class BubbleSort { private int a[]=new int[5]; private int i,j,temp; private String s; public void main(String args[])throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); //reads 5 integers into an array for(i=0;ia[j+1]) { //swapping temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } //display the elements after sorting System.out.println("The Elements after sorting : "); for(i=0;i
Description
its computer applications
Presentation Transcript
Your Facebook Friends on WizIQ