Sunday 8 March 2015

Solved Theory Questions of ICSE Board Exam Part-2

(a) What is the difference between an object and a class? [2]
Ans. 1. A class is a blueprint or a prototype of a real world object. It contains instance variables and methods whereas an object is an instance of a class.
2. A class exists in the memory of a computer while an object does not.
3. There will be only one copy of a class whereas multiple objects can be instantiated from the same class.
(b) What does the token ‘keyword’ refer to in the context of Java? Give an example for keyword. [2]
Ans. Keywords are reserved words which convey special meanings to the compiler and cannot be used as identifiers. Example of keywords : class, public, void, int
(c) State the difference between entry controlled loop and exit controlled loop. [2]
Ans. In an entry controlled loop, the loop condition is checked before executing the body of the loop. While loop and for loop are the entry controlled loops in Java.
In exit controlled loops, the loop condition is checked after executing the body of the loop. do-while loop is the exit controlled loop in Java.
(d) What are the two ways of invoking functions? [2]
Ans. If the function is static, it can be invoked by using the class name. If the function is non-static, an object of that class should be created and the function should be invoked using that object.
(e) What is difference between / and % operator? [2]
Ans. / is the division operator whereas % is the modulo (remainder) operator. a / b gives the result obtained on diving a by b whereas a % b gives the remainder obtained on diving a by b.
Question 2.
(a) State the total size in bytes, of the arrays a [4] of char data type and p [4] of float data type. [2]
Ans. char is two bytes. So a[4] will be 2*4=8 bytes.
float is 4 bytes. So p[4] will be 4*4=16 bytes.
(b) (i) Name the package that contains Scanner class.
(ii) Which unit of the class gets called, when the object of the class is created? [2]

Ans. (i) java.util
(ii) Constructor
(c) Give the output of the following: [2]
1String n = “Computer Knowledge”;
2String m = “Computer Applications”;
3System.out.println(n.substring (0,8). concat (m.substring(9)));
4System.out.println(n.endsWith(“e”));
Ans. n.substring(0,8) gives “Computer”. m.substring(9) gives “Applications”. These two on concatenation gives “ComputerApplications”. n ends with “e”. So, it gives true.
The output is:
1ComputerApplications
2true
(d) Write the output of the following: [2]
(i) System.out.println (Character.isUpperCase(‘R’));
(ii) System.out.println(Character.toUpperCase(‘j’));

Ans. (i) true
(ii) J
(e) What is the role of keyword void in declaring functions? [2]
Ans. void indicates that the function doesn’t return any value.
Question 3
(a) Analyse the following program segment and determine how many times the loop will be executed and what will be the output of the program segment ? [2]
1int p = 200;
2while(true)
3{
4if (p<100)
5break;
6p=p-20;
7}
8System.out.println(p);
Ans.p values changes as follows : 200, 180, 160, 140, 120, 100, 80. So, the loop executes six times and value of p is 80.
(b) What will be the output of the following code? [2]
(i)
1int k = 5, j = 9;
2k += k++ – ++j + k;
3System.out.println("k= " +k);
4System.out.println("j= " +j);
Ans.
1k = 6
2j = 10
Explanation:
k += k++ – ++j + k
k = k + k++ – ++j + k
k = 5 + 5 – 10 + 6
k = 6
j = 10 as it has been incremented in the ++j operation.
(ii) [2]
1double b = -15.6;
2double a = Math.rint (Math.abs (b));
3System.out.println("a= " +a);
Ans.
1a =16.0
Maths.abs(-15.6) will give 15.6 and Math.rint(15.6) gives 16.0.
(c) Explain the concept of constructor overloading with an example . [2]
Ans. A class can have more than one constructor provided that the signatures differ. This is known as constructor overloading.
Example:
1class Age {
2 
3    int age;
4 
5    public Age() {
6        age = -1;
7    }
8 
9    public Age(int age) {
10        this.age = age;
11    }
12}
(d) Give the prototype of a function search which receives a sentence sentnc and a word wrd and returns 1 or 0 ? [2]
Ans.
1public boolean function ( sentence sentnc, word wrd )
or
1public int function ( sentence sentnc, word wrd )
(e) Write an expression in Java for z = (5×3 + 2y ) / ( x + y) [2]
Ans.
1z = ( 5 * Math.pow ( x, 3 ) + 2 * y ) / ( x + y )
(f) Write a statement each to perform the following task on a string: [2]
(i) Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type
Ans.
 (i) System.out.println(s.lastIndexOf(” “);
(ii) Double.parseDouble(x)
(g) Name the keyword that: [2]
(i) informs that an error has occurred in an input/output operation.
(ii) distinguishes between instance variable and class variables.
Ans.
 (i) throw
(ii) static
(h) What are library classes ? Give an example. [2]
Ans.
 Library classes are the predefined classes which are a part of java API. Ex: String, Scanner
(i) Write one difference between Linear Search and Binary Search . [2]
Ans.
 Linear search can be used with both sorted and unsorted arrays. Binary search can be used only with sorted arrays.