Trending ▼   ResFinder  

ICSE 2007 solved : Computer Applications

25 pages, 8 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 2007 Q1. (a) Name two types of Java programs. [2 marks] A1. (a) Applications and Applets. Q1. (b) Define instance variable. Give an example of the same. [2 marks] A1. (b) An instance variable is a non-static variable of a class, not defined inside any method of that class. It has a separate value for each object created of that class. In the example below, c.gears is an instance variable of object c, belong to class Car. class Car { int gears; // instance variable public Car() // constructor { this.gears = 5; } public static void main(String[] args) { Car c = new Car(); System.out.println( c.gears ); } } Q1. (c) Differentiate between Binary Search and Linear Search. [2 marks] A1. (c) BINARY SEARCH LINEAR SEARCH Works on sorted lists only Can work on sorted and unsorted lists More efficient when data set is large Less efficient when data set is large Maximum number of searches: log2N Maximum number of searches: N Q1. (d) Assign the value of pi (3.142) to a variable of requisite data type. [2 marks] A1. (d) double pi = 3.142D; Q1. (e) Explain with an example, the if..else if construct. [2 marks] A1. (e) The if .. else if construct is used for conditional execution of statements. The else if is not compulsory. Zero or more else if statements can follow an if statement. At the end, an option else can be placed for a catch all if none of the earlier if and else if conditions were met. The code block of the first matching condition is executed only. The if .. else if construct can be nested as well. Example: for(int x=1; x < 10; x++) { if( (x%2) == 0 ) System.out.println(x + " is even"); else if( (x%3) == 0 ) System.out.println(x + " is odd and a multiple of 3"); else System.out.println(x + " is odd and not a multiple of 3"); } Output: Q2. (a) Differentiate between Formal Parameter and Actual Parameter. [2 marks] A2. (a) A Formal Parameter is the name given to an argument inside a method or a function. For example, in the function declaration public void f(int x), the x is a formal parameter. An Actual Parameter is the actual value passed to the function during the call to that function. For example, if we use obj.f(4) and obj.x(6), the actual values are 4 and 6. Inside the function f(), the actual value of x will be 4 and 6, which is what is passed to the function when it gets called. Q2. (b) Why do we need a constructor as a class member? [2 marks] A2. (b) A constructor is like a method with no return type. This is because a constructor always returns an object of the class it belongs to. A constructor has the same name as that of its class, and this special case is recognized by Java as a constructor that creates the object and returns the newly created object. By treating a constructor as a class member, not only can we use the new followed by the name of the class and the (), just as a member method, but we can also assign the access modifiers to that constructor to define the access rules on who can create objects of the class. Also, constructors as methods means that when an object is created, the required parameters must be passed as per the signature of the constructor, otherwise the program will not compile. So, the reasons are: 1. Create a class using a function-like call with arguments. 2. Assign access modifiers to control who can call the constructors. 3. Ensure that the signature is followed when an object is created using new. Q2. (c) Explain the term type casting. [2 marks] A2. (c) Type casting is the conversion of data from one type to another. There are two types of type casting: implicit and explicit. An implicit type cast is a safe type cast where, a value of a lower capacity, can be converted and stored as a value of a higher capacity. For example, int can be converted into a long. If int x = 5; and long y = x; we have the int x value directly getting converted and stored as a long in variable y. There is no loss of data / precision. An explicit type cast is when we need to inform the compiler that we plan to convert a potentially larger value into a smaller capacity. Normally, a compiler will not allow compilation unless we apply a type cast to the conversion. For example, if we want to convert a long to an int, we need to say long x = 5L; int y = (int)x;. This tells the compiler that we are aware of the danger of type casting and want it to happen. Q2. (d) Name the following: i) A package that is invoked by default. ii) A keyword, to use the classes defined in a package. [2 marks] A2. (d) i) java.lang ii) import Q2. (e) Name the class that is used for different mathematical functions. Give an example of a mathematical function. [2 marks] A2. (e) The Math class (java.lang.Math) is used for different mathematical functions. An example of a mathematical function is Math.sqrt() which returns a double value that is the square root of the argument passed to it. Q3. (a) State the difference between = and ==. [2 marks] A3. (a) The = operator is used for assignment, as in x = 5, where the value of 5 is assigned to the variable x. The == operator is used to check equality, as in x == 5, which returns a boolean true if the expression is true, and a false otherwise. Q3. (b) Write an equivalent Java syntax for the following expression: a = 0.05 2y3 / x y [2 marks] A3. (b) a = 0.05 2 * Math.pow(y, 3) / x y; Q3. (c) Rewrite the following using ternary operator: if(income < 10000) tax = 0; else tax = 12; [2 marks] A3. (c) tax = (income < 10000) ? 0 : 12; Q3. (d) Write a statement for each of the following: 1. Store a number 275 as a String 2. Convert the String to a numeric value 3. Add it to the existing total of 1000 to update the field [3 marks] A3. (d) 1. String s = + 275; 2. String n = Integer.parseInt(s); 3. int field = 1000; field = field + n; Q3. (e) i) What is the role of the keyword void in declaring functions? ii) If a function contains several return statements, how many of them will be executed? iii) Which OOP principle implements function overloading? [3 marks] A3. (e) i) The keyword void is used to specify that a function does not return any value or object; it has no return data type. ii) In any call to a function, only 1 return statement can be executed. If a function is void, it can still have several return; statements, but in such a case, one or none could be executed this is because if the end of the function code is reached without a return, an empty return; is assumed by the compiler; but only when the function is having a void return type. Otherwise, only 1 return statement can be executed. iii) Polymorphism Q3. (f) What is the output of the following? i) System.out.println ("four :" + 4 + 2); System.out.println ("four :" + (2+2)); [2 marks] ii) String S1 = "Hi"; String S2 = "Hi"; String S3 = "there"; String S4 = "HI"; System.out.println(S1 + "equals" + S2 + "->" + S1.equals(S2)); System.out.println(S1 + "equals" + S3 + "->" + S1 .equals(S3)); System.out.println(S1 + "equals" + S4 + "->" + S1 .equals(S4)); System.out.println(S1 + "equalsIgnoreCase" +S4 + "->" + S1.equalsIgnoreCase(S4)); [4 marks] A3. (f) i) ii) Q3. (g) Evaluate the following expressions, if the values of the variables are a = 2, b=3 and c=9 1. a (b++) * ( c) 2. a * (++b) % c [2 marks] A3. (g) i) -22 ii) 8 Q4. Define a class salary described as below:Data Members : Name, Address, Phone, Subject Specialization, Monthly Salary, Income Tax. Member methods : (i) To accept the details of a teacher including the monthly salary. (ii) To display the details of the teacher. (iii) To compute the annual Income Tax as 5% of the annual salary above Rs.1,75,000/-. Write a main method to create object of a class and call the above member method. [15 marks] A4. import java.util.*; class salary { // data members String Name, Address; long Phone; String Subject; int MonthlySalary; double IncomeTax; // accept the details of the teacher public void accept() { Scanner sc = new Scanner( System.in ); String temp; System.out.print("Enter the name: "); this.Name = sc.nextLine(); System.out.print("Enter the address: "); this.Address = sc.nextLine(); System.out.print("Enter the phone: "); this.Phone = sc.nextLong(); temp = sc.nextLine(); // for the trailing newline System.out.print("Enter the subject: "); this.Subject = sc.nextLine(); System.out.print("Enter the monthly salary: "); this.MonthlySalary = sc.nextInt(); temp = sc.nextLine(); // for the trailing newline } // display the details public void display() { System.out.println("DISPLAY"); System.out.println("Name : " + this.Name); System.out.println("Address : " + this.Address); System.out.println("Phone : " + this.Phone); System.out.println("Subject : " + this.Subject); System.out.println("Monthly Income: " + this.MonthlySalary); System.out.println("Income Tax : " + this.IncomeTax); } // compute the income tax public void compute() { int annualSalary = this.MonthlySalary * 12; double taxRate = (annualSalary > 175000) ? 5.0 : 0.0; double tax = (annualSalary - 175000) * taxRate / 100; this.IncomeTax = tax; } // main method public static void main(String[] args) { salary s = new salary(); s.accept(); s.compute(); s.display(); } } Sample output: Q5. Write a program to compute and display the sum of the following series:1 + 2 + 1 2 + 1 + 2 + 3 + 1 2 3 + .+ 1 + 2 + 3 + 4 .n 4 .n [15 marks] A5. import java.util.*; class Q5 { public static void main(String[] args) { // user input of n Scanner sc = new Scanner(System.in); System.out.print("Enter integer n: "); int n = sc.nextInt(); + 1 2 3 String temp = sc.nextLine(); // for the trailing newline int sum = 0; int i; int term1, term2; // the series starts from 2 to n, and not 1 to n!!! for(i=2; i <= n; i++) { // terms: sum of first i numbers = i*(i+1)/2 term1 = (i) * (i+1) / 2; term2 = getFactorial(i); sum += term1 + term2; } System.out.println("Sum: " + sum); } // return the factorial of the int parameter passed public static int getFactorial(int n) { int fact = 1; for(int i=2; i<=n; i++) fact = fact * i; return fact; } } Sample output: Q6. Write a program to initialize the given data in an array and find the minimum and maximum values along with the sum of the given elements. Numbers : 2 5 4 l 3 Output : Maximum Sum of the elements : 15 Minimum value [15 marks] A6. import java.util.*; class Q6 { public static void main(String[] args) { // user input value : : l 5 Scanner sc = new Scanner( System.in ); System.out.print("Enter the number of elements (N): "); int N = sc.nextInt(); String temp; temp = sc.nextLine(); // for the trailing newline // create the array int[] arr = new int[N]; // enter the elements of the array System.out.print("Enter the " + N + " elements: "); int i; for(i=0; i < N; i++) arr[i] = sc.nextInt(); temp = sc.nextLine(); // for the trailing newline // comptue min, max, sum int min = arr[0], max=arr[0], sum = arr[0]; for(i=1; i < arr.length; i++) { min = (min < arr[i]) ? min : arr[i]; max = (max > arr[i]) ? max : arr[i]; sum += arr[i]; } // display the results System.out.println("Minimum value: " + min); System.out.println("Maximum value: " + max); System.out.println("Sum of the elements: " + sum); } } Sample output: Q7. Write a program to enter a sentence from the keyboard and count the number of times a particular word occurs in it. Display the frequency of the search word. Example: INPUT: Enter a sentence Enter a OUTPUT: : the word Searched word occurs : 2 times. [15 marks] A7. quick brown fox to be jumps over searched the lazy : dog. the import java.util.*; class Q7 { public static void main(String[] args) { // user input Scanner sc = new Scanner( System.in ); System.out.print("Enter a sentence: "); String sentence = sc.nextLine(); System.out.print("Enter a word to be searched: "); String word = sc.nextLine(); // split into tokens StringTokenizer st = new StringTokenizer(sentence, " ,!.;?"); int count = 0; while( st.hasMoreTokens() ) { String token = st.nextToken(); if( token.equalsIgnoreCase(word) ) count++; } System.out.print("Search word occurs: " + count + " time"); if(count != 1) // add s if not 1 System.out.print("s"); System.out.println("."); } } Sample output: Q8. Using a switch statement, write a menu driven program to convert a given temperature from Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message should be displayed. (HINT: C = 5 9 (F 32) and F = 1.8 C + 32 ) [15 marks] A8. import java.util.*; class Q8 { public static void main(String[] args) { char menuOption = ' '; // space do { menuOption = askMenuOption(); switch(menuOption) { case 'C': C_to_F(); break; case 'F': F_to_C(); break; case 'X': System.out.println("Exiting program. Thank you!"); break; } }while( menuOption != 'X' ); } // menu - returns the option chosen // only valid options are allowed, otherwise // user has to reenter public static char askMenuOption() { Scanner sc = new Scanner( System.in ); char option = ' '; // space do { System.out.print("Enter C for C to F, F for F to C, X to Exit: "); option = sc.nextLine().toUpperCase().charAt(0); if( "CFX".indexOf(option) < 0 ) System.out.println("Incorrect option. Try again."); }while( "CFX".indexOf(option) < 0 ); return option; } // ask user to enter a double and return that int // display the prompt public static double askDouble(String prompt) { Scanner sc = new Scanner( System.in ); double n; String temp; System.out.print(prompt); n = sc.nextDouble(); temp = sc.nextLine(); // for the trailing newline return n; } // Celcius to Fahrenheit public static void C_to_F() { double C = askDouble("Enter Celcius Temperature: "); double F = (C*1.8) + 32.0; System.out.println("Fahrenheit equivalent: " + F); } // Fahrenheit to Celcius public static void F_to_C() { double F = askDouble("Enter Fahrenheit Temperature: "); double C = (F - 32.0) * 5.0 / 9.0; System.out.println("Celcius equivalent: " + C); } } Sample output: Q9. Write a program using a method Palin( ), to check whether a string is a Palindrome or not. A Palindrome is a string that reads the same from left to right and vice versa. E.g. MADAM, ARORA, ABBA, etc. [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 string: "); String s = sc.nextLine(); Palin(s); } // function to check if Palindrome public static void Palin(String s) { String rev = ""; int i; for(i=s.length() - 1; i >= 0; i--) { rev = rev + s.charAt(i); } if(rev.equalsIgnoreCase(s)) System.out.println(s + " is a Palindrome."); else System.out.println(s + " is not a Palindrome."); } } Sample outputs:

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