Short Q2
Short Q2
What is the result of compiling and running the Write the output of the following code
following program String animal=”unknown”;
class example int weight=700;
{ char sex=’m’;
public void method(int k) double colorWaveLength=1.630;
{ if(weight>=500)
System.out.println(k); animal=”elephant”;
} if(colorWaveLength>1.621)
public int method(int i) animal=Gray”+amimal;
{ if(sex>=’f’)
Systme.out.println(i+1); animal=”female”+animal;
return i+1; Systme.out.println(“The animal is a “+ animal);
}
public static void main(string [] args) Answer the following:
{ a) A keyword used to call a package in the program
example e=new example(); b) what are the two ways of invoking function?
e.method(5); A array with elements 18,13,2,9,5 become 13,2,9,18,5 after three
} consecutive presses, whish sort is being implemented in the above
} array?
Debug the error and rewrite the following function prototype, i) int sum(x,y); ii) flaot operate(int x,flaot 3.5f)
Predict the output int a=5, b=8; a+=a++ + ++a + (b++ + ++a) +a+ b; System.out.println(a);
Sumedh has designed to code to check if the number is Give the output
a prime number of not, He has written the following int a[]={2,4,6,6,19};
code but the code is giving wring output. Rewrite the a[0]=23;
code for the correct output. a[3]=a[1];
boolean flag=false; int c=a[0]+a[3];
for( int i=2;i<=num;i++) Systme.out.println(“Sum=”+c);
{ Sam executes the following program segment and the answer
if(num%i==0) displayed is zero, irrespective of any non-zero value are
flag=true; given. Name the error, How the program can be modified to
} get the correct answer.
if(flag) void triangle(double b, double h)
Syustem.out.println(“prime number”); {
else double a;
System.out.println(“Not a prime number”); a=1/2*b*h;
System.out.println(“Area=”+a);
Rewrite the following program segment using if –else statements instead of the ternary operator:
T=(x+y<z || x+z<=y || y+z<=x)? “Triangle is not possible”:”Triangle is possible”;
Declare and initialized a one dimensional int array named evenNums with five elements. Use an initializer list that
contains the first five even integers, staring with 11 and print 4th elements.