# Output Test Questions for Java/BlueJ

Set I

Time: 45 Minutes                                              Marks: 45

(Note: Write down your answer neatly only on the space provided to you. In some problem you loose your marks if you didn't show Dry run/working. Do not erase/change your answer. No pencil/eraser will be allowed on this paper.)
Q1. The following function is a part of a class. It computes x raised to the power n. Here x is the base and n is the power. Some part is replaced by ______, with numbering 1 to 5, fill this part by expressions or statements so that program works correctly. 
double integralPower(double x, int n)
{
int inverse = 0;
double result = 1.0;
if (n == 0)
return ___1___;
else if (n < 0)
{
inverse = 1;
n = ___2___;
}
for(int i=1; ___3___; i++)
{
double temp =___4___;
result = ___5___;
}
return result;
}
1 ->  1
2 ->  Math.abs(n)
3 ->  i<=n
4 -> (inverse==0)?x:1/x
5 -> result*temp
Q2. If the function trial() is member of some class.
public static void trial()
{
int item[]={9,0,4,1,16};
int j,k;
for(j=0,k=0;j<4;j++,k++)
{
if(item[k]<item[k+1])
{
int a = item [k] ;
item [k] = item [k+1];
item [k+1] = a;
}
}
}
(i) State the final value of j, k at the end of function. 
(ii) What are the values stored in the array item[] after this function is executed? 
(iii) In one line say what is being done by the function trial() state the technique used.  
(i) j = 4 k = 4
(ii) 9 4 1 16 0
(iii) The function move the lowest number to the end of the array. The name of the technique used is called bubble sort.
Q3. What will be the output of the following programs 
public class q2
{
int a=10;
public void myfunc(int a,int b)
{
a=a+b;
a = a;
System.out.println(this.a+"\t"+a+"\t"+b);
}
public void main()
{
int a=20,b=30;
q2 x=new q2();
x.myfunc(a,b);
System.out.println(this.a+"\t"+a+"\t"+x.a);
}
}
10 50 30
10 20 10
Q4. What will be the output of the following program  [3+2]
public class q3
{
public static void main()
{
int r;
for(int i=1;i<5;i++)
{
r=output(i);
System.out.print(r+" ");
}
}
private static int output(int num)
{
if(num==1)
return(1);
else
return (num + output(num-1));
}
}
What type of function "output()" is called and what care should be taken when we write the definition of this type of function?
1 3 6 10
The function "output()" is called recursive function. While writing this type of function the function definition part must contains a condition to stop the process.
Q5. State the output of the following code segment:
[A]
public class q5      
{
public static void main(String args[])
{
int result=0;
for(int i=0;i<=5;i++)
result=result+nextNum(i);
System.out.println(result);
}
public static int nextNum(int curr)
{
return (curr%2==0)?curr:0;
}
}
What is the above function do?  

[B]
public class q6        
{
public static void sampleMethod()
{
int x=10,y=10;
System.out.println(x==y);
}
}

[C]
public class q6        
{
public static void sampleMethod()
{
int x=50,y=20;
x=(x<y)?x+y:x-y;
System.out.println(x);
}
}
[A] 6
The above function print sum of all even numbers from 0 to 5.
[B] true
[C] 30
Q6. Write the output after each pass (only five pass), if we apply insertion sort algorithm in descending order on following numbers: 2,8,1,9,4,3    
Pass I    8 2 1 9 4 3
Pass II   8 2 1 9 4 3
Pass III  9 8 2 1 4 3
Pass IV  9 8 4 2 1 3
Pass V   9 8 4 3 2 1
Set II

Time: 45 Minutes                                              Marks: 45

(Note: Write down your answer neatly only on the space provided to you. In some problem you loose your marks if you didn't show Dry run/working. Do not erase/change your answer. No pencil/eraser will be allowed on this paper.)
Q1. The following function is part of a class, which finds the lowest common devisor (except 1) of two given numbers. Some part is replaced by _____, with numbering 1 to 5, fill this part by expressions or statements so that program works correctly. 
public class lcd
{
public int main(int num1,int num2)
{
int small, res, found;
small = ___1___;
res = 2;
found = 0;
while(found==0 && ___2___)
{
if(num1%res==0 && num2%res==0)
___3___;
else
___4___;
}
if(found==0)
return(___5___);
else
return(res);
}
}
1 -> (num1>num2)?num2:num1
2 -> res<=small
3 -> found=1
4 -> res++
5 -> num1*num2
Q2. The following public function is part of some class. Assume n is always positive. Answer the given questions. Give the dry run/working.
public static int unknown(int n)
{
int i,k;
if(n%2==0)
{ i=n/2; k=1; }
else
{ k=n; n--; i=n/2; }
while(i>0)
{ k=k*i*n; i--; n--; }
return(k);
}
i. What value will be returned by the expression unknown(7) 
ii. What value will be returned by the expression unknown(6)
iii. In one line say what is being computed by the function 
(I) 5040
(II) 720
(III) The above function calculates the Factorial of a given number.
Q3. What will be the output of the following program?   
public class q2
{
int a=10;
public void myfunc(int a,int b)
{
a=a+b;
this.a = a;
System.out.println(this.a+"\t"+a+"\t"+b);
}
public void main()
{
int a=20,b=30;
q2 x=new q2();
x.myfunc(a,b);
System.out.println(this.a+"\t"+a+"\t"+x.a);
}
}
50 50 30
10 20 50
Q4. What will be the output of the following program?   [3+2]
public class q6
{
public void sampleMethod(int val)
{
int n=500;
int res=n+val>1750?400:200;
System.out.println(res);
}
}
What will be the result if val=2000 and val=1000.
400
200
Q5. State the output of the following code segment:
[A]
public class q6     
{
public static void sampleMethod()
{
int k=20,l=1;
int j=--k+2*k+(l=k);
System.out.println(j);
}
}

[B]
public class q6      
{
public static void sampleMethod()
{
System.out.println(1101 & 1100);
System.out.println(1101 | 1100);
System.out.println(1101 ^ 1100);
}
}

[C]
public class q6    
{
public static void sampleMethod()
{
int i=17;
i>>=2;
System.out.println(i);
}
}

[D]
public class q6    
{
public static void sampleMethod()
{
System.out.println(13 & 25);
}
}

[E]
public class q6    
{
public static void sampleMethod()
{
int x=10,y=20;
if((x=5)>10)
System.out.println(x);
else
System.out.println(y);
}
}
[A] 76
[B]
1100
1101
0001
[C] 4
[D] 9
[E] 20
Q6. Write the output after each pass (only five pass), if we apply selection sort algorithm in descending order on following numbers 2, 8, 1, 9, 4, 3 
Pass I     9 8 1 2 4 3
Pass II    9 8 1 2 4 3
Pass III   9 8 4 2 1 3
Pass IV   9 8 4 3 1 2
Pass V    9 8 4 3 2 1
Set III

Time: 45 Minutes                                              Marks: 40

(Note: Write down your answer neatly only on the space provided to you. In some problem you loose your marks if you didn't show Dry run/working. Do not erase/change your answer. No pencil/eraser will be allowed on this paper.)
Q1. The public function find() has been written to do a binary search for the integer m in the sorted integer array data. The array is sorted in ascending order. If m is present in data then it returns 1 else 0. Some part is replaced by ______, with numbering 1 to 5, fill this part by expressions or statements so that program works correctly. 
int find(int m, int data[], int size)
{
/* data is a sorted array of integers arranged in ascending order. size gives the number of elements in the array. m is the integer which we are trying to search in the array.*/
int found = 0;
int j = 0, k = ___1___;
int i=(j+k)/2;
if(___2___)
{
while(___3___)
{
i=(j+k)/2;
if(m==data[i])
{
___4___; break;
}
else
{
if(___5___) j=i+1;
else k=i-1;
}
}
}
return(found);
}
1 -> size-1
2 -> data[(j+k)/2]>m
3 -> j<=k && found==0
4 -> found=1
5 -> data[I]<m
Q2. The following public function is part of some class. Assume n is always positive. Answer the given questions. Give the dry run/working.  [4+4+2]
int someFn(int a, int b)
{
int ans,sm,la;
if(a<b) { sm=a; la=b; }
else { sm=b; la=a; }
ans=la;
while(ans%sm!=0)
ans+=la;
return(ans);
}
What will be the output of the following function for?
(i) someFn(7,5) (ii) someFn(6,8)
(iii) In one line state what the function someFn() doing?
(i) 35
(ii) 24
(iii) The above functions find the LCM of two given value.
Q3. What will be the output of the following program?  
[A]
public class q2
{
int a=10;
public void myfunc(int a,int b)
{
a=a+b;
a = this.a;
System.out.println(this.a+"\t"+a+"\t"+b);
}
public void main()
{
int a=20,b=30;
q2 x=new q2();
x.myfunc(a,b);
System.out.println(this.a+"\t"+a+"\t"+x.a);
}
}

[B]
Give the output of the following program segment 
String x = new String("LOYOLA");
char c; String p=""; int i;
for(i=0;i<x.length();i++)
{
c=x.charAt(i);
p=c+p;
System.out.println(p);
}

[C]
Give the output of the following program segment  
int x=1, i=2;
do
{
x*=i;
}while(++i<=5);
System.out.println(x);

[D]
What will be the output of the following method  
public class qwe
{
static int a=100,b=200;
public static void main()
{
int count,c;
for(count=1;count<=5;++count)
{
c=4*count*count;
System.out.println(function(c));
}
}
static int function(int x)
{
int c ;
c = (x < 50) ? ( a + x ) : ( b - x ) ;
return(c);
}
}
[A]
10 10 30
10 20 10

[B]
L
OL
YOL
OYOL
LOYOL
ALOYOL

[C] 120

[D]
104
116
136
136
100
Q4. Write the output after each pass (only five pass), if we apply bubble sort algorithm in descending order on following numbers: 2,8,1,9,4,3   
Pass I    8 2 9 4 3 1
Pass II   8 9 4 3 2 1
Pass III  9 8 4 3 2 1
Pass IV   9 8 4 3 2 1
Pass V    9 8 4 3 2 1
Set IV

Time: 45 Minutes                                              Marks: 40

(Note: Write down your answer neatly only on the space provided to you. In some problem you loose your marks if you didn't show Dry run/working. Do not erase/change your answer. No pencil/eraser will be allowed on this paper.)
Q1. Given an array of 6 elements [2,6,12,20,30,42], the difference of successive elements may be different of previous numbers are as follows. The following program supposed to print out the following output.
2 6 12 20 30 42
4 6 8 10 12
2 2 2 2
0 0 0
0 0
0
Some part is replaced by ____, with numbering 1 to 5, fill this part so that program works correctly. 
public class elements
{
public static void main()
{
int a[]={2,6,12,20,30,42};
int n=___1___;
for(int i=0;i<6;i++)
{
System.out.println();
for(int j=0;j<___2___;j++)
System.out.print(" "+a[j]);
___3___;
for(int j=0;j<n;j++)
a[j]=___4___ - ___5___;
}
}
}
1 -> 6
2 -> n
3 -> n--
4 -> a[j+1]
5 -> a[j]
Q2. What will be the output of the following programs?
[A]
public class qwe    
{
static int a=100,b=200;
public static void main()
{
int count,c;
for(count=1;count<=5;++count)
{
c=4*count*count;
System.out.println(function(c));
}
}
public static int function(int x)
{
int c = (x < 50)?( a + x ):( b - x );
return(c);
}
}

[B]
public class q2    
{
int a=10;
public void myfunc(int a,int b)
{
a=a+b;
a = a;
System.out.println(this.a+"\t"+a+"\t"+b);
}
public void main()
{
int a=20,b=30;
q2 x=new q2();
x.myfunc(a,b);
System.out.println(this.a+"\t"+a+"\t"+b);
}
}

[C]
public class q4    
{
public static void main()
{
String s="Object";
int l=s.length();
for(int c=0;c<l;c++)
{
if(Character.isLowerCase(s.charAt(c)))
System.out.print(Character.toUpperCase(s.charAt(c)));
else if(c%22==0)
System.out.print('E');
else
System.out.print(Character.toLowerCase(s.charAt(c)));
}
}
}

[D]
public class q1    
{
public static void main()
{
int x[]={60,50,30,40},y=3,size=4;
for(int i=size-1;i>=0;i--)
switch(i)
{
case 0:
case 2: System.out.println(y*x[i]);break;
case 1:
case 3: System.out.println(y+x[i]);
}
}
}

[E]
public class xyz    
{
public static void main()
{
int a,b,w,x,y,z;
a=10;b=8;
w = ++a - b-- ;
System.out.println(w);
a=10;b=8;
x = a % b++;
System.out.println(x);
a=10;b=8;
a*=b+5;
System.out.println(a);
z=69>>>2;
System.out.println(z);
}
}

[F]
public class op3    
{
public static void work( )
{
char ch=' ';
String test= new String("abcd");
String test2=test;
if(test.equals(test2)) {
if(test==test2) {
System.out.println("not equal");
ch=test.charAt(0); }
else {
System.out.println("not equal");
ch=test.charAt(1); } }
else {
if(test==test2)
ch=test.charAt(3); }
System.out.println(ch);
}
}
[A]
104
116
136
136
100

[B]
10 50 30
10 20 30

[C] EBJECT

[D]
43
90
53
180

[E]
3
2
130
17

[F]
not equal
a
Q3. Write the output after each pass (only five pass), if we apply insertion sort algorithm in ascending order on following numbers: 2,8,1,9,4,3   
Pass I    2 8 1 9 4 3
Pass II   1 2 8 9 4 3
Pass III  1 2 8 9 4 3
Pass IV   1 2 4 8 9 3
Pass V    1 2 3 4 8 9
 Set V Time: 1 Hours                                                Marks: 50 (Note: Write down your answer neatly only on the space provided to you. In some problem you loose your marks if you didn't show Dry run/working. Do not erase/change your answer. No pencil/eraser will be allowed on this paper.) Q1. What is the output of the following program?         [2x4=8] public class check {   public static void change(String nm)   {     nm = "Kush";   }   public static void main()   {     String name = "Sandeep";     System.out.println(name);     change(name);     System.out.println(name);   } } Sandeep Sandeep public class output1 {   static int m=0,n=0;   public static void main()   {     int m=10, x=20;     {       int n=30;       System.out.println("m+n="+(m+n));     }     x+=m+n;     System.out.println("x="+x);   } } m+n=40 x=30 public static void main() {   String x = new String("AMIT");   char c; String p=""; int i;   for(i=0;i>>2 (i) 3                   (ii) 2               (iii)  130                  (iv)  17 Q5. Write the java expressions for each of the following:      (i)  x = e|2x - 4x|   (ii) s=`/sin a + tan-1 a - e2x (i) Math.exp(Math.abs(2*x-4*x))              (ii) Math.sqrt(Math.sin(a)+Math.atan(a)-Math.exp(2*x)) Q6. Find out the output of each of the following programs. (Note: Dry run or working is necessary)      public class t100 {   public static void main()   {     int i=1,n=6,f=1;     while(i<=n)     {       f=f*i; i++;       System.out.print(f+" ");     }   } } 1  2  6  24  120  720 public class t200 {   public static void main()   {     int i,n=5,s=0;     double f=0;     for(i=n;i>0;i--)     {       s=i*i;       f=(Math.pow(s,2))-i;       System.out.println(f);     }   } } 620.0 252.0 78.0 14.0 0.0 Q7. Given an array of 6 elements [2,6,12,20,30,42], the difference of successive elements may be different of previous numbers are as follows. The following program supposed to print out the following output.   2 6 12 20 30 42   4 6 8 10 12   2 2 2 2   0 0 0   0 0   0 Some part is replaced by ____, with numbering 1 to 5, fill this part so that program works correctly.  public class elements {   public static void main()   {     int a[]={2,6,12,20,30,42};     int n=___1___;     for(int i=0;i<6;i++)     {       System.out.println();       for(int j=0;j<___2___;j++)         System.out.print(" "+a[j]);       ___3___;       for(int j=0;j0)     {       k=k*i*n;       i--;       n--;     }     return(k);   } } 120            it calculate the factorial of a number. public class output3 {   public static void main()   {     int n=5732,result=0;     do     {       result *= 10;       int digit = n%10;       result += digit ;       n = n/10;     }while(n>0);     System.out.println("Output = " + result);   } } Output = 2375                 it reverse the number.