Trending ▼   ResFinder  

ICSE 2006 solved : Computer Applications

21 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 2006 Q1. (a) Define encapsulation. [2 marks] A1. (a) Encapsulation is the process of hiding the implementation details of a class and allowing access to the class through a public interface. Encapsulation makes it easy to maintain and modify code. For example, if we change the internal design of the class, the variable names, their data types, and so on, that change need not be known to the users of the class. They can still continue to use the same publicmethods only. Encapsulation, when used in the proper manner, makes it easy to define methods with standard names. For example, if an instance variable is called x, then the method to get the value of x is called getX(), and the method to set the value of x is called setX(). Such methods are also known as getter methods and setter methods. Q1. (b) Explain the term object using an example. [2 marks] A1. (b) An object is an instance of a class; a class is a blueprint based on which objects of that class can be created. The blueprint specifies the variables of that object as well as the behaviour of the objects of that class (methods). class Car { // instance variables int gears, speed; public Car() // constructor { gears = 5; speed = 0; } // member method public void changeSpeed(int howMuch) { speed += howMuch; } // member method public void changeGear(int direction) { gears += direction; } public static void main(String[] args) { Car c = new Car(); c.changeGear(1); c.changeSpeed(10); } } In the example above, Car is a class and c is an object of the class. There are 2 instance variables: gears and speed. There are two member methods: changeSpeed() and changeGear() that define the behaviour of the class. A constructor initializes the instance variables. Q1. (c) Define a variable. [2 marks] A1. (c) A variable is a name given to a value or to a reference of an object. For example, in the statement int x = 5; x is a variable. In the statement Car c = new Car(); c is a variable. A variable may be a variable of a primitive data type or a reference to an object of a class. Q1. (d) What is a wrapper class? Give an example. [2 marks] A1. (d) Everything in Java is an object well, almost everything in Java is an object except for the eight primitive data types. Many times, it is important to treat the values in the eight primitive data types as if they are classes (objects). Java provides eight classes for this. These classes are called wrapper classes, since they wrap primitive values and behavior inside the garb of a class. An example of a wrapper class as Integer, for the primitive data type int. Q1. (e) What is the purpose of the new operator? [2 marks] A1. (e) The new operator is used to create an object of a class. For example: Student s = new Student( Sunil ); Q2. (a) State the two kinds of data types. [2 marks] A2. (a) Primitive data types (e.g. int) and object data types of classes (e.g. String). Q2. (b) Write the corresponding expressions for the following mathematical operations :i) a2 + b2 ii) z = x3 + y3 xy -- z [2 marks] A2. (b) i) a*a + b*b Can also use, for double: Math.pow(a,2) + Math.pow(b,2) ii) Math.pow(x,3) + Math.pow(y,3) x*y/z Q2. (c) Define an inpure function. [2 marks] A2. (c) An impure function is a function that produces different results for the same set of parameters, or whose execution may result in an observable side effect. An explanation of the difference between pure and impure functions is shown in the table below: PURE FUNCTIONS IMPURE FUNCTIONS Produces the same result (return value) for the same set of Produces different result for the same set of parameters, in each call parameters, in each call The execution of the function does not result in any side The execution of the function may result in an effect (such as the change in state of a mutable object, or observable side effect. an output to an I/O device.) Example: Math.random() returns a random Example: Math.sqrt() value. Example: System.out.println() since it causes Example: Math.sin() an output to an I/O device (monitor) as a side effect. Q2. (d) Differentiate between if and switch statements. [2 marks] A2. (d) IF STATEMENT SWITCH STATEMENT condition evaluates to a boolean true or condition can be byte, short, char, int. false only. More than one code block may execute if no break statements Only one code block executes. are present. Q2. (e) What will be the output for the following program segment? [2 marks] String s = new String("abc"); System.out.println(s.toUpperCase()); A2. (e) ABC Q3. (a) What is meant by private visibility of a method? [2 marks] A3. (a) A private method of a class can be accessed (called, invoked) by other methods defined in that class only; and not by methods in sub classes of the class, in the same package as that of the class, or any other class. Q3. (b) Find and correct the errors in the following program segment :int n[] = (2,4,6,8,10); for(int i=0; i<=5; i++) System.out.println("n[" + i + "] = " + n[i]); [2 marks] A3. (b) Two errors: change the () to {} when defining the array and use i < 5 instead of i <= 5 in the loop. The correct code is below: int n[] = {2,4,6,8,10}; for(int i=0; i<5; i++) System.out.println("n[" + i + "] = " + n[i]); Q3. (c) Explain function overloading with an example. [4 marks] A3. (c) Function overloading is the ability to use the same function name within the same class with a different parameter set (signature). The signature of a function will include the name of the function along with the number and data types of its arguments; not the return value. If the signature is different, the same name can be used more than one time in a class. Refer to the example below: class Test { public static void main(String[] args) { area(4); area(5, 7); } // overloaded function #1 public static void area(int side) { // call overloaded function #2 area(side, side); } // overloaded function #2 public static void area(int length, int breadth) { int a = length * breadth; System.out.println("Area: " + a); } } The area() function is overloaded two times: the first with one parameter of type int, and the other with two parameters of type int and int. The first one calls the overloaded 2nd version of the same function, in order to keep the code small and in one place. The output of the above program would be: 16 and 35. The first, an area of a square of side 4, and the other of a rectangle of sides 5 and 7. Q3. (d) Find the output of the following program segment, when: i) val = 500 ii) val = 1600 int val, sum, n=550; sum = n + val > 1750 ? 400 : 200; System.out.println(sum); [2 marks] A3. (d) Based on the operator precedence table, + has higher precedence over ?:. Therefore, first (n+val) gets computed and that result is checked with 1750. This is equivalent to: n+val > 1750 ? 400:200 gives (n+val) > 1750 ? 400:200. i) 200 (n=550, val=500, n+val=1050, condition is false, answer is 200) ii) 400 (n=550, val=1600, n+val=2150, condition is true, answer is 400) Q3. (e) What is a default constructor? [2 marks] A3. (e) A default constructor is a constructor provided to a class automatically by Java, provided no constructor is explicitly written for that class. A default constructor: has zero arguments makes an implicit call to the parent class constructor using super() initializes are primitive data members to 0, 0.0, false, etc. initializes all object reference data members to null Q3. (f) What will be the output for the following program segment? int a=0, b=30, c=40; a = --b + c++ + b; System.out.println("a=" + a); [2 marks] A3. (f) a=98 Working: a= 30 + c++ + b = 29 + 40++ + b = 29 + 40 + 29 = 98 Q3. (g) Differentiate between compareTo() and equals() methods. [2 marks] A3. (g) COMPARETO() EQUALS() Returns an integer value. Returns a boolean value. If two Strings are same, If two Strings are the same, returns a 0. returns a true. If two Strings are different, returns the difference in ASCII of the first different If two Strings are different, character. In case a String is a subset of another String, it will return a +1 or a returns a false. -1 based on which String is the subset. Applies to all classes, since Applies to any class that extends the Comparable interface. it is defined in the Object class. Q3. (h) What is a package? Give an example. [2 marks] A3. (h) A package is a name given to a collection of classes. It is a mechanism for organizing classes and preventing name conflicts between classes. Java packages are stored as compressed JAR files; this also allows for faster downloading of class files as a group. An example package is java.lang. Another is java.util. Q3. (i) Explain the function of a return statement. [2 marks] A3. (i) A return statement has two functions: 1. When a function has a return data type of void: it exits from the function and returns control back to the calling program. 2. When a function has a return data type other that void: It returns a value or an object reference back to the calling program. Q4. Write a program to calculate and print the sum of odd numbers and the sum of even numbers for the first n natural numbers. [15 marks] A4. import java.util.*; class Q4 { public static void main(String[] args) { // user entry Scanner sc = new Scanner( System.in ); System.out.print("Enter an integer n: "); int n = sc.nextInt(); String temp = sc.nextLine(); // for the trailing newline // compute int sumOdd = 0, sumEven = 0; int i; for(i=1; i <= n; i++) { if( (i%2) == 1 ) sumOdd += i; else sumEven += i; } // display the results System.out.println("Sum of odd numbers : " + sumOdd); System.out.println("Sum of even numbers: " + sumEven); } } Sample output: Q5. A cloth showroom has announced the following festival discounts on the purchase of items, based on the total cost of the items purchased :TOTAL COST DISCOUNT (IN PERCENTAGE) Less than Rs. 2000 5% Rs. 2001 to Rs. 5000 25% Rs. 5001 to Rs. 10000 35% Above Rs. 10000 50% Write a program to input the total cost and display the amount to be paid by the customer after availing the discount. [15 marks] A5. import java.util.*; class Q5 { public static void main(String[] args) { // user entry Scanner sc = new Scanner( System.in ); System.out.print("Enter the total cost of purchase: "); int cost = sc.nextInt(); String temp = sc.nextLine(); // for the trailing newline // compute double discountRate, discountAmt, finalAmt; discountRate = 2.0; if( (cost > 2000) && (cost <= 5000) ) discountRate = 25.0; else if( (cost > 5000) && (cost <= 10000) ) discountRate = 35.0; else discountRate = 50.0; discountAmt = cost * discountRate / 100.0; finalAmt = cost - discountAmt; // display the results System.out.println("Total cost : " + cost); System.out.println("Discount % : " + discountRate); System.out.println("Discount Amt.: " + discountAmt); System.out.println("Final Price : " + finalAmt); } } Sample outputs: Q6. Consider the following statement: January 26 is celebrated as the Republic Day of India . Write a program to change 26 to 15, January to August, Republic to Independenceand finally print August 15 is celebrated as the Independence Day of India . [15 marks] A6. class Q6 { public static void main(String[] args) { String sentence = "January 26 is celebrated as the Republic Day of India"; String newSentence = sentence.replace("26","15").replace("January","August").replace("Republic" ,"Independence"); System.out.println( newSentence ); } } Output: Q7. Write a program that outputs the results of the following evaluations based on the number entered by the user. i) Natural logarithm of the number ii) Absolute value of the number iii) Square root of the number iv) Random number between 0 and 1 [15 marks] A7. import java.util.*; class Q7 { public static void main(String[] args) { // number entry Scanner sc = new Scanner( System.in ); System.out.print("Enter a number: "); double n = sc.nextDouble(); String temp = sc.nextLine(); // for the trailing newline // display the values System.out.println("Natural log : " + Math.log(n)); System.out.println("Absolute value: " + Math.abs(n)); System.out.println("Square root : " + Math.sqrt(n)); System.out.println("Random number : " + Math.random()); } } Sample output: Q8. The marks obtained by 50 students in a subject are tabulated as follows:Name Marks . . . . Write a program to input the names and the marks of the students in the subject. Calculate and display:[15 marks] A8. import java.util.*; class Q8 { public static void main(String[] args) { // variables int N = 5; // number of students String[] names = new String[N]; int[] marks = new int[N]; int i; int sum = 0, maxIndex = 0; double average; // user input Scanner sc = new Scanner(System.in); for(i=0; i < N; i++) { System.out.print("Enter marks and name of student#" + (i+1) + ": "); marks[i] = sc.nextInt(); names[i] = sc.nextLine().trim(); } // compute for(i=0; i < N; i++) { maxIndex = (marks[i] > marks[maxIndex]) ? i : maxIndex; sum += marks[i]; } average = sum / (double)N; // display System.out.println("Subject average marks: " + average); System.out.println("Highest mark: " + marks[maxIndex] + " obtained by "); for(i=0; i < N; i++) if(marks[i] == marks[maxIndex]) System.out.print(names[i] + " "); System.out.println(); } } Sample output: Q9. Write a program to accept 15 integers from the keyboard, assuming that no integer entered is a zero. Perform selection sort on the integers and then print them in ascending order. [15 marks] A9. import java.util.*; class Q9 { public static void main(String[] args) { // user input Scanner sc = new Scanner( System.in ); int[] arr = new int[15]; String temp; int i, j; System.out.print("Enter " + arr.length + " integers: "); for(i=0; i < arr.length; i++) arr[i] = sc.nextInt(); temp = sc.nextLine(); // for the trailing newline // do selection sort int maxIndex; // index of max value int t; // temp variable for swapping for(i=0; i < (arr.length-1); i++) { maxIndex = 0; // assume 0 is the max value index for(j=1; j <= (arr.length-1-i); j++) { if(arr[j] > arr[maxIndex]) maxIndex = j; } // do the swap t = arr[maxIndex]; arr[maxIndex] = arr[arr.length-1-i]; arr[arr.length-1-i] = t; } // display the sorted output System.out.print("Sorted output: "); for(i=0; i<arr.length; i++) System.out.print( arr[i] + " "); System.out.println(); } } 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 ...

 

  Print intermediate debugging step

Show debugging info


 

 

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

 

manumansi chat