Sunday 8 March 2015

Solved Theory Questions of ICSE Board Exam Part-1

Question 1.
(a) Which of the following are valid comments? [2]
(i) /* comment */
(ii) /*comment
(iii) //comment
(iv) */ comment */
Ans. (i) and (iii) are valid comments. (i) is a multi line comment and (iii) is a single line comment.
(b) What is meant by a package? Name any two Java Application Programming Interface packages. [2]
Ans.
 Related classes are grouped together as a package. A package provides namespace management and access protection.
Some of the Java API packages – java.lang, java,util and java,io
(c) Name the primitive data type in Java that is:
(i) a 64-bit integer and is used when you need a range of values wider than those provided by int.
(ii) a single 16-bit Unicode character whose default value is ‘\u0000′ [2]
Ans.
 (i) long
(ii) char
(d) State one difference between floating point literals float and double. [2]
Ans.
 (i) Float requires 4 bytes of storage while double requires 8 bytes.
(ii) Float is of single precision while double is of double precision.
(e) Find the errors in the given program segment and re-write the statements correctly to assign values to an integer array. [2]
1int a = new int5 );
2forint i=0; i<=5; i++ ) a[i]=i;
Ans. The corrected program segment is
1int a = new int[5];
2forint i=0; i<=4; i++ ) a[i]=i;
Error 1: The size of an array is specified using square brackets [] and not parentheses ().
Error 2: Since the array is of length 5, the indices range from 0 to 4. The loop variable i is used as an index to the array and in the given program segment, it takes values from 0 to 5. a[4] would result in an ArrayIndexOutOfBoundsException. Therefore, the loop condition should be changed to i<=4. The given program segment creates an array of size 5 and stores numbers from 0 to 4 in the array.
Question 2.
(a) Operators with higher precedence are evaluated before operators with relatively lower precedence. Arrange the operators given below in order of higher precedence to lower precedence. [2] (i) && (ii) % (iii) >= (iv) ++
Ans.
 ++ , % , >= , &&
(b) Identify the statements listed below as assignment, increment, method invocation or object creation statements. [2]
(i) System.out.println(“Java”);
(ii) costPrice = 457.50;
(iii) Car hybrid = new Car();
(iv) petrolPrice++;
Ans. (i) Method Invocation – println() is a method of System.out.
(ii) Assignment – The value 457.70 is being assigned to the variable costPrice.
(iii) Object Creation – An object of type Car is created and stored in the variable hybrid.
(iv) Increment – The variable petrolPrice is incremented.
(c) Give two differences between switch statement and if-else statement. [2]
Ans.
 (i) Switch statement can be used to test only for equality of a particular variable with a given set of values while an if-else statement can be used for testing arbitrary boolean expressions.
(ii) If else can handle only primitive types and String while if-else can handle all data types.
(d) What is an infinite loop? Write an infinite loop statement. [2]
Ans.
 An infinite loop is a loop whose body executes continuously.
Infinite Loop using while:
1while(true) {
2}
Infinite Loop using for
1for( ; ;){
2}
Infinite loop using do-while
1do {
2while(true);
(e) What is a constructor? When is it invoked? [2]
Ans.
 A constructor is a member function of a class used to perform initialization of the object. It is invoked during object creation.
Question 3.
(a) List the variables from those given below that are composite data types. [2]
(i) static int x;
(ii) arr[i] = 10;
(iii) obj.display();
(iv) boolean b;
(v) private char chr;
(vi) String str;
Ans. The primitive data types in Java are byte, short, int, long, float, double, boolean and char. All other data types – arrays, objects and interfaces – are composite data types.
(i) x is of a primitive data type
(ii) arr is variable of a composite data type of type int. i can be a variable of a primitive data type (byte, short or int) or of a composite data type (Byte, Short, Integer)
(iii) obj is a variable of a composite data type
(iv) b is a variable of a primitive data type
(v) chr is a variable of a primitive data type
(vi) str is a variable of a composite data type – String which is of class type
(b) State the output of the following program segment: [2]
1String str1 = "great"; String str2 = "minds";
2System.out.println(strl.substring(0,2).concat(str2.substring(l)));
3System.out.println(("WH" + (strl.substring(2).toUpperCase())));
Ans. Output is
1grinds
2WHEAT
Explanation for first print statement:
strl.substring(0,2) gives “gr”
str2.substring(l) “inds”
The two on concatenation gives “grinds”
Explanation for second print statement
strl.substring(2) gives “eat”
“eat” on conversion to upper case using the function toUpperCase() results in EAT
“WH” on concatenation with “EAT” using + gives “WHEAT”
(c) What are the final values stored in variables x and y below? [2]
1double a = - 6.35;
2double b = 14.74;
3double x = Math.abs(Math.ceil(a));
4double y = Math.rint(Math.max(a,b));
Ans. Math.ceil() gives the smallest of all those integers that are greater than the input. So, Math.ceil(-6.35) gives -6.0 (not -6 as ceil returns a double value). Math.abs() gives the absolute value of the number i.e. removes negative sign, if present. So, Math.abs(-6.0) results in 6.0. So, x will be 6.0.
Math.max(-6.35, 14.74) returns 14.74. rint() returns the double value which is closest in value to the argument passed and is equal to an integer. So, Math.rint(14.74) returns 15.0. So, y will hold 15.0.
(d) Rewrite the following program segment using the if-else statements instead of the ternary operator. [2]
1String grade = (mark >= 90) ? "A" : (mark >= 80) ? "B" "C";
Ans.
1String grade;
2if(marks >= 90) {
3grade = "A";
4else if( marks >= 80 ) {
5grade = "B";
6else {
7grade = "C";
8}
(e) Give output of the following method: [2]
1public static void main(String[] args) {
2int a = 5;
3a++;
4System.out.println(a);
5a -= (a--) - (--a);
6System.out.println(a); }
Ans.
16
24
a++ increments a from 5 to 6. The first print statement prints the value of a which is 6.
a -= (a–) – (–a);
is equivalent to
a = a – ( (a–) – (–a) )
a = 6 – ( 6 – 4 )
a = 6 – 2
a = 4
(f) What is the data type returned by the library functions: [2]
(i) compareTo()
(ii) equals()
Ans.
 (i) int
(ii) boolean
(g) State the value of characteristic and mantissa when the following code is executed. [2]
1String s = "4.3756";
2int n = s.indexOf('.');
3int characteristic = Integer.parseInt(s.substring(0,n));
4int mantissa = Integer.valueOf(s.substring(n+1));
Ans. n = 1
characteristic = Integer.parseInt(s.substring(0,n)) = Integer.parseInt(“4.3756″.substring(0,1)) = Integer.parseInt(“4″) = 4
characteristic = 4
mantissa = Integer.valueOf(“4.3756″.substring(1+1)) = Integer.valueOf(“3756″) = 3756
mantissa = 3756
(h) Study the method and answer the given questions. [2]
1public void sampleMethod()
2forint i=0; i<3; i++ )
3forint j=0; j<2; j++)
4int number = (int)(Math.random() * 10);
5System.out.println(number); } } }
(i) How many times does the loop execute?
(ii) What is the range of possible values stored in the variable number?
Ans. 
(i) The outer loop executes 3 times ( i= 0, 1, 2) and for each execution of the outer loop, the inner loop executes two times ( j= 0, 1). So, the inner loop executes 3 * 2 = 6 times
(ii) Math.random() returns a value between 0 (inclusive) and 1 (exclusive).
Math.random() * 10 will be in the range 0 (inclusive) and 10 (exclusive)
(int)(Math.random() * 10) will be in the range 0 (inclusive) and 9 (inclusive) (only integer values)
(i) Consider the following class: [2]
1public class myClass {
2public static int x = 3, y = 4;
3public int a = 2, b = 3; }
(i) Name the variables for which each object of the class will have its own distinct copy.
(ii) Name the variables that are common to all objects of the class.
Ans.
 For static variables, all objects share a common copy while for non static variable, each object gets its own copy. Therefore, the answer is
(i) a, b
(ii) x, y
(j) What will be the output when the following code segments are executed? [2]
(i)
1String s = "1001";
2int x = Integer.valueOf(s);
3double y = Double.valueOf(s);
4System.out.println("x=" +x);
5System.out.println("y=" +y);
(ii)
1System.out.println("The King said \"Begin at the beginning!\" to me.");
Ans. (i) s = “1001″
x = 1001
y = 1001.0
Output will be
1x=1001
2y=1001.0
(ii) The King said “Begin at the beginning!” to me.
\” is an escape sequence which prints ”

No comments:

Post a Comment