Trending ▼   ResFinder  

ICSE 2012 solved : Computer Applications

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

Formatting page ...

ICSE 2012 Q1. (a) Give one example each of a primitive data type and a composite data type. [2 marks] A1. (a) class Car { int gears, maxSpeed; // int primitive data type String model, manufacturer; } Car c = new Car(); // composite data type In the example above, gears is a primitive data type (int for integer), and c is a composite data type of class Car. Basically, int is a primitive data type, and Car is a composite data type. Q1. (b) Give one point of difference between unary and binary operators. [2 marks] A1. (b) The unary operators operate on a single operand (e.g. x++) whereas the binary operators operate on two operands (e.g. x*y). Here, ++ is a unary operator and * is a binary operator. Q1. (c) Differentiate between call by value or pass by value and call by reference or pass by reference. [2 marks] A1. (c) CALL BY VALUE CALL BY REFERENCE A copy of the argument passed is created in the A pointer (address) of the argument passed is received called method. in the called method. A change in the parameter value inside the called The parameter in the called method can invoke the method will not result in a change in the original methods of the original object passed, and the state of variable in the calling method. that object can be changed. In Java, applies to primitives only. In Java, applies to objects only. Q1. (d) Write a Java expression for (2as + u2) [2 marks] A1. (d) Math.sqrt( (2*u*s + Math.pow(u,2)) ) Q1. (e) Name the type of error (syntax, runtime or logical error) in each case given below? i) Division by a variable that contains a value of zero. ii) Multiplication operator used when the operation should be division. iii) Missing semi colon. [2 marks] A1. (e) i) runtime error ii) logical error iii) syntax error Q2. (a) Create a class with one integer instance variable. Initialize the variable using: i) default constructor ii) parameterized constructor [2 marks] A2. (a) class Q2a { int x; // zero argument constructor public Q2a() { this(0); // call the one-argument constructor } // parameterized constructor public Q2a(int x) { this.x = x; } public static void main(String[] args) { // initialize using zero argument constructor Q2a obj1 = new Q2a(); // initialize using the parameterized constructor Q2a obj2 = new Q2a(4); System.out.println("obj1.x: " + obj1.x); System.out.println("obj2.x: " + obj2.x); } } Output: Q2. (b) Complete the code below to create an object of Scanner class. Scanner sc = _____ Scanner( _____ ); [2 marks] A2. (b) Scanner sc = new Scanner( System.in ); Q2. (c) What is an array? Write a statement to declare an integer array of 10 elements. [2 marks] A2. (c) An array is a container object (data structure) that can hold multiple values. An array object has a single name, and the individual elements of the array are accessed using an indexed position within that array. In Java, the index starts from 0. int[] x = new int[10]; // statement to declare an int array of 10 elements Q2. (d) Name the search or sort algorithm that: i) Makes several passes through the array, selecting the next smallest item in the array each time and placing it where it belongs in the array ii) At each stage, compares the sought key value with the key value of middle element of the array. [2 marks] A2. (d) i) selection sort ii) binary search Q2. (e) Differentiate between public and private modifiers for members of a class. [2 marks] A2. (e) Private members can be accessed by methods defined within the class itself, and not from anywhere else. The public members of an object can be accessed from anywhere methods of the same class, of a subclass of the class, classes within the same package as the class, as well as from any other classes as well. Q3. (a) What are the values of x and y when the following statements are executed? int a = 63, b = 36; boolean x = (a > b) ? true : false; int y = (a < b) ? a : b; [2 marks] A3. (a) x has the boolean value true and y has the integer value 36. Q3. (b) State the values of n and ch? char c = 'A'; int n = c + 1; char ch = (char)n; [2 marks] A3. (b) n has the int value 66, and ch has the char value B . Q3. (c) What will be the result stored in x after evaluating the following expression? int x=4; x+=(x++) + (++x) + x; [2 marks] A3. (c) x will have the int value 20. Explanation: The precedence of shortcut operators such as += are the lowest. Hence, x +=(x++) + (++x) + x becomes x = x + ( (x++) + (++x) + x) = 4 + ((x++) + (++x) + x) = 4 + ((4++) + (++x) + x) = 4 + (4 + ++5 + x) = 4 + (4 + 6 + 6) = 4 + 16 = 20 Q3. (d) Give the output of the following program segment: double x = 2.9, y = 2.5; System.out.println(Math.min(Math.floor(x),y)); System.out.println(Math.min(Math.ceil(x),y)); [2 marks] A3. (d) Q3. (e) State the output of the following program segment: String s = "Examination"; int n = s.length(); System.out.println(s.startsWith(s.substring(5,n))); System.out.println(s.charAt(2) == s.charAt(6)); [2 marks] A3. (e) Q3. (f) State the method that: i) Converts a String to a primitive float data type. ii) Determines if the specified character is an uppercase character. [2 marks] A3. (f) i) Float.parseFloat() ii) Character.isUpperCase() Q3. (g) State the data type and values of a and b after the following segment is executed. String s1 = "Computer", s2 = "Applications"; a = (s1.compareTo(s2)); b = (s1.equals(s2)); [2 marks] A3. (g) The data type of a is int and its value is 2. The data type of b is boolean and its value is false. Q3. (h) What will the following code output? String s="malayalam"; System.out.println(s.indexOf('m')); System.out.println(s.lastIndexOf('m')); [2 marks] A3. (h) Q3. (i) Rewrite the following program segment using while instead of for statement int f=1, i; for(i=1;i<=5;i++) {f*=i; System.out.println(f);} [2 marks] A3. (i) int f=1, i=1; while(i<=5) { f*=i; System.out.println(f); i++; } Although not asked for, the output of the code snippet would be: Q3. (j) In the program given below, state the name and the value of the i) method argument or argument variable ii) class variable iii) local variable iv) instance variable class myClass { static int x=7; int y=2; public static void main(String[] args) { myClass obj = new myClass(); System.out.println(x); obj.sampleMethod(5); int a=6; System.out.println(a); } void sampleMethod(int n) { System.out.println(n); System.out.println(y); } } [2 marks] A3. (j) i) name is n, value is 5 ii) name is x, value is 7 iii) name is a, value is 6 iv) name is y, value is 2 Q4. Define a class called Library with the following description: Instance variables/data members: int acc_num stores the accession number of the book String title stores the title of the book String author stores the name of the author Member Methods: void input() to input and store the accession number, title and author void compute() to accept the number of days late, calculate and display the fine charged at the rate of Rs. 2 per day void display() to display the details in following format Accession Number Title Author ---------------- ----- ------ Write a main method to create an object of the class and call the above member methods. [15 marks] A4. import java.util.*; class Library { // instance variables, data members int acc_num; String title, author; // zero argument constructor public Library() { acc_num = 0; title = ""; author = ""; } // input public void input() { Scanner sc = new Scanner( System.in ); String temp = ""; System.out.print("Enter the accession number: "); acc_num = sc.nextInt(); temp = sc.nextLine(); // for the trailing newline System.out.print("Enter the title: "); title = sc.nextLine(); System.out.print("Enter the author: "); author = sc.nextLine(); } // compute and display the fine charged public void compute(int nDays) { int fine = nDays * 2; System.out.println("Fine charged: " + fine); } // display the info public void display() { printInt(acc_num, 16+1); // print acc_num in width of 16 printStr(title, 30+1); // print title in width of 30 printStr(author, 20); // print author in width of 20 System.out.println(); } // static method to print the header once only public static void displayHeader() { printStr("Accession Number", 16+1); printStr("Title", 30+1); printStr("Author", 20); System.out.println(); printStr("----------------", 16+1); printStr("------------------------------", 30+1); printStr("--------------------", 20); System.out.println(); } // main method public static void main(String[] args) { Library book1 = new Library(); Library book2 = new Library(); book1.input(); book2.input(); book1.compute(5); book2.compute(7); Library.displayHeader(); book1.display(); book2.display(); } // print argument1 in a field of width argument2 public static void printInt(int n, int width) { String s = "" + n; printStr(s, width); // call the String method } // print argument1 in a field of width argument2 public static void printStr(String s, int width) { System.out.print(s); // print spaces to fill up the field width for(int i=s.length()+1; i<=width; i++) System.out.print(" "); } } Sample output: Q5. Given below is a hypothetical table showing rates of income tax for the male citizens below the age of 65 years: TAXABLE INCOME (TI) IN RS. INCOME TAX IN RS. Does not exceed Rs. 1,60,000 NIL Is greater than Rs. 1,60,000 and less than or equal to Rs. 5,00,000 (TI 1,60,000)*10% Is greater than Rs. 5,00,000 and less than or equal to Rs. 8,00,000 [(TI 5,00,000)*20%] + 34,000 Is greater than Rs. 8,00,000 [(TI 8,00,000)*30%] + 94,000 Write a program to input the age, gender (male or female) and taxable income of a person. If the age is more than 65 years or the gender is female, display wrong category . If the age is less than or equal to 65 years and the gender is male, compute and display the income tax payable as per the table given above. [15 marks] A5. import java.util.*; class Q5 { public static void main(String[] args) { // local variables Scanner sc = new Scanner(System.in); int age; char gender; int taxableIncome; String temp; // user inputs System.out.print("Enter the age, gender and taxable income: "); age = sc.nextInt(); gender = sc.next().toUpperCase().charAt(0); taxableIncome = sc.nextInt(); temp = sc.nextLine(); // for the trailing newline // check for correctness and if( (age > 65) || (gender != 'M') ) System.out.println("wrong category"); else // compute and display the income tax { double incomeTax = 0.0; if( (taxableIncome > 160000) && (taxableIncome <= 500000) ) incomeTax = (taxableIncome - 160000)*0.1; else if( (taxableIncome > 500000) && (taxableIncome <= 800000) ) incomeTax = (taxableIncome - 500000) * 0.2 + 34000; else if( taxableIncome > 800000 ) incomeTax = (taxableIncome - 800000) * 0.3 + 94000; System.out.println("Income tax payable: " + incomeTax); } } } Sample outputs from multiple iterations of the program: Q6. Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter sequences that exist in the string. Sample input: SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE Sample output: 4 [15 marks] A6. import java.util.*; class Q6 { public static void main(String[] args) { // user input Scanner sc = new Scanner(System.in); System.out.print("Enter a sentence: "); String sentence = sc.nextLine().toUpperCase(); // count the double letter sequences int count = 0; char previousChar = ' ', currentChar = ' '; for(int i=0; i < sentence.length(); i++) { currentChar = sentence.charAt(i); // exclude spaces when checking if( (currentChar == previousChar) && (currentChar != ' ') ) { count++; i++; // will make aaaa give a count of 2, not 3 } previousChar = currentChar; } // display the output System.out.println("Number of double letter sequences: " + count); } } Sample output: Q7. Design a class to overload a function polygon() as follows: a) void polygon(int n, char ch) with one integer argument and one character type argument that draws a filled square of side n using the character stored in ch b) void polygon(int x, int y) with two integer arguments that draws a filled rectangle of length x and breadth y, usign the symbol @ c) void polygon() with no arguments that draws a filled triangle shown below. Example: i) Input value of n = 2, ch = O . Output: OO OO ii) Input value of n=2, y=5. Output: @@@@@ @@@@@ iii) Output: * ** *** [15 marks] A7. class Q7 { // overloaded function 1 public static void polygon(int n, char ch) { System.out.println("Overloaded method #1"); for(int i=1; i <= n; i++) // n lines { printChar(ch, n); // of n characters each System.out.println(); } } // overloaded function 2 public static void polygon(int n, int y) { System.out.println("Overloaded method #2"); for(int i=1; i <= n; i++) // n rows { printChar('@',y); // each of '@' y times System.out.println(); } } // overloaded function 3 public static void polygon() { System.out.println("Overloaded method #3"); for(int i=1; i <= 3; i++) // 3 rows { printChar('*', i); // each of '*', i times System.out.println(); } } // utility function to print ch, n times public static void printChar(char ch, int n) { for(int i=1; i <= n; i++) System.out.print(ch); } // main method to call the 3 overloaded functions public static void main(String[] args) { Q7.polygon(2, 'O'); Q7.polygon(2, 5); Q7.polygon(); } } Output: Q8. Using the switch statement, write a menu driven program to: i) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5, The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. ii) Find the sum of the digits of an integer that is input: Sample input: 15390 Sample output: Sum of the digits = 18 For an incorrect choice, an appropriate error message should be displayed. [15 marks] A8. import java.util.*; class Q8 { public static void main(String[] args) { char menuOption = ' '; do { // Ask the menu option. If a wrong choice is entered, // display an appropriate message in the // askMenuOption() method itself. menuOption = askMenuOption(); switch(menuOption) { case 'F': // do Fibonacci doFibonacci(); break; case 'S': // do sum of the digits doSumOfDigits(); break; case 'X': System.out.println("Quitting program. Thank you."); break; default: break; } }while( menuOption != 'X' ); // Keep asking until X is entered } // Return the menu option selected. // If a wrong value is entered, display an appropriate // message and ask once again. public static char askMenuOption() { Scanner sc = new Scanner( System.in ); char option = ' '; do { System.out.print("Enter F for Fibonacci, S for Sum of digits, X for Exit: "); option = sc.nextLine().toUpperCase().charAt(0); if( "FSX".indexOf(option) < 0 ) // wrong entry { System.out.println("Incorrect option. Try again..."); } }while( "FSX".indexOf(option) < 0 ); // if wrong, ask again return option; } // First 10 digits of the Fibonacci series public static void doFibonacci() { int first = 0, second = 1, next; System.out.print("Fibonacci: 0, 1"); int i; // compute 8 more values only, 0 and 1 are displayed already for(i=1; i <= 8; i++) { next = first + second; System.out.print(", " + next); first = second; second = next; } System.out.println(); } // Sum of digits public static void doSumOfDigits() { Scanner sc = new Scanner( System.in ); System.out.print("Enter an integer: "); int n = sc.nextInt(); String temp = sc.nextLine(); // for the trailing newline int sum = 0; while( n > 0 ) { int digit = n % 10; sum += digit; n = n / 10; } System.out.println("Sum of the digits = " + sum); } } Sample output: Q9. Write a program to accept the names of 10 cities in a single dimension string array and their STD (Subscriber Trunk Dialing) codes in another single dimension integer array. Search for a name of a city input by the user in the list. If found, display Search Successful and print the name of the city along with its STD code, or else display the message Search Unsuccessful. No such city in the list . [15 marks] A9. import java.util.*; class Q9 { static String[] city = new String[10]; static int[] std = new int[10]; public static void main(String[] args) { // fill up the array askDetails(); // keep asking for city name // till user enters EXIT String searchCity = ""; while( !searchCity.equals("EXIT") ) { searchCity = askCityName(); if(searchCity.equals("EXIT")) break; // exit the loop int index = huntForCity(searchCity); if( index < 0 ) { System.out.println("Search Unsuccessful. No such city in list."); } else { System.out.println("Search Successful."); System.out.println(city[index] + " has STD code: " + std[index] + "."); } } } // ask user to enter details of 10 cities public static void askDetails() { Scanner sc = new Scanner( System.in ); int i; for(i=0; i < city.length; i++) { System.out.print("Enter STD-code and city-name: "); std[i] = sc.nextInt(); city[i] = sc.nextLine().trim().toUpperCase(); System.out.println( std[i] + "." + city[i] + "."); } } // ask user to enter a city name to search for public static String askCityName() { Scanner sc = new Scanner( System.in ); System.out.print("Enter a city to get STD code: "); String c = sc.nextLine().toUpperCase(); return c; } // hunt for city name in array // return index of the location found // if not exists, return -1 public static int huntForCity(String searchCity) { for(int i=0; i < city.length; i++) if( city[i].equals( searchCity ) ) return i; return -1; // if not found } } Sample output: Explanation: The trim() is needed because when nextInt() is followed by nextLine(), the space is read as part of the city name. This space will result in all searches for a city name to result in not found, and hence we need to remove the leading space from the city name when storing the same into the array. We also convert all city names and the search-for city name to upper case, so that the search is not case sensitive. Another way is to compare using equalsIgnoreCase().

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 ...

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