In this sample paper on BlueJ, some questions and answers are given. The continuation of this sample paper will be the next post.
Question : Write any two rules for naming variable.
Answer : Variable names must start with alphabet and keywords can not be used in variable names. These are two rules for naming variable.
Question : Write any two differences in Implicit Type Casting and Explicit Type Casting.
Answer :In implicit type casting, variables of different types are converted into one type if they are used in any expression where as in explicit casting, variables of one type are to be converted to other type using coding.
Question : Write a main difference between while loop and do while loop in BlueJ.
Answer :While loop is an entry controlled loop where do while is an exit control loop. This is the main difference between while and do-while loop.
Question : Give an example of conditional operator.
If (a >b)
{
max=a;
}
else
{
max=b;
}
This above statement can be written as
max=(a>b)?a:b;
Read the following program and answer the given questions :
class demo{
public void main( ) {
int a,b,i,j;
for(i=1;i< 5;i=i+1)
{
for(j=1;j< =I;j=j+1)
{
System.out.print(“ “+j);
}
System.out.println( );
}}}
( i ) How many times the first loop of i will perform? Ans: 4 times (ii) How many times the first print statement work ? Ans: 10 times
(iii) What will be the output of program ?
Output would be like:
1
1 2
1 2 3
1 2 3 4
Write the output of following statements : class output
{
public void fmn( )
{
int a=12,b=10;
System.out.println (“a++=”+a++);
System.out.println (“a>b=”+a>b);
System.out.println (“++a=”+++a);
System.out.println (“--a=”+--a);
System.out.println (“a!=(a+b)=”+a!=(a+b));
}
}
Ans: a++=12
a>b=true
++a=14
--a=13
a!=(a+b)=true
Find the errors if any in the following program :
class 12design
{
public void mn ()
{
byte 1a=10;
int b=20;
int c=a>b?a,b;
System.out.println(‘The value of c is :’+c);
}
}
Ans: Class name should start with alphabet, so class name 12design will generate compile time error.
byte 1a; variable name must start with alphabet.
int c=a>b?a,b; this should be int c=a>b?a:b;
The value of c is: should be kept within “ “.
Explain the bubble sort technique in array with an example.
Bubble sort technique compares consecutive values and if necessary exchange the values between them. In every pass it starts from the first location (starting index of the array) . This technique arranges the values from the last location, so on every pass it reduces the checking from right end. The main advantage of this sorting process is that if no exchange is found in any pass, the process is terminated.
Suppose the values in an array of size 5 are as follows:
4 2 3 66 5 and we want to arrange them ascending orer.
In the first pass
array[0]>array[1] exchange. Now the array is 2 4 3 66 5
array[1]>array[2] No exchange
array[2]>array[3] No exchange
array[3]>array[4] exchange. Now the array is 2 4 3 5 66
This is the end of first pass and the highest value is set in the last location. In the second pass array[3] and array[4] will not be compared.
In the start of second pass the array is 2 4 3 5 66
array[0]>array[1] No exchange
array[1]>array[2] exchange. Now the array is 2 3 4 5 66
array[2]>array[3] No exchange
End of second pass and the array is now 2 3 4 5 66
Third pass
array[0]>array[1] No exchange
array[1]>array[2] No exchange
End of third pass and as there is no exchange, the process is terminated.
No comments:
Post a Comment