Trending ▼   ResFinder  

ICSE 2005 solved: Computer Applications

29 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 2005 Q1. (a) Name any two OOP principles. [2 marks] A1. (a) Encapsulation, Inheritance, Abstraction, Polymorphism. Q1. (b) Mention two different styles of expressing a comment in a program. [2 marks] A1. (b) Two styles of comments are: i) Single line comment using //, for example: // This is a single-line comment ii) Multi line comment using /* and */, for example: /* This is a multi-line comment that spans across 4 lines. */ Q1. (c) Which element is num[9] of the array num? [2 marks] A1. (c) The 10th element, since the array index numbering starts from 0. Q1. (d) Differentiate between operator and expression. [2 marks] A1. (d) An operator is a symbol that acts on one, two, or three operands to form an expression that has a value. An expression is a combination of operators and operands. For example, x + y is an expression which has the binary + operator that combines with two operands x and y and adds their values to give a resultant value. Q1. (e) If m=5 and n=2 output the values of m and n after execution in (i) and (ii) :- i) m -= n; ii) n = m + m/n [2 marks] A1. (e) i) 3 Working: m -= n => m = m n = 5 2 = 3 ii) 7 Working: n = m + m/n = 5 + 5/2 = 5 + 2 = 7 Q2. (a) Explain the term loop with an example. [2 marks] A2. (a) Loop is a construct that provides conditional iteration of a code block repeatedly. There are 3 types of loops in Java: for loop, while loop, and do while loop. int i; for(i=1; i<=3; i++) System.out.println(i); In the above for loop, there are 3 sections. The initialization section sets the loop variable i to 1. Next is the condition section where the condition i <= 3 is evaluated. If this results in a boolean true, the loop body (printing the value of i) is executed; else the loop is terminated. Once the loop body iteration gets over, the 3rd section (iteration section) is executed, where i++ makes the value of i increase by 1 from 1 to 2, the condition i <= 3 gets checked again, and the process repeats. In the above case, the process repeats for i being 1, 2, and 3. Afterwards, i becomes 4, the condition becomes false, and control is transferred outside of the loop. The output of the above loop would be the numbers 1, 2 and 3 on separate lines. Q2. (b) What is a compound statement? Give an example. [2 marks] A2. (b) A compound statement is a group of statements enclosed in braces (curly brackets: { and }). In the example below, the code inside the while block is a compound statement of 2 statements. int i = 1; while(i < 4) { // compount statement below System.out.println(i); i++; } Q2. (c) State the difference between function and constructor. [2 marks] A2. (c) FUNCTION CONSTRUCTOR Called anytime Called when an object of a class is created Called by a program code Called by Java whenever an object is created Has a return data type, even void if it does Does not have any return type specified. not return anything. Name is different from that of its class Name is same as the name of the class Use of super() and this() are permitted, but as the first Use of super() and this() are not permitted. statement of the constructor only. If no constructor is written for a class, Java provides a default There is nothing like a default function constructor for that class. Q2. (d) State one similarity and one difference between while and do .. while loop. [2 marks] A2. (d) Similarity: The break and continue statements can be used within the while and do .. while loops. Another similarity is that the condition to be checked must evaluate into a boolean value. They are generally preferred for usage over the for loop when the number of iterations is not known. Difference: The while loop body may not execute even once, whereas the do .. while loop body executes at least once. This is because the condition in the while loop is checked at the start of the loop, and the condition in the do .. while loop is checked at the end of the loop. Q2. (e) Explain, with the help of an example, the purpose of default in a switch statement. [2 marks] A2. (e) The switch statement is like an if else if else statement. The default clause is used like the final else clause, to execute its block only if none of the other conditions are satisfied via the case statements. char ch = 'C'; switch( ch ) { case 'C': System.out.println("Centigrade to Fahrenheit"); break; case 'F': System.out.println("Fahrenheit to Centigrade"); break; default: System.out.println("Wrong option entered"); break; } If the value of ch is C or F , the appropriate code blocks will execute. If the value is none of those, the code block associated with default will get executed and the incorrect message output will be displayed. Q3. (a) What will be the output of the following if x=5 initially? i) 5 * ++x ii) 5 * x++ [2 marks] A3. (a) i) 30 Working: 5 * ++x = 5 * ++5 = 5 * 6 = 30 ii) 25 Working: 5 * x++ = 5 * 5++ = 5 * 5 = 25 Q3. (b) What is the output of the following? char c = 'A'; short m = 26; int n = c+m; System.out.println(n); [2 marks] A3. (b) 91 Working: n = c+m = A + 26 = 65 + 26 = 91. Q3. (c) Explain the meaning of break and continue statements. [3 marks] A3. (c) The break statement is used to stop execution of a loop iteration and transfer control out of the loop immediately. The continue statement is used to stop execution of the current loop iteration and transfer control to the end of the loop body, and perform the iteration and/or condition checking for the next iteration of the loop. for(int i=1; i<100; i++) { if( (i%10) == 0 ) break; if( (i%3) == 0 ) continue; System.out.println(i); } Above, when i becomes 10, the loop breaks and terminates. For the values 1 through 9, whenever i%3 == 0, which means for 3, 6, and 9, the continue executes and the println() does not execute. The output will be the numbers 1, 2, 4, 5, 7, 8 on separate lines. Q3. (d) i) What is call be value? ii) How are the following passed? 1. Primitive types 2. Reference types [3 marks] A3. (d) i) Call be value is when a copy of the value of the variable is passed to the function as an argument. Any change to the copy in the function will not change the original variables value. It is used for primitive data type passing only, in Java. ii) 1. Call by value 2. Call by reference Q3. (e) Enter any two values through constructor with parameters and write a program to swap and print the values. [4 marks] A3. (e) class Point { // instance variables int x, y; // constructor public Point(int a, int b) { this.x = a; this.y = b; } // swap function public void swap() { int temp = x; x = y; y = temp; } // display the values public void display() { System.out.println( "Point = (" + x + "," + y + ")"); } // entry point main method public static void main(String[] args) { Point p = new Point(3, 4); p.display(); // values before swapping p.swap(); p.display(); // values after swapping } } Output: Q3. (f) What do the following functions return for: String x = "hello"; String y = "world"; 1. System.out.println(x + y); 2. System.out.println(x.length()); 3. System.out.println(x.charAt(3)); 4. System.out.println(x.equals(y)); [4 marks] A3. (f) 1. helloworld (no spaces) 2. 5 3. l (lowercase L) 4. false Q3. (g) Differentiate between toLowerCase() and toUpperCase() methods. [2 marks] A3. (g) The toLowerCase() and toUpperCase() methods return String objects that are the lowercase and uppercase equivalents of the original String object. For example: Hello .toLowerCase() will evaluate to hello and Hello .toUpperCase() will evaluate to HELLO Q4. Write a class with name employee and basic as its data member, to find the gross pay of an employee for the following allowances and deduction. Use meaningful variables. Dearness Allowance = 25% of House Rent Allowance = 15% Provident Fund = 8.33% Net Pay = Basic Pay + Dearness Allowance Gross Pay = Net Pay Provident Fund [15 marks] A4. import java.util.*; class employee { // data members String name; double basic; // constructor public employee(String name, double basic) { this.name = name; this.basic = basic; } the of Basic Pay Basic Pay of Basic Pay + House Rent Allowance // compute and display the gross pay public void computeGross() { double da = 0.25 * this.basic; double hra = 0.15 * this.basic; double pf = .0833 * this.basic; double net = this.basic + da + hra; double gross = net - pf; System.out.println("Dearness allowance: " + da); System.out.println("HRA : " + hra); System.out.println("Net pay : " + net); System.out.println("PF deduction : " System.out.println("Gross pay : " + gross); + pf); } // entry point main method public static void main(String[] args) { // ask user to enter the details of an employee Scanner sc = new Scanner( System.in ); System.out.print("Enter name: "); String n = sc.nextLine(); System.out.print("Enter basic: "); double b = sc.nextDouble(); String temp = sc.nextLine(); // for the trailing newline // create the object employee e = new employee(n, b); e.computeGross(); } } Sample output: Q5. Write a program to input any given string to calculate the total number of characters and vowels present in the string and also reverse the string :Example : INPUT Enter String : SNOWY OUTPUT Total number Number of Reverse string of characters Vowels : YWONS [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 string: "); String s = sc.nextLine(); // compute int noVowels = 0; String rev = ""; for(int i=0; i<s.length(); i++) { : : 05 01 char c = s.toUpperCase().charAt(i); switch(c) { case 'A': case 'E': case 'I': case 'O': case 'U': noVowels++; break; } // reversal c = s.charAt( s.length() - 1 - i ); rev = rev + c; } // disp;ay System.out.println("Total number of characters: " + s.length()); System.out.println("Number of Vowels : " + noVowels); System.out.println("Reverse string : " + rev); } } Sample output: Q6. Write a program using a function called area() to compute the area of a : (i) circle( * r2) where = 3.14 (ii) square(side*side) (iii) rectangle ( length*breadth) Display the menu to output the area as per User s choice. [15 marks] A6. import java.util.*; class Q6 { public static void main(String[] args) { char menuOption = ' '; // space double radius, side, length, breadth; do { menuOption = askMenuOption(); switch(menuOption) { case 'R': // Rectangle length = askDouble("Enter the length: "); breadth = askDouble("Enter the breadth: "); area(length, breadth); // overloaded function #1 break; case 'S': // Square side = askDouble("Enter the side: "); area(side, side); // overloaded function #1 break; case 'C': // Circle radius = askDouble("Enter the radius: "); area(radius); // overloaded function #2 break; case 'X': System.out.println("Exiting program. Thank you!"); break; } }while( menuOption != 'X' ); } // Ask user to enter menu option. // If invalid option, ask to enter again. // Return the option selected public static char askMenuOption() { Scanner sc = new Scanner( System.in ); char option = ' '; // space do { System.out.print("Enter R-Rectangle, S-Square, C-Circle, XExit: "); option = sc.nextLine().toUpperCase().charAt(0); if( "SRCX".indexOf(option) < 0 ) System.out.println("Incorrect option. Try again..."); }while( "SRCX".indexOf(option) < 0 ); return option; } // Utility method to display a prompt, // ask user to enter a double value and // return that value. public static double askDouble(String prompt) { Scanner sc = new Scanner( System.in ); System.out.print(prompt); double d = sc.nextDouble(); String temp = sc.nextLine(); // for the trailing newline return d; } // overloaded area function #1 public static void area(double length, double breadth) { System.out.println("Area: " + (length*breadth)); } // overloaded area function #2 public static void area(double radius) { System.out.println("Area: " + (3.14 * Math.pow(radius,2))); } } Sample output: Q7. Write program to bubble sort the following set of values in ascending order: 5,3,8,4,9,2,1,12,98,16 OUTPUT : 1 2 3 4 5 8 9 12 16 98 [15 marks] A7. import java.util.*; class Q7 { public static void main(String[] args) { // initialize the array as given in the question int[] x = {5,3,8,4,9,2,1,12,98,16}; // perform the bubble sort int i, j; for(i=0; i < (x.length-1); i++) for(j=0; j < (x.length-1-i); j++) if( x[j] > x[j+1] ) { // do the swap int temp = x[j]; x[j] = x[j+1]; x[j+1] = temp; } // display the sorted array for(i=0; i < x.length; i++) System.out.println( x[i] ); } } Output: Q8. Write a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd numbers from a list of numbers(N) entered by the user. The list terminates when the user enters a zero. [15 marks] A8. import java.util.*; class Q8 { public static void main(String[] args) { // variables to hold the sums int sumNegative = 0, sumPositiveEven = 0, sumPositiveOdd = 0; // loop to ask until a 0 is entered int nextNumber; for(nextNumber = askInt(); nextNumber != 0; nextNumber = askInt()) { if(nextNumber < 0) // negative sumNegative += nextNumber; else if( (nextNumber%2) == 0 ) // positive even sumPositiveEven += nextNumber; else if( (nextNumber%2) == 1 ) // positive odd sumPositiveOdd += nextNumber; } // display the result System.out.println("Sum of negative numbers : " + sumNegative); System.out.println("Sum of positive even numbers: " + sumPositiveEven); System.out.println("Sum of positive odd numbers : " + sumPositiveOdd); } // utility method to ask integer from keyboard // and return the same back. public static int askInt() { Scanner sc = new Scanner( System.in ); System.out.print("Enter an integer (0 to exit): "); int n = sc.nextInt(); String temp = sc.nextLine(); // for the trailing newline return n; } } Sample output: Comments: The for loop needs some explanation. The initialization section asks for a number. The condition section checks if it is not 0; if it is 0, then the loop stops. Else, the loop executes, and the iteration section is reached, where the askInt() method is used to ask the user to enter another number, which will then be checked by the condition section, and so on. Q9. Write a program to initialize an array of 5 names and initialize another array with their respective telephone numbers. Search for a name input by the User, in the list. If found, display Search Successful and print the name along with the telephone number, otherwise display Search unsuccessful. Name not enlisted . [15 marks] A9. import java.util.*; class Q9 { public static void main(String[] args) { // local variables Scanner sc = new Scanner( System.in ); int N = 5; int i; // the 2 arrays as local variables String[] names = new String[N]; long[] phones = new long[N]; String temp; int lastSpaceIndex; // user input for the 2 arrays for(i=0; i<names.length; i++) { System.out.print("Enter the name and phone #" + (i+1) + ": "); // entry on a single line // the phone number is after the last space temp = sc.nextLine(); lastSpaceIndex = temp.lastIndexOf(" "); names[i] = temp.substring(0, lastSpaceIndex).toUpperCase(); phones[i] = Long.parseLong( temp.substring(lastSpaceIndex+1) ); } // ask the user to enter a name to search // user to enter QUIT to exit. while(true) { System.out.print("Enter a name to search for (QUIT to exit): "); String searchFor = sc.nextLine().toUpperCase(); if(searchFor.equals("QUIT")) break; int searchIndex = -1; for(i=0; i < names.length; i++) if( names[i].equals(searchFor) ) { searchIndex = i; break; } if( searchIndex < 0 ) System.out.println("Search unsuccessful. Name not enlisted."); else System.out.println("Name: " + names[searchIndex] + " has Tel No: " + phones[i]); } } } Sample output:

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