Assignment 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1)

    a)
public class Class1
{
public static void main(String[] args)
{
int []b={2,5,6,3,8,5};
for(int i=1;i<6;i++) //adding 2 with elements in array, but
exclude first element.
b[i]=b[i]+2; //finally b=[2,7,8,5,10,7]
for(int i=5;i>0;i--) //access the fifth element ,then print
the array reverse order
System.out.println(b[i]); //[7,10,5,8,7]
//here also avoid first element
}
}
output
b)
{
public static void main(String[] args)
{
int a[][]=new int[2][3];
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)

{
a[i][j]=(i+j)*(i-j);
/* a[0][0]=0
a[0][1]=-1
a[0][2]=-4
a[1][0]=1
a[1][1]=0
a[1][2]=-3
*/
}
}
}
}
output
c) the output of the c part is error, Some exception will occured. The output is as follows.

This is because,

public void calculate(int a[])


        {
           a[0]=a[5]*2;
            a[1]=a[2]+5;
            a[2]=a[3]*2;
            a[3]=a[1]+a[0];
            a[4]=a[4]*3;
        }
}
The bold part is the error, because here we are accessing index element 5, There is no index
element, that is why exception occured.
The length of the array is 5, but it start from 0,
d)

This will also give error,

2)

a) ama
b) //empty space
c)Cd
d)-1 //since there is no matched char 'M' present
e) SARAKhalid
OUTPUT
3)

public class Main


{
public static void main(String[] args) {
int[] egArray={2,4,6,8,10,1,3,5,7,9};
for(int index=egArray.length-1;index>=0;index--)
{
System.out.print(egArray[index]+" ");
}
}
}

OUTPUT
4)

public class Main


{
public static void main(String[] args) {
int[] array=new int[10];
for(int index=0;index<array.length;index++)
{
array[index]=2*index;
System.out.print(array[index]+" ");
}
}
}

OUTPUT
5) options B,C are valid
B) float average[];
C) double[] marks;
6)
Option D and E are correct

i.e D) number[2] is 0 and E) length of the array is 5

number[0] =0,number[1]=0, number[2]=0,number[3]=0,number[4]=0 and number[5] = index out of


bounds exception

7) option b ----> catch block will take appropriate action during exceptions


8) option a ---> Throwable is correct
The parent class of Exception and Error is Throwable

9) char []streetAddress=new char[80];


10) int[] NUM=new int[]{10,20,30,40,50,60,70,80,90,100};

You might also like