Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Arrays Operations In Java

Mcq

What will be the output of the following program? A. 12


class ArrayOutput 15
{ 16
    public static void main(String s[]) 17
    { 19
        int i[] = {12, 15, 16, 17, 19}; B. 12
15
        for(int i = 0; i < 5; i++) 16
        { 17
            System.out.println(i[i]); C. 15
        } 16
    } 17
} 19
D. Compilation error
Mcq
class ArrayOutput
{ A. Sum= 40
    public static void main(String s[]) B. Sum= 30
    { C. Sum= 10
        int a[] = new int[50]; D. Throws
ArrayIndexOutOfBoundsExcep
        int i = 27 % 11;
        int j = 5 * 3; tion
        int k = i - j;

        a[i] = i;
        a[j] = j;
        a[k] = k;

        int sum = 0;

        for(int l = 0; l < a.length; l++)
        {
            sum += a[l];
        }

        System.out.println("Sum = " + sum);
    }
}
Mcq

What will be the output of the following program? A. 12


class ArrayOutput 15
{ 16
    public static void main(String s[]) 17
    { 19
        int a[] = {12, 15, 16, 17, 19}; B. 12
17
        for(int i = 0; i < 5; i += 2) C. 12
        { 16
            System.out.println(a[i]); 19
        } D. Compiler Error
    }
}
QUESTION 01

In Java arrays are

A. Objects
B. Object references
C. Primitive data type
D. None of the above

Answer : A
QUESTION 02

What will be the output of the following program?


class ArrayOutput A. 10
{
public static void main(String s[]) 100
{
int i = 50; 50
int[] a = new int[10];
System.out.println(a.length); B. 10
a = new int[100];
System.out.println(a.length); 10
a = new int[i]; 10
System.out.println(a.length);
} C. Compilation Error
}
D. Throws
ArrayIndexOutOfBoundsException

Answer: A
QUESTION 03

What is the output of the following program


A. My Array length is: 3
public class ArrayOutput Student Marks Array length
{ is: 39
public static void main(String s[])
{ B. My Array length is: 39
int student_marks[] = new int[3]; Student Marks Array length
student_marks = new int[]{10, 20, 30};
int[] my_array = null; is: 39
my_array = student_marks; C. My Array length is: 3
System.out.println("My Array length is: " + Student Marks Array length
my_array.length);
System.out.println("Student Marks Array length is: 3
is: " + student_marks.length); D. Some other output
}
}
Answer : C
QUESTION 05
What will be the output of the following program?
class Main A. 50
{ 100
public static void main(String s[]) 100
{ B. 50
int i = 50;
int[] a = new int[i]; 100
System.out.println(a.length); 100
System.out.println(a.length); C. 50
System.out.println(a.length); 50
} 50
}
D. Compilation
Error

Answer : C
QUESTION 07
What will be the order of the array elements at the end of the program?

public class Main A. 12, 15, 11, 13, 9, 25


{ B. 12, 11, 13, 9, 15, 25
public static void main(String[] args)
{ C. 9, 11, 12, 13, 15, 25,
int[] array = {12, 15, 11, 13, 9, 25}; D. Throws
for (int i = 0; i < array.length - 1; i++) ArrayIndexOutOfBoundsException
{
if (array[i] > array[i + 1])
{
int temp = array[i];
array[i] = array[i + 1]; Answer: C
array[i + 1] = temp;
}
}
}
}
QUESTION 09
What will be the output of the following program
class Main
{
public static void main(String[] args) A. sum = 58
{ B. sum = 8
int a[][] = {{1, 3, 4}, {2, 3}, {7, 6, 5, 4, 3, 2, 1}, C. sum = 73
{9}, {8}}; D. Throws
int sum = 0;
for(int i = 0; i < a.length; i++) ArrayIndexOutOfBoundsExcepti
{ on
for(int j = 0; j < a[0].length; j++)
{
sum += a[i][j];
} Answer: D
}
System.out.println("sum = " + sum);
}
}
QUESTION 10
What will be the output of the following program?
public class Main A. sum = 5
{
public static void main(String[] args) B. Sum=38
{ C. sum=15
int[] a = {12, 15, 11, 13, 9, 25};
int[] a2 = {12, 15, 11, 13, 9, 25};
D. sum12
int sum = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] % 3>0) Answer: B
{
sum += i * i;
}
}
System.out.println("sum = " + sum);
}
}
QUESTION 11
What will be the output of the following program?

class Main A. 12
{
public static void main(String 15
s[]) 16
{ 17
int a[] = {12, 15, 16, 17, 19
19}; B. 12
for(int i = 0; i < 5; i++) 15
{ 16
System.out.println(a[i]); 17 Answer: A
} C. 15
} 16
}
17
19
D. Compilation
error
QUESTION 12
What will be the output of the following program?
class Main int sum = 0;
{ A. Sum=20
public static void for(int l = 0; l < B. Sum=30
main(String s[]) a.length; l++)
{ { C. Sum=40
int a[] = new sum += a[l]; D. Compilation error
int[50]; }

int i = 27 % 11; ANSWER: A


int j = 5 * 3; System.out.println("Sum =
int k = i - j; " + sum);
}
a[i] = i; }
a[j] = j;
//a[k] = k;
QUESTION 13
What will be the output of the following program?
class Main
{ A. 1874563
public static void main(String s[]) B. 1874563
{ 1874563
int list[] = new int[] {1, 8, 7, 4, 5, 6, 3}; 1874563
int count = 1; 1874563
int copy[][] = new int[list.length][count];
for(int i = 0; i < list.length; i++) 1874563
{ C. 1111111
copy[i][0] = list[i]; 8888888
for(int j = 0; j < count; j++) 7 7 7 7 7 7 7 
{ 4444444
System.out.print(copy[i][j] + " ");
} D. Compilation Error
}
System.out.println(); Answer: A
}
}

You might also like