Java Arrays: Wrong

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Java Arrays

Last Updated : 07 Mar, 2018

Question 1 WRONG

Which of the following is FALSE about arrays on Java

A java array is always an object

Length of array can be changed after creation of array

C Arrays in Java are always allocated on heap

Java Arrays
Discuss it

Question 1 Explanation:
In Java, arrays are objects, they have members like length. The length member is
final and cannot be changed. All objects are allocated on heap in Java, so arrays
are also allocated on heap.

Question 2 CORRECT

Predict the output?

// file name: Main.java


public class Main {
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
We use cookies to ensure you have the best browsing experience on our website. By using our site,
System.out.print(" " + arr[i]); Got It !
you acknowledge that you have read and understood our Cookie
▲ Policy & Privacy Policy
}
}
}
Run on IDE

10 20 30 40 50

B Compiler Error

C 10 20 30 40

Java Arrays
Discuss it

Question 2 Explanation:
It is a simple program where an array is first created then traversed. The important
thing to note is, unlike C++, arrays are first class objects in Java. For example, in the
following program, size of array is accessed using length which is a member of arr[]
object.

Question 3 WRONG

class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Run on IDE

0
0

garbage value
B garbage value

Compiler
We use cookies Error
to ensure you have the best browsing experience on our website. By using our site,
Got It !
you acknowledge that you have read and understood our Cookie
▲ Policy & Privacy Policy
D Exception

Java Arrays
Discuss it

Question 3 Explanation:
In Java, it is not allowed to put the size of the array in the declaration because an
array declaration specifies only the element type and the variable name. The size is
specified when you allocate space for the array. Even the following simple program
won't compile.
class Test {
public static void main(String args[]) {
int arr[5]; //Error
}
}

Question 4 WRONG

class Test {
public static void main(String args[]) {
int arr[] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Run on IDE

0
0

garbage value
garbage value

C Compiler Error

D Exception
We use cookies to ensure you have the best browsing experience on our website. By using our site,
Got It !
you acknowledge that you have read and understood our Cookie
▲ Policy & Privacy Policy
Java Arrays
Discuss it

Question 4 Explanation:
Java arrays are first class objects and all members of objects are initialized with de-
fault values like o, null.

Question 5 WRONG

public class Main {


public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];

int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}
Run on IDE

Compiler Error

0
1 2
3 4 5
6 7 8 9

C 0
0 0to ensure you have the best browsing experience on our website. By using our site,
We use cookies
Got It !
you acknowledge that you have read and understood our Cookie
▲ Policy & Privacy Policy
0 0 0
0 0 0 0

9
7 8
D 4 5 6
0 1 2 3

Java Arrays
Discuss it

Question 5 Explanation:
In Java, we can create jagged arrays. Refer Jagged Array in Java for details.

Question 6 WRONG

Output of following Java program?

class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}
}
Run on IDE

Same

Not Same

Java Arrays
Discuss it

We use cookies to ensure you have the best browsing experience on our website. By using our site,
Got It !
you acknowledge that you have read and understood our Cookie
▲ Policy & Privacy Policy
Question 6 Explanation:
See http://www.geeksforgeeks.org/compare-two-arrays-java/

Question 7 WRONG

Output of following Java program?

import java.util.Arrays;
class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (Arrays.equals(arr1, arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}
Run on IDE

Same

Not Same

Java Arrays
Discuss it

Question 7 Explanation:
See http://www.geeksforgeeks.org/compare-two-arrays-java/

Question 8 WRONG

class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1.equals(arr2))
We use cookies to ensure you have the best browsing experience on our website. By using our site,
Got It !
System.out.println("Same");
you acknowledge that you have read and understood our Cookie
▲ Policy & Privacy Policy
else
System.out.println("Not same");
}
}
Run on IDE

Same

Not same

Java Arrays
Discuss it

Question 8 Explanation:
arr1.equals(arr2) is same as (arr1 == arr2)

Question 9 WRONG

Consider the following C program which is supposed to compute the transpose of a given 4 x
4 matrix M. Note that, there is an X in the program which indicates some missing statements.
Choose the correct option to replace X in the program.

#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (1 = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ("%d", M[i][j]);
}
Run on IDE

A) for(j = 0; j < 4; ++j){


We use cookies tto ensure you have the best browsing experience on our website. By using our site,
= M[i][j]; Got It !
you acknowledge that you have read and understood our Cookie▲ Policy & Privacy Policy
M[i][j] = M[j][i];
M[j][i] = t;
}

for(j = 0; j < 4; ++j){


M[i][j] = t;
t = M[j][i];
B) M[j][i] = M[i][j];
}

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Programming Pyt
for(j = i; j < 4; ++j){
t = M[i][j];
M[i][j] = M[j][i];
C) M[j][i] = t;
}

for(j = i; j < 4; ++j){


M[i][j] = t;
t = M[j][i];
D) M[j][i] = M[i][j];
}

B B

D D

Java Arrays GATE-IT-2004


Discuss it

We use cookies to ensure you have the best browsing experience on our website. By using our site,
Question 9 Explanation: Got It !
you acknowledge that you have read and understood our Cookie
▲ Policy & Privacy Policy
To compute transpose j needs to be star ted with i,so A and B are WRONG

In D, given statement is wrong as temporar y variable t needs to be assigned

some value and NOT vice versa

M[i][j] = t;

So the answer is C Check out the correct option C at Solution:


http://code.geeksforgeeks.org/r7wbP6

You have completed 9/9 questions .


Your accuracy is 11%.

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

We use cookies to ensure you have the best browsing experience on our website. By using our site,
Got It !
you acknowledge that you have read and understood our Cookie
▲ Policy & Privacy Policy

You might also like