Trending ▼   ResFinder  

ICSE 2008 solved : Computer Applications

31 pages, 9 questions, 1 questions with responses, 1 total responses,    0    0
Manasa Preethi
Sikkim manipal university, Bangalore
Master of Science Information Technology
+Fave Message
 Home > manumansi >

Formatting page ...

ICSE 2008 Q1. (a) Mention any two attributes required for class declaration. [2 marks] A1. (a) public and abstract Q1. (b) State the difference between token and identifier. [2 marks] A1. (b) A token is the smallest element of a program that is meaningful to the compiler. A token is the basic building block which is put together to construct a program. A token can be: i) A reserved word or keyword e.g. while ii) An identifier e.g. sum (variable name) iii) A separator or a delimiter e.g. , { ( etc. iv) An operator e.g. + v) A literal e.g. 25 vi) Comments are also treated as tokens to be ignored An identifier is a name given to a variable, class, etc. e.g. in int sum = 0; the variable sum is an identifier. Q1. (c) Explain instance variable. Give an example. [2 marks] A1. (c) An instance variable is a data member of a class. Each object of a class has its own value of the instance variables of that class. For example, consider the class Car as defined below: class Car { int gears, maxSpeed; } We can create two objects of the class Car, c1 and c2: Car c1 = new Car(); Car c2 = new Car(); c1.gears = 5; c2.gears = 4; c1.maxSpeed = 200; c2.maxSpeed = 80; Each object c1 and c2 has its own set of values for the instance variables gears and maxSpeed. Q1. (d) What is inheritance and how is it useful in Java? [2 marks] A1. (d) Inheritance is the principle by which a class can inherit from an existing class the features of that class. The original class is known as the parent class, and the new class is called the child class. The child class gets all the features of the parent class automatically, and can add additional features of its own in addition to what the parent class already provides. In Java, a child class can inherit from a single parent only. Inheritance is denoted by using the extends keyword when defining a class, for example: class MyChild extends MyParent { } Inheritance is useful in many ways: i) Create a specialized class with additional features that the parent class does not provide. For example, all teachers can belong to a Teacher class. However, the Principal class can have additional behaviour such as performance appraisal of the teachers, taking decisions on school holidays, and so on. We can then have a class Principal that extends the Teacher class. ii) A parent class can be abstract, and denote the methods that child classes must have. Once the child classes are created and have these methods, the objects of the child classes can be stored in parent class variables. The principle of dynamic binding (polymorphism) can be used to access the appropriate method of the appropriate child class, when working on an array of objects of different child classes. For example, a Shape class can be abstract and define an abstract area() method. We can now have a Square, Circle and Triangle class that all extend the Shape class, and each has a different implementation of the area() method. Now, assume we have an array of 3 Shape objects, and the name of this array is shapes. shapes[0] can be a Square, shapes[1] a Circle, and shapes[2] a Triangle object. When we execute shapes[i].area(), the area of the appropriate Square, Circle of Triangle will be computed depending on the class the actual object belongs to, as determined at run-time. Q1. (e) Explain any two types of access specifier. [2 marks] A1. (e) The public access modifier: the public data members and methods of the class can be accessed from anywhere within the same class, within the same package, by a subclass of the class, or by any other class. It does not matter. The protected access modifier: the protected data members and methods of the class can be access from withing the same class, within the same package, and by a subclass of the class. But not from a class that is neither a subclass of the class or in some other package. The access modifiers for methods and instance variables are summarized in the table below: Access modifiers also exist for classes. There is public and a default (no access) modifier. The public classes can be accessed by any class from anywhere. The noaccess modifier class (also known as package-private) can be accessed by classes from within its own package only. Q2. (a) What is meant by an infinite loop. Give one example. [2 marks] A2. (a) An infinite loop is a loop that keeps on iterating. Some of the ways of transferring control out of an infinite loop are using return and break. Some examples of infinite loops are shown below: while(true) { ... } for(; true;) { ... } do{ ... } while( true ); Q2. (b) State the difference between == operator and equals() method. [2 marks] A2. (b) The == operator is used to test the equality of primitives, whereas the equals() method is used to test the equality of objects. Whereas objects can also be checked using ==, this may not return the correct result. For example, if two strings are s1 = ABC and s2 = AB + C , both are, in effect ABC . However, s1 == s2 would return a false since the memory addresses of both are difference. The == operator on objects, checks the equality of memory addresses and not the content (state) of the objects. Many classes provide their own equals() method and can define equality based on the state of their variables. The equals() method is defined in the Object class and is overridden() in the other classes. This happens even with the String class. Q2. (c) Differentiate between actual parameter and formal parameter. [2 marks] A2. (c) A formal parameter is the name of the argument variable in a function declaration,. For example, in f(int x), x is the formal parameter. When this function f() gets called using f(3) or f(6), x assumes the value of 3 and 6. These values are the actual values used during the execution of the function and are the actual parameters. Q2, (d) What is the use of exception handling in Java? [2 marks] A2. (d) Exception handling has many uses in Java, as follows: i) Keep the error handling code separate from the normal flow of the program. This helps make the program easier to write, read and understand. All the stiff that should normally not happen (the exceptions) are handled and coded separately inside a set of catch() blocks. ii) Provides a clean way in which a program can quit, should it encounter an exception (problem). For example, if the database cannot be opened, there is no pint in having a db.close() at the end. iii) the nature of the error can be passed to the catch block via the Exception object, and appropriate action can be taken by analyzing the same. One of the functions available with the Exception object is printStackTrace() which can help print a trace of what executed before the exception occurred. Q2. (e) Differentiate between base and derived class. [2 marks] A2. (e) Base class is a parent class, derived class is a child class. The derived class inherits the variables and methods (behaviour) of the parent class, and has the ability to extend the same by adding its own variables, its own methods, as well as its own versions of the parent class methods for example, the Shape class could be a base class with a generic area() method that contains a print statement that area is unknown. A Square and Triangle class can be the derived classes of the Shape class, and their area() methods can actually print the areas of the Square and Triangle objects. A derived class can have only one parent class in Java. The extends keyword is used to indicate inheritance. For example, class Square extends Shape { ,,, } Q3. (a) Explain the function of each of the following with an example: i) break; ii) continue; [4 marks] A3. (a) The break statement is used to exit out of a loop completely. The continue statement is used to stop execution of the current loop body and move on to the end of the loop body, for the next iteration section / condition check. The example below explains both (read the comments): for(i=1; i<=10; i++) { if( (i%2) == 0 ) //for even numbers continue; //skip the loop body, go to the next iteration if( i==5 ) // on reaching 5 break; // exit the loop body totally System.out.println(i); } The output would only be the numbers 1 and 3. When i is 2 and 4, the continue statement kicks in, and the iteration terminates and goes to the iteration section (i++) directly. When i becomes 5, the break is executed and control transfers out of the for loop immediately, and no more loop body processing happens. Q3. (b) Convert the following segment into equivalent for loop: int i; i=0; while(i<=20) { System.out.print(i+" "); i++; } [2 marks] A3. (b) for(int i=0; i<=20; i++) System.out.print(i+" "); Q3. (c) If a=5, b=9 calculate the value of: a += a++-++b+a [2 marks] A3. (c) a becomes 6 and b becomes 10. Working: a += a++-++b+a = a + (a++-++b+a) = 5 + (5++-++b+a) = 5 + (5-++9+a) = 5 + (5-10+6) = 6, with be becoming 10. Q3. (d) Give the output of the following expressions: i) If x = -9.99, calculate Math.abs(x); ii) If x = 9.0, calculate Math.sqrt(x); [2 marks] A3. (d) i) 9.99 ii) 3.0 Q3. (e) If String x = Computer ; String y = Applications ; what do the following functions return for: i) System.out.println(x.substring(1,5)); ii) System.out.println(x.indexOf(x.charAt(4))); iii) System.out.println(y+x.substring(5)); iv) System.out.println(x.equals(y)); [4 marks] A3. (e) i) ompu ii) 4 iii) Applicationster iv) false Q3. (f) If, array[] = {1,9,8,5,2}; i) what is array length? ii) what is array[2]? [2 marks] A3. (f) i) 5 ii) 8 Q3. (g) What does the following mean? Employee stuff = new Employee(); [2 marks] A3. (g) An object of class Employee is created and its address is assigned to the object variable stuff. Q3. (h) Write a Java statement to input / read the following from the user using the keyboard: i) Character ii) String A3. (h) Assume import java.util.*; and Scanner sc = new Scanner( System.in ); i) sc.next().char(0) ii) sc.nextLine() Q4. Define a class Employee having the following description : Instance variables : int pan to store personal account number String name to store name double taxIncome to store annual taxable income double tax to store tax that is calculated Member functions : input ( ) Store the pan number, name, taxable income calc( ) Calculate tax for an employee display ( ) Output details of an employee Write a program to compute the tax according to the given conditions and display the output as per given format. Total Annual Taxable Income Upto Rs, 1,00,000 Tax Rate No tax From 1,00,001 to 1,50,000 10% of the income exceeding Rs. 1,00,000 From 1,50,001 to 2,50,000 1,50,000 Rs. 5000 + 20% of the income exceeding Rs. Above Rs. 2,50,000 Rs. 2,50,000 Rs. 25,000 + 30% of the income exceeding Output : Pan Number Name Tax-income Tax __ __ __ __ __ __ __ __ __ __ __ __ [15 marks] A4. import java.util.*; class Employee { // instance variables int pan; String name; double taxIncome, tax; // read the data from keyboard public void input() { Scanner sc = new Scanner( System.in ); System.out.print("Enter the pan, taxable income and name: "); pan = sc.nextInt(); taxIncome = sc.nextDouble(); name = sc.nextLine().trim(); } // calculate tax public void calc() { tax = 0.0; if( (taxIncome > 100000) && (taxIncome <= 150000) ) tax = (taxIncome - 100000) * 0.1; // 10% else if( (taxIncome > 150000) && (taxIncome <= 250000) ) tax = 5000.0 + (taxIncome - 150000) * 0.2; // 20% else tax = 25000.0 + (taxIncome - 250000) * 0.3; } // display details of an employee public void display() { printInt(pan, 10+1); printString(name, 20+1); printDouble(taxIncome, 10+1); printDouble(tax, 10+1); System.out.println(); } // utility method - print Int in a field width public static void printInt(int n, int width) { String s = "" + n; // convert to String printString(s, width); } // utility method - print double in a field width public static void printDouble(double d, int width) { d = d * 100; // convert to paise d = Math.round(d); // round this long l = (long)d; // make it a long String t = "" + l; // convert paise (long) into String // convert String t into Rupees with 2 decimal places rounding String s = t.substring(0, t.length() - 2) + "." + t.substring( t.length() - 2 ); printString(s, width); } // utility method - print string in a field width public static void printString(String s, int width) { System.out.print(s); int spaces = width - s.length(); while(spaces > 0) { System.out.print(" "); spaces--; } } // one time header public static void printHeader() { printString("Pan number", 10+1); printString("Name", 20+1); printString("Tax income", 10+1); printString("Tax", 10+1); System.out.println(); printString("----------", 10+1); printString("--------------------", 20+1); printString("----------", 10+1); printString("----------", 10+1); System.out.println(); } // entry point main method public static void main(String[] args) { Employee[] emps = new Employee[3]; // array of 3 employees int i; for(i=0; i < emps.length; i++) { emps[i] = new Employee(); // create object emps[i].input(); // ask data emps[i].calc(); // compute tax } printHeader(); // print the header of display once for(i=0; i < emps.length; i++) { emps[i].display(); // display for each employee } } } Sample output: Q5. Write a program to input a string and print out the text with the uppercase and lowercase letters reversed, but all other characters should remain the same as before. Example : INPUT : WelComE TO School OUTPUT : wELcOMe to sCHOOL [15 marks] A5. import java.util.*; class Q5 { public static void main(String[] args) { // user input Scanner sc = new Scanner( System.in ); System.out.print("Enter a sentence: "); String sentence = sc.nextLine(); // do the reversal of case String result = ""; char ch; int i; for(i=0; i < sentence.length(); i++) { ch = sentence.charAt(i); if( Character.isUpperCase(ch) ) ch = Character.toLowerCase(ch); else if ( Character.isLowerCase(ch) ) ch = Character.toUpperCase( ch ); result = result + ch; } System.out.println("Result: " + result); } } Sample Output: Q6. Define a class and store the given city names in a single dimensional array. Sort these names in alphabetical order using the Bubble Sort technique only. INPUT : Delhi,Bangalore,Agra, Mumbai,Calcutta OUTPUT : Agra,Bangalore,Calcutta,Delhi, Mumbai [15 marks] A6. class Q6 { public static void main(String[] args) { // single dimensional array String[] city = {"Delhi","Bangalore","Agra","Mumbai","Calcutta"}; // bubble sort logic int i, j; for(i=0; i < city.length - 1; i++) { for(j=0; j < city.length - 1 - i; j++) { if( city[j].compareTo( city[j+1] ) > 0 ) { String temp = city[j]; city[j] = city[j+1]; city[j+1] = temp; } } } // display the output System.out.print("Sorted output: "); for(i=0; i < city.length; i++) { System.out.print( city[i] ); if( i < (city.length - 1) ) System.out.print(", "); } System.out.println(); } } Output: Q7. Write a menu driven class to accept a number from the user and check whether it is a palindrome or a Perfect number. (a) Palindrome number : (a number is a Palindrome which when read in reverse order is same as read in the right order) Example : 11, 101, 151 etc. (b) Perfect number : (a number is called Perfect if it is equal to the sum of its factors other than the number itself) Example : 6 = 1 + 2 + 3 [15 marks] A7. import java.util.*; class Q7 { public static void main(String[] args) { char menuOption = ' '; // space do { menuOption = askMenuOption(); switch(menuOption) { case 'A': // palindrome doPalindrome(); break; case 'B': // perfect number doPerfectNumber(); break; case 'X': // exit System.out.println("Exiting program. Thank you!"); break; } }while( menuOption != 'X' ); } // keep asking for user option until a correct // option is entered by the user. // Return the option chosen as a char public static char askMenuOption() { Scanner sc = new Scanner( System.in ); char option = ' '; // space do { System.out.print("Enter A for Palindrome, B for Perfect number, X to exit: "); option = sc.nextLine().toUpperCase().charAt(0); if( "ABX".indexOf(option) < 0 ) System.out.println("Incorrect option. Try again..."); }while( "ABX".indexOf(option) < 0 ); return option; } // display the prompt and ask the user to enter an integer // return the int entered public static int askInt(String prompt) { Scanner sc = new Scanner( System.in ); System.out.print( prompt ); int n = sc.nextInt(); String temp = sc.nextLine(); // for the trailing newline return n; } // Palindrome logic public static void doPalindrome() { int n = askInt("Enter int for Palindrome check: "); int x = n; int rev = 0; while(x > 0) { int digit = x % 10; rev = (rev * 10) + digit; x = x / 10; } if( rev == n ) System.out.println(n + " is a Palindrome."); else System.out.println(n + " is not a Palindrome."); } // Perfect number logic public static void doPerfectNumber() { int n = askInt("Enter an int for Perfect Number check: "); int sumOfFactors = 0; int i; for(i=1; i < n; i++) if( (n%i) == 0 ) sumOfFactors += i; if(sumOfFactors == n) System.out.println(n + " is a Perfect Number."); else System.out.println(n + " is not a Perfect Number."); } } Sample output: Q8. Write a class with the name volume using function overloading that computes the volume of a cube, a sphere and a cuboid. [15] Formula : volume of a cube (vc) = s*s*s volume of a sphere (vs) = 4/3 * pi * r * r * r (where pi = 3.14 or 22/7) Volume of a cuboid (vcd) = l * b * h [15 marks] A8. import java.util.*; class volume { // overloaded function 1 (cube) public void vol(double s) { // call the overloaded function of cuboid vol(s, s, s); } // overloaded function 2 (sphere) // use a 2nd parameter (useless) to differentiate // from the single parameter vol() function above public void vol(double r, double pi) { double vs = 4.0 / 3.0 * pi * Math.pow(r, 3); System.out.println("Volume: " + vs); } // overloaded function 3 (cuboid) public void vol(double l, double b, double h) { double vcd = l * b * h; System.out.println("Volume: " + vcd); } // entry point main method public static void main(String[] args) { volume v = new volume(); Scanner sc = new Scanner( System.in ); double S, L, B, H, R; String temp; System.out.print("Enter S of Cube: "); S = sc.nextDouble(); temp = sc.nextLine(); // for the trailing newline v.vol(S); // overloaded function #1 System.out.print("Enter R of Sphere: "); R = sc.nextDouble(); temp = sc.nextLine(); // for the trailing newline // overloaded function #2 v.vol(R, 3.14); // use 3.14 and not Math.PI System.out.print("Enter L, B, H of Cuboid: "); L = sc.nextDouble(); B = sc.nextDouble(); H = sc.nextDouble(); temp = sc.nextLine(); // for the trailing newline v.vol(L, B, H); // overloaded function #3 } } Sample output: Q9. Question 9 Write a program to calculate and print the sum of each of the following series: (a) Sum (S) = 2 4 + 6 8 + . -20 (b) Sum (S) = x + x + x + x + .. + x - - - - -- -- 2 8 11 20 5 (Value of x to be input by the user. ) [15 marks] A9. import java.util.*; class Q9 { public static void main(String[] args) { // user input of x Scanner sc = new Scanner( System.in ); System.out.print("Enter x: "); double x = sc.nextDouble(); String temp = sc.nextLine(); // for the trailing newline doSeries1(); doSeries2(x); } // Series 1 public static void doSeries1() { int sum = 0; for(int i=2; i<=20; i+=2) { if( (i%4) == 0 ) sum = sum - i; else sum = sum + i; } System.out.println("Sum 1: " + sum); } // Series 2 public static void doSeries2(double x) { double sum = 0.0, term, nr; int dr; nr = x; for(dr=2; dr <= 20; dr+=3) { term = nr / (double)dr; sum += term; } System.out.println("Sum 2: " + sum); } } Sample output:

Formatting page ...

Top Contributors
to this ResPaper
(answers/comments)


Rockey Ron

(2)

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

Formatting page ...

 

  Print intermediate debugging step

Show debugging info


 

 

© 2010 - 2025 ResPaper. Terms of ServiceContact Us Advertise with us

 

manumansi chat