Monday, December 21, 2009

ICSE sample papers on BlueJ(1)


Today I will discuss few basic questions on BlueJ.

Question 1

(A) Give the output of the following program segment.
int x=0;
do
{
 if(x<  3)
{
 x+=2;
System.out.println(x);
continue;
}
else
{
System.out.println(++x);
break;
}
}
while(x< 10);

Output :
2
4
5

Explanation of the output

Initially value of x is 0, so within the loop body it enters the if block and the value becomes 2 (x+=2 means x=x+2). Thus the first output is 2.
After that the continue statement is encountered and again the if block is executed. So the second output is 4.

Next the control skips the if block and within else block the expression ++x with println() function means x is incremented by 1 first and then the value (5) is displayed. The break statement then causes premature termination of the loop.


The program has been written to determine a natural number n is prime or not. There are five places in the code marked by ?1?, ?2?, ?3?, ?4?, ?5? which must be replaced by expressions or statements so that the program works correctly.

public static void main(String args[])throws IOException
{
InputStrreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
int n,i,c=0;
System.out.println(”Enter No”);
n=Integer.parseInt(br.readLine());
for(i=1;?1?;i++)
{
 if(?2?= =0)
{
 ?3?;
?4?;
}
}
if(?5?)
System.out.println(”Prime No”+n);

}

(i) What is the expression or statement at ?1?     [1]i < n
(ii) What is the expression or statement at ?2?     [1]
(iii) n%i+1 (because loop control variable I starts with 1)
(iv) What is the expression or statement at ?3?     [1]C=1
(v) What is the expression or statement at ?4?     [1]break
(vi) What is the expression or statement at ?5?     [1]C==0

Question 3

Class MyArray contains an array of n intergers(n<=100) that are already arranged in ascending order. The index of the array elements very from 0 to n-1. Some of the member functions of MyArray are given below:

Class name:      MyArray

Data members      arr-an array of n integers
n-size of the array
Member functionsMyArray( )      constructor to initialize n=nn and the array arr

void readArray( ) reads n integers that are arranged in ascending order

void displayArray( ) display n integers
int binarySearch(int value) searches for the value in the array using the binary search technique. It returns the subscript of the array element if the value is found, otherwise it returns –999.

(a) Specify the class MyArray giving the details of the void displayArray( ) and int binarySearch(int value) only. You may assume that the other functions are written for you. You do not need to write the main function.

(b) What change would be necessary in the function binarySearch(int value). If the given array was arranged in descending order?
(c) When will binary search technique fail to work?

import java.io.*;
class MyArray
{
private int n;
private int arr[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
MyArray(int m)
{
n=m;arr=new int[n];
}
public void readArray()throws Exception{
for(int i=0;i<  n;i++)
{
System.out.println("Value=");
arr[i]=Integer.parseInt(br.readLine());
}
}
public void displayArray()
{
for(int i=0;i< n;i++)
{System.out.println(arr[i]);
}
}
public int binarySearch(int value)
{
int i,j,mid;i=0;j=n-1;
while(i< =j)
{
mid=(i+j)/2;
if(arr[mid]==value)
{
break;
}
else if(arr[mid] < value)
{
i=mid+1;
}
else
{
j=mid-1;
}
}
if(i < =j)return 1;
else
return 0;
}
}
For Question (b) Simply change the binarySearch() as follows

public int binarySearch(int value)
{
int i,j,mid;
i=0;
j=n-1;
while(j > =i)
{
mid=(i+j)/2;
if(arr[mid]==value)
{

break;
}
else if(arr[mid] > value)
{
i=mid+1;
}
else
{
j=mid-1;
}
}
if(i < =j)
return 1;
else
return 0;
}
}
Question C, The basic condition for binary searching is that the list must be sorted and the middle element of the list can be directly accessed. In array this is possible, but in case of linked list , the middle element can not be accessed directly. So in linked list searching binary search can not be applied.

3 comments:

  1. can i know all abt break n continue statements i cracked ma head i dint get it......

    ReplyDelete
  2. In next week you will find the answers of your query

    ReplyDelete
  3. thanks , hmm.... I wish i had found it earlier...

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner