• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

Java Arrays Exercises

  • Categories Java, Arrays in Java, Arrays, Arrays
Java Arrays Exercises
  1. Give the total number of bytes occupied by these arrays?
    1. int ar[25]
    2. char M[3][4]
    3. double A[12]

Ans.

The number of bytes:

    1. ar[25] = 4*25 =100 bytes.
    2. M[3][4] = 2*3*4 =24 bytes
    3. A[12] = 8*12 =96 bytes
  1. What is the difference between these two statements:
    1. int sum[]=new int[10]
    2. sum[1]=10;

Ans.

    1. Declares an integer array of size 10 having the name ‘sum’.
    2. Initialises the second element of the array ‘sum’ with 10.
  1. Write the correct syntax of the following arrays:
    1. A1(2)
    2. Name[2,5]
    3. Roll[2;]
    4. Matrix(5)(5)

Ans.

    1. A1[2]
    2. Name[2][5]
    3. Roll[2]
    4. Matrix[5][5]
  1. Give the proper array declaration for the following:
    1. Declare an integer array, which can hold 25 values.
    2. Declare a two dimensional array called mat 3×4 of integer.
    3. Declare and initialise a two dimensional array of size 5×5 to 0.

Ans.

    1. int a[]=new int[25];
    2. int mat[][]=new int[3][4];
    3. int a[][]={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
  1. Find the errors in the given program segment and re-write the statements correctly to assign values to an integer array.
int a= new int(5);
for(int i=0; i<=5; i++)
a[i]= I;

Ans.

The statement 1 has two errors: the square brackets „[ ]’ is required as “int[ ] a” or “int a[ ]”.

Also, the size of the array 5 should be within the „[ ]‟ bracket instead of ( ).

The statement 2 has one error. The loop should be 0 to 4 i.e. for( int i=0; i< 5; i++) or for( int

i= 0; i <5;i++)

Correct program code
int a[ ]= new int[ 5 ];
for(int i= 0; i <5; i++)
a[i]= i;

  1. What will be the output of the following program:
class Output
{
public static void main(String args[])
{
int a[]={6,5,4,3,2,1};
int x;
for(x=5;x>=0;x--)
{
System.out.println(a[x]);
}
}
}

Ans.

1
2
3
4
5
6

  1. What will be the output of the following program:
class First
{

public static void main(String args[])
{
int a[]={5,1,15,20,25};
int i,j;
int m;
i=++a[1];
j=a[2]++;
m=a[i++];
System.out.print(i+“ ”+j+“ ”+m);
}
}

Ans.

3 15 16

  1. If String nam[ ]={ “simply”, “coding”, “While”, “programs”, “Boy”, “java”, “Cat”} ;.
    Write java for loop to print all the strings into different lines.

Ans.

for( int a=0; a < 7; a+ + )
{
System.out.println( nam[ a ]);
}
  1. What is the output of this code:
int mat[ ][ ]= {{13,64},{77,46}};
for(int x=0;x<2;x++){
for(int y= 0; y<2; y++)
System.out.print(mat[x][y]+ " ");
System.out.println();

Ans:

13 64
77 46

  1. Given the array a[ ]= {25, 58, 41, 32, 55, 30, 75, 80}; Answer the following questions.
    1. What are the indices of the array a[ ]?
    2. How many elements are in the array?
    3. What is the 3th element of array?
    4. Write the statement to print elements of 2nd and 6th indexes
    5. What is the start index and end index of array a[ ]

Ans.

    1. The indices of the array ar[ ] are 0-7
    2. Eight elements are in the array
    3. The element at 3 index is 41.
    4. System.out.printin(ar[2]+ “\t” +ar[6]);
    5. he start index is 0 and the end index is= 7
  1. Write valid Java statements for the following:
    1. Initialize 10 integers in a single dimensional array.
    2. Initialize a double dimensional array of 3 rows and 3 columns.
    3. Initialize 7 real or decimal numbers in a single dimensional array.

Ans.

    1. int y[ ]= {10, 5, 6, 12, 7, 8,9, 4, 24, 88};
    2. int num[ ][ ]= {1, 2, 3}, {7, 8, 9}, {4, 5, 6}};
    3. double x[ ]= {2.0, 4.5, 6.8, 3.0, 1.1, 7.5, 9.0};
  1. Consider the given array and write a valid Java program code to update each element of the array by adding 10.

int m[ ]= {4, 6, 8, 12, 16, 3, 9, 7 };

Ans.

for( int j= 0; j< m.length ; j++)
{
m[j]= m[j]+10;
}
  1. What is the output of this code segment?
int G[][] = { {4, 3, 6}, {3, 4, 9}, {3, 7, 2} };
int c=0;
int arr[ ]= new int[ 9 ];
for( int j= 0; j<3; j++){
for( int h=0; h <3 ; h++){
arr[c]= G[ j ][ h ];
c++;
}
}
for (int j = 0; j <9 ; j++)
System.out.print(arr[j] + “ “);

Ans: 4 3 6 3 4 9 3 7 2

  1. Given the array ar[ ]= {10, 22, 33, 44, 55. 66, 77}; Answer the following questions.
    1. How many elements are in the array?
    2. What are the indices of the array ar[ ]?
    3. What is the 4th element of array?
    4. Write the statement to print elements of 2nd and 4th indexes
    5. What is the start index and end index of array ar[ ]

Ans.

    1. Seven elements are in the array
    2. The indices of the array ar[ ] are 0-6
    3. The element at 4 index is 44,
    4. System.out.println(ar[2]+ “\t” +ar[4]);
    5. The start index is 0 and the end index is= 6
  1. From the given array y[ ][ ]={{3, 4, 5},{6, 7, 8}.{1, 2, 3}}; answer the following
    1. Element at y[2][2]
    2. Element at y[0][1]
    3. Element at y[1][2]

Ans.

    1. Element at y[2][2]= 3
    2. Element at y[0][1]=4
    3. Element at y[1][2]= 8
  1. If int x[]= {5,9,7,3,2,8}; What are the value of A and B?
    1. A= x.length
    2. B= x[3]+x[1]*x[2]

Ans.

    1. A=6
    2. B=66
  1. Write single line statement to implement the following:
    1. If String nam[ ] = {“Can”, “Do”, “While”, “Text”, “Boy”, “Rat”, “Cat”);
      Count the number of names in the data structure nam.
    2. To print the string present at 4th position in string array nam[ ]
    3. To print character present at 3rd position in the string present at 2nd index.
    4. To join the strings present at 2nd and 4th position.

Ans.

    1. int y= nam . length;
    2. System.out.println( nam[ 4 ] );
    3. System.out.println(nam [2].charAt(3));
    4. System.out.println(nam [2].concat(nam[4]));
  1. What will be the output of the following program:
class Output
{
public static void main(String args[])
{
int a,b=0;
int c[]={1,2,3,4,5,6,7,8,9,10};
for(a=0;a<10;a++)
{
if(a%2==0)
b+=c[a];
}
System.out.print(b);
}
}

In what statement, what is the output? State what the above program is doing?

Ans.

25
Adding all nos which are at even index

  1. Find the syntax error(s), if any in the following program.
Class First
{
public static void main(String args[])
{
int sum[2,4];
for(i=0;i<2;i++)
{
for(j=0;j<=3;j++)
{
System.print(sum);
}
}
}
}

Ans.

class First
{
public static void main(String args[])
{
int sum[][]=new int[2][4];
for(i=0;i<2;i++)
{
for(j=0;j<=3;j++)
{
System.out.print(sum[i][j]+“ ”);
}
}
}
}
  1. Identify error(s), if any, in the following program.
class First
{
public static void main(String args[])
{
int i;
int a[6]={0,1,8,7,6,4};
for(i=0;i<=a.length();i++)
System.out.println(a[i]);
}
}

Ans.

class First
{
public static void main(String args[])
{
int i;
int a[]={0,1,8,7,6,4};
for(i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
  1. Find the errors in the given program segment and re-write the statements correctly to assign values to an integer array.
int a= new int(8); // statement 1
for(int i=0; i<=5; i++) // statement 2
a[i]= I;

Ans.

The statement 1 has two errors: the square brackets „[ ]’ is required as “int[ ] a” or “int a[ ]”.

Also, the size of the array 5 should be within the „[ ]‟ bracket instead of ( ).

The statement 2 has one error. The loop should be 0 to 4 i.e. for( int i=0; i< 5; i++) or for( int

i= 0; i <5;i++)

Correct program code

int a[ ]= new int[ 5 ];
for(int i= 0; i <5; i++)
a[i]= i;

  • Share:
author avatar
Simply Coding

Previous post

Cracking Java String class questions
June 11, 2020

Next post

Forming Math Expressions
June 13, 2020

You may also like

2d array
2D array in Java
29 June, 2021
Questions on Arrays 1
Arrays in Java
22 June, 2021
single dimensional array in java
Important Java Single Dimensional Array Programs
17 February, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now