(a) Analyze the following program segment and determine how many times the loop will be executed and what will be the output of the program segment?

ICSE Class 10 Computer Applications PYQ · 2011 Paper · Section A

31 Mark · 2011 · Section A · 2011 Paper

(a) Analyze the following program segment and determine how many times the loop will be executed and what will be the output of the program segment?

int p = 200;
while(true){
    if(p < 100)
        break;
    p = p - 20;
}
System.out.println(p);
Show SolutionHide Solution↓
80

The loop executes 7 times

pRemarks
2001st Iteration
1802nd Iteration
1603rd Iteration
1404th Iteration
1205th Iteration
1006th Iteration.
807th Iteration. Now p < 100 becomes true so break statement is executed terminating the loop.

(b) What will be the output of the following code?

(i)

int k = 5, j = 9;
k += k++ - ++j + k;
System.out.println("k = " + k);
System.out.println("j = " + j);

(ii)

double b = -15.6;
double a = Math.rint(Math.abs(b));
System.out.println("a = " + a);

(i)

k = 6
j = 10

    k += k++ - ++j + k
⇒ k = 5 + (5 - 10 + 6)
⇒ k = 5 + 1
⇒ k = 6

++j increments j to 10

(ii)

16.0

Math.abs(-15.6) gives 15.6. Math.rint(15.6) gives 16.0.

(c) Explain the concept of constructor overloading with an example.

Constructor overloading is a technique in Java through which a class can have more than one constructor with different parameter lists. The different constructors of the class are differentiated by the compiler using the number of parameters in the list and their types. For example, the Rectangle class below has two constructors:

class Rectangle {
    int length;
    int breadth;

    //Constructor 1
    public Rectangle() {
        length = 0;
        breadth = 0;
    }

    //Constructor 2
    public Rectangle(int l, int b) {
        length = l;
        breadth = b;
    }

    public static void main(String[] args) {
        //Constructor 1 called
        Rectangle obj1 = new Rectangle();

        //Constructor 2 called
        Rectangle obj2 = new Rectangle(10, 15);
    }
}

(d) Give the prototype of a function search which receives a sentence 'sent' and word 'w' and returns 1 or 0.

int search(String sent, String w)

(e) Write an expression in Java for:

$z = \frac{5x^3 + 2y}{x + y}$

z = (5 * x * x * x + 2 * y) / (x + y)

(f) Write a statement each to perform the following tasks on a string:

  1. Find and display the position of the last space in a string s.
  2. Convert a number stored in a string variable x to double data type.
  1. System.out.println(s.lastIndexOf(" "));
  2. double num = Double.parseDouble(x);

(g) Name the keyword that:

  1. informs that an error has occurred in an input/output operation.
  2. distinguishes between instance variable and class variable.
  1. throws IOException
  2. static

(h) What are library classes? Give an example.

Java Library classes are a set of predefined classes in the form of packages that are provided to the programmer as part of Java installation. Library classes simplify the job of programmers by providing built-in methods for common and non-trivial tasks like taking input from user, displaying output to user, etc. For example, System class in java.lang package of Java Library classes provides the print() and println() methods for displaying output to user.

(i) Write one difference between Linear Search and Binary Search.

Linear search works on unsorted as well as sorted arrays whereas Binary search works on only sorted arrays.

← Previous questionNext question →

Browse the free Computer Applications sample bank