Trending ▼   ResFinder  

ICSE 2010 solved : Computer Applications

34 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 2010 Q1. (a) Define the term byte code. [2 marks] A1. (a) Byte code is the code that can execute on a Java Virtual Machine (JVM). Byte code is the code created when a Java program is compiled. It can be executed on any machine that contains the Java Runtime Environment (JRE). Q1. (b) What do you mean by type conversion? How is implicit conversion different from explicit conversion? [2 marks] A1. (b) Type conversion means the conversion from one data type to another. For example, an int value being converted to a long value. Implicit type conversion happens when the value being converted (source) has a data type that is smaller than the destination data type. For example, any int value can be converted into a long value there will be no loss of precision. This is because a long is 8 bytes and in int 4 bytes. This does not require an explicit type cast. Explicit type conversion is the opposite for example a long being converted into an int. Here, if the long value is more than what is supported by an int, the rest of the bits get knocked off, and an unpredictable int may result. There could be a loss of precision. The compiler will not allow this to happen, unless we explicitly tell the compiler that we are OK with this conversion, we understand the impact, please allow it to happen . For this, we need to use a type cast. For example, int long x = 12345; we can have int y = (int)x; where the (int) type cast is explicitly needed. Q1. (c) Name two jump statements and their use. [2 marks] A1. (c) Two jump statements are break and continue. The break statement is used to stop execution of a loop body and get out of the loop. The continue statement is used to stop execution of the current iteration of the loop body , and move to the end of the loop, to check for the next iteration; it just skips the balance execution of the current loop body iteration. Q1. (d) What is an exception? Name two exception handling blocks. [2 marks] A1. (d) An exception is the occurrence of something that should not normally happen. A Java method will throw an exception if it encounters a situation which it cannot handle. When an exception occurs, an object is created that contains details of the problem (exception). This object is created automatically by Java. Some exceptions, like a divide by zero, can be avoided through careful programming. Other exceptions, such as hard disk full, or network connection lost, cannot be avoided and nor can they be predicted. Dealing with exceptions is known as handling the exception. There are two types of exceptions: checked exceptions and unchecked exceptions. Unchecked exceptions are those that do not have to be addressed (handled) by the Java program. This includes division by zero, array index out of bounds, etc. Unchecked exceptions are normally the result of bad programming (programmer bugs). Checked exceptions are those that must be handled by the Java program. These include hard disk is full, file not found, unable to connect to database, and so on. Exception handling happens using the try..catch..finally keywords. The code for which an exception can happen, is put inside the try block. If an exception happens, that is caught in the catch block. Once the code in the try block executes, or the exception in the catch block is executed, the code in the optional finally block gets executed. So, the 3 blocks used with exceptions are try, catch, and finally. The handling of the exceptions happens in the catch and finally blocks, and the code to exception-checked is put in the try block. Q1. (e) Write two advantages of using functions in a program. [2 marks] A1. (e) The advantages of functions are: i) Re-use: the same function can be reused multiple times, from different parts of the program, as well as from different programs (classes) as well. ii) Maintenance: Since the re-used logic is in a single place, if any change needs to be made, the change needs to be made in one place only. The maintenance is simpler. Also, it becomes difficult to identify all places where the same code would have been, and sometimes one change may get overlooked. With functions, this is not the case since the code is in a single place only. iii) Readability: By using functions, each doing its own job and having a meaningful name, the readability of a program increases. Many times, a programmer needs to look at source code written 6 months later; or some other programmer needs to look at someone else s source code. The use of good function names make the source code easier to read and understand, since the overall flow of the program is more readily apparent. Q2. (a) State the purpose and return data type of the following String functions: i) indexOf() ii) compareTo() [2 marks] A2. (a) i) indexOf(): Return data type is int. It returns the index of the first occurrence of the argument (which could be char or String) within a String. For example, Hello .indexOf( e ) returns 1, and Hello .indexOf( ll ) returns 2. If the argument is not found, a -1 is returned. ii) compareTo(): Return data type is int. It returns a 0 if the two String values are the same. It returns a positive integer if the first value is lexicographically greater than the second, and a negative value if the first value is lexicographically earlier than the first. The positive or nagative value returned is the difference is ASCII code of the first mismatching character of the two Strings. For example, Hello .compareTo( Hello ) gives a 0, since they are the same. Hello .compareTo( Goodbye ) returns a 1 (since H is 1 later than G). Q2. (b) What is the result stored in x, after evaluating the following expression? int x = 5; x = x++ * 2 + 3 * --x; [2 marks] A2. (b) 25 Explanation: x = x++ * 2 + 3 * x = (x++ * 2) + (3 * x) = (5++ * 2) + (3 * x) = (5 * 2) + (3 * 6) = 10 + (3 * 5) 10 + 15 = 25 Q2. (c) Differentiate between static and non-static data members. [2 marks] A2. (c) STATIC NON-STATIC Belongs to the class, not to any object of that class. Belongs to an object of the class. Value is shared across all objects of the class, and Value is separate for each object of the class, does not even when there is no object created. exist without an object. Can be accessed using ClassName.MemberName or Can be accessing using Object.MemberName only, Object.MemberName never using ClassName.MemberName Q2. (d) Write the difference between length and length() functions. [2 marks] A2. (d) length is used to determine the number of elements in an array dimension and applies to array objects. The length() function applies to String objects and is used to determine the length of a String. For example: String[] s = { One , Two , Three , Four , Five , Six }; Here, s.length is 6, as s is an array containing 6 String elements. However, s[2].length() is 5 since s[2] is a String object. Q2. (e) Differentiate between private and protected visibility modifiers. [2 marks] A2. (e) The private access modifier makes the member visible and accessible within the class in which it is defined only; not in any subclass, not in any other class of the same package, not anywhere else. A member defined with the protected access modifier can be used in the same class, in any class of the same package, and in any subclass of the class (it does not matter in which package that subclass may be in). Q3. (a) What do you understand by the term data abstraction? [2 marks] A3. (a) Programming is done by managing complexity reducing the task into smaller manageable portions, and so on. Abstraction is a tool for managing complexity which concerns with representing the essential features without including unnecessary / implementation details. It helps in the design of a class. For example, when creating a Team class for a cricket team, we specify that we need 11 players. Whether these are implemented as a single String[] array or a set of 11 variables (player1, player2, and so on) is an implementation / design decision to be taken later on. Abstraction is used to manage complexity. Abstraction is used to make us think in terms of what is needed, and not how to get it done. Q3. (b) What will be the output of the following code: i) int m = 2; int n = 15; for(int i=1; i<=5; i++); m++; --n; System.out.println("m=" + m); System.out.println("n=" + n); [2 marks] ii) char x='A'; int m; m=(x=='a') ? 'A':'a'; System.out.println("m=" + m); [2 marks] A3. (b) i) ii) Q3. (c) Analyze the following program segment and determine how many times the loop will be executed and what will be the output of the program segment. int k=1, i=2; while(++i<6) k*=i; System.out.println(k); [2 marks] A3. (c) The loop will be executed 3 times, and the output will be 60. The 3 iterations of the loop are with i=3, 4, and 5 hence k = 1 * 3 * 4 * 5 = 60. Q3. (d) Give the prototype of a function check which received a character ch and an integern and returns true or false. [2 marks] A3. (d) boolean check(char ch, int n) Q3. (e) State two features of a constructor. [2 marks] A3. (e) 1. A constructor has the same name as the name of the class. 2. A constructor is called only when an object of a class is created using new. 3. A constructor is not defined with any return type. 4. The calls to super() and this() can be made inside a constructor only, not inside any other method. If made, these calls must be the first statement inside the constructor. Hence, if used, super() or this() cannot both be inside the same constructor. 5. Constructors can be overloaded. 6. If a class has no explicit constructor defined, Java provides a default constructor for that class. Q3. (f) Write a statement each to perform the following task on a string: i) Extract the second last character of a word stored in a variable wd. [2 marks] ii) Check if the second character of a string str is in uppercase. [2 marks] A3. (f) i) char c = wd.charAt( wd.length() 2 ); ii) if( Character.isUpperCase( str.charAt(1) ) ) { } Q3. (g) What will the following functions return when executed? i) Math.max(-17,-19) ii) Math.ceil(7.8) [2 marks] A3. (g) i) -17 ii) 8.0 Q3. (h) i) Why is an object called an instance of a class? ii) What is the use of the keyword import? A3. (h) i) An object is an instance of a class because it is the in-memory implementation of the instance variables and the methods of the class it is created from. A class can have multiple objects created, each object having its own set of instance variables. The set of values of the instance variables of an object at any point of time defines the state of that object at that point of time. Each object has its own state and the state may change with time, as the values of the instance variables change. Each object has a state that is independent of other objects of the class. ii) The keyword import is used to specify which packages or classes need to be used in a program. Some packages (and classed within them) are not loaded or included by default. The only package included by default is java.lang. So, if we want to use a class from some other package, say java.util, when we need to either use the full package name in our code like: java.util.Scanner sc = new java.util.Scanner(System.in); or we can import java.util.Scanner; and then use a shorter notation such as Scanner sc = new Scanner(System.in);. Q4. Write a program to perform binary search on a list of integers given below, to search for an element input by the user, if it is found display the element along with its position, otherwise display the message Search element not found 5,7,9,11,15,20,30,45,89,97 [15 marks] A4. import java.util.*; class Q4 { public static void main(String[] args) { int[] x = {5,7,9,11,15,20,30,45,89,97}; int searchFor = 0; String temp = ""; Scanner sc = new Scanner( System.in ); // keep asking for an element to search for // until a 0 is entered do { System.out.print("Enter an integer to search, or 0 to exit: "); searchFor = sc.nextInt(); temp = sc.nextLine(); // for the trailing newline if(searchFor != 0) { int index = binarySearch(x, searchFor); if(index >= 0) System.out.println("Value " + searchFor + " found at index " + index); else System.out.println("Search element not found"); } }while(searchFor != 0); // exit when 0 } public static int binarySearch(int[] x, int el) { int low = 0, high = x.length, mid; do { mid = (low + high) / 2; if(x[mid] == el) // search element is found return mid; // exit with the index position if(el < x[mid]) // now explore the lower half { high = mid - 1; } else // upper half { low = mid + 1; } }while(high > low); return -1; // if not found } } Sample output: Q5. Define a class student as described below: Data members/instance variables: name, age, m1, m2, m3 (marks in three subjects), maximum, average Member methods: (i) A parameterized constructor to initialize the data members (ii) To accept the details of a student (iii) To compute the average and maximum out of three marks (iv) To display the name, age, marks in three subjects, maximum and average. Write a main method to create an object of the class and call the above member methods. [15 marks] A5. import java.util.*; class student { // data members / instance variables String name; int age, m1, m2, m3; int maximum; double average; // parameterized constructor public student(String name, int age, int m1, int m2, int m3) { this.name = name; this.age = age; this.m1 = m1; this.m2 = m2; this.m3 = m3; this.maximum = 0; this.average = 0; } // Accept the student details // This is static and will use new student() // to create the student object and return the same public static student accept() { String name; int age, m1, m2, m3; Scanner sc = new Scanner(System.in); System.out.print("Enter the name: "); name = sc.nextLine(); System.out.print("Enter the age and 3 marks: "); age = sc.nextInt(); m1 = sc.nextInt(); m2 = sc.nextInt(); m3 = sc.nextInt(); // create the object student s = new student(name, age, m1, m2, m3); // return the created object return s; } // compute the maximum and average public void compute() { maximum = Math.max( Math.max(m1,m2), m3 ); average = (m1 + m2 + m3) / 3.0D; } // display the details public void display() { System.out.println("Student details"); System.out.print("Name: " + name); System.out.println(" Age: " + age); System.out.println("Marks: " + m1 + ", " + m2 + ", " + m3); System.out.println("Maximum: " + maximum + ", Average: " + average); } // entry point main method public static void main(String[] args) { // call the static method accept() which will // ask the user to enter the student details. // Then, it will create an object of the student class // and return the same. student s1 = student.accept(); // static method // Call the other methods s1.compute(); s1.display(); } } Sample output: Q6. Shasha Travels Pvt. Ltd. gives the following discount to its customers: TICKET AMOUNT DISCOUNT Above Rs. 70000 18% Rs. 55001 to 70000 16% Rs. 35001 to 55000 12% Rs. 25001 to 35000 10% less than Rs. 25001 2% Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer : Sr. No. Name Ticket charges Discount Net amount 1 (Assume that there are 15 customers, first customer is given the serial number (Sr.No.) 1, next customer 2 and so on) [15 marks] A6. import java.util.*; class Q6 { public static void main(String[] args) { Scanner sc = new Scanner( System.in ); int N = 5; // use 15 in the program // create the arrays of size N String[] name = new String[N]; int[] tktAmt = new int[N]; int[] discountAmt = new int[N]; int[] finalAmt = new int[N]; int i; // ask user to enter N name and ticket amount details for(i=0; i < N; i++) { System.out.print("Enter ticket amount and name: "); tktAmt[i] = sc.nextInt(); // remove the leading space using trim() name[i] = sc.nextLine().trim(); } // compute the discount and final amount double discountRate; for(i=0; i < N; i++) { discountRate = 2; if( (tktAmt[i] >= 25001) && (tktAmt[i] <= 35000) ) discountRate = 10; else if( (tktAmt[i] >= 35001) && (tktAmt[i] <= 55000) ) discountRate = 12; else if( (tktAmt[i] >= 55001) && (tktAmt[i] <= 70000) ) discountRate = 16; else if( tktAmt[i] > 70000 ) discountRate = 18; discountAmt[i] = (int)Math.round(tktAmt[i] * discountRate / 100.00D); finalAmt[i] = tktAmt[i] - discountAmt[i]; } // display the header printStr("Sl. No.", 7+1); printStr("Name", 20+1); printStr("Ticket charges",14+1); printStr("Discount",8+1); printStr("Net amount",10); System.out.println(); // display the detailed array for(i=0; i < N; i++) { printInt(i+1, 7+1); printStr(name[i], 20+1); printInt(tktAmt[i], 14+1); printInt(discountAmt[i], 8+1); printInt(finalAmt[i], 10+1); System.out.println(); } } // print int argument1 in a field of width argument2 public static void printInt(int n, int width) { String s = "" + n; // convert int to String printStr(s, width); } // print String argument1 in a field of width argument2 public static void printStr(String s, int width) { System.out.print(s); int spaces = width - s.length(); while(spaces > 0) { System.out.print(" "); spaces--; } } } Sample output with 5 names (for brevity): Q7. Write a menu driven program to accept a number and check and display whether it is a prime number or not OR an automorphic number or not. (Use switch-case statement) (a) Prime number: A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number. Example: 3, 5, 7, 11, 13, etc. (b) Automorphic number: An automorphic number is the number which is contained in the last digit(s) of its square. Example: 25 is an automorphic number as its square is 625 and 25 is present as the last 2 digits. [15 marks] A7. import java.util.*; class Q7 { public static void main(String[] args) { char menuOption = ' '; do // keep asking in a loop { menuOption = askMenuOption(); switch(menuOption) { case 'P': doPrime(); break; case 'A': doAutomorphic(); break; case 'X': System.out.println("Exiting program. Thank you!"); break; } }while( menuOption != 'X' ); // exit when X is entered } // Ask menu option. // Keep asking until a valid option is entered. public static char askMenuOption() { Scanner sc = new Scanner( System.in ); char option = ' '; // single space do { System.out.print("Enter P for Prime, A for Automorphic, X to Exit: "); option = sc.nextLine().toUpperCase().charAt(0); if("PAX".indexOf(option) < 0) System.out.println("Incorrect option. Try again..."); }while("PAX".indexOf(option) < 0 ); return option; } // prime number logic public static void doPrime() { int n = askInt(); int count = 2; for(int i=2; i < n; i++) if( (n%i) == 0 ) count++; if( count > 2) System.out.println(n + " is not a prime number."); else System.out.println(n + " is a prime number."); } // automorphic number logic public static void doAutomorphic() { int n = askInt(); int square = n * n; String N = "" + n; String S = "" + square; if( S.substring(S.length()-N.length()).equals(N) ) System.out.println(N + " is an automorphic number."); else System.out.println(N + " is not an automorphic number."); } // utility method to ask user to enter an int public static int askInt() { Scanner sc = new Scanner( System.in ); System.out.print("Enter an integer: "); int n = sc.nextInt(); String temp = sc.nextLine(); // for the trailing newline return n; } } Sample output: Q8. Write a program to store 6 elements in an array P, and 4 elements in an array Q and produce a third array R, containing all the elements of array P and Q. Display the resultant array. Example: P[] Q[] R[] 4 19 4 6 23 6 1 7 1 2 8 2 3 3 10 10 19 23 7 8 [15 marks] A8. import java.util.*; class Q8 { public static void main(String[] args) { int[] P = new int[6]; int[] Q = new int[4]; int[] R = new int[P.length+Q.length]; // user input of arrays P and Q populate(P, "Enter " + P.length + " values for P: "); populate(Q, "Enter " + Q.length + " values for Q: "); // merge the arrays P and Q into R int i; int rIndex = 0; for(i=0; i < P.length; i++) R[rIndex++] = P[i]; for(i=0; i < Q.length; i++) R[rIndex++] = Q[i]; // display P, Q, and R System.out.println("P[]\tQ[]\tR[]"); for(i=0; i < R.length; i++) { if(i < P.length) System.out.print(P[i]); System.out.print("\t"); if(i < Q.length) System.out.print(Q[i]); System.out.print("\t"); System.out.println(R[i]); } } // ask user to enter int values and populate the array // the prompt is displayed before user enters the values public static void populate(int[] arr, String prompt) { Scanner sc = new Scanner( System.in ); System.out.print(prompt); for(int i=0; i < arr.length; i++) arr[i] = sc.nextInt(); String temp = sc.nextLine(); // for the trailing newline } } Sample output: Q9. Write a program to input a string in uppercase and print the frequency of each character. Example: INPUT: COMPUTER HARDWARE OUTPUT: CHARACTERS FREQUENCY A 2 C 1 D 1 E 2 H 1 M 1 O 1 P 1 R 3 T 1 U 1 W 1 [15 marks] A9. import java.util.*; class Q9 { 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().replace(" ", ""); // Above, remove all occurences of space, // since that is not being counted. // Also, convert everything to uppercase. // For A through Z int[] freq = new int[26]; int i; for(i=0; i < sentence.length(); i++) { char ch = sentence.charAt(i); if( (ch >= 'A') && (ch <= 'Z') ) { int index = ch - 'A'; freq[index]++; } } // display the result System.out.println("CHARACTERS FREQUENCY"); for(i=0; i < 26; i++) // A through Z { if(freq[i] > 0) // where frequency > 0 { char c = (char)('A' + i); System.out.print(c + " System.out.println(freq[i]); } } } } Sample output: "); // 10 spaces

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